Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
|
| 4 |
+
from diffusers.utils import export_to_video
|
| 5 |
+
|
| 6 |
+
# Initialize the diffusion pipeline
|
| 7 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 8 |
+
"heboya8/text2video-test",
|
| 9 |
+
torch_dtype=torch.float16,
|
| 10 |
+
variant="fp16"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Optimize for GPU memory
|
| 14 |
+
pipe.enable_model_cpu_offload()
|
| 15 |
+
pipe.enable_vae_slicing()
|
| 16 |
+
|
| 17 |
+
def generate_video(prompt):
|
| 18 |
+
try:
|
| 19 |
+
# Generate video frames
|
| 20 |
+
video_frames = pipe(
|
| 21 |
+
prompt,
|
| 22 |
+
num_inference_steps=50,
|
| 23 |
+
num_frames=200
|
| 24 |
+
).frames
|
| 25 |
+
|
| 26 |
+
# Export frames to video file
|
| 27 |
+
video_path = export_to_video(video_frames, output_video_path="output_video.mp4")
|
| 28 |
+
return video_path
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"Error generating video: {str(e)}"
|
| 31 |
+
|
| 32 |
+
# Create Gradio interface
|
| 33 |
+
interface = gr.Interface(
|
| 34 |
+
fn=generate_video,
|
| 35 |
+
inputs=gr.Textbox(
|
| 36 |
+
label="Enter your prompt",
|
| 37 |
+
placeholder="e.g., a flower in a garden"
|
| 38 |
+
),
|
| 39 |
+
outputs=gr.Video(label="Generated Video"),
|
| 40 |
+
title="Text-to-Video Generator",
|
| 41 |
+
description="Enter a text prompt to generate a video using the diffusion model."
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Launch the app
|
| 45 |
+
interface.launch()
|