File size: 15,663 Bytes
ec8483d a760dcb ec8483d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
import os
import gradio as gr
import torch
import spaces
from PIL import Image
import tempfile
import subprocess
import sys
import time
import shutil
from huggingface_hub import snapshot_download
# Configuration
MODEL_NAME = "Skywork/Matrix-Game-2.0"
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
print(f"๐ Matrix-Game-2.0 Clean Setup")
print(f"๐ฑ Device: {DEVICE}")
print(f"๐ฅ CUDA: {torch.cuda.is_available()}")
if torch.cuda.is_available():
print(f"๐ฎ GPU: {torch.cuda.get_device_name()}")
@spaces.GPU(duration=900) # 15 minutes
def generate_matrix_video(input_image, num_frames, seed, use_streaming):
"""
Matrix-Game-2.0 generation following official workflow
"""
if input_image is None:
return None, "โ Please upload an input image"
log = ["๐ **MATRIX-GAME-2.0 CLEAN GENERATION**\n"]
original_cwd = os.getcwd()
try:
# Step 1: Clone repository (official workflow)
log.append("๐ฅ **STEP 1: git clone Matrix-Game**")
base_dir = os.getcwd()
matrix_root = os.path.join(base_dir, "Matrix-Game")
# Clean previous installation
if os.path.exists(matrix_root):
shutil.rmtree(matrix_root)
log.append("๐งน Cleaned previous installation")
# Clone fresh repository
clone_result = subprocess.run([
'git', 'clone', 'https://github.com/SkyworkAI/Matrix-Game.git'
], capture_output=True, text=True, timeout=300, cwd=base_dir)
if clone_result.returncode != 0:
log.append(f"โ Clone failed: {clone_result.stderr}")
return None, "\n".join(log)
log.append("โ
Repository cloned successfully")
# Step 2: cd Matrix-Game/Matrix-Game-2 (official workflow)
log.append("\n๐ **STEP 2: cd Matrix-Game/Matrix-Game-2**")
matrix_2_dir = os.path.join(matrix_root, "Matrix-Game-2")
if not os.path.exists(matrix_2_dir):
log.append(f"โ Matrix-Game-2 not found: {matrix_2_dir}")
return None, "\n".join(log)
# Change to Matrix-Game-2 directory (as per official instructions)
os.chdir(matrix_2_dir)
log.append(f"โ
Changed to: {os.getcwd()}")
# Verify key files exist
key_files = ['inference.py', 'requirements.txt', 'setup.py', 'configs']
for file in key_files:
if os.path.exists(file):
log.append(f"โ
{file} found")
else:
log.append(f"โ {file} missing")
return None, "\n".join(log)
# Step 3: pip install -r requirements.txt (official workflow)
log.append("\n๐ฆ **STEP 3: pip install -r requirements.txt**")
req_result = subprocess.run([
sys.executable, "-m", "pip", "install", "-r", "requirements.txt",
"--no-cache-dir", "--force-reinstall"
], capture_output=True, text=True, timeout=600)
if req_result.returncode == 0:
log.append("โ
Requirements installed successfully")
else:
log.append(f"โ ๏ธ Requirements warning (continuing): {req_result.stderr[:200]}")
# Step 3.5: Install missing critical dependencies
log.append("\n๐ง **STEP 3.5: Install missing dependencies**")
critical_deps = [
"omegaconf", "einops", "transformers", "accelerate",
"diffusers", "opencv-python", "imageio", "imageio-ffmpeg"
]
for dep in critical_deps:
try:
dep_result = subprocess.run([
sys.executable, "-m", "pip", "install", dep, "--no-cache-dir"
], capture_output=True, text=True, timeout=120)
if dep_result.returncode == 0:
log.append(f"โ
{dep} installed")
else:
log.append(f"โ ๏ธ {dep} warning: {dep_result.stderr[:100]}")
except Exception as e:
log.append(f"โ ๏ธ {dep} error: {str(e)[:100]}")
# Step 4: python setup.py develop (official workflow)
log.append("\n๐ง **STEP 4: python setup.py develop**")
setup_result = subprocess.run([
sys.executable, "setup.py", "develop"
], capture_output=True, text=True, timeout=300)
if setup_result.returncode == 0:
log.append("โ
Setup.py completed successfully")
else:
log.append(f"โ ๏ธ Setup.py warning (continuing): {setup_result.stderr[:200]}")
# Step 5: Download model weights
log.append("\n๐ฅ **STEP 5: Download model weights**")
try:
model_path = snapshot_download(
repo_id=MODEL_NAME,
cache_dir=os.path.join(base_dir, "model_cache"),
force_download=False
)
log.append(f"โ
Model downloaded: {os.path.basename(model_path)}")
except Exception as e:
log.append(f"โ Model download failed: {e}")
return None, "\n".join(log)
# Step 6: Prepare input image
log.append("\n๐พ **STEP 6: Prepare input image**")
temp_dir = tempfile.mkdtemp(prefix="matrix_game_")
input_path = os.path.join(temp_dir, "input.jpg")
# Create outputs directory (relative path as per official instructions)
outputs_dir = "outputs"
if os.path.exists(outputs_dir):
shutil.rmtree(outputs_dir)
os.makedirs(outputs_dir, exist_ok=True)
# Resize image if too large (for memory efficiency)
original_size = input_image.size
if max(input_image.size) > 1024:
ratio = 1024 / max(input_image.size)
new_size = (int(input_image.size[0] * ratio), int(input_image.size[1] * ratio))
input_image = input_image.resize(new_size, Image.Resampling.LANCZOS)
log.append(f"๐ท Image resized: {original_size} โ {input_image.size}")
else:
log.append(f"๐ท Image size: {input_image.size}")
input_image.save(input_path, "JPEG", quality=90)
# Step 7: Configure inference paths
log.append("\n๐ง **STEP 7: Configure inference**")
# Find config file (relative path)
config_dir = "configs/inference_yaml"
config_path = None
if os.path.exists(config_dir):
yaml_files = [f for f in os.listdir(config_dir) if f.endswith(('.yaml', '.yml'))]
if yaml_files:
config_path = os.path.join(config_dir, yaml_files[0])
log.append(f"โ
Config found: {config_path}")
if not config_path:
log.append(f"โ No config found in {config_dir}")
return None, "\n".join(log)
# Find checkpoint
checkpoint_path = None
for root, dirs, files in os.walk(model_path):
for file in files:
if file.endswith(('.bin', '.pt', '.pth', '.safetensors')):
checkpoint_path = os.path.join(root, file)
break
if checkpoint_path:
break
if not checkpoint_path:
log.append(f"โ No checkpoint found in {model_path}")
return None, "\n".join(log)
log.append(f"โ
Checkpoint: {os.path.basename(checkpoint_path)}")
# Step 8: Run inference (official workflow)
log.append("\n๐ **STEP 8: Matrix-Game inference**")
script_name = "inference_streaming.py" if use_streaming else "inference.py"
# Build command exactly as per official instructions
cmd = [sys.executable, script_name]
cmd.extend([
"--config_path", config_path,
"--checkpoint_path", checkpoint_path,
"--img_path", input_path,
"--output_folder", outputs_dir,
"--seed", str(seed),
"--pretrained_model_path", model_path
])
# Add num_output_frames for regular inference
if not use_streaming:
cmd.extend(["--num_output_frames", str(num_frames)])
log.append(f"๐ง Running: python {script_name}")
log.append(f"๐ Working directory: {os.getcwd()}")
log.append(f"โ๏ธ Frames: {num_frames} | Seed: {seed} | Streaming: {use_streaming}")
# Set environment for subprocess
env = os.environ.copy()
env['PYTHONPATH'] = matrix_2_dir
try:
# Run the inference with proper timeout
log.append("โ
Starting Matrix-Game generation...")
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
cwd=matrix_2_dir,
env=env
)
# Wait for completion with timeout
try:
stdout, stderr = process.communicate(timeout=900) # 15 minutes
except subprocess.TimeoutExpired:
process.terminate()
process.wait()
log.append("โฐ Timeout: Generation took too long (>15 min)")
return None, "\n".join(log)
log.append(f"๐ง Process completed with code: {process.returncode}")
if process.returncode != 0:
log.append(f"โ Inference failed:")
log.append(f"Error: {stderr[:500]}")
log.append(f"Output: {stdout[:200]}")
return None, "\n".join(log)
log.append("โ
Inference completed successfully!")
except Exception as e:
log.append(f"โ Process error: {str(e)}")
return None, "\n".join(log)
# Step 9: Find generated videos
log.append("\n๐ **STEP 9: Find generated videos**")
video_files = []
outputs_abs = os.path.join(matrix_2_dir, outputs_dir)
for root, dirs, files in os.walk(outputs_abs):
for file in files:
if file.lower().endswith(('.mp4', '.avi', '.mov', '.mkv', '.webm')):
video_path = os.path.join(root, file)
video_files.append(video_path)
log.append(f"๐ฅ Video found: {file}")
if video_files:
final_video = video_files[0]
file_size = os.path.getsize(final_video) / 1e6
log.append(f"\n๐ **SUCCESS!**")
log.append(f"๐ Video size: {file_size:.1f} MB")
log.append(f"๐ท Input: {original_size}")
log.append(f"๐ฎ GPU: {torch.cuda.get_device_name() if torch.cuda.is_available() else 'CPU'}")
log.append(f"โจ Matrix-Game-2.0 generation complete!")
return final_video, "\n".join(log)
else:
log.append("โ No videos generated")
# Debug: list all files in outputs
if os.path.exists(outputs_abs):
all_files = []
for root, dirs, files in os.walk(outputs_abs):
for file in files:
all_files.append(file)
log.append(f"๐ Files in outputs: {all_files}")
return None, "\n".join(log)
except Exception as e:
log.append(f"\nโ **CRITICAL ERROR:** {str(e)}")
import traceback
log.append(f"๐ Full traceback: {traceback.format_exc()}")
return None, "\n".join(log)
finally:
# Always return to original directory
os.chdir(original_cwd)
# Clean Gradio interface (avoiding Gradio 4.44.0 bugs)
with gr.Blocks(
title="Matrix-Game-2.0 Clean",
css=".container { max-width: 1200px; margin: auto; }"
) as demo:
gr.HTML("""
<div style="text-align: center; padding: 30px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 15px; margin-bottom: 30px;">
<h1 style="margin: 0; font-size: 2.8em;">๐ฎ Matrix-Game-2.0</h1>
<p style="margin: 15px 0; font-size: 1.3em;">Interactive World Model - Clean Implementation</p>
<p style="margin: 0; opacity: 0.9;">โก Real-time generation at 25 FPS | ๐ฏ Precise control | ๐ Complex environments</p>
</div>
""")
with gr.Row():
with gr.Column():
gr.Markdown("### ๐ท Input Configuration")
input_image = gr.Image(
label="Input Image",
type="pil",
height=300
)
gr.Markdown("### โ๏ธ Generation Settings")
with gr.Row():
num_frames = gr.Slider(
minimum=50,
maximum=300,
value=150,
step=25,
label="Number of Frames"
)
seed = gr.Number(
value=42,
label="Seed",
precision=0
)
use_streaming = gr.Checkbox(
label="Streaming Mode",
value=False
)
generate_btn = gr.Button(
"๐ Generate Matrix-Game Video",
variant="primary",
size="lg"
)
gr.Markdown("""
### ๐ก Usage Tips:
- **Upload**: Clear images with good depth and structure
- **Frames**: 150 frames โ 6 seconds at 25 FPS
- **Time**: Generation takes 5-15 minutes depending on complexity
- **Streaming**: Continuous generation mode (experimental)
- **Best results**: Landscapes, cityscapes, or structured scenes
""")
with gr.Column():
gr.Markdown("### ๐ฅ Generated Video")
output_video = gr.Video(
label="Matrix-Game Video Output",
height=400
)
gr.Markdown("### ๐ Generation Log")
status_log = gr.Textbox(
label="Detailed Status and Progress",
lines=20,
max_lines=25,
show_copy_button=True
)
# Connect the generation function
generate_btn.click(
fn=generate_matrix_video,
inputs=[input_image, num_frames, seed, use_streaming],
outputs=[output_video, status_log],
show_progress=True
)
gr.HTML("""
<div style="text-align: center; padding: 25px; margin-top: 30px; border-top: 2px solid #eee;">
<p style="margin-bottom: 15px;">
๐ <a href="https://arxiv.org/pdf/2508.13009" target="_blank" style="text-decoration: none;">Research Paper</a> |
๐ป <a href="https://github.com/SkyworkAI/Matrix-Game" target="_blank" style="text-decoration: none;">GitHub Repository</a> |
๐ค <a href="https://huggingface.co/Skywork/Matrix-Game-2.0" target="_blank" style="text-decoration: none;">Model Hub</a>
</p>
<p style="margin: 0;"><em>โก Powered by Skywork AI | Clean Implementation avoiding setup issues</em></p>
</div>
""")
if __name__ == "__main__":
demo.launch(share=True) |