Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,8 @@ 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"
|
@@ -22,67 +23,61 @@ def _save_resized(img: Image.Image, path: Path, max_w=640):
|
|
22 |
img = img.resize((int(w * scale), int(h * scale)), Image.BILINEAR)
|
23 |
img.save(path, quality=90)
|
24 |
|
25 |
-
def
|
26 |
-
|
27 |
-
|
28 |
-
|
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 |
-
#
|
|
|
|
|
|
|
|
|
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(
|
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="
|
65 |
|
66 |
if result.returncode == 0 and OUTPUT_GIF.exists():
|
|
|
67 |
progress(1.0, desc="Done")
|
68 |
-
return
|
69 |
else:
|
70 |
return None, "β GIF generation failed. Try smaller images or check your model path."
|
71 |
|
72 |
-
#
|
73 |
with gr.Blocks() as demo_ui:
|
74 |
-
gr.Markdown("
|
|
|
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()
|
88 |
demo_ui.launch()
|
|
|
2 |
from pathlib import Path
|
3 |
import subprocess
|
4 |
import gradio as gr
|
5 |
+
from PIL import Image, ImageSequence
|
6 |
+
import io
|
7 |
|
8 |
# βοΈ CPU optimization tips
|
9 |
os.environ["OMP_NUM_THREADS"] = "1"
|
|
|
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 |
+
# π Run inference
|
43 |
cmd = [
|
44 |
"python", "inference_img.py",
|
45 |
"--img", str(FRAME1_PATH), str(FRAME2_PATH),
|
46 |
f"--exp={int(exp)}",
|
47 |
+
"--model", "train_log/"
|
|
|
48 |
]
|
49 |
+
print("Running:", " ".join(cmd))
|
50 |
+
progress(0.25, desc="Running inference")
|
|
|
51 |
result = subprocess.run(cmd, capture_output=True, text=True)
|
52 |
|
53 |
print("STDOUT:", result.stdout)
|
54 |
print("STDERR:", result.stderr)
|
55 |
|
56 |
+
progress(0.85, desc="Rendering result")
|
57 |
|
58 |
if result.returncode == 0 and OUTPUT_GIF.exists():
|
59 |
+
gif_image = load_gif_as_pil(OUTPUT_GIF)
|
60 |
progress(1.0, desc="Done")
|
61 |
+
return gif_image, "β
GIF generated successfully!"
|
62 |
else:
|
63 |
return None, "β GIF generation failed. Try smaller images or check your model path."
|
64 |
|
65 |
+
# π¨ Hugging Face UI
|
66 |
with gr.Blocks() as demo_ui:
|
67 |
+
gr.Markdown("## π Img_InterpolationCutoff β Frame-to-GIF Generator")
|
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 |
exp = gr.Slider(1, 4, value=2, step=1, label="Interpolation Exponent (lower = faster)")
|
75 |
+
|
76 |
run = gr.Button("Generate GIF")
|
77 |
out_gif = gr.Image(label="Animated GIF")
|
78 |
status = gr.Markdown()
|
79 |
|
80 |
run.click(generate_demo_gif, [img1, img2, exp], [out_gif, status])
|
81 |
|
|
|
82 |
demo_ui.queue()
|
83 |
demo_ui.launch()
|