rahul7star commited on
Commit
3610958
Β·
verified Β·
1 Parent(s): e81aa63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -47
app.py CHANGED
@@ -1,9 +1,9 @@
 
1
  import os
2
  import subprocess
3
  import tempfile
4
  from huggingface_hub import snapshot_download
5
  import gradio as gr
6
- import spaces
7
 
8
  # ---------------- Step 1: Download Model ----------------
9
  repo_id = "Wan-AI/Wan2.2-TI2V-5B"
@@ -12,45 +12,23 @@ ckpt_dir = snapshot_download(repo_id, local_dir_use_symlinks=False)
12
  print(f"Using checkpoints from {ckpt_dir}")
13
 
14
  # ---------------- Step 2: Duration Calculation ----------------
15
- def get_duration(prompt, size, duration_seconds):
16
  try:
17
  h, w = size.lower().replace(" ", "").split("*")
18
  h, w = int(h), int(w)
19
  except Exception:
20
  h, w = 704, 1280
21
- duration = int(duration_seconds) * 50 * 2.25 + 5 # 50 is fixed sample_steps
22
- return duration
23
-
24
- # ---------------- Step 3: Helpers ----------------
25
- def find_generated_mp4():
26
- mp4_files = [f for f in os.listdir(".") if f.lower().endswith(".mp4")]
27
- if not mp4_files:
28
- return None
29
- mp4_files.sort(key=lambda x: os.path.getmtime(x), reverse=True)
30
- return mp4_files[0]
31
-
32
- def run_generate_command(cmd):
33
- print(f"[RUN] {' '.join(cmd)}")
34
- try:
35
- subprocess.run(cmd, check=True)
36
- except subprocess.CalledProcessError as e:
37
- return None, None, f"Error: {e}"
38
 
39
- mp4_file = find_generated_mp4()
40
- if not mp4_file:
41
- return None, None, "No output video found."
42
-
43
- temp_dir = tempfile.mkdtemp()
44
- output_path = os.path.join(temp_dir, mp4_file)
45
- os.rename(mp4_file, output_path)
46
- return output_path, output_path, "Generation successful!"
47
-
48
- # ---------------- Step 4: Generation Functions ----------------
49
  @spaces.GPU(duration=get_duration)
50
- def generate_t2v(prompt, size="1280*704", duration_seconds=5):
51
  if not prompt.strip():
52
  return None, None, "Please enter a prompt."
53
 
 
 
 
54
  cmd = [
55
  "python", "generate.py",
56
  "--task", "ti2v-5B",
@@ -59,14 +37,25 @@ def generate_t2v(prompt, size="1280*704", duration_seconds=5):
59
  "--offload_model", "True",
60
  "--convert_model_dtype",
61
  "--t5_cpu",
62
- "--prompt", prompt,
63
- "--steps", "50",
64
- "--guidance_scale", "5.0"
65
  ]
66
- return run_generate_command(cmd)
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  @spaces.GPU(duration=get_duration)
69
- def generate_i2v(image, prompt, size="1280*704", duration_seconds=5):
70
  if image is None or not prompt.strip():
71
  return None, None, "Please upload an image and enter a prompt."
72
 
@@ -74,6 +63,8 @@ def generate_i2v(image, prompt, size="1280*704", duration_seconds=5):
74
  image_path = os.path.join(temp_dir, "input.jpg")
75
  image.save(image_path)
76
 
 
 
77
  cmd = [
78
  "python", "generate.py",
79
  "--task", "ti2v-5B",
@@ -83,16 +74,27 @@ def generate_i2v(image, prompt, size="1280*704", duration_seconds=5):
83
  "--convert_model_dtype",
84
  "--t5_cpu",
85
  "--image", image_path,
86
- "--prompt", prompt,
87
- "--steps", "50",
88
- "--guidance_scale", "5.0"
89
  ]
90
- return run_generate_command(cmd)
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
- # ---------------- Step 5: Gradio UI ----------------
93
  with gr.Blocks() as demo:
94
  gr.Markdown("## πŸŽ₯ Wan2.2-TI2V-5B Video Generator")
95
- gr.Markdown("Generate AI videos from text or image prompts with download option.")
96
 
97
  with gr.Tab("Text-to-Video"):
98
  t2v_prompt = gr.Textbox(
@@ -101,13 +103,14 @@ with gr.Blocks() as demo:
101
  )
102
  t2v_size = gr.Textbox(label="Video Size", value="1280*704")
103
  t2v_duration = gr.Number(label="Video Length (seconds)", value=5)
 
104
  t2v_btn = gr.Button("Generate from Text")
105
- t2v_video = gr.Video(label="Generated Video", autoplay=True)
106
- t2v_download = gr.File(label="Download Video")
107
  t2v_status = gr.Textbox(label="Status")
108
  t2v_btn.click(
109
  generate_t2v,
110
- [t2v_prompt, t2v_size, t2v_duration],
111
  [t2v_video, t2v_download, t2v_status]
112
  )
113
 
@@ -126,13 +129,14 @@ with gr.Blocks() as demo:
126
  )
127
  i2v_size = gr.Textbox(label="Video Size", value="1280*704")
128
  i2v_duration = gr.Number(label="Video Length (seconds)", value=5)
 
129
  i2v_btn = gr.Button("Generate from Image")
130
- i2v_video = gr.Video(label="Generated Video", autoplay=True)
131
- i2v_download = gr.File(label="Download Video")
132
  i2v_status = gr.Textbox(label="Status")
