Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,44 +2,22 @@ import os
|
|
2 |
from pathlib import Path
|
3 |
import subprocess
|
4 |
import gradio as gr
|
5 |
-
from PIL import Image
|
6 |
import io
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
TMP.mkdir(parents=True, exist_ok=True)
|
15 |
-
FRAME1_PATH = TMP / "frame1.jpg"
|
16 |
-
FRAME2_PATH = TMP / "frame2.jpg"
|
17 |
-
OUTPUT_GIF = TMP / "output.gif"
|
18 |
|
19 |
-
|
20 |
-
w, h = img.size
|
21 |
-
if w > max_w:
|
22 |
-
scale = max_w / w
|
23 |
-
img = img.resize((int(w * scale), int(h * scale)), Image.BILINEAR)
|
24 |
-
img.save(path, quality=90)
|
25 |
-
|
26 |
-
def load_gif_as_pil(gif_path: Path):
|
27 |
-
with open(gif_path, "rb") as f:
|
28 |
-
gif_bytes = f.read()
|
29 |
-
return Image.open(io.BytesIO(gif_bytes))
|
30 |
-
|
31 |
-
def generate_demo_gif(img1, img2, exp=2, progress=gr.Progress(track_tqdm=True)):
|
32 |
-
progress(0.05, desc="Preparing frames")
|
33 |
-
|
34 |
-
# πΌ Resize input images
|
35 |
-
_save_resized(img1, FRAME1_PATH)
|
36 |
-
_save_resized(img2, FRAME2_PATH)
|
37 |
-
|
38 |
-
# π Clean previous output
|
39 |
if OUTPUT_GIF.exists():
|
40 |
OUTPUT_GIF.unlink(missing_ok=True)
|
41 |
|
42 |
-
#
|
43 |
cmd = [
|
44 |
"python", "inference_img.py",
|
45 |
"--img", str(FRAME1_PATH), str(FRAME2_PATH),
|
@@ -47,37 +25,36 @@ def generate_demo_gif(img1, img2, exp=2, progress=gr.Progress(track_tqdm=True)):
|
|
47 |
"--model", "train_log/"
|
48 |
]
|
49 |
print("Running:", " ".join(cmd))
|
50 |
-
progress(0.25, desc="Running
|
51 |
-
result = subprocess.run(cmd, capture_output=True, text=True)
|
52 |
|
|
|
53 |
print("STDOUT:", result.stdout)
|
54 |
print("STDERR:", result.stderr)
|
55 |
|
56 |
-
|
57 |
-
|
58 |
if result.returncode == 0 and OUTPUT_GIF.exists():
|
59 |
-
|
|
|
|
|
|
|
60 |
progress(1.0, desc="Done")
|
61 |
-
return
|
62 |
else:
|
63 |
-
return None, "β GIF generation failed.
|
64 |
|
65 |
-
#
|
66 |
with gr.Blocks() as demo_ui:
|
67 |
-
gr.Markdown("## π Img_InterpolationCutoff β
|
68 |
-
|
69 |
-
with gr.Row():
|
70 |
-
img1 = gr.Image(label="Frame 1", type="pil")
|
71 |
-
img2 = gr.Image(label="Frame 2", type="pil")
|
72 |
|
73 |
with gr.Row():
|
74 |
-
|
|
|
75 |
|
76 |
-
|
|
|
77 |
out_gif = gr.Image(label="Animated GIF")
|
78 |
status = gr.Markdown()
|
79 |
|
80 |
-
|
81 |
|
82 |
-
demo_ui.queue()
|
83 |
demo_ui.launch()
|
|
|
2 |
from pathlib import Path
|
3 |
import subprocess
|
4 |
import gradio as gr
|
5 |
+
from PIL import Image
|
6 |
import io
|
7 |
|
8 |
+
# Paths to pre-existing demo images
|
9 |
+
FRAME1_PATH = Path("demo/frame1.png")
|
10 |
+
FRAME2_PATH = Path("demo/frame2.png")
|
11 |
+
OUTPUT_GIF = Path("/tmp/output.gif") # Use /tmp for fast access
|
12 |
|
13 |
+
def generate_demo_gif(exp=2, progress=gr.Progress(track_tqdm=True)):
|
14 |
+
progress(0.05, desc="Starting...")
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# Clean previous output if any
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
if OUTPUT_GIF.exists():
|
18 |
OUTPUT_GIF.unlink(missing_ok=True)
|
19 |
|
20 |
+
# Build command using demo images directly
|
21 |
cmd = [
|
22 |
"python", "inference_img.py",
|
23 |
"--img", str(FRAME1_PATH), str(FRAME2_PATH),
|
|
|
25 |
"--model", "train_log/"
|
26 |
]
|
27 |
print("Running:", " ".join(cmd))
|
28 |
+
progress(0.25, desc="Running model")
|
|
|
29 |
|
30 |
+
result = subprocess.run(cmd, capture_output=True, text=True)
|
31 |
print("STDOUT:", result.stdout)
|
32 |
print("STDERR:", result.stderr)
|
33 |
|
34 |
+
# Check result
|
|
|
35 |
if result.returncode == 0 and OUTPUT_GIF.exists():
|
36 |
+
progress(0.85, desc="Loading GIF")
|
37 |
+
with open(OUTPUT_GIF, "rb") as f:
|
38 |
+
gif_bytes = f.read()
|
39 |
+
image = Image.open(io.BytesIO(gif_bytes))
|
40 |
progress(1.0, desc="Done")
|
41 |
+
return image, "β
GIF generated successfully!"
|
42 |
else:
|
43 |
+
return None, "β GIF generation failed. Check model and paths."
|
44 |
|
45 |
+
# Build Gradio UI (no uploads)
|
46 |
with gr.Blocks() as demo_ui:
|
47 |
+
gr.Markdown("## π Img_InterpolationCutoff β Demo GIF from Preloaded Frames")
|
|
|
|
|
|
|
|
|
48 |
|
49 |
with gr.Row():
|
50 |
+
frame_preview_1 = gr.Image(value=str(FRAME1_PATH), interactive=False, label="Frame 1")
|
51 |
+
frame_preview_2 = gr.Image(value=str(FRAME2_PATH), interactive=False, label="Frame 2")
|
52 |
|
53 |
+
exp = gr.Slider(1, 4, value=2, step=1, label="Interpolation Exponent")
|
54 |
+
run_btn = gr.Button("Generate GIF")
|
55 |
out_gif = gr.Image(label="Animated GIF")
|
56 |
status = gr.Markdown()
|
57 |
|
58 |
+
run_btn.click(fn=generate_demo_gif, inputs=[exp], outputs=[out_gif, status])
|
59 |
|
|
|
60 |
demo_ui.launch()
|