laloadrianmorales commited on
Commit
a9779fe
ยท
verified ยท
1 Parent(s): 21ee3f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -66
app.py CHANGED
@@ -6,7 +6,7 @@ from PIL import Image
6
  import tempfile
7
  import subprocess
8
  import sys
9
- from huggingface_hub import snapshot_download, hf_hub_download
10
  import shutil
11
 
12
  # Configuration
@@ -63,20 +63,17 @@ def download_and_setup_model():
63
  print(f"โŒ Setup failed: {e}")
64
  return False
65
 
66
- @spaces.GPU(duration=120) # Allocate GPU for 2 minutes max
67
- def generate_video(input_image, num_frames, seed, progress=gr.Progress()):
68
  """Generate video using Matrix-Game-2.0"""
69
 
70
  if input_image is None:
71
  return None, "โŒ Please upload an input image first"
72
 
73
  # Setup model if not already done
74
- progress(0.1, desc="๐Ÿ”ง Setting up model...")
75
  if not download_and_setup_model():
76
  return None, "โŒ Failed to setup model"
77
 
78
- progress(0.2, desc="๐Ÿ“ท Processing input image...")
79
-
80
  try:
81
  # Create temporary directories
82
  temp_dir = tempfile.mkdtemp(prefix="matrix_gen_")
@@ -84,7 +81,7 @@ def generate_video(input_image, num_frames, seed, progress=gr.Progress()):
84
  os.makedirs(output_dir, exist_ok=True)
85
 
86
  # Prepare input image
87
- if max(input_image.size) > 512: # Resize for faster processing
88
  ratio = 512 / max(input_image.size)
89
  new_size = (int(input_image.size[0] * ratio), int(input_image.size[1] * ratio))
90
  input_image = input_image.resize(new_size, Image.Resampling.LANCZOS)
@@ -92,18 +89,16 @@ def generate_video(input_image, num_frames, seed, progress=gr.Progress()):
92
  input_path = os.path.join(temp_dir, "input.jpg")
93
  input_image.save(input_path, "JPEG", quality=95)
94
 
95
- progress(0.4, desc="๐Ÿš€ Generating video...")
96
-
97
  # Find the inference script and config
98
  matrix_dir = os.path.join("Matrix-Game", "Matrix-Game-2")
99
 
100
- # Basic inference command (simplified)
101
  cmd = [
102
  sys.executable,
103
  os.path.join(matrix_dir, "inference.py"),
104
  "--img_path", input_path,
105
  "--output_folder", output_dir,
106
- "--num_output_frames", str(min(num_frames, 100)), # Limit frames for HF Spaces
107
  "--seed", str(seed)
108
  ]
109
 
@@ -120,19 +115,15 @@ def generate_video(input_image, num_frames, seed, progress=gr.Progress()):
120
  if model_path:
121
  cmd.extend(["--pretrained_model_path", model_path])
122
 
123
- progress(0.6, desc="๐ŸŽฌ Running inference...")
124
-
125
  # Execute with timeout
126
  process = subprocess.run(
127
  cmd,
128
  capture_output=True,
129
  text=True,
130
- timeout=300, # 5 minute timeout
131
  cwd=matrix_dir
132
  )
133
 
134
- progress(0.9, desc="๐Ÿ“น Finalizing video...")
135
-
136
  # Find output video
137
  video_files = []
138
  for root, dirs, files in os.walk(output_dir):
@@ -145,22 +136,11 @@ def generate_video(input_image, num_frames, seed, progress=gr.Progress()):
145
  final_output = f"output_{seed}.mp4"
146
  shutil.copy(video_files[0], final_output)
147
 
148
- log = f"""
149
- โœ… **Generation Successful!**
150
- ๐Ÿ“Š Input: {input_image.size}
151
- ๐ŸŽฌ Frames: {num_frames}
152
- ๐ŸŽฒ Seed: {seed}
153
- ๐Ÿ“ Output: {final_output}
154
- """
155
 
156
- progress(1.0, desc="โœ… Complete!")
157
  return final_output, log
158
  else:
159
- error_log = f"""
160
- โŒ **Generation Failed**
161
- ๐Ÿ“ Error output: {process.stderr[:500] if process.stderr else 'No error details'}
162
- ๐Ÿ’ญ Try adjusting parameters or using a different input image
163
- """
164
  return None, error_log
165
 
166
  except subprocess.TimeoutExpired:
@@ -172,7 +152,7 @@ def generate_video(input_image, num_frames, seed, progress=gr.Progress()):
172
  if 'temp_dir' in locals() and os.path.exists(temp_dir):
173
  shutil.rmtree(temp_dir, ignore_errors=True)
174
 
175
- # Gradio Interface
176
  def create_interface():
177
  with gr.Blocks(title="Matrix-Game-2.0") as interface:
178
 
