Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,95 @@
|
|
|
|
|
|
|
|
1 |
import subprocess
|
2 |
from pathlib import Path
|
|
|
|
|
3 |
import gradio as gr
|
4 |
|
5 |
-
#
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
def interpolate(img_a_path, img_b_path):
|
13 |
try:
|
14 |
-
#
|
15 |
subprocess.run([
|
16 |
"python3", "inference_img.py",
|
17 |
-
"--img", str(img_a_path), str(img_b_path)
|
|
|
18 |
], check=True)
|
19 |
|
20 |
-
#
|
21 |
subprocess.run([
|
22 |
-
"ffmpeg", "-
|
23 |
-
"-
|
|
|
|
|
24 |
], check=True)
|
25 |
|
26 |
-
#
|
27 |
subprocess.run([
|
28 |
-
"ffmpeg", "-
|
29 |
-
"-i", f"{
|
|
|
|
|
|
|
30 |
], check=True)
|
31 |
|
32 |
-
return
|
33 |
|
34 |
-
except subprocess.CalledProcessError:
|
35 |
-
raise gr.Error("Interpolation failed
|
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 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
with gr.Tab("Upload your images"):
|
52 |
-
gr.Markdown("### Upload two images
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
demo.launch()
|
|
|
1 |
+
# app.py
|
2 |
+
import os
|
3 |
+
import uuid
|
4 |
import subprocess
|
5 |
from pathlib import Path
|
6 |
+
from typing import Tuple
|
7 |
+
|
8 |
import gradio as gr
|
9 |
|
10 |
+
# Demo images
|
11 |
FRAME1 = Path("demo/frame1.jpg")
|
12 |
FRAME2 = Path("demo/frame2.jpg")
|
13 |
+
|
14 |
TMP_PREFIX = "/tmp/gradio/"
|
15 |
TARGET_DIR = f"{TMP_PREFIX}output/"
|
16 |
+
EXTENSION = "gif"
|
17 |
+
|
18 |
+
def enhance_image(img_a_path: str, img_b_path: str, mode: str) -> Tuple[str, str]:
|
19 |
+
"""
|
20 |
+
3-step pipeline:
|
21 |
+
1) inference_img.py --img <A> <B> --exp=4 -> writes frames as /tmp/gradio/output/img%d.png
|
22 |
+
2) ffmpeg palettegen
|
23 |
+
3) ffmpeg paletteuse -> final GIF at a unique path
|
24 |
+
Returns: (gif_filepath_for_preview, gif_filepath_for_download)
|
25 |
+
"""
|
26 |
+
# Ensure temp dirs exist
|
27 |
+
os.makedirs(TARGET_DIR, exist_ok=True)
|
28 |
+
|
29 |
+
# Unique output path per run
|
30 |
+
interpolated_path = f"{TARGET_DIR}{uuid.uuid4()}.{EXTENSION}"
|
31 |
+
palette_path = f"{TMP_PREFIX}palette.png"
|
32 |
|
|
|
33 |
try:
|
34 |
+
# 1) Run interpolation (frames -> TARGET_DIR/img1.png ... imgN.png)
|
35 |
subprocess.run([
|
36 |
"python3", "inference_img.py",
|
37 |
+
"--img", str(img_a_path), str(img_b_path),
|
38 |
+
"--exp", "4"
|
39 |
], check=True)
|
40 |
|
41 |
+
# 2) Generate palette
|
42 |
subprocess.run([
|
43 |
+
"ffmpeg", "-y", "-r", "14", "-f", "image2",
|
44 |
+
"-i", f"{TARGET_DIR}img%d.png",
|
45 |
+
"-vf", "palettegen=stats_mode=single",
|
46 |
+
palette_path
|
47 |
], check=True)
|
48 |
|
49 |
+
# 3) Use palette to produce final GIF
|
50 |
subprocess.run([
|
51 |
+
"ffmpeg", "-y", "-r", "14", "-f", "image2",
|
52 |
+
"-i", f"{TARGET_DIR}img%d.png",
|
53 |
+
"-i", palette_path,
|
54 |
+
"-lavfi", "paletteuse",
|
55 |
+
interpolated_path
|
56 |
], check=True)
|
57 |
|
58 |
+
return interpolated_path, interpolated_path
|
59 |
|
60 |
+
except subprocess.CalledProcessError as e:
|
61 |
+
raise gr.Error(f"Interpolation failed. {e}")
|
|
|
|
|
|
|
62 |
|
63 |
+
# Gradio UI
|
64 |
with gr.Blocks(title="RIFE Interpolation") as demo:
|
65 |
with gr.Tab("Demo"):
|
66 |
+
gr.Markdown("### Demo: Preloaded images")
|
67 |
+
input_imageA = gr.Image(type="filepath", value=str(FRAME1), label="Image A")
|
68 |
+
input_imageB = gr.Image(type="filepath", value=str(FRAME2), label="Image B")
|
69 |
+
enhance_mode = gr.Dropdown(choices=["default"], value="default", label="Mode") # placeholder to match signature
|
70 |
+
g_btn = gr.Button("Interpolate")
|
71 |
+
|
72 |
+
output_image = gr.Image(type="filepath", label="Interpolated GIF")
|
73 |
+
enhance_image_path = gr.Textbox(label="Output path", interactive=False)
|
74 |
+
|
75 |
+
g_btn.click(
|
76 |
+
fn=enhance_image,
|
77 |
+
inputs=[input_imageA, input_imageB, enhance_mode],
|
78 |
+
outputs=[output_image, enhance_image_path],
|
79 |
+
)
|
80 |
|
81 |
with gr.Tab("Upload your images"):
|
82 |
+
gr.Markdown("### Upload any two images")
|
83 |
+
user_A = gr.Image(type="filepath", label="Image A")
|
84 |
+
user_B = gr.Image(type="filepath", label="Image B")
|
85 |
+
user_mode = gr.Dropdown(choices=["default"], value="default", label="Mode")
|
86 |
+
user_btn = gr.Button("Interpolate")
|
87 |
+
user_out = gr.Image(type="filepath", label="Interpolated GIF")
|
88 |
+
user_path = gr.Textbox(label="Output path", interactive=False)
|
89 |
+
user_btn.click(
|
90 |
+
fn=enhance_image,
|
91 |
+
inputs=[user_A, user_B, user_mode],
|
92 |
+
outputs=[user_out, user_path],
|
93 |
+
)
|
94 |
|
95 |
demo.launch()
|