Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import numpy as np
|
4 |
+
from diffusers import I2VGenXLPipeline
|
5 |
+
from PIL import Image
|
6 |
+
from moviepy.editor import ImageSequenceClip
|
7 |
+
import io
|
8 |
+
|
9 |
+
def generate_video(image, prompt, negative_prompt, video_length):
|
10 |
+
generator = torch.manual_seed(8888)
|
11 |
+
|
12 |
+
# Set the device to CPU or a non-NVIDIA GPU
|
13 |
+
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
|
14 |
+
print(f"Using device: {device}")
|
15 |
+
|
16 |
+
# Load the pipeline
|
17 |
+
pipeline = I2VGenXLPipeline.from_pretrained("ali-vilab/i2vgen-xl", torch_dtype=torch.float32)
|
18 |
+
pipeline.to(device) # Move the model to the selected device
|
19 |
+
|
20 |
+
# Generate frames with progress tracking
|
21 |
+
frames = []
|
22 |
+
total_frames = video_length * 30 # Assuming 30 frames per second
|
23 |
+
|
24 |
+
for i in range(total_frames):
|
25 |
+
frame = pipeline(
|
26 |
+
prompt=prompt,
|
27 |
+
image=image,
|
28 |
+
num_inference_steps=5,
|
29 |
+
negative_prompt=negative_prompt,
|
30 |
+
guidance_scale=9.0,
|
31 |
+
generator=generator,
|
32 |
+
num_frames=1
|
33 |
+
).frames[0]
|
34 |
+
frames.append(np.array(frame))
|
35 |
+
|
36 |
+
# Update progress
|
37 |
+
yield (i + 1) / total_frames # Yield progress
|
38 |
+
|
39 |
+
# Create a video clip from the frames
|
40 |
+
output_file = "output_video.mp4"
|
41 |
+
clip = ImageSequenceClip(frames, fps=30) # Set the frames per second
|
42 |
+
clip.write_videofile(output_file, codec='libx264', audio=False)
|
43 |
+
|
44 |
+
return output_file
|
45 |
+
|
46 |
+
# Gradio interface
|
47 |
+
def interface(image, prompt, negative_prompt, video_length):
|
48 |
+
# Convert the uploaded image to a PIL Image
|
49 |
+
image = Image.open(io.BytesIO(image.read()))
|
50 |
+
|
51 |
+
# Generate video and track progress
|
52 |
+
return generate_video(image, prompt, negative_prompt, video_length)
|
53 |
+
|
54 |
+
# Create Gradio Blocks
|
55 |
+
with gr.Blocks() as demo:
|
56 |
+
gr.Markdown("# AI-Powered Video Generation")
|
57 |
+
|
58 |
+
with gr.Row():
|
59 |
+
image_input = gr.Image(type="file", label="Upload Image")
|
60 |
+
prompt_input = gr.Textbox(label="Enter the Prompt")
|
61 |
+
negative_prompt_input = gr.Textbox(label="Enter the Negative Prompt")
|
62 |
+
video_length_input = gr.Number(label="Video Length (seconds)", value=10, precision=0)
|
63 |
+
|
64 |
+
generate_button = gr.Button("Generate Video")
|
65 |
+
output_video = gr.Video(label="Output Video")
|
66 |
+
|
67 |
+
# Define the button action
|
68 |
+
generate_button.click(
|
69 |
+
interface,
|
70 |
+
inputs=[image_input, prompt_input, negative_prompt_input, video_length_input],
|
71 |
+
outputs=output_video,
|
72 |
+
show_progress=True # Show progress bar
|
73 |
+
)
|
74 |
+
|
75 |
+
# Launch the Gradio app
|
76 |
+
demo.launch()
|