Spaces:
Paused
Paused
math
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
import spaces
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
from diffusers import DiffusionPipeline
|
@@ -21,38 +20,31 @@ quant_config = PipelineQuantizationConfig(
|
|
21 |
components_to_quantize=["transformer", "text_encoder"]
|
22 |
)
|
23 |
|
24 |
-
# Load pipeline with quantization
|
25 |
pipe = DiffusionPipeline.from_pretrained(
|
26 |
ckpt_id,
|
27 |
quantization_config=quant_config,
|
28 |
torch_dtype=torch.bfloat16
|
29 |
)
|
30 |
-
|
31 |
-
# Enable CPU offload and compile after offload
|
32 |
pipe.enable_model_cpu_offload()
|
33 |
torch._dynamo.config.recompile_limit = 1000
|
34 |
torch._dynamo.config.capture_dynamic_output_shape_ops = True
|
35 |
|
36 |
-
#
|
37 |
-
def get_duration(prompt, height, width,
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
if steps > 4 and duration_seconds > 2:
|
42 |
-
return 90
|
43 |
-
elif steps > 4 or duration_seconds > 2:
|
44 |
-
return 75
|
45 |
else:
|
46 |
-
return
|
47 |
|
48 |
# Gradio inference function with spaces GPU decorator
|
49 |
-
@spaces.GPU(duration=
|
50 |
-
def generate_video(prompt,
|
51 |
generator = torch.manual_seed(seed) if seed else None
|
52 |
fps = 8
|
53 |
-
num_frames = duration_seconds * fps if duration_seconds else 16
|
54 |
|
55 |
-
# Run pipeline on default device with automatic offload
|
56 |
video_frames = pipe(
|
57 |
prompt=prompt,
|
58 |
num_frames=num_frames,
|
@@ -60,7 +52,6 @@ def generate_video(prompt, seed, steps, duration_seconds,progress=gr.Progress(tr
|
|
60 |
num_inference_steps=steps
|
61 |
).frames[0]
|
62 |
|
63 |
-
# Ensure frames are uint8 numpy arrays for imageio
|
64 |
processed_frames = [
|
65 |
(np.clip(frame * 255, 0, 255).astype(np.uint8) if frame.dtype in [np.float32, np.float64] else frame)
|
66 |
for frame in video_frames
|
@@ -70,20 +61,26 @@ def generate_video(prompt, seed, steps, duration_seconds,progress=gr.Progress(tr
|
|
70 |
imageio.mimsave(out_path, processed_frames, fps=fps)
|
71 |
return out_path
|
72 |
|
73 |
-
# Build Gradio UI
|
74 |
-
with gr.Blocks() as demo:
|
75 |
-
gr.Markdown("## 🚀 Wan2.1 T2V - Text to Video Generator (Quantized,
|
76 |
with gr.Row():
|
77 |
with gr.Column():
|
78 |
prompt_input = gr.Textbox(label="Prompt", lines=3, value="A futuristic cityscape with flying cars and neon lights.")
|
79 |
-
|
|
|
|
|
|
|
80 |
steps_input = gr.Slider(1, 50, value=20, step=1, label="Inference Steps")
|
81 |
-
|
|
|
|
|
82 |
run_btn = gr.Button("Generate Video")
|
83 |
with gr.Column():
|
84 |
output_video = gr.Video(label="Generated Video")
|
85 |
|
86 |
-
|
|
|
87 |
|
88 |
# Launch demo
|
89 |
demo.launch()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from diffusers import DiffusionPipeline
|
|
|
20 |
components_to_quantize=["transformer", "text_encoder"]
|
21 |
)
|
22 |
|
23 |
+
# Load pipeline with quantization
|
24 |
pipe = DiffusionPipeline.from_pretrained(
|
25 |
ckpt_id,
|
26 |
quantization_config=quant_config,
|
27 |
torch_dtype=torch.bfloat16
|
28 |
)
|
|
|
|
|
29 |
pipe.enable_model_cpu_offload()
|
30 |
torch._dynamo.config.recompile_limit = 1000
|
31 |
torch._dynamo.config.capture_dynamic_output_shape_ops = True
|
32 |
|
33 |
+
# Smart duration function using all UI params
|
34 |
+
def get_duration(prompt, height, width, negative_prompt, duration_seconds, guidance_scale, steps, seed, randomize_seed, progress):
|
35 |
+
# Calculate dynamic duration based on steps and requested duration
|
36 |
+
if duration_seconds <= 2.5:
|
37 |
+
return steps * 18
|
|
|
|
|
|
|
|
|
38 |
else:
|
39 |
+
return steps * 25
|
40 |
|
41 |
# Gradio inference function with spaces GPU decorator
|
42 |
+
@spaces.GPU(duration=get_duration)
|
43 |
+
def generate_video(prompt, height, width, negative_prompt, duration_seconds, guidance_scale, steps, seed, randomize_seed, progress=gr.Progress(track_tqdm=True)):
|
44 |
generator = torch.manual_seed(seed) if seed else None
|
45 |
fps = 8
|
46 |
+
num_frames = int(duration_seconds * fps) if duration_seconds else 16
|
47 |
|
|
|
48 |
video_frames = pipe(
|
49 |
prompt=prompt,
|
50 |
num_frames=num_frames,
|
|
|
52 |
num_inference_steps=steps
|
53 |
).frames[0]
|
54 |
|
|
|
55 |
processed_frames = [
|
56 |
(np.clip(frame * 255, 0, 255).astype(np.uint8) if frame.dtype in [np.float32, np.float64] else frame)
|
57 |
for frame in video_frames
|
|
|
61 |
imageio.mimsave(out_path, processed_frames, fps=fps)
|
62 |
return out_path
|
63 |
|
64 |
+
# Build Gradio UI with all parameters
|
65 |
+
with gr.Blocks(css="body { max-width: 100vw; overflow-x: hidden; }") as demo:
|
66 |
+
gr.Markdown("## 🚀 Wan2.1 T2V - Text to Video Generator (Quantized, Smart Duration)")
|
67 |
with gr.Row():
|
68 |
with gr.Column():
|
69 |
prompt_input = gr.Textbox(label="Prompt", lines=3, value="A futuristic cityscape with flying cars and neon lights.")
|
70 |
+
negative_prompt_input = gr.Textbox(label="Negative Prompt", lines=3, value="")
|
71 |
+
height_input = gr.Slider(256, 1024, step=8, value=512, label="Height")
|
72 |
+
width_input = gr.Slider(256, 1024, step=8, value=512, label="Width")
|
73 |
+
duration_input = gr.Slider(1, 10, value=2, step=0.1, label="Duration (seconds)")
|
74 |
steps_input = gr.Slider(1, 50, value=20, step=1, label="Inference Steps")
|
75 |
+
guidance_scale_input = gr.Slider(0.0, 20.0, step=0.5, value=7.5, label="Guidance Scale")
|
76 |
+
seed_input = gr.Number(value=42, label="Seed (optional)")
|
77 |
+
randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True)
|
78 |
run_btn = gr.Button("Generate Video")
|
79 |
with gr.Column():
|
80 |
output_video = gr.Video(label="Generated Video")
|
81 |
|
82 |
+
ui_inputs = [prompt_input, height_input, width_input, negative_prompt_input, duration_input, guidance_scale_input, steps_input, seed_input, randomize_seed_checkbox]
|
83 |
+
run_btn.click(fn=generate_video, inputs=ui_inputs, outputs=output_video)
|
84 |
|
85 |
# Launch demo
|
86 |
demo.launch()
|