AItool's picture
Update app.py
b9a22ab verified
raw
history blame
2.13 kB
# app.py
import subprocess
from pathlib import Path
import gradio as gr
# --- Static demo image paths ---
FRAME1 = Path("demo/frame1.jpg") # First image
FRAME2 = Path("demo/frame2.jpg") # Second image
RESULT_GIF = Path("output/retro.gif") # Expected result path
def interpolate(img_a_path, img_b_path):
"""
Run the interpolation pipeline from the Space root:
1. Run img_inference.py to generate intermediate frames
2. Create color palette with ffmpeg
3. Build final interpolated GIF with palette
"""
try:
# Step 1: Run the inference script
subprocess.run([
"python3", "img_inference.py",
"--img", str(img_a_path), str(img_b_path)
], check=True)
# Step 2: Generate optimized palette
subprocess.run([
"ffmpeg", "-r", "14", "-f", "image2", "-i", "output/img%d.png",
"-vf", "palettegen=stats_mode=single", "palette.png"
], check=True)
# Step 3: Create final GIF with palette
subprocess.run([
"ffmpeg", "-r", "14", "-f", "image2", "-i", "output/img%d.png",
"-i", "palette.png", "-lavfi", "paletteuse", str(RESULT_GIF)
], check=True)
return str(RESULT_GIF)
except subprocess.CalledProcessError:
raise gr.Error("Interpolation failed β€” check your script and paths.")
def reset_demo():
"""
Return paths to static demo images.
"""
return str(FRAME1), str(FRAME2)
# --- Gradio Interface ---
with gr.Blocks(title="RIFE Image Interpolation") as demo:
with gr.Tab("Demo"):
gr.Markdown("### Interpolate between two images β†’ Retro GIF")
img_a = gr.Image(type="filepath", value=str(FRAME1), label="Image A")
img_b = gr.Image(type="filepath", value=str(FRAME2), label="Image B")
run_btn = gr.Button("Interpolate")
reset_btn = gr.Button("Reset")
result_img = gr.Image(type="filepath", label="Interpolated GIF")
run_btn.click(interpolate, inputs=[img_a, img_b], outputs=result_img)
reset_btn.click(reset_demo, outputs=[img_a, img_b])
demo.launch()