Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +92 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
import sys
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from diffusers import DiffusionPipeline
|
| 7 |
+
from diffusers.utils import export_to_video
|
| 8 |
+
|
| 9 |
+
# Отладка
|
| 10 |
+
print(f"Python: {sys.version}")
|
| 11 |
+
print(f"Torch: {torch.__version__}")
|
| 12 |
+
print(f"CUDA available: {torch.cuda.is_available()}")
|
| 13 |
+
if torch.cuda.is_available():
|
| 14 |
+
print(f"GPU: {torch.cuda.get_device_name(0)}")
|
| 15 |
+
|
| 16 |
+
# Загрузка модели Wan 2.2 (Image-to-Video)
|
| 17 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 18 |
+
"Wan-AI/Wan2.2-T2V-A14B",
|
| 19 |
+
torch_dtype=torch.float16,
|
| 20 |
+
variant="fp16"
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Единственная безопасная оптимизация
|
| 24 |
+
pipe.enable_vae_slicing()
|
| 25 |
+
|
| 26 |
+
# Перемещение на GPU
|
| 27 |
+
if torch.cuda.is_available():
|
| 28 |
+
pipe = pipe.to("cuda")
|
| 29 |
+
print("✓ Model loaded on GPU")
|
| 30 |
+
|
| 31 |
+
def generate_video(prompt, image=None, negative_prompt="blurry, low quality, jittery"):
|
| 32 |
+
try:
|
| 33 |
+
print(f"Generating video for prompt: '{prompt}'")
|
| 34 |
+
|
| 35 |
+
# Генерация (если есть изображение — img2vid, иначе text2vid)
|
| 36 |
+
if image is not None and isinstance(image, np.ndarray):
|
| 37 |
+
image_pil = Image.fromarray(image).convert("RGB")
|
| 38 |
+
# Для Wan 2.2 img2vid требуется специальный режим — используем text2vid как fallback
|
| 39 |
+
output = pipe(
|
| 40 |
+
prompt=prompt,
|
| 41 |
+
negative_prompt=negative_prompt,
|
| 42 |
+
num_frames=24, # 1 секунда @ 24fps
|
| 43 |
+
height=480, # 480p для стабильности на T4
|
| 44 |
+
width=720,
|
| 45 |
+
num_inference_steps=30,
|
| 46 |
+
generator=torch.Generator(device="cuda").manual_seed(42)
|
| 47 |
+
)
|
| 48 |
+
else:
|
| 49 |
+
# Text-to-Video (основной режим)
|
| 50 |
+
output = pipe(
|
| 51 |
+
prompt=prompt,
|
| 52 |
+
negative_prompt=negative_prompt,
|
| 53 |
+
num_frames=24,
|
| 54 |
+
height=480,
|
| 55 |
+
width=720,
|
| 56 |
+
num_inference_steps=30,
|
| 57 |
+
generator=torch.Generator(device="cuda").manual_seed(42)
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Сохранение
|
| 61 |
+
output_path = "/tmp/output.mp4"
|
| 62 |
+
export_to_video(output.frames[0], output_path, fps=24)
|
| 63 |
+
|
| 64 |
+
return output_path
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print(f"ERROR: {str(e)}")
|
| 68 |
+
import traceback
|
| 69 |
+
traceback.print_exc()
|
| 70 |
+
return None
|
| 71 |
+
|
| 72 |
+
# Интерфейс
|
| 73 |
+
demo = gr.Interface(
|
| 74 |
+
fn=generate_video,
|
| 75 |
+
inputs=[
|
| 76 |
+
gr.Textbox(label="Prompt (English)", value="a panda eating bamboo in a forest, cinematic, 4k"),
|
| 77 |
+
gr.Image(label="Optional Image (for img2vid)", type="numpy"),
|
| 78 |
+
gr.Textbox(label="Negative Prompt", value="blurry, low quality, jittery motion")
|
| 79 |
+
],
|
| 80 |
+
outputs=gr.Video(label="Generated Video (720×480, 24 frames, 1 second)"),
|
| 81 |
+
title="🎥 Wan 2.2 Video Generator (Apache 2.0 License)",
|
| 82 |
+
description="✅ Commercial use allowed • 720p resolution • Based on Alibaba's open-source model",
|
| 83 |
+
examples=[
|
| 84 |
+
["a robot dancing in cyberpunk city, neon lights, cinematic", None, "blurry"],
|
| 85 |
+
["a cat wearing sunglasses riding a skateboard, cartoon style", None, "jittery motion"],
|
| 86 |
+
["a spaceship flying through nebula, sci-fi, 4k", None, "low quality"]
|
| 87 |
+
],
|
| 88 |
+
cache_examples=False
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
diffusers>=0.30.0
|
| 2 |
+
transformers>=4.40.0
|
| 3 |
+
accelerate>=0.30.0
|
| 4 |
+
safetensors>=0.4.0
|
| 5 |
+
imageio>=2.31.0
|
| 6 |
+
imageio-ffmpeg>=0.4.9
|
| 7 |
+
einops>=0.7.0
|