rahul7star commited on
Commit
789bafb
·
verified ·
1 Parent(s): 06cf62f

Create app1.py

Browse files
Files changed (1) hide show
  1. app1.py +79 -0
app1.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import DiffusionPipeline
4
+ from diffusers.quantizers import PipelineQuantizationConfig
5
+ import imageio
6
+
7
+ # Checkpoint ID
8
+ ckpt_id = "Wan-AI/Wan2.1-T2V-14B-Diffusers"
9
+
10
+ # Configure quantization (bitsandbytes 4-bit)
11
+ quant_config = PipelineQuantizationConfig(
12
+ quant_backend="bitsandbytes_4bit",
13
+ quant_kwargs={
14
+ "load_in_4bit": True,
15
+ "bnb_4bit_quant_type": "nf4",
16
+ "bnb_4bit_compute_dtype": torch.bfloat16
17
+ },
18
+ components_to_quantize=["transformer", "text_encoder"]
19
+ )
20
+
21
+ # Load pipeline with quantization
22
+ pipe = DiffusionPipeline.from_pretrained(
23
+ ckpt_id,
24
+ quantization_config=quant_config,
25
+ torch_dtype=torch.bfloat16
26
+ ).to("cuda")
27
+
28
+ # Optimize memory and performance
29
+ pipe.enable_model_cpu_offload()
30
+ torch._dynamo.config.recompile_limit = 1000
31
+ torch._dynamo.config.capture_dynamic_output_shape_ops = True
32
+ pipe.transformer.compile()
33
+
34
+ # Duration function
35
+ def get_duration(prompt, height, width,
36
+ negative_prompt, duration_seconds,
37
+ guidance_scale, steps,
38
+ seed, randomize_seed):
39
+ if steps > 4 and duration_seconds > 2:
40
+ return 90
41
+ elif steps > 4 or duration_seconds > 2:
42
+ return 75
43
+ else:
44
+ return 60
45
+
46
+ # Gradio inference function (no @spaces.GPU decorator) to avoid progress ContextVar error
47
+ def generate_video(prompt, seed, steps, duration_seconds):
48
+ generator = torch.manual_seed(seed) if seed else None
49
+ fps = 8
50
+ num_frames = duration_seconds * fps if duration_seconds else 16
51
+
52
+ video_frames = pipe(
53
+ prompt=prompt,
54
+ num_frames=num_frames,
55
+ generator=generator,
56
+ num_inference_steps=steps
57
+ ).frames[0]
58
+
59
+ out_path = "output.gif"
60
+ imageio.mimsave(out_path, video_frames, fps=fps)
61
+ return out_path
62
+
63
+ # Build Gradio UI
64
+ with gr.Blocks() as demo:
65
+ gr.Markdown("## 🚀 Wan2.1 T2V - Text to Video Generator (Quantized, Dynamic Duration)")
66
+ with gr.Row():
67
+ with gr.Column():
68
+ prompt_input = gr.Textbox(label="Prompt", lines=3, value="A futuristic cityscape with flying cars and neon lights.")
69
+ seed_input = gr.Number(value=42, label="Seed (optional)")
70
+ steps_input = gr.Slider(1, 50, value=20, step=1, label="Inference Steps")
71
+ duration_input = gr.Slider(1, 10, value=2, step=1, label="Video Duration (seconds)")
72
+ run_btn = gr.Button("Generate Video")
73
+ with gr.Column():
74
+ output_video = gr.Video(label="Generated Video")
75
+
76
+ run_btn.click(fn=generate_video, inputs=[prompt_input, seed_input, steps_input, duration_input], outputs=output_video)
77
+
78
+ # Launch demo
79
+ demo.launch()