Add application file
Browse files- README.md +6 -5
- app_i2v.py +104 -0
- app_t2v.py +99 -0
- requirements.txt +8 -0
- video_model.py +12 -0
README.md
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 5.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Test Video
|
3 |
+
emoji: 🐨
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 5.9.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: mit
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app_i2v.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
import time
|
4 |
+
import torch
|
5 |
+
import gc
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
from diffusers.utils import export_to_video, load_image
|
9 |
+
|
10 |
+
from video_model import i2v_pipe
|
11 |
+
|
12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
|
14 |
+
def create_demo() -> gr.Blocks:
|
15 |
+
|
16 |
+
@spaces.GPU(duration=60)
|
17 |
+
def image_to_video(
|
18 |
+
image_path: str,
|
19 |
+
prompt: str,
|
20 |
+
negative_prompt: str,
|
21 |
+
width: int = 768,
|
22 |
+
height: int = 512,
|
23 |
+
num_frames: int = 121,
|
24 |
+
frame_rate: int = 25,
|
25 |
+
num_inference_steps: int = 30,
|
26 |
+
seed: int = 8,
|
27 |
+
progress=gr.Progress(),
|
28 |
+
):
|
29 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
30 |
+
input_image = load_image(image_path)
|
31 |
+
run_task_time = 0
|
32 |
+
time_cost_str = ''
|
33 |
+
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
34 |
+
try:
|
35 |
+
with torch.no_grad():
|
36 |
+
video = i2v_pipe(
|
37 |
+
image=input_image,
|
38 |
+
prompt=prompt,
|
39 |
+
negative_prompt=negative_prompt,
|
40 |
+
generator=generator,
|
41 |
+
width=width,
|
42 |
+
height=height,
|
43 |
+
num_frames=num_frames,
|
44 |
+
num_inference_steps=num_inference_steps,
|
45 |
+
).frames[0]
|
46 |
+
finally:
|
47 |
+
torch.cuda.empty_cache()
|
48 |
+
gc.collect()
|
49 |
+
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
50 |
+
|
51 |
+
output_path = tempfile.mktemp(suffix=".mp4")
|
52 |
+
export_to_video(video, output_path, fps=frame_rate)
|
53 |
+
|
54 |
+
del video
|
55 |
+
torch.cuda.empty_cache()
|
56 |
+
return output_path, time_cost_str
|
57 |
+
|
58 |
+
|
59 |
+
def get_time_cost(run_task_time, time_cost_str):
|
60 |
+
now_time = int(time.time()*1000)
|
61 |
+
if run_task_time == 0:
|
62 |
+
time_cost_str = 'start'
|
63 |
+
else:
|
64 |
+
if time_cost_str != '':
|
65 |
+
time_cost_str += f'-->'
|
66 |
+
time_cost_str += f'{now_time - run_task_time}'
|
67 |
+
run_task_time = now_time
|
68 |
+
return run_task_time, time_cost_str
|
69 |
+
|
70 |
+
with gr.Blocks() as demo:
|
71 |
+
with gr.Row():
|
72 |
+
with gr.Column():
|
73 |
+
i2vid_image_path = gr.File(label="Input Image")
|
74 |
+
i2vid_prompt = gr.Textbox(
|
75 |
+
label="Enter Your Prompt",
|
76 |
+
placeholder="Describe the video you want to generate (minimum 50 characters)...",
|
77 |
+
value="A woman with long brown hair and light skin smiles at another woman with long blonde hair. The woman with brown hair wears a black jacket and has a small, barely noticeable mole on her right cheek. The camera angle is a close-up, focused on the woman with brown hair's face. The lighting is warm and natural, likely from the setting sun, casting a soft glow on the scene. The scene appears to be real-life footage.",
|
78 |
+
lines=5,
|
79 |
+
)
|
80 |
+
|
81 |
+
i2vid_negative_prompt = gr.Textbox(
|
82 |
+
label="Enter Negative Prompt",
|
83 |
+
placeholder="Describe what you don't want in the video...",
|
84 |
+
value="low quality, worst quality, deformed, distorted, disfigured, motion smear, motion artifacts, fused fingers, bad anatomy, weird hand, ugly",
|
85 |
+
lines=2,
|
86 |
+
)
|
87 |
+
|
88 |
+
i2vid_generate = gr.Button(
|
89 |
+
"Generate Video",
|
90 |
+
variant="primary",
|
91 |
+
size="lg",
|
92 |
+
)
|
93 |
+
|
94 |
+
with gr.Column():
|
95 |
+
i2vid_output = gr.Video(label="Generated Output")
|
96 |
+
i2vid_generated_cost = gr.Textbox(label="Time cost by step (ms):", visible=True, interactive=False)
|
97 |
+
|
98 |
+
i2vid_generate.click(
|
99 |
+
fn=image_to_video,
|
100 |
+
inputs=[i2vid_image_path, i2vid_prompt, i2vid_negative_prompt],
|
101 |
+
outputs=[i2vid_output, i2vid_generated_cost],
|
102 |
+
)
|
103 |
+
|
104 |
+
return demo
|
app_t2v.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
import time
|
4 |
+
import torch
|
5 |
+
import gc
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
from diffusers.utils import export_to_video
|
9 |
+
|
10 |
+
from video_model import t2v_pipe
|
11 |
+
|
12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
|
14 |
+
def create_demo() -> gr.Blocks:
|
15 |
+
|
16 |
+
@spaces.GPU(duration=60)
|
17 |
+
def text_to_video(
|
18 |
+
prompt: str,
|
19 |
+
negative_prompt: str,
|
20 |
+
width: int = 768,
|
21 |
+
height: int = 512,
|
22 |
+
num_frames: int = 121,
|
23 |
+
frame_rate: int = 25,
|
24 |
+
num_inference_steps: int = 30,
|
25 |
+
seed: int = 8,
|
26 |
+
progress: gr.Progress = gr.Progress(),
|
27 |
+
):
|
28 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
29 |
+
run_task_time = 0
|
30 |
+
time_cost_str = ''
|
31 |
+
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
32 |
+
try:
|
33 |
+
with torch.no_grad():
|
34 |
+
video = t2v_pipe(
|
35 |
+
prompt=prompt,
|
36 |
+
negative_prompt=negative_prompt,
|
37 |
+
generator=generator,
|
38 |
+
width=width,
|
39 |
+
height=height,
|
40 |
+
num_frames=num_frames,
|
41 |
+
num_inference_steps=num_inference_steps,
|
42 |
+
).frames[0]
|
43 |
+
finally:
|
44 |
+
torch.cuda.empty_cache()
|
45 |
+
gc.collect()
|
46 |
+
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
47 |
+
|
48 |
+
output_path = tempfile.mktemp(suffix=".mp4")
|
49 |
+
export_to_video(video, output_path, fps=frame_rate)
|
50 |
+
|
51 |
+
del video
|
52 |
+
torch.cuda.empty_cache()
|
53 |
+
return output_path, time_cost_str
|
54 |
+
|
55 |
+
def get_time_cost(run_task_time, time_cost_str):
|
56 |
+
now_time = int(time.time()*1000)
|
57 |
+
if run_task_time == 0:
|
58 |
+
time_cost_str = 'start'
|
59 |
+
else:
|
60 |
+
if time_cost_str != '':
|
61 |
+
time_cost_str += f'-->'
|
62 |
+
time_cost_str += f'{now_time - run_task_time}'
|
63 |
+
run_task_time = now_time
|
64 |
+
return run_task_time, time_cost_str
|
65 |
+
|
66 |
+
with gr.Blocks() as demo:
|
67 |
+
with gr.Row():
|
68 |
+
with gr.Column():
|
69 |
+
txt2vid_prompt = gr.Textbox(
|
70 |
+
label="Enter Your Prompt",
|
71 |
+
placeholder="Describe the video you want to generate (minimum 50 characters)...",
|
72 |
+
value="A woman with long brown hair and light skin smiles at another woman with long blonde hair. The woman with brown hair wears a black jacket and has a small, barely noticeable mole on her right cheek. The camera angle is a close-up, focused on the woman with brown hair's face. The lighting is warm and natural, likely from the setting sun, casting a soft glow on the scene. The scene appears to be real-life footage.",
|
73 |
+
lines=5,
|
74 |
+
)
|
75 |
+
|
76 |
+
txt2vid_negative_prompt = gr.Textbox(
|
77 |
+
label="Enter Negative Prompt",
|
78 |
+
placeholder="Describe what you don't want in the video...",
|
79 |
+
value="low quality, worst quality, deformed, distorted, disfigured, motion smear, motion artifacts, fused fingers, bad anatomy, weird hand, ugly",
|
80 |
+
lines=2,
|
81 |
+
)
|
82 |
+
|
83 |
+
txt2vid_generate = gr.Button(
|
84 |
+
"Generate Video",
|
85 |
+
variant="primary",
|
86 |
+
size="lg",
|
87 |
+
)
|
88 |
+
|
89 |
+
with gr.Column():
|
90 |
+
txt2vid_output = gr.Video(label="Generated Output")
|
91 |
+
txt2vid_generated_cost = gr.Textbox(label="Time cost by step (ms):", visible=True, interactive=False)
|
92 |
+
|
93 |
+
txt2vid_generate.click(
|
94 |
+
fn=text_to_video,
|
95 |
+
inputs=[txt2vid_prompt, txt2vid_negative_prompt],
|
96 |
+
outputs=[txt2vid_output, txt2vid_generated_cost],
|
97 |
+
)
|
98 |
+
|
99 |
+
return demo
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
torchvision
|
4 |
+
diffusers
|
5 |
+
transformers
|
6 |
+
accelerate
|
7 |
+
mediapipe
|
8 |
+
spaces
|
video_model.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
|
3 |
+
from diffusers import LTXPipeline, LTXImageToVideoPipeline
|
4 |
+
|
5 |
+
|
6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
7 |
+
|
8 |
+
t2v_pipe = LTXPipeline.from_pretrained("Skywork/SkyReels-V1-Hunyuan-T2V", torch_dtype=torch.bfloat16)
|
9 |
+
t2v_pipe.to(device)
|
10 |
+
|
11 |
+
i2v_pipe = LTXImageToVideoPipeline.from_pipe(t2v_pipe)
|
12 |
+
i2v_pipe.to(device)
|