Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from diffusers import DiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Load the model (we'll use caching to improve performance)
|
| 9 |
+
def load_model():
|
| 10 |
+
model_id = "cerspense/zeroscope_v2_576w"
|
| 11 |
+
pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 12 |
+
pipe = pipe.to("cuda")
|
| 13 |
+
return pipe
|
| 14 |
+
|
| 15 |
+
# Generate video from text
|
| 16 |
+
def generate_video(prompt, num_frames=24, num_inference_steps=50):
|
| 17 |
+
# Load model (with caching)
|
| 18 |
+
if not hasattr(generate_video, "pipe"):
|
| 19 |
+
generate_video.pipe = load_model()
|
| 20 |
+
|
| 21 |
+
pipe = generate_video.pipe
|
| 22 |
+
|
| 23 |
+
# Generate video
|
| 24 |
+
video_frames = pipe(prompt, num_frames=num_frames, num_inference_steps=num_inference_steps).frames
|
| 25 |
+
|
| 26 |
+
# Convert frames to video file
|
| 27 |
+
output_path = "output.mp4"
|
| 28 |
+
frame_rate = 8 # frames per second
|
| 29 |
+
|
| 30 |
+
# Save as GIF (simpler implementation)
|
| 31 |
+
gif_path = "output.gif"
|
| 32 |
+
video_frames[0].save(
|
| 33 |
+
gif_path,
|
| 34 |
+
save_all=True,
|
| 35 |
+
append_images=video_frames[1:],
|
| 36 |
+
duration=1000//frame_rate,
|
| 37 |
+
loop=0
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
return gif_path
|
| 41 |
+
|
| 42 |
+
# Gradio interface
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown("# 🎥 Text-to-Video Generator")
|
| 45 |
+
gr.Markdown("Generate short video clips from text prompts using Zeroscope model")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column():
|
| 49 |
+
prompt = gr.Textbox(label="Enter your prompt", placeholder="A robot dancing on the moon")
|
| 50 |
+
frames = gr.Slider(minimum=8, maximum=48, value=24, step=8, label="Number of frames")
|
| 51 |
+
steps = gr.Slider(minimum=20, maximum=100, value=50, step=5, label="Inference steps")
|
| 52 |
+
submit = gr.Button("Generate Video")
|
| 53 |
+
|
| 54 |
+
with gr.Column():
|
| 55 |
+
output = gr.Image(label="Generated Video", format="gif")
|
| 56 |
+
|
| 57 |
+
examples = gr.Examples(
|
| 58 |
+
examples=[
|
| 59 |
+
["A spaceship flying through a nebula"],
|
| 60 |
+
["A cat wearing sunglasses surfing on a wave"],
|
| 61 |
+
["A futuristic city with flying cars at sunset"]
|
| 62 |
+
],
|
| 63 |
+
inputs=prompt
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
submit.click(
|
| 67 |
+
fn=generate_video,
|
| 68 |
+
inputs=[prompt, frames, steps],
|
| 69 |
+
outputs=output
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
demo.launch()
|