AItool's picture
Update app.py
69b5186 verified
raw
history blame
2.66 kB
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()