Riz0030 commited on
Commit
0e9171b
·
verified ·
1 Parent(s): 0d930ca

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -0
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import torch
4
+ import numpy as np
5
+ import gradio as gr
6
+ from PIL import Image
7
+ from diffusers import StableVideoDiffusionPipeline
8
+
9
+ # =========================
10
+ # CONFIG
11
+ # =========================
12
+ MODEL_ID = "stabilityai/stable-video-diffusion-img2vid"
13
+ OUTPUT_DIR = "output"
14
+
15
+ NUM_FRAMES = 25
16
+ FPS = 7
17
+ SEED = 42
18
+
19
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
20
+ DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
21
+
22
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
23
+
24
+ # =========================
25
+ # LOAD MODEL (ONCE)
26
+ # =========================
27
+ pipe = StableVideoDiffusionPipeline.from_pretrained(
28
+ MODEL_ID,
29
+ torch_dtype=DTYPE,
30
+ variant="fp16"
31
+ )
32
+
33
+ pipe.to(DEVICE)
34
+ pipe.enable_model_cpu_offload()
35
+ pipe.enable_attention_slicing()
36
+
37
+ generator = torch.Generator(DEVICE).manual_seed(SEED)
38
+
39
+ # =========================
40
+ # SAVE VIDEO FUNCTION
41
+ # =========================
42
+ def save_video(frames, path, fps):
43
+ h, w, _ = frames[0].shape
44
+ video = cv2.VideoWriter(
45
+ path,
46
+ cv2.VideoWriter_fourcc(*"mp4v"),
47
+ fps,
48
+ (w, h)
49
+ )
50
+
51
+ for frame in frames:
52
+ frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
53
+ video.write(frame)
54
+
55
+ video.release()
56
+
57
+ # =========================
58
+ # MAIN FUNCTION
59
+ # =========================
60
+ def images_to_video(images):
61
+ if images is None or len(images) == 0:
62
+ return None
63
+
64
+ output_videos = []
65
+
66
+ for idx, img in enumerate(images):
67
+ image = Image.fromarray(img).convert("RGB")
68
+ image = image.resize((1024, 576))
69
+
70
+ with torch.autocast(DEVICE):
71
+ result = pipe(
72
+ image,
73
+ num_frames=NUM_FRAMES,
74
+ generator=generator
75
+ )
76
+
77
+ frames = [
78
+ (frame * 255).astype(np.uint8)
79
+ for frame in result.frames[0]
80
+ ]
81
+
82
+ output_path = os.path.join(OUTPUT_DIR, f"video_{idx}.mp4")
83
+ save_video(frames, output_path, FPS)
84
+
85
+ output_videos.append(output_path)
86
+
87
+ return output_videos
88
+
89
+ # =========================
90
+ # GRADIO UI
91
+ # =========================
92
+ with gr.Blocks(title="Image to Video AI") as demo:
93
+ gr.Markdown("""
94
+ ## 🎥 Image to Video AI (Multi Image)
95
+ Upload **multiple images**, otomatis jadi **video MP4**
96
+ """)
97
+
98
+ image_input = gr.File(
99
+ file_types=["image"],
100
+ file_count="multiple",
101
+ label="Upload Images"
102
+ )
103
+
104
+ video_output = gr.Video(label="Generated Video")
105
+
106
+ generate_btn = gr.Button("🚀 Generate Video")
107
+
108
+ def process(files):
109
+ images = [np.array(Image.open(f.name)) for f in files]
110
+ videos = images_to_video(images)
111
+ return videos[0] if videos else None
112
+
113
+ generate_btn.click(
114
+ fn=process,
115
+ inputs=image_input,
116
+ outputs=video_output
117
+ )
118
+
119
+ demo.launch()