Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,116 +6,181 @@ import numpy as np
|
|
| 6 |
import tempfile
|
| 7 |
from typing import Optional, Tuple
|
| 8 |
import time
|
| 9 |
-
import subprocess
|
| 10 |
-
import sys
|
| 11 |
|
| 12 |
# ZeroGPU import
|
| 13 |
try:
|
| 14 |
import spaces
|
| 15 |
SPACES_AVAILABLE = True
|
| 16 |
-
print("β
Spaces library loaded successfully")
|
| 17 |
except ImportError:
|
| 18 |
-
print("β οΈ Spaces library not available")
|
| 19 |
SPACES_AVAILABLE = False
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
# Environment checks
|
| 28 |
IS_ZERO_GPU = os.environ.get("SPACES_ZERO_GPU") == "true"
|
| 29 |
IS_SPACES = os.environ.get("SPACE_ID") is not None
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def check_and_install_requirements():
|
| 34 |
-
"""Check and install missing requirements"""
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
try:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
-
print(f"
|
| 50 |
-
return
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
def
|
| 53 |
-
"""
|
| 54 |
-
|
| 55 |
-
# First, ensure requirements are installed
|
| 56 |
-
if not check_and_install_requirements():
|
| 57 |
-
return None, "Failed to install required packages"
|
| 58 |
-
|
| 59 |
try:
|
| 60 |
-
print("π
|
| 61 |
|
| 62 |
-
|
| 63 |
-
from diffusers import LTXVideoPipeline
|
| 64 |
-
import torch
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
print("π₯ Loading pipeline...")
|
| 75 |
-
pipe = LTXVideoPipeline.from_pretrained(
|
| 76 |
-
model_id,
|
| 77 |
-
torch_dtype=torch.bfloat16,
|
| 78 |
-
use_safetensors=True,
|
| 79 |
-
variant="fp16"
|
| 80 |
-
)
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
pipe.enable_vae_tiling()
|
| 91 |
-
print("β‘ Memory optimizations enabled")
|
| 92 |
-
except Exception as e:
|
| 93 |
-
print(f"β οΈ Some optimizations failed: {e}")
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
print(f"β {error_msg}")
|
| 101 |
-
return None, error_msg
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
-
|
| 105 |
-
print(f"β {error_msg}")
|
| 106 |
-
return None, error_msg
|
| 107 |
|
| 108 |
-
# Global
|
| 109 |
MODEL = None
|
|
|
|
|
|
|
| 110 |
MODEL_ERROR = None
|
| 111 |
|
| 112 |
def initialize_model():
|
| 113 |
-
"""Initialize model
|
| 114 |
-
global MODEL, MODEL_ERROR
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
@spaces.GPU(duration=120) if SPACES_AVAILABLE else lambda x: x
|
| 121 |
def generate_video(
|
|
@@ -128,45 +193,85 @@ def generate_video(
|
|
| 128 |
guidance_scale: float = 7.5,
|
| 129 |
seed: int = -1
|
| 130 |
) -> Tuple[Optional[str], str]:
|
| 131 |
-
"""Generate video
|
| 132 |
-
|
| 133 |
-
global MODEL, MODEL_ERROR
|
| 134 |
|
| 135 |
-
# Initialize model
|
| 136 |
-
|
| 137 |
-
error_msg = f"β Model initialization failed: {MODEL_ERROR or 'Unknown error'}"
|
| 138 |
-
return None, error_msg
|
| 139 |
|
| 140 |
# Input validation
|
| 141 |
if not prompt.strip():
|
| 142 |
return None, "β Please enter a valid prompt."
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
# Limit parameters for stability
|
| 148 |
-
num_frames = min(max(num_frames, 8), 24)
|
| 149 |
-
num_inference_steps = min(max(num_inference_steps, 10), 25)
|
| 150 |
height = min(max(height, 256), 768)
|
| 151 |
width = min(max(width, 256), 768)
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
try:
|
| 154 |
# Clear memory
|
| 155 |
if torch.cuda.is_available():
|
| 156 |
torch.cuda.empty_cache()
|
| 157 |
gc.collect()
|
| 158 |
|
| 159 |
-
|
| 160 |
-
if seed == -1:
|
| 161 |
-
seed = np.random.randint(0, 2**32 - 1)
|
| 162 |
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
result = MODEL(
|
| 171 |
prompt=prompt,
|
| 172 |
negative_prompt=negative_prompt if negative_prompt.strip() else None,
|
|
@@ -175,49 +280,32 @@ def generate_video(
|
|
| 175 |
width=width,
|
| 176 |
num_inference_steps=num_inference_steps,
|
| 177 |
guidance_scale=guidance_scale,
|
| 178 |
-
generator=generator
|
| 179 |
)
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
video_frames = result.frames[0]
|
| 186 |
-
|
| 187 |
-
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp_file:
|
| 188 |
-
try:
|
| 189 |
from diffusers.utils import export_to_video
|
| 190 |
export_to_video(video_frames, tmp_file.name, fps=8)
|
| 191 |
video_path = tmp_file.name
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
# Clear memory
|
| 198 |
-
if torch.cuda.is_available():
|
| 199 |
-
torch.cuda.empty_cache()
|
| 200 |
-
gc.collect()
|
| 201 |
-
|
| 202 |
-
success_msg = f"""β
Video generated successfully!
|
| 203 |
|
| 204 |
-
π
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
return video_path, success_msg
|
| 215 |
-
|
| 216 |
-
except torch.cuda.OutOfMemoryError:
|
| 217 |
-
if torch.cuda.is_available():
|
| 218 |
-
torch.cuda.empty_cache()
|
| 219 |
-
gc.collect()
|
| 220 |
-
return None, "β GPU memory exceeded. Try reducing frames/resolution or try again in a moment."
|
| 221 |
|
| 222 |
except Exception as e:
|
| 223 |
if torch.cuda.is_available():
|
|
@@ -226,165 +314,115 @@ def generate_video(
|
|
| 226 |
return None, f"β Generation failed: {str(e)}"
|
| 227 |
|
| 228 |
def get_system_info():
|
| 229 |
-
"""Get
|
| 230 |
-
|
| 231 |
-
# Check package versions
|
| 232 |
-
package_info = {}
|
| 233 |
-
try:
|
| 234 |
-
import diffusers
|
| 235 |
-
package_info['diffusers'] = diffusers.__version__
|
| 236 |
-
except ImportError:
|
| 237 |
-
package_info['diffusers'] = 'β Not installed'
|
| 238 |
|
|
|
|
| 239 |
try:
|
| 240 |
-
import
|
| 241 |
-
|
| 242 |
-
except ImportError:
|
| 243 |
-
package_info['transformers'] = 'β Not installed'
|
| 244 |
-
|
| 245 |
-
# GPU info
|
| 246 |
-
gpu_info = "β Not available"
|
| 247 |
-
gpu_memory = 0
|
| 248 |
-
if torch.cuda.is_available():
|
| 249 |
try:
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
except:
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
-
return f"""
|
|
|
|
| 256 |
|
| 257 |
**Environment:**
|
| 258 |
-
- π ZeroGPU: {'β
Active' if IS_ZERO_GPU else 'β Not detected'}
|
| 259 |
- π HF Spaces: {'β
' if IS_SPACES else 'β'}
|
| 260 |
- π₯ CUDA: {'β
' if torch.cuda.is_available() else 'β'}
|
| 261 |
-
- π₯οΈ GPU: {gpu_info} ({gpu_memory:.1f} GB)
|
| 262 |
|
| 263 |
**Packages:**
|
| 264 |
- PyTorch: {torch.__version__}
|
| 265 |
-
- Diffusers: {
|
| 266 |
-
-
|
| 267 |
-
- Spaces: {'β
' if SPACES_AVAILABLE else 'β'}
|
| 268 |
|
| 269 |
**Model Status:**
|
| 270 |
-
-
|
|
|
|
| 271 |
|
| 272 |
-
**
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
"""
|
| 277 |
-
results = []
|
| 278 |
-
|
| 279 |
-
# Test torch
|
| 280 |
-
try:
|
| 281 |
-
import torch
|
| 282 |
-
results.append(f"β
PyTorch {torch.__version__}")
|
| 283 |
-
if torch.cuda.is_available():
|
| 284 |
-
results.append(f"β
CUDA {torch.version.cuda}")
|
| 285 |
-
else:
|
| 286 |
-
results.append("β οΈ CUDA not available")
|
| 287 |
-
except Exception as e:
|
| 288 |
-
results.append(f"β PyTorch: {e}")
|
| 289 |
-
|
| 290 |
-
# Test diffusers
|
| 291 |
-
try:
|
| 292 |
-
import diffusers
|
| 293 |
-
results.append(f"β
Diffusers {diffusers.__version__}")
|
| 294 |
-
except Exception as e:
|
| 295 |
-
results.append(f"β Diffusers: {e}")
|
| 296 |
-
|
| 297 |
-
# Test transformers
|
| 298 |
-
try:
|
| 299 |
-
import transformers
|
| 300 |
-
results.append(f"β
Transformers {transformers.__version__}")
|
| 301 |
-
except Exception as e:
|
| 302 |
-
results.append(f"β Transformers: {e}")
|
| 303 |
-
|
| 304 |
-
return "\n".join(results)
|
| 305 |
|
| 306 |
# Create Gradio interface
|
| 307 |
-
with gr.Blocks(title="
|
| 308 |
|
| 309 |
gr.Markdown("""
|
| 310 |
-
#
|
| 311 |
|
| 312 |
-
|
| 313 |
""")
|
| 314 |
|
| 315 |
-
# Status indicator
|
| 316 |
-
with gr.Row():
|
| 317 |
-
gr.Markdown(f"""
|
| 318 |
-
**Status:** {'π’ ZeroGPU Active' if IS_ZERO_GPU else 'π‘ CPU Mode'} |
|
| 319 |
-
**Environment:** {'HF Spaces' if IS_SPACES else 'Local'}
|
| 320 |
-
""")
|
| 321 |
-
|
| 322 |
with gr.Tab("π₯ Generate Video"):
|
| 323 |
with gr.Row():
|
| 324 |
with gr.Column(scale=1):
|
| 325 |
prompt_input = gr.Textbox(
|
| 326 |
label="π Video Prompt",
|
| 327 |
-
placeholder="A
|
| 328 |
-
lines=3
|
| 329 |
-
max_lines=5
|
| 330 |
)
|
| 331 |
|
| 332 |
negative_prompt_input = gr.Textbox(
|
| 333 |
-
label="π« Negative Prompt
|
| 334 |
-
placeholder="blurry, low quality
|
| 335 |
lines=2
|
| 336 |
)
|
| 337 |
|
| 338 |
-
with gr.
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
seed = gr.Number(value=-1, precision=0, label="π² Seed (-1=random)")
|
| 350 |
|
| 351 |
generate_btn = gr.Button("π Generate Video", variant="primary", size="lg")
|
| 352 |
|
| 353 |
with gr.Column(scale=1):
|
| 354 |
video_output = gr.Video(label="π₯ Generated Video", height=400)
|
| 355 |
-
result_text = gr.Textbox(label="π Results", lines=
|
| 356 |
|
| 357 |
-
# Event handlers
|
| 358 |
generate_btn.click(
|
| 359 |
fn=generate_video,
|
| 360 |
inputs=[prompt_input, negative_prompt_input, num_frames, height, width, num_steps, guidance_scale, seed],
|
| 361 |
outputs=[video_output, result_text]
|
| 362 |
)
|
| 363 |
|
| 364 |
-
# Examples
|
| 365 |
gr.Examples(
|
| 366 |
examples=[
|
| 367 |
-
["A peaceful cat
|
| 368 |
-
["Ocean waves at
|
| 369 |
-
["A
|
| 370 |
],
|
| 371 |
inputs=[prompt_input, negative_prompt_input, num_frames, height, width, num_steps, guidance_scale, seed]
|
| 372 |
)
|
| 373 |
|
| 374 |
with gr.Tab("βΉοΈ System Info"):
|
| 375 |
-
info_btn = gr.Button("π Check System"
|
| 376 |
system_output = gr.Markdown()
|
| 377 |
|
| 378 |
info_btn.click(fn=get_system_info, outputs=system_output)
|
| 379 |
demo.load(fn=get_system_info, outputs=system_output)
|
| 380 |
-
|
| 381 |
-
with gr.Tab("π§ Debug"):
|
| 382 |
-
test_btn = gr.Button("π§ͺ Test Dependencies")
|
| 383 |
-
test_output = gr.Textbox(label="Test Results", lines=10)
|
| 384 |
-
|
| 385 |
-
test_btn.click(fn=test_dependencies, outputs=test_output)
|
| 386 |
|
| 387 |
-
# Launch
|
| 388 |
if __name__ == "__main__":
|
| 389 |
demo.queue(max_size=5)
|
| 390 |
demo.launch(
|
|
|
|
| 6 |
import tempfile
|
| 7 |
from typing import Optional, Tuple
|
| 8 |
import time
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# ZeroGPU import
|
| 11 |
try:
|
| 12 |
import spaces
|
| 13 |
SPACES_AVAILABLE = True
|
|
|
|
| 14 |
except ImportError:
|
|
|
|
| 15 |
SPACES_AVAILABLE = False
|
| 16 |
+
class spaces:
|
| 17 |
+
@staticmethod
|
| 18 |
+
def GPU(duration=60):
|
| 19 |
+
def decorator(func):
|
| 20 |
+
return func
|
| 21 |
+
return decorator
|
| 22 |
|
|
|
|
| 23 |
IS_ZERO_GPU = os.environ.get("SPACES_ZERO_GPU") == "true"
|
| 24 |
IS_SPACES = os.environ.get("SPACE_ID") is not None
|
| 25 |
|
| 26 |
+
def load_ltx_model_manual():
|
| 27 |
+
"""Manually load LTX-Video model using transformers"""
|
|
|
|
|
|
|
| 28 |
try:
|
| 29 |
+
print("π Attempting to load LTX-Video with transformers...")
|
| 30 |
+
|
| 31 |
+
from transformers import AutoModel, AutoTokenizer, AutoProcessor
|
| 32 |
+
|
| 33 |
+
model_id = "Lightricks/LTX-Video"
|
| 34 |
+
|
| 35 |
+
# Try loading with AutoModel
|
| 36 |
try:
|
| 37 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 38 |
+
model = AutoModel.from_pretrained(
|
| 39 |
+
model_id,
|
| 40 |
+
torch_dtype=torch.float16,
|
| 41 |
+
low_cpu_mem_usage=True,
|
| 42 |
+
trust_remote_code=True # Important for new models
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
if torch.cuda.is_available():
|
| 46 |
+
model = model.to("cuda")
|
| 47 |
+
|
| 48 |
+
print("β
Model loaded with transformers")
|
| 49 |
+
return model, processor, None
|
| 50 |
+
|
| 51 |
except Exception as e:
|
| 52 |
+
print(f"AutoModel failed: {e}")
|
| 53 |
+
return None, None, str(e)
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return None, None, f"Manual loading failed: {e}"
|
| 57 |
|
| 58 |
+
def load_alternative_video_model():
|
| 59 |
+
"""Load a working alternative video generation model"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
try:
|
| 61 |
+
print("π Loading alternative video model...")
|
| 62 |
|
| 63 |
+
from diffusers import DiffusionPipeline
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# Use Zeroscope or ModelScope as alternatives
|
| 66 |
+
alternatives = [
|
| 67 |
+
"cerspense/zeroscope_v2_576w",
|
| 68 |
+
"damo-vilab/text-to-video-ms-1.7b",
|
| 69 |
+
"ali-vilab/text-to-video-ms-1.7b"
|
| 70 |
+
]
|
| 71 |
|
| 72 |
+
for model_id in alternatives:
|
| 73 |
+
try:
|
| 74 |
+
print(f"Trying {model_id}...")
|
| 75 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 76 |
+
model_id,
|
| 77 |
+
torch_dtype=torch.float16,
|
| 78 |
+
use_safetensors=True,
|
| 79 |
+
variant="fp16"
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
if torch.cuda.is_available():
|
| 83 |
+
pipe = pipe.to("cuda")
|
| 84 |
+
|
| 85 |
+
# Enable optimizations
|
| 86 |
+
pipe.enable_sequential_cpu_offload()
|
| 87 |
+
pipe.enable_vae_slicing()
|
| 88 |
+
|
| 89 |
+
print(f"β
Successfully loaded {model_id}")
|
| 90 |
+
return pipe, model_id, None
|
| 91 |
+
|
| 92 |
+
except Exception as e:
|
| 93 |
+
print(f"Failed to load {model_id}: {e}")
|
| 94 |
+
continue
|
| 95 |
|
| 96 |
+
return None, None, "All alternative models failed"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
except Exception as e:
|
| 99 |
+
return None, None, f"Alternative loading failed: {e}"
|
| 100 |
+
|
| 101 |
+
def create_mock_video(prompt, num_frames=16, width=512, height=512):
|
| 102 |
+
"""Create a mock video for demonstration"""
|
| 103 |
+
try:
|
| 104 |
+
import cv2
|
| 105 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 106 |
|
| 107 |
+
# Create temporary video file
|
| 108 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp_file:
|
| 109 |
+
video_path = tmp_file.name
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
# Video settings
|
| 112 |
+
fps = 8
|
| 113 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 114 |
+
out = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
|
| 115 |
+
|
| 116 |
+
# Color themes
|
| 117 |
+
colors = [(255, 100, 100), (100, 255, 100), (100, 100, 255), (255, 255, 100)]
|
| 118 |
+
|
| 119 |
+
for i in range(num_frames):
|
| 120 |
+
# Create frame
|
| 121 |
+
img = Image.new('RGB', (width, height), color=colors[i % len(colors)])
|
| 122 |
+
draw = ImageDraw.Draw(img)
|
| 123 |
+
|
| 124 |
+
try:
|
| 125 |
+
font = ImageFont.truetype("arial.ttf", 24)
|
| 126 |
+
except:
|
| 127 |
+
font = ImageFont.load_default()
|
| 128 |
+
|
| 129 |
+
# Add text
|
| 130 |
+
draw.text((50, height//2 - 50), f"Frame {i+1}/{num_frames}", fill=(255, 255, 255), font=font)
|
| 131 |
+
draw.text((50, height//2), f"Prompt: {prompt[:30]}...", fill=(255, 255, 255), font=font)
|
| 132 |
+
draw.text((50, height//2 + 50), "DEMO MODE", fill=(0, 0, 0), font=font)
|
| 133 |
+
|
| 134 |
+
# Convert to OpenCV format
|
| 135 |
+
frame = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
| 136 |
+
out.write(frame)
|
| 137 |
|
| 138 |
+
out.release()
|
| 139 |
+
return video_path
|
|
|
|
|
|
|
| 140 |
|
| 141 |
except Exception as e:
|
| 142 |
+
return None
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
# Global variables
|
| 145 |
MODEL = None
|
| 146 |
+
PROCESSOR = None
|
| 147 |
+
MODEL_TYPE = None
|
| 148 |
MODEL_ERROR = None
|
| 149 |
|
| 150 |
def initialize_model():
|
| 151 |
+
"""Initialize model with fallback options"""
|
| 152 |
+
global MODEL, PROCESSOR, MODEL_TYPE, MODEL_ERROR
|
| 153 |
+
|
| 154 |
+
if MODEL is not None:
|
| 155 |
+
return True
|
| 156 |
+
|
| 157 |
+
if MODEL_ERROR is not None:
|
| 158 |
+
return False
|
| 159 |
+
|
| 160 |
+
print("π Initializing video model...")
|
| 161 |
+
|
| 162 |
+
# Strategy 1: Try manual LTX-Video loading
|
| 163 |
+
print("Trying LTX-Video...")
|
| 164 |
+
MODEL, PROCESSOR, error = load_ltx_model_manual()
|
| 165 |
+
if MODEL is not None:
|
| 166 |
+
MODEL_TYPE = "LTX-Video"
|
| 167 |
+
return True
|
| 168 |
+
|
| 169 |
+
print(f"LTX-Video failed: {error}")
|
| 170 |
+
|
| 171 |
+
# Strategy 2: Try alternative models
|
| 172 |
+
print("Trying alternative models...")
|
| 173 |
+
MODEL, MODEL_TYPE, error = load_alternative_video_model()
|
| 174 |
+
if MODEL is not None:
|
| 175 |
+
PROCESSOR = None # Diffusion pipeline doesn't need separate processor
|
| 176 |
+
return True
|
| 177 |
+
|
| 178 |
+
print(f"Alternative models failed: {error}")
|
| 179 |
+
|
| 180 |
+
# Strategy 3: Use mock generation
|
| 181 |
+
MODEL_TYPE = "mock"
|
| 182 |
+
MODEL_ERROR = "All models failed - using demo mode"
|
| 183 |
+
return False
|
| 184 |
|
| 185 |
@spaces.GPU(duration=120) if SPACES_AVAILABLE else lambda x: x
|
| 186 |
def generate_video(
|
|
|
|
| 193 |
guidance_scale: float = 7.5,
|
| 194 |
seed: int = -1
|
| 195 |
) -> Tuple[Optional[str], str]:
|
| 196 |
+
"""Generate video with fallback strategies"""
|
|
|
|
|
|
|
| 197 |
|
| 198 |
+
# Initialize model
|
| 199 |
+
model_loaded = initialize_model()
|
|
|
|
|
|
|
| 200 |
|
| 201 |
# Input validation
|
| 202 |
if not prompt.strip():
|
| 203 |
return None, "β Please enter a valid prompt."
|
| 204 |
|
| 205 |
+
# Limit parameters
|
| 206 |
+
num_frames = min(max(num_frames, 8), 25)
|
| 207 |
+
num_inference_steps = min(max(num_inference_steps, 10), 30)
|
|
|
|
|
|
|
|
|
|
| 208 |
height = min(max(height, 256), 768)
|
| 209 |
width = min(max(width, 256), 768)
|
| 210 |
|
| 211 |
+
# Set seed
|
| 212 |
+
if seed == -1:
|
| 213 |
+
seed = np.random.randint(0, 2**32 - 1)
|
| 214 |
+
|
| 215 |
try:
|
| 216 |
# Clear memory
|
| 217 |
if torch.cuda.is_available():
|
| 218 |
torch.cuda.empty_cache()
|
| 219 |
gc.collect()
|
| 220 |
|
| 221 |
+
start_time = time.time()
|
|
|
|
|
|
|
| 222 |
|
| 223 |
+
if MODEL_TYPE == "mock" or not model_loaded:
|
| 224 |
+
# Mock generation
|
| 225 |
+
print("π Using mock generation")
|
| 226 |
+
video_path = create_mock_video(prompt, num_frames, width, height)
|
| 227 |
+
|
| 228 |
+
if video_path:
|
| 229 |
+
end_time = time.time()
|
| 230 |
+
return video_path, f"""
|
| 231 |
+
π **Demo Video Generated**
|
| 232 |
+
|
| 233 |
+
π Prompt: {prompt}
|
| 234 |
+
β οΈ Note: This is a demo mode because video models couldn't be loaded.
|
| 235 |
+
|
| 236 |
+
π¬ Frames: {num_frames}
|
| 237 |
+
π Resolution: {width}x{height}
|
| 238 |
+
β±οΈ Time: {end_time - start_time:.1f}s
|
| 239 |
+
π§ Status: {MODEL_ERROR or 'Demo mode'}
|
| 240 |
+
|
| 241 |
+
π‘ **To enable real video generation:**
|
| 242 |
+
- Check if LTX-Video is available in your region
|
| 243 |
+
- Try upgrading diffusers: `pip install diffusers --upgrade`
|
| 244 |
+
- Or wait for official LTX-Video support in diffusers
|
| 245 |
+
"""
|
| 246 |
+
else:
|
| 247 |
+
return None, "β Even demo generation failed"
|
| 248 |
|
| 249 |
+
elif MODEL_TYPE == "LTX-Video":
|
| 250 |
+
# Manual LTX-Video generation
|
| 251 |
+
print("π Using LTX-Video")
|
| 252 |
+
|
| 253 |
+
# This would need the actual implementation based on the model's API
|
| 254 |
+
# For now, return a message about manual implementation needed
|
| 255 |
+
return None, f"""
|
| 256 |
+
β οΈ **Manual Implementation Required**
|
| 257 |
+
|
| 258 |
+
LTX-Video model was loaded but requires custom generation code.
|
| 259 |
+
The model API is not yet standardized in diffusers.
|
| 260 |
+
|
| 261 |
+
π **Next Steps:**
|
| 262 |
+
1. Check Lightricks/LTX-Video model documentation
|
| 263 |
+
2. Implement custom inference pipeline
|
| 264 |
+
3. Or wait for official diffusers support
|
| 265 |
+
|
| 266 |
+
π§ **Current Status:** Model loaded, awaiting implementation
|
| 267 |
+
"""
|
| 268 |
|
| 269 |
+
else:
|
| 270 |
+
# Alternative model generation
|
| 271 |
+
print(f"π Using {MODEL_TYPE}")
|
| 272 |
+
|
| 273 |
+
generator = torch.Generator(device="cuda" if torch.cuda.is_available() else "cpu").manual_seed(seed)
|
| 274 |
+
|
| 275 |
result = MODEL(
|
| 276 |
prompt=prompt,
|
| 277 |
negative_prompt=negative_prompt if negative_prompt.strip() else None,
|
|
|
|
| 280 |
width=width,
|
| 281 |
num_inference_steps=num_inference_steps,
|
| 282 |
guidance_scale=guidance_scale,
|
| 283 |
+
generator=generator
|
| 284 |
)
|
| 285 |
+
|
| 286 |
+
# Export video
|
| 287 |
+
video_frames = result.frames[0]
|
| 288 |
+
|
| 289 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp_file:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
from diffusers.utils import export_to_video
|
| 291 |
export_to_video(video_frames, tmp_file.name, fps=8)
|
| 292 |
video_path = tmp_file.name
|
| 293 |
+
|
| 294 |
+
end_time = time.time()
|
| 295 |
+
|
| 296 |
+
return video_path, f"""
|
| 297 |
+
β
**Video Generated Successfully!**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
|
| 299 |
+
π Prompt: {prompt}
|
| 300 |
+
π€ Model: {MODEL_TYPE}
|
| 301 |
+
π¬ Frames: {num_frames}
|
| 302 |
+
π Resolution: {width}x{height}
|
| 303 |
+
βοΈ Steps: {num_inference_steps}
|
| 304 |
+
π― Guidance: {guidance_scale}
|
| 305 |
+
π² Seed: {seed}
|
| 306 |
+
β±οΈ Time: {end_time - start_time:.1f}s
|
| 307 |
+
π₯οΈ Device: {'CUDA' if torch.cuda.is_available() else 'CPU'}
|
| 308 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
except Exception as e:
|
| 311 |
if torch.cuda.is_available():
|
|
|
|
| 314 |
return None, f"β Generation failed: {str(e)}"
|
| 315 |
|
| 316 |
def get_system_info():
|
| 317 |
+
"""Get system information"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
+
# Check what's available
|
| 320 |
try:
|
| 321 |
+
from diffusers import __version__ as diffusers_version
|
| 322 |
+
available_pipelines = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
try:
|
| 324 |
+
from diffusers import LTXVideoPipeline
|
| 325 |
+
available_pipelines.append("β
LTXVideoPipeline")
|
| 326 |
+
except ImportError:
|
| 327 |
+
available_pipelines.append("β LTXVideoPipeline")
|
| 328 |
+
|
| 329 |
+
try:
|
| 330 |
+
from diffusers import DiffusionPipeline
|
| 331 |
+
available_pipelines.append("β
DiffusionPipeline")
|
| 332 |
+
except ImportError:
|
| 333 |
+
available_pipelines.append("β DiffusionPipeline")
|
| 334 |
+
|
| 335 |
+
except ImportError:
|
| 336 |
+
diffusers_version = "β Not installed"
|
| 337 |
+
available_pipelines = ["β Diffusers not available"]
|
| 338 |
|
| 339 |
+
return f"""
|
| 340 |
+
## π₯οΈ System Information
|
| 341 |
|
| 342 |
**Environment:**
|
| 343 |
+
- π ZeroGPU: {'β
Active' if IS_ZERO_GPU else 'β Not detected'}
|
| 344 |
- π HF Spaces: {'β
' if IS_SPACES else 'β'}
|
| 345 |
- π₯ CUDA: {'β
' if torch.cuda.is_available() else 'β'}
|
|
|
|
| 346 |
|
| 347 |
**Packages:**
|
| 348 |
- PyTorch: {torch.__version__}
|
| 349 |
+
- Diffusers: {diffusers_version}
|
| 350 |
+
- Available Pipelines: {', '.join(available_pipelines)}
|
|
|
|
| 351 |
|
| 352 |
**Model Status:**
|
| 353 |
+
- Current Model: {MODEL_TYPE or 'Not loaded'}
|
| 354 |
+
- Status: {'β
Ready' if MODEL is not None else 'β οΈ ' + (MODEL_ERROR or 'Not initialized')}
|
| 355 |
|
| 356 |
+
**Recommendation:**
|
| 357 |
+
- LTX-Video is very new and may not be in stable diffusers yet
|
| 358 |
+
- Using alternative models or demo mode
|
| 359 |
+
- Check back later for official support
|
| 360 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
|
| 362 |
# Create Gradio interface
|
| 363 |
+
with gr.Blocks(title="Video Generator with Fallbacks", theme=gr.themes.Soft()) as demo:
|
| 364 |
|
| 365 |
gr.Markdown("""
|
| 366 |
+
# π¬ Advanced Video Generator
|
| 367 |
|
| 368 |
+
Attempts to use LTX-Video, falls back to alternative models, or provides demo mode.
|
| 369 |
""")
|
| 370 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
with gr.Tab("π₯ Generate Video"):
|
| 372 |
with gr.Row():
|
| 373 |
with gr.Column(scale=1):
|
| 374 |
prompt_input = gr.Textbox(
|
| 375 |
label="π Video Prompt",
|
| 376 |
+
placeholder="A serene mountain lake at sunrise...",
|
| 377 |
+
lines=3
|
|
|
|
| 378 |
)
|
| 379 |
|
| 380 |
negative_prompt_input = gr.Textbox(
|
| 381 |
+
label="π« Negative Prompt",
|
| 382 |
+
placeholder="blurry, low quality...",
|
| 383 |
lines=2
|
| 384 |
)
|
| 385 |
|
| 386 |
+
with gr.Row():
|
| 387 |
+
num_frames = gr.Slider(8, 25, value=16, step=1, label="π¬ Frames")
|
| 388 |
+
num_steps = gr.Slider(10, 30, value=20, step=1, label="π Steps")
|
| 389 |
+
|
| 390 |
+
with gr.Row():
|
| 391 |
+
width = gr.Dropdown([256, 512, 768], value=512, label="π Width")
|
| 392 |
+
height = gr.Dropdown([256, 512, 768], value=512, label="π Height")
|
| 393 |
+
|
| 394 |
+
with gr.Row():
|
| 395 |
+
guidance_scale = gr.Slider(1.0, 15.0, value=7.5, step=0.5, label="π― Guidance")
|
| 396 |
+
seed = gr.Number(value=-1, precision=0, label="π² Seed")
|
|
|
|
| 397 |
|
| 398 |
generate_btn = gr.Button("π Generate Video", variant="primary", size="lg")
|
| 399 |
|
| 400 |
with gr.Column(scale=1):
|
| 401 |
video_output = gr.Video(label="π₯ Generated Video", height=400)
|
| 402 |
+
result_text = gr.Textbox(label="π Results", lines=8, show_copy_button=True)
|
| 403 |
|
|
|
|
| 404 |
generate_btn.click(
|
| 405 |
fn=generate_video,
|
| 406 |
inputs=[prompt_input, negative_prompt_input, num_frames, height, width, num_steps, guidance_scale, seed],
|
| 407 |
outputs=[video_output, result_text]
|
| 408 |
)
|
| 409 |
|
|
|
|
| 410 |
gr.Examples(
|
| 411 |
examples=[
|
| 412 |
+
["A peaceful cat in a sunny garden", "", 16, 512, 512, 20, 7.5, 42],
|
| 413 |
+
["Ocean waves at golden hour", "blurry", 20, 512, 512, 20, 8.0, 123],
|
| 414 |
+
["A butterfly on a flower", "", 16, 512, 512, 15, 7.0, 456]
|
| 415 |
],
|
| 416 |
inputs=[prompt_input, negative_prompt_input, num_frames, height, width, num_steps, guidance_scale, seed]
|
| 417 |
)
|
| 418 |
|
| 419 |
with gr.Tab("βΉοΈ System Info"):
|
| 420 |
+
info_btn = gr.Button("π Check System")
|
| 421 |
system_output = gr.Markdown()
|
| 422 |
|
| 423 |
info_btn.click(fn=get_system_info, outputs=system_output)
|
| 424 |
demo.load(fn=get_system_info, outputs=system_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
|
|
|
|
| 426 |
if __name__ == "__main__":
|
| 427 |
demo.queue(max_size=5)
|
| 428 |
demo.launch(
|