Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from pathlib import Path
|
3 |
+
import subprocess
|
4 |
+
import gradio as gr
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# βοΈ CPU optimization tips
|
8 |
+
os.environ["OMP_NUM_THREADS"] = "1"
|
9 |
+
os.environ["MKL_NUM_THREADS"] = "1"
|
10 |
+
|
11 |
+
# π Paths
|
12 |
+
TMP = Path("/tmp")
|
13 |
+
TMP.mkdir(parents=True, exist_ok=True)
|
14 |
+
FRAME1_PATH = TMP / "frame1.jpg"
|
15 |
+
FRAME2_PATH = TMP / "frame2.jpg"
|
16 |
+
OUTPUT_GIF = TMP / "output.gif"
|
17 |
+
|
18 |
+
def _save_resized(img: Image.Image, path: Path, max_w=640):
|
19 |
+
w, h = img.size
|
20 |
+
if w > max_w:
|
21 |
+
scale = max_w / w
|
22 |
+
img = img.resize((int(w * scale), int(h * scale)), Image.BILINEAR)
|
23 |
+
img.save(path, quality=90)
|
24 |
+
|
25 |
+
def write_gif(frames, out_path, duration_ms=60):
|
26 |
+
frames[0].save(
|
27 |
+
out_path,
|
28 |
+
save_all=True,
|
29 |
+
append_images=frames[1:],
|
30 |
+
duration=duration_ms,
|
31 |
+
loop=0,
|
32 |
+
disposal=2,
|
33 |
+
optimize=False,
|
34 |
+
dither=0
|
35 |
+
)
|
36 |
+
|
37 |
+
def generate_demo_gif(img1, img2, exp=2, progress=gr.Progress(track_tqdm=True)):
|
38 |
+
progress(0.05, desc="Preparing frames")
|
39 |
+
|
40 |
+
# π§Ή Clean previous output
|
41 |
+
if OUTPUT_GIF.exists():
|
42 |
+
OUTPUT_GIF.unlink(missing_ok=True)
|
43 |
+
|
44 |
+
# πΌ Resize input images
|
45 |
+
_save_resized(img1, FRAME1_PATH)
|
46 |
+
_save_resized(img2, FRAME2_PATH)
|
47 |
+
|
48 |
+
# π§ Build and run inference command
|
49 |
+
cmd = [
|
50 |
+
"python", "inference_img.py",
|
51 |
+
"--img", str(FRAME1_PATH), str(FRAME2_PATH),
|
52 |
+
f"--exp={int(exp)}",
|
53 |
+
"--model", "train_log/",
|
54 |
+
"--out", str(OUTPUT_GIF) # Use if your script supports explicit output path
|
55 |
+
]
|
56 |
+
print("Running:", " ".join(map(str, cmd)))
|
57 |
+
|
58 |
+
progress(0.20, desc="Running inference")
|
59 |
+
result = subprocess.run(cmd, capture_output=True, text=True)
|
60 |
+
|
61 |
+
print("STDOUT:", result.stdout)
|
62 |
+
print("STDERR:", result.stderr)
|
63 |
+
|
64 |
+
progress(0.85, desc="Checking result")
|
65 |
+
|
66 |
+
if result.returncode == 0 and OUTPUT_GIF.exists():
|
67 |
+
progress(1.0, desc="Done")
|
68 |
+
return str(OUTPUT_GIF), "β
GIF generated successfully!"
|
69 |
+
else:
|
70 |
+
return None, "β GIF generation failed. Try smaller images or check your model path."
|
71 |
+
|
72 |
+
# π₯ Gradio UI
|
73 |
+
with gr.Blocks() as demo_ui:
|
74 |
+
gr.Markdown("### πΌ Generate a GIF animation from two frames (CPU-optimized)")
|
75 |
+
with gr.Row():
|
76 |
+
img1 = gr.Image(label="Frame 1", type="pil")
|
77 |
+
img2 = gr.Image(label="Frame 2", type="pil")
|
78 |
+
with gr.Row():
|
79 |
+
exp = gr.Slider(1, 4, value=2, step=1, label="Interpolation Exponent (lower = faster)")
|
80 |
+
run = gr.Button("Generate GIF")
|
81 |
+
out_gif = gr.Image(label="Animated GIF")
|
82 |
+
status = gr.Markdown()
|
83 |
+
|
84 |
+
run.click(generate_demo_gif, [img1, img2, exp], [out_gif, status])
|
85 |
+
|
86 |
+
# π§΅ Enable queuing to prevent app cutoff
|
87 |
+
demo_ui.queue(concurrency_count=1, max_size=8)
|
88 |
+
demo_ui.launch()
|