Spaces:
Running
Running
import os | |
from pathlib import Path | |
import subprocess | |
import gradio as gr | |
from PIL import Image | |
# βοΈ CPU optimization tips | |
os.environ["OMP_NUM_THREADS"] = "1" | |
os.environ["MKL_NUM_THREADS"] = "1" | |
# π Paths | |
TMP = Path("/tmp") | |
TMP.mkdir(parents=True, exist_ok=True) | |
FRAME1_PATH = TMP / "frame1.jpg" | |
FRAME2_PATH = TMP / "frame2.jpg" | |
OUTPUT_GIF = TMP / "output.gif" | |
def _save_resized(img: Image.Image, path: Path, max_w=640): | |
w, h = img.size | |
if w > max_w: | |
scale = max_w / w | |
img = img.resize((int(w * scale), int(h * scale)), Image.BILINEAR) | |
img.save(path, quality=90) | |
def write_gif(frames, out_path, duration_ms=60): | |
frames[0].save( | |
out_path, | |
save_all=True, | |
append_images=frames[1:], | |
duration=duration_ms, | |
loop=0, | |
disposal=2, | |
optimize=False, | |
dither=0 | |
) | |
def generate_demo_gif(img1, img2, exp=2, progress=gr.Progress(track_tqdm=True)): | |
progress(0.05, desc="Preparing frames") | |
# π§Ή Clean previous output | |
if OUTPUT_GIF.exists(): | |
OUTPUT_GIF.unlink(missing_ok=True) | |
# πΌ Resize input images | |
_save_resized(img1, FRAME1_PATH) | |
_save_resized(img2, FRAME2_PATH) | |
# π§ Build and run inference command | |
cmd = [ | |
"python", "inference_img.py", | |
"--img", str(FRAME1_PATH), str(FRAME2_PATH), | |
f"--exp={int(exp)}", | |
"--model", "train_log/", | |
"--out", str(OUTPUT_GIF) # Use if your script supports explicit output path | |
] | |
print("Running:", " ".join(map(str, cmd))) | |
progress(0.20, desc="Running inference") | |
result = subprocess.run(cmd, capture_output=True, text=True) | |
print("STDOUT:", result.stdout) | |
print("STDERR:", result.stderr) | |
progress(0.85, desc="Checking result") | |
if result.returncode == 0 and OUTPUT_GIF.exists(): | |
progress(1.0, desc="Done") | |
return str(OUTPUT_GIF), "β GIF generated successfully!" | |
else: | |
return None, "β GIF generation failed. Try smaller images or check your model path." | |
# π₯ Gradio UI | |
with gr.Blocks() as demo_ui: | |
gr.Markdown("### πΌ Generate a GIF animation from two frames (CPU-optimized)") | |
with gr.Row(): | |
img1 = gr.Image(label="Frame 1", type="pil") | |
img2 = gr.Image(label="Frame 2", type="pil") | |
with gr.Row(): | |
exp = gr.Slider(1, 4, value=2, step=1, label="Interpolation Exponent (lower = faster)") | |
run = gr.Button("Generate GIF") | |
out_gif = gr.Image(label="Animated GIF") | |
status = gr.Markdown() | |
run.click(generate_demo_gif, [img1, img2, exp], [out_gif, status]) | |
# π§΅ Enable queuing to prevent app cutoff | |
demo_ui.queue() | |
demo_ui.launch() | |