AItool commited on
Commit
a6f1332
·
verified ·
1 Parent(s): f1d0309

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -87
app.py CHANGED
@@ -1,91 +1,58 @@
1
- import os
2
- import shutil
3
- import tempfile
4
- import threading
5
  import subprocess
6
  from pathlib import Path
7
-
8
  import gradio as gr
9
- from PIL import Image
10
-
11
- RIFE_DIR = Path("rife") # Local bundled repo
12
- OUTPUT_DIR = RIFE_DIR / "output"
13
- LOCK = threading.Lock()
14
-
15
- def interpolate(a: Image.Image, b: Image.Image, fps: int = 14, exp: int = 2):
16
- with LOCK:
17
- # Normalize inputs
18
- a = a.convert("RGB")
19
- b = b.convert("RGB")
20
- if b.size != a.size:
21
- b = b.resize(a.size, Image.BICUBIC)
22
-
23
- work_dir = Path(tempfile.mkdtemp(prefix="rife_run_"))
24
- p1 = work_dir / "a.png"
25
- p2 = work_dir / "b.png"
26
- a.save(p1, "PNG")
27
- b.save(p2, "PNG")
28
-
29
- # Clean previous outputs
30
- if OUTPUT_DIR.exists():
31
- shutil.rmtree(OUTPUT_DIR)
32
- OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
33
-
34
- # Run RIFE inference
35
- cmd = ["python3", str(RIFE_DIR / "inference_img.py"), "--img", str(p1), str(p2)]
36
- if isinstance(exp, int) and exp >= 1:
37
- cmd += ["--exp", str(exp)]
38
- subprocess.run(cmd, cwd=str(RIFE_DIR), check=True)
39
-
40
- # Collect interpolated frames
41
- frames = []
42
- i = 1
43
- while True:
44
- fp = OUTPUT_DIR / f"img{i}.png"
45
- if not fp.exists():
46
- break
47
- frames.append(fp)
48
- i += 1
49
- if not frames:
50
- raise RuntimeError("No frames generated.")
51
-
52
- # Build GIF
53
- images = [Image.open(p).convert("RGBA") for p in frames]
54
- duration_ms = max(1, int(1000 / max(1, fps)))
55
- gif_path = work_dir / "interpolation.gif"
56
- images[0].save(
57
- gif_path,
58
- save_all=True,
59
- append_images=images[1:],
60
- optimize=False,
61
- duration=duration_ms,
62
- loop=0,
63
- disposal=2,
64
- )
65
-
66
- # Optional cleanup
67
- try:
68
- shutil.rmtree(OUTPUT_DIR)
69
- except Exception:
70
- pass
71
-
72
- return str(gif_path)
73
-
74
- # Gradio UI
75
- TITLE = "🔥 RIFE Interpolation Demo (PyTorch, Local)"
76
- with gr.Blocks(title=TITLE, analytics_enabled=False) as demo:
77
- gr.Markdown(f"# {TITLE}")
78
- with gr.Row():
79
- with gr.Column():
80
- img_a = gr.Image(type="pil", label="Image A")
81
- img_b = gr.Image(type="pil", label="Image B")
82
- with gr.Column():
83
- fps = gr.Slider(6, 30, value=14, step=1, label="FPS")
84
- exp = gr.Slider(1, 4, value=2, step=1, label="Interpolation exponent")
85
- run = gr.Button("Interpolate", variant="primary")
86
- gif_out = gr.Image(type="filepath", label="Result GIF")
87
- run.click(interpolate, inputs=[img_a, img_b, fps, exp], outputs=[gif_out])
88
- demo.queue(concurrency_count=1, max_size=8)
89
 
90
- if __name__ == "__main__":
91
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
 
 
 
2
  import subprocess
3
  from pathlib import Path
 
4
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ # --- Paths ---
7
+ DEMO_DIR = Path("demo") # Folder with input frames
8
+ FRAME1 = DEMO_DIR / "frame1.png" # First image
9
+ FRAME2 = DEMO_DIR / "frame2.png" # Second image
10
+ RETRO_GIF = DEMO_DIR / "demo.gif" # Final result
11
+
12
+ # --- Interpolation function ---
13
+ def interpolate(img_a_path, img_b_path):
14
+ """
15
+ Run interpolation pipeline using existing shell scripts and commands.
16
+ Expects two input image paths. Outputs interpolated GIF.
17
+ """
18
+ try:
19
+ # Step 1: Run your inference script to generate intermediate frames
20
+ subprocess.run([
21
+ "python3", "img_inference.py",
22
+ "--img", str(img_a_path), str(img_b_path)
23
+ ], check=True)
24
+
25
+ # Step 2: Generate optimized palette using ffmpeg
26
+ subprocess.run([
27
+ "ffmpeg", "-r", "14", "-f", "image2", "-i", "output/img%d.png",
28
+ "-vf", "palettegen=stats_mode=single", "palette.png"
29
+ ], check=True)
30
+
31
+ # Step 3: Apply palette to produce final gif
32
+ subprocess.run([
33
+ "ffmpeg", "-r", "14", "-f", "image2", "-i", "output/img%d.png",
34
+ "-i", "palette.png", "-lavfi", "paletteuse", str(RETRO_GIF)
35
+ ], check=True)
36
+
37
+ return str(RETRO_GIF)
38
+ except subprocess.CalledProcessError:
39
+ raise gr.Error("Interpolation failed. Please check script and inputs.")
40
+
41
+ # --- Demo reset: returns static frames ---
42
+ def reset_demo():
43
+ return str(FRAME1), str(FRAME2)
44
+
45
+ # --- Gradio UI ---
46
+ with gr.Blocks(title="RIFE Image Interpolation") as demo:
47
+ with gr.Tab("Demo"):
48
+ gr.Markdown("### Interpolate between two images")
49
+ img_a = gr.Image(type="filepath", value=str(FRAME1), label="Image A")
50
+ img_b = gr.Image(type="filepath", value=str(FRAME2), label="Image B")
51
+ run = gr.Button("Interpolate")
52
+ reset = gr.Button("Reset")
53
+
54
+ result = gr.Image(type="filepath", label="Interpolated GIF")
55
+ run.click(interpolate, inputs=[img_a, img_b], outputs=result)
56
+ reset.click(reset_demo, outputs=[img_a, img_b])
57
+
58
+ demo.launch()