image-to-video / app.py
tittoos's picture
Update app.py
50758c7 verified
import subprocess, sys
# 🧠 Safe dependency install with NumPy fix
subprocess.run(
[sys.executable, "-m", "pip", "install",
"numpy<2", "moviepy==1.0.3", "imageio[ffmpeg]",
"diffusers==0.30.0", "transformers", "accelerate",
"torch==2.2.2", "gradio"], check=True
)
import gradio as gr
from diffusers import StableDiffusionPipeline, StableVideoDiffusionPipeline
import torch, moviepy.editor as mpy
device = "cpu" # 🧩 Force CPU-only mode
# πŸ”Ή Step 1: Lightweight Stable Diffusion v1.5
img_pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float32
).to(device)
# πŸ”Ή Step 2: Image β†’ Video
vid_pipe = StableVideoDiffusionPipeline.from_pretrained(
"stabilityai/stable-video-diffusion-img2vid",
torch_dtype=torch.float32
).to(device)
def generate_from_prompt(prompt, frames=12, motion_strength=0.6, fps=6):
# Generate image from prompt
image = img_pipe(prompt).images[0]
# Animate it (limited for CPU)
result = vid_pipe(
image,
num_frames=frames,
motion_bucket_id=int(motion_strength * 127),
noise_aug_strength=0.05
)
frames_list = result.frames[0]
clip = mpy.ImageSequenceClip(frames_list, fps=fps)
output_path = "/tmp/generated_video.mp4"
clip.write_videofile(output_path, codec="libx264", audio=False, verbose=False, logger=None)
return output_path
iface = gr.Interface(
fn=generate_from_prompt,
inputs=[
gr.Textbox(label="🎨 Prompt (e.g. 'Indian influencer smiling in sunlight')"),
gr.Slider(8, 20, value=12, step=1, label="Frames (lower = faster on CPU)"),
gr.Slider(0.0, 1.0, value=0.6, step=0.05, label="Motion Strength"),
gr.Slider(4, 10, value=6, step=1, label="FPS")
],
outputs=gr.Video(label="🎬 Generated AI Video"),
title="πŸͺ„ AI Prompt β†’ Image β†’ Video (CPU Mode)",
description="Runs fully on CPU. Generates an image from text and animates it into a short AI video."
)
iface.launch()