133
  i2v_btn.click(
134
  generate_i2v,
135
- [i2v_image, i2v_prompt, i2v_size, i2v_duration],
136
  [i2v_video, i2v_download, i2v_status]
137
  )
138
 
 
1
+ import spaces
2
  import os
3
  import subprocess
4
  import tempfile
5
  from huggingface_hub import snapshot_download
6
  import gradio as gr
 
7
 
8
  # ---------------- Step 1: Download Model ----------------
9
  repo_id = "Wan-AI/Wan2.2-TI2V-5B"
 
12
  print(f"Using checkpoints from {ckpt_dir}")
13
 
14
  # ---------------- Step 2: Duration Calculation ----------------
15
+ def get_duration(prompt, size, duration_seconds, steps):
16
  try:
17
  h, w = size.lower().replace(" ", "").split("*")
18
  h, w = int(h), int(w)
19
  except Exception:
20
  h, w = 704, 1280
21
+ return int(duration_seconds) * int(steps) * 2.25 + 5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ # ---------------- Step 3: Generation Functions ----------------
 
 
 
 
 
 
 
 
 
24
  @spaces.GPU(duration=get_duration)
25
+ def generate_t2v(prompt, size="1280*704", duration_seconds=5, steps=25):
26
  if not prompt.strip():
27
  return None, None, "Please enter a prompt."
28
 
29
+ temp_dir = tempfile.mkdtemp()
30
+ output_path = os.path.join(temp_dir, "output.mp4")
31
+
32
  cmd = [
33
  "python", "generate.py",
34
  "--task", "ti2v-5B",
 
37
  "--offload_model", "True",
38
  "--convert_model_dtype",
39
  "--t5_cpu",
40
+ "--prompt", prompt
 
 
41
  ]
42
+ print(f"[T2V] Running command: {' '.join(cmd)}")
43
+
44
+ try:
45
+ subprocess.run(cmd, check=True)
46
+ except subprocess.CalledProcessError as e:
47
+ return None, None, f"Error during T2V generation: {e}"
48
+
49
+ if os.path.exists("output.mp4"):
50
+ os.rename("output.mp4", output_path)
51
+ if not os.path.exists(output_path):
52
+ return None, None, "Generation finished but output file not found."
53
+
54
+ download_link = f"<a href='file/{output_path}' download>πŸ“₯ Download Video</a>"
55
+ return output_path, download_link, "Text-to-Video generated successfully!"
56
 
57
  @spaces.GPU(duration=get_duration)
58
+ def generate_i2v(image, prompt, size="1280*704", duration_seconds=5, steps=25):
59
  if image is None or not prompt.strip():
60
  return None, None, "Please upload an image and enter a prompt."
61
 
 
63
  image_path = os.path.join(temp_dir, "input.jpg")
64
  image.save(image_path)
65
 
66
+ output_path = os.path.join(temp_dir, "output.mp4")
67
+
68
  cmd = [
69
  "python", "generate.py",
70
  "--task", "ti2v-5B",
 
74
  "--convert_model_dtype",
75
  "--t5_cpu",
76
  "--image", image_path,
77
+ "--prompt", prompt
 
 
78
  ]
79
+ print(f"[I2V] Running command: {' '.join(cmd)}")
80
+
81
+ try:
82
+ subprocess.run(cmd, check=True)
83
+ except subprocess.CalledProcessError as e:
84
+ return None, None, f"Error during I2V generation: {e}"
85
+
86
+ if os.path.exists("output.mp4"):
87
+ os.rename("output.mp4", output_path)
88
+ if not os.path.exists(output_path):
89
+ return None, None, "Generation finished but output file not found."
90
+
91
+ download_link = f"<a href='file/{output_path}' download>πŸ“₯ Download Video</a>"
92
+ return output_path, download_link, "Image-to-Video generated successfully!"
93
 
94
+ # ---------------- Step 4: Gradio UI ----------------
95
  with gr.Blocks() as demo:
96
  gr.Markdown("## πŸŽ₯ Wan2.2-TI2V-5B Video Generator")
97
+ gr.Markdown("Choose **Text-to-Video** or **Image-to-Video** mode below.")
98
 
99
  with gr.Tab("Text-to-Video"):
100
  t2v_prompt = gr.Textbox(
 
103
  )
104
  t2v_size = gr.Textbox(label="Video Size", value="1280*704")
105
  t2v_duration = gr.Number(label="Video Length (seconds)", value=5)
106
+ t2v_steps = gr.Number(label="Inference Steps", value=25)
107
  t2v_btn = gr.Button("Generate from Text")
108
+ t2v_video = gr.Video(label="Generated Video")
109
+ t2v_download = gr.HTML()
110
  t2v_status = gr.Textbox(label="Status")
111
  t2v_btn.click(
112
  generate_t2v,
113
+ [t2v_prompt, t2v_size, t2v_duration, t2v_steps],
114
  [t2v_video, t2v_download, t2v_status]
115
  )
116
 
 
129
  )
130
  i2v_size = gr.Textbox(label="Video Size", value="1280*704")
131
  i2v_duration = gr.Number(label="Video Length (seconds)", value=5)
132
+ i2v_steps = gr.Number(label="Inference Steps", value=25)
133
  i2v_btn = gr.Button("Generate from Image")
134
+ i2v_video = gr.Video(label="Generated Video")
135
+ i2v_download = gr.HTML()
136
  i2v_status = gr.Textbox(label="Status")
137
  i2v_btn.click(
138
  generate_i2v,
139
+ [i2v_image, i2v_prompt, i2v_size, i2v_duration, i2v_steps],
140
  [i2v_video, i2v_download, i2v_status]
141
  )
142