Stiphan commited on
Commit
e9892ba
·
verified ·
1 Parent(s): 37432d9

Add Motion Photo Video feature

Browse files

Generates cinematic animated motion video from still image using Stable Video Diffusion model.

Files changed (1) hide show
  1. motion_video.py +56 -0
motion_video.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import tempfile
4
+ import os
5
+
6
+ # Hugging Face API Config
7
+ HF_API_TOKEN = "YOUR_HF_API_KEY" # Apna token lagao
8
+ MODEL_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-video-diffusion-img2vid"
9
+ HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
10
+
11
+ def image_to_motion_video(image, motion_strength):
12
+ """🖼 Still Image → Motion Animated Video"""
13
+ if image is None:
14
+ return None
15
+
16
+ try:
17
+ # Save image temporarily
18
+ temp_img = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
19
+ image.save(temp_img.name)
20
+
21
+ # Send to Hugging Face model
22
+ with open(temp_img.name, "rb") as img_file:
23
+ resp = requests.post(
24
+ MODEL_URL,
25
+ headers=HEADERS,
26
+ data=img_file,
27
+ params={"motion_strength": motion_strength} # if model supports extra param
28
+ )
29
+
30
+ if resp.status_code != 200:
31
+ print("Error:", resp.text)
32
+ return None
33
+
34
+ # Save output video
35
+ temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
36
+ with open(temp_video.name, "wb") as f:
37
+ f.write(resp.content)
38
+
39
+ return temp_video.name
40
+ except Exception as e:
41
+ print(f"Error: {e}")
42
+ return None
43
+
44
+ # ---------- Gradio UI ----------
45
+ with gr.Blocks() as demo:
46
+ gr.Markdown("<h2 style='text-align:center'>🎞 Motion Photo Video — SaEdit MultiAi</h2>")
47
+
48
+ img_input = gr.Image(type="pil", label="📷 Upload Image")
49
+ motion_input = gr.Slider(0.5, 2.0, value=1.0, label="🎛 Motion Strength")
50
+ btn = gr.Button("✨ Create Cinematic Motion Video")
51
+ video_output = gr.Video(label="🎬 Animated Output")
52
+
53
+ btn.click(image_to_motion_video, inputs=[img_input, motion_input], outputs=video_output)
54
+
55
+ if __name__ == "__main__":
56
+ demo.launch()