Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import StableDiffusionPipeline
|
4 |
+
from moviepy.editor import ImageSequenceClip
|
5 |
+
import os
|
6 |
+
import numpy as np
|
7 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
8 |
+
|
9 |
+
# Initialize text-to-image pipeline
|
10 |
+
model_id = "CompVis/stable-diffusion-v1-4"
|
11 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
12 |
+
pipe = pipe.to("cuda")
|
13 |
+
|
14 |
+
# Load a text summarization model for better prompts (optional)
|
15 |
+
summarizer_model = "facebook/bart-large-cnn"
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(summarizer_model)
|
17 |
+
summarizer = AutoModelForSeq2SeqLM.from_pretrained(summarizer_model)
|
18 |
+
|
19 |
+
# Function to create video from text
|
20 |
+
def text_to_video(input_text, num_frames=10, fps=2):
|
21 |
+
# Summarize input text for better image prompts
|
22 |
+
inputs = tokenizer(input_text, return_tensors="pt", truncation=True)
|
23 |
+
summary_ids = summarizer.generate(inputs["input_ids"], max_length=30, min_length=5, length_penalty=2.0)
|
24 |
+
prompt = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
25 |
+
|
26 |
+
# Generate images
|
27 |
+
frames = []
|
28 |
+
for i in range(num_frames):
|
29 |
+
prompt_with_frame = f"{prompt}, frame {i+1}"
|
30 |
+
image = pipe(prompt_with_frame).images[0]
|
31 |
+
frames.append(np.array(image))
|
32 |
+
|
33 |
+
# Save images as video
|
34 |
+
video_path = "output.mp4"
|
35 |
+
clip = ImageSequenceClip(frames, fps=fps)
|
36 |
+
clip.write_videofile(video_path, codec="libx264")
|
37 |
+
return video_path
|
38 |
+
|
39 |
+
# Define Gradio Interface
|
40 |
+
def generate_video(text, frames, fps):
|
41 |
+
video_file = text_to_video(text, num_frames=frames, fps=fps)
|
42 |
+
return video_file
|
43 |
+
|
44 |
+
interface = gr.Interface(
|
45 |
+
fn=generate_video,
|
46 |
+
inputs=[
|
47 |
+
gr.Textbox(label="Enter your text prompt"),
|
48 |
+
gr.Slider(5, 30, value=10, step=1, label="Number of Frames"),
|
49 |
+
gr.Slider(1, 10, value=2, step=1, label="Frames per Second (FPS)"),
|
50 |
+
],
|
51 |
+
outputs=gr.Video(label="Generated Video"),
|
52 |
+
title="Text-to-Video Generator",
|
53 |
+
description="Enter a text prompt to generate a short video."
|
54 |
+
)
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
interface.launch()
|