Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,65 @@
|
|
|
|
1 |
import os
|
2 |
-
|
3 |
import subprocess
|
|
|
4 |
import gradio as gr
|
5 |
-
from PIL import Image
|
6 |
-
import io
|
7 |
|
8 |
-
|
|
|
9 |
|
10 |
-
|
11 |
-
FRAME2_PATH = "demo/frame2.png"
|
12 |
-
TARGET_DIR = "/home/user/app/output/"
|
13 |
-
#TARGET_DIR = "/home/user/app/output/"
|
14 |
-
OUTPUT_GIF = "output/output.gif"
|
15 |
PALETTE_PNG = "demo/palette.png"
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
"
|
32 |
-
"
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
# UI setup
|
70 |
-
with gr.Blocks() as demo_ui:
|
71 |
-
gr.Markdown("## 🎞️ Demo GIF Generator — Interpolate Two Frames")
|
72 |
-
|
73 |
-
with gr.Row():
|
74 |
-
gr.Image(value=FRAME1_PATH, label="Frame 1", interactive=False)
|
75 |
-
gr.Image(value=FRAME2_PATH, label="Frame 2", interactive=False)
|
76 |
-
|
77 |
-
exp = gr.Slider(1, 4, value=2, step=1, label="Interpolation Exponent")
|
78 |
-
run_btn = gr.Button("Generate GIF")
|
79 |
-
out_gif = gr.Image(label="Output GIF")
|
80 |
-
status = gr.Markdown()
|
81 |
-
|
82 |
-
run_btn.click(fn=generate_demo_gif, inputs=[exp], outputs=[out_gif, status])
|
83 |
-
|
84 |
-
|
85 |
-
# Launch the app
|
86 |
-
demo_ui.launch(ssr_mode=False)
|
87 |
-
|
|
|
1 |
+
# app.py
|
2 |
import os
|
3 |
+
import uuid
|
4 |
import subprocess
|
5 |
+
from pathlib import Path
|
6 |
import gradio as gr
|
|
|
|
|
7 |
|
8 |
+
FRAME1 = Path("demo/frame1.png")
|
9 |
+
FRAME2 = Path("demo/frame2.png")
|
10 |
|
11 |
+
TARGET_DIR = "/home/user/app/output/" # absolute path that inference writes to
|
|
|
|
|
|
|
|
|
12 |
PALETTE_PNG = "demo/palette.png"
|
13 |
+
OUTPUT_GIF_DIR = "output"
|
14 |
+
os.makedirs("demo", exist_ok=True)
|
15 |
+
os.makedirs(TARGET_DIR, exist_ok=True)
|
16 |
+
os.makedirs(OUTPUT_GIF_DIR, exist_ok=True)
|
17 |
+
|
18 |
+
def interpolate_image(img_a_path: str, img_b_path: str) -> tuple[str, str]:
|
19 |
+
# 1) Run inference to generate frames into TARGET_DIR/img%d.png
|
20 |
+
subprocess.run([
|
21 |
+
"python3", "inference_img.py",
|
22 |
+
"--img", str(img_a_path), str(img_b_path),
|
23 |
+
"--exp", "4"
|
24 |
+
], check=True)
|
25 |
+
|
26 |
+
# 2) Generate palette from frames
|
27 |
+
subprocess.run([
|
28 |
+
"ffmpeg", "-y", "-r", "14", "-f", "image2",
|
29 |
+
"-i", f"{TARGET_DIR}img%d.png",
|
30 |
+
"-vf", "palettegen=stats_mode=single",
|
31 |
+
PALETTE_PNG
|
32 |
+
], check=True)
|
33 |
+
|
34 |
+
# 3) Generate final GIF using palette
|
35 |
+
out_gif = os.path.join(OUTPUT_GIF_DIR, f"{uuid.uuid4()}.gif")
|
36 |
+
subprocess.run([
|
37 |
+
"ffmpeg", "-y", "-r", "14", "-f", "image2",
|
38 |
+
"-i", f"{TARGET_DIR}img%d.png",
|
39 |
+
"-i", PALETTE_PNG,
|
40 |
+
"-lavfi", "paletteuse",
|
41 |
+
out_gif
|
42 |
+
], check=True)
|
43 |
+
|
44 |
+
return out_gif, out_gif
|
45 |
+
|
46 |
+
with gr.Blocks(title="RIFE Interpolation") as demo:
|
47 |
+
with gr.Tab("Demo"):
|
48 |
+
gr.Markdown("### Demo: Preloaded images")
|
49 |
+
input_imageA = gr.Image(type="filepath", value=str(FRAME1), label="Image A")
|
50 |
+
input_imageB = gr.Image(type="filepath", value=str(FRAME2), label="Image B")
|
51 |
+
run_btn = gr.Button("Interpolate")
|
52 |
+
result_img = gr.Image(type="filepath", label="Interpolated GIF")
|
53 |
+
result_path = gr.Textbox(label="Output path", interactive=False)
|
54 |
+
run_btn.click(interpolate_image, [input_imageA, input_imageB], [result_img, result_path])
|
55 |
+
|
56 |
+
with gr.Tab("Upload your images"):
|
57 |
+
gr.Markdown("### Upload any two images")
|
58 |
+
user_A = gr.Image(type="filepath", label="Image A")
|
59 |
+
user_B = gr.Image(type="filepath", label="Image B")
|
60 |
+
run_btn2 = gr.Button("Interpolate")
|
61 |
+
user_img = gr.Image(type="filepath", label="Interpolated GIF")
|
62 |
+
user_path = gr.Textbox(label="Output path", interactive=False)
|
63 |
+
run_btn2.click(interpolate_image, [user_A, user_B], [user_img, user_path])
|
64 |
+
|
65 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|