Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,59 @@
|
|
1 |
-
# app.py
|
2 |
-
|
3 |
import subprocess
|
4 |
from pathlib import Path
|
5 |
import gradio as gr
|
6 |
|
7 |
-
# ---
|
8 |
-
FRAME1 = Path("demo/frame1.jpg")
|
9 |
-
FRAME2 = Path("demo/frame2.jpg")
|
10 |
-
|
|
|
|
|
11 |
|
12 |
def interpolate(img_a_path, img_b_path):
|
13 |
-
"""
|
14 |
-
Run the interpolation pipeline from the Space root:
|
15 |
-
1. Run img_inference.py to generate intermediate frames
|
16 |
-
2. Create color palette with ffmpeg
|
17 |
-
3. Build final interpolated GIF with palette
|
18 |
-
"""
|
19 |
try:
|
20 |
-
# Step 1: Run
|
21 |
subprocess.run([
|
22 |
"python3", "inference_img.py",
|
23 |
"--img", str(img_a_path), str(img_b_path)
|
24 |
], check=True)
|
25 |
|
26 |
-
# Step 2: Generate
|
27 |
subprocess.run([
|
28 |
-
"ffmpeg", "-r", "14", "-f", "image2", "-i", "
|
29 |
-
"-vf", "palettegen=stats_mode=single", "palette.png"
|
30 |
], check=True)
|
31 |
|
32 |
-
# Step 3:
|
33 |
subprocess.run([
|
34 |
-
"ffmpeg", "-r", "14", "-f", "image2", "-i", "
|
35 |
-
"-i", "palette.png", "-lavfi", "paletteuse", str(RESULT_GIF)
|
36 |
], check=True)
|
37 |
|
38 |
return str(RESULT_GIF)
|
39 |
|
40 |
except subprocess.CalledProcessError:
|
41 |
-
raise gr.Error("Interpolation failed — check
|
42 |
|
43 |
def reset_demo():
|
44 |
-
"""
|
45 |
-
Return paths to static demo images.
|
46 |
-
"""
|
47 |
return str(FRAME1), str(FRAME2)
|
48 |
|
49 |
-
|
50 |
-
with gr.Blocks(title="RIFE Image Interpolation") as demo:
|
51 |
with gr.Tab("Demo"):
|
52 |
-
gr.Markdown("###
|
53 |
-
|
54 |
img_a = gr.Image(type="filepath", value=str(FRAME1), label="Image A")
|
55 |
img_b = gr.Image(type="filepath", value=str(FRAME2), label="Image B")
|
56 |
-
|
57 |
run_btn = gr.Button("Interpolate")
|
58 |
reset_btn = gr.Button("Reset")
|
59 |
-
|
60 |
result_img = gr.Image(type="filepath", label="Interpolated GIF")
|
61 |
-
|
62 |
run_btn.click(interpolate, inputs=[img_a, img_b], outputs=result_img)
|
63 |
reset_btn.click(reset_demo, outputs=[img_a, img_b])
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
demo.launch()
|
|
|
|
|
|
|
1 |
import subprocess
|
2 |
from pathlib import Path
|
3 |
import gradio as gr
|
4 |
|
5 |
+
# --- Paths ---
|
6 |
+
FRAME1 = Path("demo/frame1.jpg")
|
7 |
+
FRAME2 = Path("demo/frame2.jpg")
|
8 |
+
TMP_PREFIX = "/tmp/gradio/"
|
9 |
+
TARGET_DIR = f"{TMP_PREFIX}output/"
|
10 |
+
RESULT_GIF = Path(f"{TARGET_DIR}retro.gif")
|
11 |
|
12 |
def interpolate(img_a_path, img_b_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
try:
|
14 |
+
# Step 1: Run inference
|
15 |
subprocess.run([
|
16 |
"python3", "inference_img.py",
|
17 |
"--img", str(img_a_path), str(img_b_path)
|
18 |
], check=True)
|
19 |
|
20 |
+
# Step 2: Generate color palette
|
21 |
subprocess.run([
|
22 |
+
"ffmpeg", "-r", "14", "-f", "image2", "-i", f"{TARGET_DIR}img%d.png",
|
23 |
+
"-vf", "palettegen=stats_mode=single", f"{TMP_PREFIX}palette.png"
|
24 |
], check=True)
|
25 |
|
26 |
+
# Step 3: Build final GIF
|
27 |
subprocess.run([
|
28 |
+
"ffmpeg", "-r", "14", "-f", "image2", "-i", f"{TARGET_DIR}img%d.png",
|
29 |
+
"-i", f"{TMP_PREFIX}palette.png", "-lavfi", "paletteuse", str(RESULT_GIF)
|
30 |
], check=True)
|
31 |
|
32 |
return str(RESULT_GIF)
|
33 |
|
34 |
except subprocess.CalledProcessError:
|
35 |
+
raise gr.Error("Interpolation failed — check 'inference_img.py' and output files.")
|
36 |
|
37 |
def reset_demo():
|
|
|
|
|
|
|
38 |
return str(FRAME1), str(FRAME2)
|
39 |
|
40 |
+
with gr.Blocks(title="RIFE Interpolation") as demo:
|
|
|
41 |
with gr.Tab("Demo"):
|
42 |
+
gr.Markdown("### Demo")
|
|
|
43 |
img_a = gr.Image(type="filepath", value=str(FRAME1), label="Image A")
|
44 |
img_b = gr.Image(type="filepath", value=str(FRAME2), label="Image B")
|
|
|
45 |
run_btn = gr.Button("Interpolate")
|
46 |
reset_btn = gr.Button("Reset")
|
|
|
47 |
result_img = gr.Image(type="filepath", label="Interpolated GIF")
|
|
|
48 |
run_btn.click(interpolate, inputs=[img_a, img_b], outputs=result_img)
|
49 |
reset_btn.click(reset_demo, outputs=[img_a, img_b])
|
50 |
|
51 |
+
with gr.Tab("Upload your images"):
|
52 |
+
gr.Markdown("### Upload two images to interpolate between them")
|
53 |
+
user_img_a = gr.Image(type="filepath", label="Upload Image A")
|
54 |
+
user_img_b = gr.Image(type="filepath", label="Upload Image B")
|
55 |
+
run_custom = gr.Button("Interpolate")
|
56 |
+
user_result = gr.Image(type="filepath", label="Interpolated Result")
|
57 |
+
run_custom.click(interpolate, inputs=[user_img_a, user_img_b], outputs=user_result)
|
58 |
+
|
59 |
demo.launch()
|