rahul7star commited on
Commit
1bf3e32
Β·
verified Β·
1 Parent(s): 3610958
Files changed (1) hide show
  1. app.py +25 -15
app.py CHANGED
@@ -4,6 +4,7 @@ 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"
@@ -20,6 +21,14 @@ def get_duration(prompt, size, duration_seconds, steps):
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):
@@ -27,7 +36,6 @@ def generate_t2v(prompt, size="1280*704", duration_seconds=5, steps=25):
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",
@@ -46,13 +54,15 @@ def generate_t2v(prompt, size="1280*704", duration_seconds=5, steps=25):
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):
@@ -63,8 +73,6 @@ def generate_i2v(image, prompt, size="1280*704", duration_seconds=5, steps=25):
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",
@@ -83,13 +91,15 @@ def generate_i2v(image, prompt, size="1280*704", duration_seconds=5, steps=25):
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:
 
4
  import tempfile
5
  from huggingface_hub import snapshot_download
6
  import gradio as gr
7
+ import glob
8
 
9
  # ---------------- Step 1: Download Model ----------------
10
  repo_id = "Wan-AI/Wan2.2-TI2V-5B"
 
21
  h, w = 704, 1280
22
  return int(duration_seconds) * int(steps) * 2.25 + 5
23
 
24
+ # Helper to find the most recent .mp4 file in cwd
25
+ def find_latest_mp4(search_path="."):
26
+ mp4_files = glob.glob(os.path.join(search_path, "*.mp4"))
27
+ if not mp4_files:
28
+ return None
29
+ latest_file = max(mp4_files, key=os.path.getmtime)
30
+ return latest_file
31
+
32
  # ---------------- Step 3: Generation Functions ----------------
33
  @spaces.GPU(duration=get_duration)
34
  def generate_t2v(prompt, size="1280*704", duration_seconds=5, steps=25):
 
36
  return None, None, "Please enter a prompt."
37
 
38
  temp_dir = tempfile.mkdtemp()
 
39
 
40
  cmd = [
41
  "python", "generate.py",
 
54
  except subprocess.CalledProcessError as e:
55
  return None, None, f"Error during T2V generation: {e}"
56
 
57
+ video_file = find_latest_mp4()
58
+ if video_file is None:
59
+ return None, None, "Generation finished but no video file found."
60
+
61
+ dest_path = os.path.join(temp_dir, os.path.basename(video_file))
62
+ os.rename(video_file, dest_path)
63
 
64
+ download_link = f"<a href='file/{dest_path}' download>πŸ“₯ Download Video</a>"
65
+ return dest_path, download_link, "Text-to-Video generated successfully!"
66
 
67
  @spaces.GPU(duration=get_duration)
68
  def generate_i2v(image, prompt, size="1280*704", duration_seconds=5, steps=25):
 
73
  image_path = os.path.join(temp_dir, "input.jpg")
74
  image.save(image_path)
75
 
 
 
76
  cmd = [
77
  "python", "generate.py",
78
  "--task", "ti2v-5B",
 
91
  except subprocess.CalledProcessError as e:
92
  return None, None, f"Error during I2V generation: {e}"
93
 
94
+ video_file = find_latest_mp4()
95
+ if video_file is None:
96
+ return None, None, "Generation finished but no video file found."
97
+
98
+ dest_path = os.path.join(temp_dir, os.path.basename(video_file))
99
+ os.rename(video_file, dest_path)
100
 
101
+ download_link = f"<a href='file/{dest_path}' download>πŸ“₯ Download Video</a>"
102
+ return dest_path, download_link, "Image-to-Video generated successfully!"
103
 
104
  # ---------------- Step 4: Gradio UI ----------------
105
  with gr.Blocks() as demo: