Spaces:
Running
Running
import os | |
from pathlib import Path | |
import subprocess | |
import gradio as gr | |
from PIL import Image | |
import io | |
# Fixed input paths and output location | |
FRAME1_PATH = "demo/frame1.png" | |
FRAME2_PATH = "demo/frame2.png" | |
OUTPUT_GIF = "/tmp/output.gif" | |
def generate_demo_gif(exp=2, progress=gr.Progress(track_tqdm=True)): | |
success = cv2.imwrite('/output/test.png', img_array) | |
print("Saved?", success) | |
progress(0.1, desc="Starting inference...") | |
# Delete old output if exists | |
try: | |
os.remove(OUTPUT_GIF) | |
except FileNotFoundError: | |
pass | |
# Build and run command | |
cmd = [ | |
"python", "inference_img.py", | |
"--img", FRAME1_PATH, FRAME2_PATH, | |
"--exp", str(exp), | |
"--model", "train_log/" | |
] | |
print("Running:", " ".join(cmd)) | |
result = subprocess.run(cmd, capture_output=True, text=True) | |
print("STDOUT:", result.stdout) | |
print("STDERR:", result.stderr) | |
print("Exists?", os.path.exists("/output/output.gif")) # β¬ οΈ Add it here | |
# Check and display result | |
if result.returncode == 0 and os.path.exists(OUTPUT_GIF): | |
with open(OUTPUT_GIF, "rb") as f: | |
gif = Image.open(io.BytesIO(f.read())) | |
progress(1.0, desc="GIF created!") | |
return gif, "β Done!" | |
else: | |
return None, "β Inference failed or output missing" | |
# UI setup | |
with gr.Blocks() as demo_ui: | |
gr.Markdown("## ποΈ Demo GIF Generator β Interpolate Two Frames") | |
with gr.Row(): | |
gr.Image(value=FRAME1_PATH, label="Frame 1", interactive=False) | |
gr.Image(value=FRAME2_PATH, label="Frame 2", interactive=False) | |
exp = gr.Slider(1, 4, value=2, step=1, label="Interpolation Exponent") | |
run_btn = gr.Button("Generate GIF") | |
out_gif = gr.Image(label="Output GIF") | |
status = gr.Markdown() | |
run_btn.click(fn=generate_demo_gif, inputs=[exp], outputs=[out_gif, status]) | |
# Launch the app | |
demo_ui.launch(ssr_mode=False) | |