Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import StableVideoDiffusionPipeline
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import imageio
|
| 6 |
+
import uuid
|
| 7 |
+
import numpy as np
|
| 8 |
+
import cv2
|
| 9 |
+
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
|
| 12 |
+
pipe = StableVideoDiffusionPipeline.from_pretrained(
|
| 13 |
+
"stabilityai/stable-video-diffusion-img2vid-xt",
|
| 14 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 15 |
+
)
|
| 16 |
+
pipe = pipe.to(device)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# 🎥 Extract first frame from video
|
| 20 |
+
def extract_frame(video_path):
|
| 21 |
+
cap = cv2.VideoCapture(video_path)
|
| 22 |
+
success, frame = cap.read()
|
| 23 |
+
cap.release()
|
| 24 |
+
|
| 25 |
+
if success:
|
| 26 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 27 |
+
return Image.fromarray(frame)
|
| 28 |
+
return None
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def generate_video(image, video, fps, motion_strength):
|
| 32 |
+
# Decide input source
|
| 33 |
+
if image is not None:
|
| 34 |
+
input_image = image.convert("RGB")
|
| 35 |
+
elif video is not None:
|
| 36 |
+
input_image = extract_frame(video)
|
| 37 |
+
if input_image is None:
|
| 38 |
+
return None
|
| 39 |
+
else:
|
| 40 |
+
return None
|
| 41 |
+
|
| 42 |
+
# Generate frames
|
| 43 |
+
output = pipe(
|
| 44 |
+
input_image,
|
| 45 |
+
num_frames=32,
|
| 46 |
+
decode_chunk_size=8,
|
| 47 |
+
motion_bucket_id=int(motion_strength)
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
frames = output.frames[0]
|
| 51 |
+
|
| 52 |
+
# Convert frames
|
| 53 |
+
frames = [(frame * 255).astype(np.uint8) for frame in frames]
|
| 54 |
+
|
| 55 |
+
filename = f"video_{uuid.uuid4().hex}.mp4"
|
| 56 |
+
|
| 57 |
+
imageio.mimsave(
|
| 58 |
+
filename,
|
| 59 |
+
frames,
|
| 60 |
+
fps=fps,
|
| 61 |
+
codec="libx264",
|
| 62 |
+
quality=8
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
return filename
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
with gr.Blocks() as demo:
|
| 69 |
+
gr.Markdown("# 🎬 StuffMotion AI (Image + Video → Motion Video)")
|
| 70 |
+
gr.Markdown("Upload an image OR a video to generate AI motion")
|
| 71 |
+
|
| 72 |
+
image_input = gr.Image(type="pil", label="🖼️ Image Input (optional)")
|
| 73 |
+
video_input = gr.Video(label="🎥 Video Input (optional)")
|
| 74 |
+
|
| 75 |
+
fps = gr.Slider(8, 30, value=12, step=1, label="FPS")
|
| 76 |
+
motion = gr.Slider(1, 255, value=127, label="Motion Strength")
|
| 77 |
+
|
| 78 |
+
generate_btn = gr.Button("Generate")
|
| 79 |
+
|
| 80 |
+
video_output = gr.Video(label="🎬 Output")
|
| 81 |
+
|
| 82 |
+
generate_btn.click(
|
| 83 |
+
fn=generate_video,
|
| 84 |
+
inputs=[image_input, video_input, fps, motion],
|
| 85 |
+
outputs=video_output
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
demo.launch()
|