rahul7star commited on
Commit
70a9091
·
verified ·
1 Parent(s): f2dc9a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -25
app.py CHANGED
@@ -1,34 +1,122 @@
1
  import os
2
  import subprocess
 
3
  from huggingface_hub import snapshot_download
 
4
 
5
- # Step 1: Download model
6
  repo_id = "Wan-AI/Wan2.2-TI2V-5B"
7
  print(f"Downloading/loading checkpoints for {repo_id}...")
8
  ckpt_dir = snapshot_download(repo_id, local_dir_use_symlinks=False)
9
  print(f"Using checkpoints from {ckpt_dir}")
10
 
11
- # Step 2: Define command arguments (same as your CLI example)
12
- cmd = [
13
- "python", "generate.py",
14
- "--task", "ti2v-5B",
15
- "--size", "1280*704",
16
- "--ckpt_dir", ckpt_dir,
17
- "--offload_model", "True",
18
- "--convert_model_dtype",
19
- "--t5_cpu",
20
- "--image", "examples/i2v_input.JPG",
21
- "--prompt", (
22
- "Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. "
23
- "The fluffy-furred feline gazes directly at the camera with a relaxed expression. "
24
- "Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, "
25
- "and a blue sky dotted with white clouds. The cat assumes a naturally relaxed posture, "
26
- "as if savoring the sea breeze and warm sunlight. A close-up shot highlights the feline's "
27
- "intricate details and the refreshing atmosphere of the seaside."
28
- )
29
- ]
30
-
31
- # Step 3: Run the command
32
- print("Running generate.py...")
33
- subprocess.run(cmd, check=True)
34
- print("Generation complete!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import subprocess
3
+ import tempfile
4
  from huggingface_hub import snapshot_download
5
+ import gradio as gr
6
 
7
+ # ---------------- Step 1: Download Model ----------------
8
  repo_id = "Wan-AI/Wan2.2-TI2V-5B"
9
  print(f"Downloading/loading checkpoints for {repo_id}...")
10
  ckpt_dir = snapshot_download(repo_id, local_dir_use_symlinks=False)
11
  print(f"Using checkpoints from {ckpt_dir}")
12
 
13
+ # ---------------- Step 2: Generation Functions ----------------
14
+ def generate_t2v(prompt, size="1280*704"):
15
+ """Text-to-Video generation."""
16
+ if not prompt.strip():
17
+ return None, "Please enter a prompt."
18
+
19
+ temp_dir = tempfile.mkdtemp()
20
+ output_path = os.path.join(temp_dir, "output.mp4")
21
+
22
+ cmd = [
23
+ "python", "generate.py",
24
+ "--task", "ti2v-5B",
25
+ "--size", size,
26
+ "--ckpt_dir", ckpt_dir,
27
+ "--offload_model", "True",
28
+ "--convert_model_dtype",
29
+ "--t5_cpu",
30
+ "--prompt", prompt
31
+ ]
32
+
33
+ print(f"[T2V] Running command: {' '.join(cmd)}")
34
+ try:
35
+ subprocess.run(cmd, check=True)
36
+ except subprocess.CalledProcessError as e:
37
+ return None, f"Error during T2V generation: {e}"
38
+
39
+ if os.path.exists("output.mp4"):
40
+ os.rename("output.mp4", output_path)
41
+ elif os.path.exists(output_path):
42
+ pass
43
+ else:
44
+ return None, "Generation finished but output file not found."
45
+
46
+ return output_path, "Text-to-Video generated successfully!"
47
+
48
+ def generate_i2v(image, prompt, size="1280*704"):
49
+ """Image-to-Video generation."""
50
+ if image is None or not prompt.strip():
51
+ return None, "Please upload an image and enter a prompt."
52
+
53
+ temp_dir = tempfile.mkdtemp()
54
+ image_path = os.path.join(temp_dir, "input.jpg")
55
+ image.save(image_path)
56
+
57
+ output_path = os.path.join(temp_dir, "output.mp4")
58
+
59
+ cmd = [
60
+ "python", "generate.py",
61
+ "--task", "ti2v-5B",
62
+ "--size", size,
63
+ "--ckpt_dir", ckpt_dir,
64
+ "--offload_model", "True",
65
+ "--convert_model_dtype",
66
+ "--t5_cpu",
67
+ "--image", image_path,
68
+ "--prompt", prompt
69
+ ]
70
+
71
+ print(f"[I2V] Running command: {' '.join(cmd)}")
72
+ try:
73
+ subprocess.run(cmd, check=True)
74
+ except subprocess.CalledProcessError as e:
75
+ return None, f"Error during I2V generation: {e}"
76
+
77
+ if os.path.exists("output.mp4"):
78
+ os.rename("output.mp4", output_path)
79
+ elif os.path.exists(output_path):
80
+ pass
81
+ else:
82
+ return None, "Generation finished but output file not found."
83
+
84
+ return output_path, "Image-to-Video generated successfully!"
85
+
86
+ # ---------------- Step 3: Gradio UI ----------------
87
+ with gr.Blocks() as demo:
88
+ gr.Markdown("## 🎥 Wan2.2-TI2V-5B Video Generator")
89
+ gr.Markdown("Choose **Text-to-Video** or **Image-to-Video** mode below.")
90
+
91
+ with gr.Tab("Text-to-Video"):
92
+ t2v_prompt = gr.Textbox(
93
+ label="Prompt",
94
+ value="Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage"
95
+ )
96
+ t2v_size = gr.Textbox(label="Video Size", value="1280*704")
97
+ t2v_btn = gr.Button("Generate from Text")
98
+ t2v_video = gr.Video(label="Generated Video")
99
+ t2v_status = gr.Textbox(label="Status")
100
+ t2v_btn.click(generate_t2v, [t2v_prompt, t2v_size], [t2v_video, t2v_status])
101
+
102
+ with gr.Tab("Image-to-Video"):
103
+ i2v_image = gr.Image(type="pil", label="Upload Image")
104
+ i2v_prompt = gr.Textbox(
105
+ label="Prompt",
106
+ value=(
107
+ "Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. "
108
+ "The fluffy-furred feline gazes directly at the camera with a relaxed expression. "
109
+ "Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, "
110
+ "and a blue sky dotted with white clouds. The cat assumes a naturally relaxed posture, "
111
+ "as if savoring the sea breeze and warm sunlight. A close-up shot highlights the feline's "
112
+ "intricate details and the refreshing atmosphere of the seaside."
113
+ )
114
+ )
115
+ i2v_size = gr.Textbox(label="Video Size", value="1280*704")
116
+ i2v_btn = gr.Button("Generate from Image")
117
+ i2v_video = gr.Video(label="Generated Video")
118
+ i2v_status = gr.Textbox(label="Status")
119
+ i2v_btn.click(generate_i2v, [i2v_image, i2v_prompt, i2v_size], [i2v_video, i2v_status])
120
+
121
+ if __name__ == "__main__":
122
+ demo.launch()