Spaces:
Paused
Paused
zhiweili commited on
Commit ·
708a6ea
1
Parent(s): 05483e9
add app_i2v
Browse files- app.py +6 -3
- app_i2v.py +104 -0
- app_video.py → app_t2v.py +3 -8
- video_model.py +9 -0
app.py
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
from
|
|
|
|
| 4 |
|
| 5 |
with gr.Blocks(css="style.css") as demo:
|
| 6 |
with gr.Tabs():
|
| 7 |
-
with gr.Tab(label="
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
from app_t2v import create_demo as create_demo_t2v
|
| 4 |
+
from app_i2v import create_demo as create_demo_i2v
|
| 5 |
|
| 6 |
with gr.Blocks(css="style.css") as demo:
|
| 7 |
with gr.Tabs():
|
| 8 |
+
with gr.Tab(label="tx2vid"):
|
| 9 |
+
create_demo_t2v()
|
| 10 |
+
with gr.Tab(label="img2vid"):
|
| 11 |
+
create_demo_i2v()
|
| 12 |
|
| 13 |
demo.launch()
|
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 video_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 = video_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_video.py → app_t2v.py
RENAMED
|
@@ -4,18 +4,13 @@ import time
|
|
| 4 |
import torch
|
| 5 |
import gc
|
| 6 |
import tempfile
|
| 7 |
-
import numpy as np
|
| 8 |
-
import cv2
|
| 9 |
|
| 10 |
-
from diffusers import
|
| 11 |
-
from diffusers.utils import export_to_video
|
| 12 |
|
|
|
|
| 13 |
|
| 14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
|
| 16 |
-
pipe = LTXPipeline.from_pretrained("Lightricks/LTX-Video", torch_dtype=torch.bfloat16)
|
| 17 |
-
pipe.to(device)
|
| 18 |
-
|
| 19 |
def create_demo() -> gr.Blocks:
|
| 20 |
|
| 21 |
@spaces.GPU(duration=60)
|
|
@@ -36,7 +31,7 @@ def create_demo() -> gr.Blocks:
|
|
| 36 |
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
| 37 |
try:
|
| 38 |
with torch.no_grad():
|
| 39 |
-
video =
|
| 40 |
prompt=prompt,
|
| 41 |
negative_prompt=negative_prompt,
|
| 42 |
generator=generator,
|
|
|
|
| 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 video_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)
|
|
|
|
| 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 = video_pipe(
|
| 35 |
prompt=prompt,
|
| 36 |
negative_prompt=negative_prompt,
|
| 37 |
generator=generator,
|
video_model.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
from diffusers import LTXPipeline
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
|
| 8 |
+
video_pipe = LTXPipeline.from_pretrained("Lightricks/LTX-Video", torch_dtype=torch.bfloat16)
|
| 9 |
+
video_pipe.to(device)
|