|
import gradio as gr |
|
import torch |
|
import numpy as np |
|
from diffusers import I2VGenXLPipeline |
|
from PIL import Image |
|
from moviepy.editor import ImageSequenceClip |
|
import io |
|
|
|
def generate_video(image, prompt, negative_prompt, video_length): |
|
generator = torch.manual_seed(8888) |
|
|
|
|
|
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu") |
|
print(f"Using device: {device}") |
|
|
|
|
|
pipeline = I2VGenXLPipeline.from_pretrained("ali-vilab/i2vgen-xl", torch_dtype=torch.float32) |
|
pipeline.to(device) |
|
|
|
|
|
frames = [] |
|
total_frames = video_length * 30 |
|
|
|
for i in range(total_frames): |
|
frame = pipeline( |
|
prompt=prompt, |
|
image=image, |
|
num_inference_steps=5, |
|
negative_prompt=negative_prompt, |
|
guidance_scale=9.0, |
|
generator=generator, |
|
num_frames=1 |
|
).frames[0] |
|
frames.append(np.array(frame)) |
|
|
|
|
|
yield (i + 1) / total_frames |
|
|
|
|
|
output_file = "output_video.mp4" |
|
clip = ImageSequenceClip(frames, fps=30) |
|
clip.write_videofile(output_file, codec='libx264', audio=False) |
|
|
|
return output_file |
|
|
|
|
|
def interface(image, prompt, negative_prompt, video_length): |
|
|
|
image = Image.open(io.BytesIO(image.read())) |
|
|
|
|
|
return generate_video(image, prompt, negative_prompt, video_length) |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# AI-Powered Video Generation") |
|
|
|
with gr.Row(): |
|
image_input = gr.Image(type="file", label="Upload Image") |
|
prompt_input = gr.Textbox(label="Enter the Prompt") |
|
negative_prompt_input = gr.Textbox(label="Enter the Negative Prompt") |
|
video_length_input = gr.Number(label="Video Length (seconds)", value=10, precision=0) |
|
|
|
generate_button = gr.Button("Generate Video") |
|
output_video = gr.Video(label="Output Video") |
|
|
|
|
|
generate_button.click( |
|
interface, |
|
inputs=[image_input, prompt_input, negative_prompt_input, video_length_input], |
|
outputs=output_video, |
|
show_progress=True |
|
) |
|
|
|
|
|
demo.launch() |
|
|