Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,14 +9,42 @@ import cv2
|
|
| 9 |
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
|
| 12 |
-
pipe =
|
| 13 |
-
|
| 14 |
-
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 15 |
-
)
|
| 16 |
-
pipe = pipe.to(device)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def extract_frame(video_path):
|
| 21 |
cap = cv2.VideoCapture(video_path)
|
| 22 |
success, frame = cap.read()
|
|
@@ -28,60 +56,75 @@ def extract_frame(video_path):
|
|
| 28 |
return None
|
| 29 |
|
| 30 |
|
| 31 |
-
def generate_video(image, video, fps, motion_strength):
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 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 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
filename,
|
| 59 |
-
frames,
|
| 60 |
-
fps=fps,
|
| 61 |
-
codec="libx264",
|
| 62 |
-
quality=8
|
| 63 |
-
)
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
with gr.Blocks() as demo:
|
| 69 |
-
gr.Markdown("# π¬ StuffMotion AI (
|
| 70 |
-
gr.Markdown("Upload an image OR a video to generate AI motion")
|
| 71 |
|
| 72 |
-
image_input = gr.Image(type="pil", label="πΌοΈ Image Input
|
| 73 |
-
video_input = gr.Video(label="π₯ Video Input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
fps = gr.Slider(8,
|
| 76 |
-
motion = gr.Slider(1, 255, value=
|
| 77 |
|
| 78 |
-
generate_btn = gr.Button("Generate")
|
| 79 |
|
| 80 |
-
video_output = gr.Video(
|
| 81 |
|
| 82 |
generate_btn.click(
|
| 83 |
fn=generate_video,
|
| 84 |
-
inputs=[image_input, video_input, fps, motion],
|
| 85 |
outputs=video_output
|
| 86 |
)
|
| 87 |
|
|
|
|
| 9 |
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
|
| 12 |
+
pipe = None
|
| 13 |
+
current_model = None
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# π Load model only when needed (fixes slow startup)
|
| 16 |
+
def load_model(model_name):
|
| 17 |
+
global pipe, current_model
|
| 18 |
|
| 19 |
+
if current_model == model_name:
|
| 20 |
+
return pipe
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
if model_name == "Fast (SVD)":
|
| 24 |
+
model_id = "stabilityai/stable-video-diffusion-img2vid"
|
| 25 |
+
else:
|
| 26 |
+
model_id = "stabilityai/stable-video-diffusion-img2vid-xt"
|
| 27 |
+
|
| 28 |
+
pipe = StableVideoDiffusionPipeline.from_pretrained(
|
| 29 |
+
model_id,
|
| 30 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
pipe = pipe.to(device)
|
| 34 |
+
|
| 35 |
+
if device == "cuda":
|
| 36 |
+
pipe.enable_attention_slicing()
|
| 37 |
+
pipe.enable_model_cpu_offload()
|
| 38 |
+
|
| 39 |
+
current_model = model_name
|
| 40 |
+
return pipe
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print("Model load error:", e)
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# π₯ Extract frame from video
|
| 48 |
def extract_frame(video_path):
|
| 49 |
cap = cv2.VideoCapture(video_path)
|
| 50 |
success, frame = cap.read()
|
|
|
|
| 56 |
return None
|
| 57 |
|
| 58 |
|
| 59 |
+
def generate_video(image, video, fps, motion_strength, model_choice):
|
| 60 |
+
try:
|
| 61 |
+
pipe = load_model(model_choice)
|
| 62 |
+
if pipe is None:
|
|
|
|
|
|
|
|
|
|
| 63 |
return None
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# Select input
|
| 66 |
+
if image is not None:
|
| 67 |
+
input_image = image.convert("RGB")
|
| 68 |
+
elif video is not None:
|
| 69 |
+
input_image = extract_frame(video)
|
| 70 |
+
if input_image is None:
|
| 71 |
+
return None
|
| 72 |
+
else:
|
| 73 |
+
return None
|
| 74 |
|
| 75 |
+
# Resize (β‘ HUGE speed boost)
|
| 76 |
+
input_image = input_image.resize((512, 512))
|
| 77 |
|
| 78 |
+
# Generate frames (reduced for speed)
|
| 79 |
+
output = pipe(
|
| 80 |
+
input_image,
|
| 81 |
+
num_frames=16, # β‘ faster
|
| 82 |
+
decode_chunk_size=4,
|
| 83 |
+
motion_bucket_id=int(motion_strength)
|
| 84 |
+
)
|
| 85 |
|
| 86 |
+
frames = output.frames[0]
|
| 87 |
+
frames = [(frame * 255).astype(np.uint8) for frame in frames]
|
| 88 |
|
| 89 |
+
filename = f"video_{uuid.uuid4().hex}.mp4"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
imageio.mimsave(
|
| 92 |
+
filename,
|
| 93 |
+
frames,
|
| 94 |
+
fps=fps,
|
| 95 |
+
codec="libx264"
|
| 96 |
+
)
|
| 97 |
|
| 98 |
+
return filename
|
| 99 |
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print("Generation error:", e)
|
| 102 |
+
return None
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
# π¨ UI
|
| 106 |
with gr.Blocks() as demo:
|
| 107 |
+
gr.Markdown("# π¬ StuffMotion AI (FAST + MODEL SELECT)")
|
|
|
|
| 108 |
|
| 109 |
+
image_input = gr.Image(type="pil", label="πΌοΈ Image Input")
|
| 110 |
+
video_input = gr.Video(label="π₯ Video Input")
|
| 111 |
+
|
| 112 |
+
model_choice = gr.Dropdown(
|
| 113 |
+
["Fast (SVD)", "High Quality (XT)"],
|
| 114 |
+
value="Fast (SVD)",
|
| 115 |
+
label="π§ Model"
|
| 116 |
+
)
|
| 117 |
|
| 118 |
+
fps = gr.Slider(8, 24, value=12, step=1, label="FPS")
|
| 119 |
+
motion = gr.Slider(1, 255, value=100, label="Motion")
|
| 120 |
|
| 121 |
+
generate_btn = gr.Button("β‘ Generate")
|
| 122 |
|
| 123 |
+
video_output = gr.Video()
|
| 124 |
|
| 125 |
generate_btn.click(
|
| 126 |
fn=generate_video,
|
| 127 |
+
inputs=[image_input, video_input, fps, motion, model_choice],
|
| 128 |
outputs=video_output
|
| 129 |
)
|
| 130 |
|