Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import AnimateDiffPipeline
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import imageio
|
| 6 |
+
import tempfile
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
MODEL_ID = "guoyww/animatediff-motion-adapter-v1-5-2"
|
| 10 |
+
|
| 11 |
+
print("Loading pipeline...")
|
| 12 |
+
|
| 13 |
+
pipe = AnimateDiffPipeline.from_pretrained(
|
| 14 |
+
MODEL_ID,
|
| 15 |
+
torch_dtype=torch.float32
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
pipe.to("cpu")
|
| 19 |
+
|
| 20 |
+
# CPU optimizations
|
| 21 |
+
pipe.enable_attention_slicing()
|
| 22 |
+
pipe.enable_vae_slicing()
|
| 23 |
+
|
| 24 |
+
print("Model Ready")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def generate_video(image, prompt, frames, steps, seed):
|
| 28 |
+
|
| 29 |
+
generator = torch.Generator("cpu").manual_seed(int(seed))
|
| 30 |
+
|
| 31 |
+
image = Image.fromarray(image).resize((384,384))
|
| 32 |
+
|
| 33 |
+
with torch.inference_mode():
|
| 34 |
+
|
| 35 |
+
result = pipe(
|
| 36 |
+
prompt=prompt,
|
| 37 |
+
image=image,
|
| 38 |
+
num_frames=frames,
|
| 39 |
+
num_inference_steps=steps,
|
| 40 |
+
generator=generator
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
frames_list = result.frames
|
| 44 |
+
|
| 45 |
+
temp_dir = tempfile.mkdtemp()
|
| 46 |
+
video_path = os.path.join(temp_dir, "video.mp4")
|
| 47 |
+
|
| 48 |
+
imageio.mimsave(video_path, frames_list, fps=6)
|
| 49 |
+
|
| 50 |
+
return video_path
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
with gr.Blocks() as demo:
|
| 54 |
+
|
| 55 |
+
gr.Markdown("# 🚀 Image → Video AI (CPU Optimized)")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
|
| 59 |
+
with gr.Column():
|
| 60 |
+
|
| 61 |
+
input_image = gr.Image(label="Upload Image")
|
| 62 |
+
|
| 63 |
+
prompt = gr.Textbox(
|
| 64 |
+
value="cinematic camera movement",
|
| 65 |
+
label="Motion Prompt"
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
frames = gr.Slider(8,20,value=12,step=1,label="Frames")
|
| 69 |
+
|
| 70 |
+
steps = gr.Slider(4,8,value=6,step=1,label="Steps")
|
| 71 |
+
|
| 72 |
+
seed = gr.Number(value=42)
|
| 73 |
+
|
| 74 |
+
btn = gr.Button("Generate Video")
|
| 75 |
+
|
| 76 |
+
with gr.Column():
|
| 77 |
+
|
| 78 |
+
output_video = gr.Video()
|
| 79 |
+
|
| 80 |
+
btn.click(
|
| 81 |
+
generate_video,
|
| 82 |
+
inputs=[input_image,prompt,frames,steps,seed],
|
| 83 |
+
outputs=output_video
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
demo.launch()
|