Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,35 +2,85 @@ import gradio as gr
|
|
| 2 |
from diffusers import DiffusionPipeline
|
| 3 |
import torch
|
| 4 |
import imageio
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
pipe = DiffusionPipeline.from_pretrained(
|
| 7 |
"cerspense/zeroscope_v2_576w",
|
| 8 |
-
torch_dtype=
|
| 9 |
)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
prompt,
|
| 17 |
-
num_inference_steps=15,
|
| 18 |
-
num_frames=16
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
frames = result.frames
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
| 2 |
from diffusers import DiffusionPipeline
|
| 3 |
import torch
|
| 4 |
import imageio
|
| 5 |
+
import numpy as np
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Detectar si hay GPU disponible
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 11 |
+
|
| 12 |
+
print(f"Usando dispositivo: {device} - dtype: {dtype}")
|
| 13 |
+
|
| 14 |
+
# Cargar pipeline (se descarga la primera vez)
|
| 15 |
pipe = DiffusionPipeline.from_pretrained(
|
| 16 |
"cerspense/zeroscope_v2_576w",
|
| 17 |
+
torch_dtype=dtype
|
| 18 |
)
|
| 19 |
|
| 20 |
+
if device == "cuda":
|
| 21 |
+
pipe.to("cuda")
|
| 22 |
+
# No usar offload en GPU
|
| 23 |
+
else:
|
| 24 |
+
pipe.enable_model_cpu_offload() # ayuda en CPU con poca RAM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
def generate_video(prompt: str):
|
| 27 |
+
"""
|
| 28 |
+
Genera un video corto a partir de un prompt de texto.
|
| 29 |
+
"""
|
| 30 |
+
if not prompt.strip():
|
| 31 |
+
return None, "El prompt no puede estar vacío."
|
| 32 |
|
| 33 |
+
# Parámetros reducidos para CPU o GPU pequeña
|
| 34 |
+
num_frames = 16 # múltiplo de 4, entre 12 y 24
|
| 35 |
+
height = 320
|
| 36 |
+
width = 576
|
| 37 |
+
steps = 15 # menos pasos = más rápido
|
| 38 |
|
| 39 |
+
try:
|
| 40 |
+
result = pipe(
|
| 41 |
+
prompt,
|
| 42 |
+
num_inference_steps=steps,
|
| 43 |
+
num_frames=num_frames,
|
| 44 |
+
height=height,
|
| 45 |
+
width=width
|
| 46 |
+
)
|
| 47 |
+
# result.frames es una lista de frames (PIL o numpy)
|
| 48 |
+
frames = result.frames[0] if isinstance(result.frames, list) else result.frames
|
| 49 |
+
|
| 50 |
+
# Convertir PIL a numpy si es necesario
|
| 51 |
+
if hasattr(frames[0], 'size'): # son PIL Images
|
| 52 |
+
frames = [np.array(frame) for frame in frames]
|
| 53 |
+
|
| 54 |
+
output_path = "/tmp/video.mp4"
|
| 55 |
+
imageio.mimsave(output_path, frames, fps=8)
|
| 56 |
+
return output_path, "✅ Video generado correctamente."
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return None, f"❌ Error: {str(e)}"
|
| 60 |
|
| 61 |
+
# Crear interfaz Gradio
|
| 62 |
+
with gr.Blocks(title="Zeroscope AI Video Generator") as demo:
|
| 63 |
+
gr.Markdown("# 🎬 Generador de Vídeos con IA (Zeroscope)")
|
| 64 |
+
gr.Markdown("Escribe un prompt y obtén un video de 2 segundos (16 frames a 8 fps).")
|
| 65 |
+
|
| 66 |
+
with gr.Row():
|
| 67 |
+
with gr.Column():
|
| 68 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="Ej: 'Un astronauta cabalgando un caballo en Marte'", lines=3)
|
| 69 |
+
generate_btn = gr.Button("🎥 Generar video", variant="primary")
|
| 70 |
+
with gr.Column():
|
| 71 |
+
video_output = gr.Video(label="Video generado")
|
| 72 |
+
status_text = gr.Textbox(label="Estado", interactive=False)
|
| 73 |
+
|
| 74 |
+
generate_btn.click(
|
| 75 |
+
fn=generate_video,
|
| 76 |
+
inputs=prompt_input,
|
| 77 |
+
outputs=[video_output, status_text]
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
gr.Markdown("### 💡 Tips")
|
| 81 |
+
gr.Markdown("- Sé específico: 'una playa tropical al atardecer con olas suaves'")
|
| 82 |
+
gr.Markdown("- La primera ejecución descarga el modelo (puede tomar 2-3 minutos).")
|
| 83 |
|
| 84 |
+
# Lanzar la app
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|