AItool commited on
Commit
c3500d3
Β·
verified Β·
1 Parent(s): 63b073e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
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()