Spaces:
Running
Running
# app.py | |
import os | |
import uuid | |
import subprocess | |
from pathlib import Path | |
import gradio as gr | |
FRAME1 = Path("demo/frame1.png") | |
FRAME2 = Path("demo/frame2.png") | |
TARGET_DIR = "/home/user/app/output/" | |
PALETTE_PNG = Path(TARGET_DIR) / "palette.png" | |
OUTPUT_GIF = Path(TARGET_DIR) / "output.gif" | |
os.makedirs(TARGET_DIR, exist_ok=True) | |
def interpolate_image(img_a_path: str, img_b_path: str) -> str: | |
# --- clear any previous output --- | |
if OUTPUT_GIF.exists(): | |
OUTPUT_GIF.unlink() # delete old GIF | |
if PALETTE_PNG.exists(): | |
PALETTE_PNG.unlink() # delete old palette | |
# optional: clear any old frame PNGs | |
for f in TARGET_DIR.glob("img*.png"): | |
f.unlink() | |
# 1) Run inference to generate frames into TARGET_DIR/img%d.png | |
subprocess.run([ | |
"python3", "inference_img.py", | |
"--img", str(img_a_path), str(img_b_path), | |
"--exp", "4" | |
], check=True) | |
# 2) Generate palette from frames | |
subprocess.run([ | |
"ffmpeg", "-y", "-r", "14", "-f", "image2", | |
"-i", f"{TARGET_DIR}img%d.png", | |
"-vf", "palettegen=stats_mode=single", | |
"-frames:v", "1", | |
str(PALETTE_PNG) | |
], check=True) | |
# 3) Generate final GIF using palette | |
subprocess.run([ | |
"ffmpeg", "-y", "-r", "14", "-f", "image2", | |
"-i", f"{TARGET_DIR}img%d.png", | |
"-i", str(PALETTE_PNG), | |
"-lavfi", "paletteuse", | |
str(OUTPUT_GIF) | |
], check=True) | |
return str(OUTPUT_GIF) | |
# helper to read the Markdown file | |
def load_description(path): | |
with open(path, "r", encoding="utf-8") as f: | |
return f.read() | |
description_text = load_description("TITLE.md") | |
with gr.Blocks(title="RIFE Image Interpolation") as demo: | |
# render the markdown text at the top of the UI | |
gr.Markdown(description_text) | |
with gr.Tab("Demo"): | |
gr.Markdown("### Demo: Preloaded images") | |
input_imageA = gr.Image(type="filepath", value=str(FRAME1), label="Image A") | |
input_imageB = gr.Image(type="filepath", value=str(FRAME2), label="Image B") | |
run_btn = gr.Button("Interpolate") | |
result_img = gr.Image(type="filepath", label="Interpolated GIF") | |
#result_path = gr.Textbox(label="Output path", interactive=False) | |
run_btn.click(interpolate_image, [input_imageA, input_imageB], [result_img]) | |
with gr.Tab("Upload your images"): | |
gr.Markdown("### Upload any two images") | |
user_A = gr.Image(type="filepath", label="Image A") | |
user_B = gr.Image(type="filepath", label="Image B") | |
run_btn2 = gr.Button("Interpolate") | |
user_img = gr.Image(type="filepath", label="Interpolated GIF") | |
#user_path = gr.Textbox(label="Output path", interactive=False) | |
run_btn2.click(interpolate_image, [user_A, user_B], [user_img]) | |
gr.HTML("""<div style="margin: 0.75em 0;"><a href="https://www.buymeacoffee.com/Artgen" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a></div> | |
<div style="margin: 0.75em 0;">But what would really help me is a <strong>PRO subscription</strong> to Google Colab, Kaggle or Hugging Face. Many thanks.</div>""") | |
demo.launch() | |