Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from diffusers import DiffusionPipeline
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# 1. Load the Model
|
| 7 |
+
# Use 'cuda' if you have an NVIDIA GPU and CUDA installed, otherwise 'cpu'
|
| 8 |
+
# Hugging Face Spaces will handle the GPU for you if you selected it
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
pipe = DiffusionPipeline.from_pretrained("cerspense/zeroscope_v2_576w", torch_dtype=torch.float16 if device == "cuda" else torch.float32)
|
| 13 |
+
pipe.to(device)
|
| 14 |
+
print(f"Model loaded successfully on {device}!")
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"Error loading model: {e}")
|
| 17 |
+
print("Ensure all dependencies are in requirements.txt and GPU is available if using 'cuda'.")
|
| 18 |
+
pipe = None # Set pipe to None if loading fails
|
| 19 |
+
|
| 20 |
+
# 2. Define the Generation Function
|
| 21 |
+
def generate_video(prompt: str, num_frames: int = 24, guidance_scale: float = 9.0):
|
| 22 |
+
if pipe is None:
|
| 23 |
+
return "Error: AI model not loaded. Please check server logs in the 'Logs' tab."
|
| 24 |
+
|
| 25 |
+
if not prompt:
|
| 26 |
+
return "Please enter a text prompt."
|
| 27 |
+
|
| 28 |
+
print(f"Generating video for prompt: '{prompt}'")
|
| 29 |
+
try:
|
| 30 |
+
# Generate video frames
|
| 31 |
+
video_frames = pipe(
|
| 32 |
+
prompt=prompt,
|
| 33 |
+
num_frames=num_frames,
|
| 34 |
+
guidance_scale=guidance_scale
|
| 35 |
+
).frames
|
| 36 |
+
|
| 37 |
+
# The output file path for the video
|
| 38 |
+
output_filename = "generated_video.mp4"
|
| 39 |
+
# pipe.to_video creates the video file from frames
|
| 40 |
+
video_path = pipe.to_video(video_frames, output_filename)
|
| 41 |
+
return video_path
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"Video generation failed: {e}")
|
| 44 |
+
return f"Video generation failed: {e}"
|
| 45 |
+
|
| 46 |
+
# 3. Create Gradio Interface
|
| 47 |
+
iface = gr.Interface(
|
| 48 |
+
fn=generate_video,
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.Textbox(label="Text Prompt", placeholder="e.g., 'A robot dancing in the rain'"),
|
| 51 |
+
gr.Slider(minimum=16, maximum=64, step=4, value=24, label="Number of Frames (Video Length)"),
|
| 52 |
+
gr.Slider(minimum=1.0, maximum=15.0, step=0.5, value=9.0, label="Guidance Scale (Creativity vs. Prompt Adherence)")
|
| 53 |
+
],
|
| 54 |
+
outputs=gr.Video(label="Generated Video"),
|
| 55 |
+
title="Simple Text-to-Video Generator (Zeroscope v2)",
|
| 56 |
+
description="Enter a text prompt to generate a short video using Zeroscope v2."
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# 4. Launch the App
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
iface.launch() # Hugging Face Spaces handles sharing automatically
|