@@ -185,49 +165,21 @@ def create_interface():
185
  """)
186
 
187
  with gr.Row():
188
- with gr.Column(scale=1):
189
  gr.Markdown("### ๐Ÿ“ธ Input")
190
- input_image = gr.Image(
191
- label="Input Image",
192
- type="pil"
193
- )
194
 
195
  gr.Markdown("### โš™๏ธ Settings")
196
  with gr.Row():
197
- num_frames = gr.Slider(
198
- minimum=25,
199
- maximum=100,
200
- value=50,
201
- step=25,
202
- label="Number of Frames"
203
- )
204
- seed = gr.Number(
205
- value=42,
206
- label="Seed",
207
- precision=0
208
- )
209
 
210
- generate_btn = gr.Button(
211
- "๐Ÿš€ Generate Video",
212
- variant="primary"
213
- )
214
 
215
- gr.Markdown("""
216
- ### ๐Ÿ’ก Tips
217
- - Use clear, well-lit images
218
- - Landscapes and scenes work best
219
- - Lower frame counts = faster generation
220
- - Try different seeds for variety
221
- """)
222
-
223
- with gr.Column(scale=1):
224
  gr.Markdown("### ๐ŸŽฌ Generated Video")
225
  output_video = gr.Video(label="Result")
226
-
227
- status_log = gr.Textbox(
228
- label="Status Log",
229
- lines=8
230
- )
231
 
232
  # Event handlers
233
  generate_btn.click(
@@ -241,4 +193,4 @@ def create_interface():
241
  # Launch the app
242
  if __name__ == "__main__":
243
  demo = create_interface()
244
- demo.launch()
 
6
  import tempfile
7
  import subprocess
8
  import sys
9
+ from huggingface_hub import snapshot_download
10
  import shutil
11
 
12
  # Configuration
 
63
  print(f"โŒ Setup failed: {e}")
64
  return False
65
 
66
+ @spaces.GPU(duration=120)
67
+ def generate_video(input_image, num_frames, seed):
68
  """Generate video using Matrix-Game-2.0"""
69
 
70
  if input_image is None:
71
  return None, "โŒ Please upload an input image first"
72
 
73
  # Setup model if not already done
 
74
  if not download_and_setup_model():
75
  return None, "โŒ Failed to setup model"
76
 
 
 
77
  try:
78
  # Create temporary directories
79
  temp_dir = tempfile.mkdtemp(prefix="matrix_gen_")
 
81
  os.makedirs(output_dir, exist_ok=True)
82
 
83
  # Prepare input image
84
+ if max(input_image.size) > 512:
85
  ratio = 512 / max(input_image.size)
86
  new_size = (int(input_image.size[0] * ratio), int(input_image.size[1] * ratio))
87
  input_image = input_image.resize(new_size, Image.Resampling.LANCZOS)
 
89
  input_path = os.path.join(temp_dir, "input.jpg")
90
  input_image.save(input_path, "JPEG", quality=95)
91
 
 
 
92
  # Find the inference script and config
93
  matrix_dir = os.path.join("Matrix-Game", "Matrix-Game-2")
94
 
95
+ # Basic inference command
96
  cmd = [
97
  sys.executable,
98
  os.path.join(matrix_dir, "inference.py"),
99
  "--img_path", input_path,
100
  "--output_folder", output_dir,
101
+ "--num_output_frames", str(min(num_frames, 100)),
102
  "--seed", str(seed)
103
  ]
104
 
 
115
  if model_path:
116
  cmd.extend(["--pretrained_model_path", model_path])
117
 
 
 
118
  # Execute with timeout
119
  process = subprocess.run(
120
  cmd,
121
  capture_output=True,
122
  text=True,
123
+ timeout=300,
124
  cwd=matrix_dir
125
  )
126
 
 
 
127
  # Find output video
128
  video_files = []
129
  for root, dirs, files in os.walk(output_dir):
 
136
  final_output = f"output_{seed}.mp4"
137
  shutil.copy(video_files[0], final_output)
138
 
139
+ log = f"โœ… Generation Successful!\n๐Ÿ“Š Input: {input_image.size}\n๐ŸŽฌ Frames: {num_frames}\n๐ŸŽฒ Seed: {seed}\n๐Ÿ“ Output: {final_output}"
 
 
 
 
 
 
140
 
 
141
  return final_output, log
142
  else:
143
+ error_log = f"โŒ Generation Failed\n๐Ÿ“ Error output: {process.stderr[:500] if process.stderr else 'No error details'}\n๐Ÿ’ญ Try adjusting parameters or using a different input image"
 
 
 
 
144
  return None, error_log
145
 
146
  except subprocess.TimeoutExpired:
 
152
  if 'temp_dir' in locals() and os.path.exists(temp_dir):
153
  shutil.rmtree(temp_dir, ignore_errors=True)
154
 
155
+ # Simple Gradio Interface
156
  def create_interface():
157
  with gr.Blocks(title="Matrix-Game-2.0") as interface:
158
 
 
165
  """)
166
 
167
  with gr.Row():
168
+ with gr.Column():
169
  gr.Markdown("### ๐Ÿ“ธ Input")
170
+ input_image = gr.Image(label="Input Image", type="pil")
 
 
 
171
 
172
  gr.Markdown("### โš™๏ธ Settings")
173
  with gr.Row():
174
+ num_frames = gr.Slider(25, 100, 50, step=25, label="Number of Frames")
175
+ seed = gr.Number(value=42, label="Seed", precision=0)
 
 
 
 
 
 
 
 
 
 
176
 
177
+ generate_btn = gr.Button("๐Ÿš€ Generate Video", variant="primary")
 
 
 
178
 
179
+ with gr.Column():
 
 
 
 
 
 
 
 
180
  gr.Markdown("### ๐ŸŽฌ Generated Video")
181
  output_video = gr.Video(label="Result")
182
+ status_log = gr.Textbox(label="Status Log", lines=8)
 
 
 
 
183
 
184
  # Event handlers
185
  generate_btn.click(
 
193
  # Launch the app
194
  if __name__ == "__main__":
195
  demo = create_interface()
196
+ demo.launch(share=True)