AItool commited on
Commit
8085664
·
verified ·
1 Parent(s): c442a20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -31
app.py CHANGED
@@ -1,59 +1,95 @@
 
 
 
1
  import subprocess
2
  from pathlib import Path
 
 
3
  import gradio as gr
4
 
5
- # --- Paths ---
6
  FRAME1 = Path("demo/frame1.jpg")
7
  FRAME2 = Path("demo/frame2.jpg")
 
8
  TMP_PREFIX = "/tmp/gradio/"
9
  TARGET_DIR = f"{TMP_PREFIX}output/"
10
- RESULT_GIF = Path(f"{TARGET_DIR}retro.gif")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- def interpolate(img_a_path, img_b_path):
13
  try:
14
- # Step 1: Run inference
15
  subprocess.run([
16
  "python3", "inference_img.py",
17
- "--img", str(img_a_path), str(img_b_path)
 
18
  ], check=True)
19
 
20
- # Step 2: Generate color palette
21
  subprocess.run([
22
- "ffmpeg", "-r", "14", "-f", "image2", "-i", f"{TARGET_DIR}img%d.png",
23
- "-vf", "palettegen=stats_mode=single", f"{TMP_PREFIX}palette.png"
 
 
24
  ], check=True)
25
 
26
- # Step 3: Build final GIF
27
  subprocess.run([
28
- "ffmpeg", "-r", "14", "-f", "image2", "-i", f"{TARGET_DIR}img%d.png",
29
- "-i", f"{TMP_PREFIX}palette.png", "-lavfi", "paletteuse", str(RESULT_GIF)
 
 
 
30
  ], check=True)
31
 
32
- return str(RESULT_GIF)
33
 
34
- except subprocess.CalledProcessError:
35
- raise gr.Error("Interpolation failed — check 'inference_img.py' and output files.")
36
-
37
- def reset_demo():
38
- return str(FRAME1), str(FRAME2)
39
 
 
40
  with gr.Blocks(title="RIFE Interpolation") as demo:
41
  with gr.Tab("Demo"):
42
- gr.Markdown("### Demo")
43
- img_a = gr.Image(type="filepath", value=str(FRAME1), label="Image A")
44
- img_b = gr.Image(type="filepath", value=str(FRAME2), label="Image B")
45
- run_btn = gr.Button("Interpolate")
46
- reset_btn = gr.Button("Reset")
47
- result_img = gr.Image(type="filepath", label="Interpolated GIF")
48
- run_btn.click(interpolate, inputs=[img_a, img_b], outputs=result_img)
49
- reset_btn.click(reset_demo, outputs=[img_a, img_b])
 
 
 
 
 
 
50
 
51
  with gr.Tab("Upload your images"):
52
- gr.Markdown("### Upload two images to interpolate between them")
53
- user_img_a = gr.Image(type="filepath", label="Upload Image A")
54
- user_img_b = gr.Image(type="filepath", label="Upload Image B")
55
- run_custom = gr.Button("Interpolate")
56
- user_result = gr.Image(type="filepath", label="Interpolated Result")
57
- run_custom.click(interpolate, inputs=[user_img_a, user_img_b], outputs=user_result)
 
 
 
 
 
 
58
 
59
  demo.launch()
 
1
+ # app.py
2
+ import os
3
+ import uuid
4
  import subprocess
5
  from pathlib import Path
6
+ from typing import Tuple
7
+
8
  import gradio as gr
9
 
10
+ # Demo images
11
  FRAME1 = Path("demo/frame1.jpg")
12
  FRAME2 = Path("demo/frame2.jpg")
13
+
14
  TMP_PREFIX = "/tmp/gradio/"
15
  TARGET_DIR = f"{TMP_PREFIX}output/"
16
+ EXTENSION = "gif"
17
+
18
+ def enhance_image(img_a_path: str, img_b_path: str, mode: str) -> Tuple[str, str]:
19
+ """
20
+ 3-step pipeline:
21
+ 1) inference_img.py --img <A> <B> --exp=4 -> writes frames as /tmp/gradio/output/img%d.png
22
+ 2) ffmpeg palettegen
23
+ 3) ffmpeg paletteuse -> final GIF at a unique path
24
+ Returns: (gif_filepath_for_preview, gif_filepath_for_download)
25
+ """
26
+ # Ensure temp dirs exist
27
+ os.makedirs(TARGET_DIR, exist_ok=True)
28
+
29
+ # Unique output path per run
30
+ interpolated_path = f"{TARGET_DIR}{uuid.uuid4()}.{EXTENSION}"
31
+ palette_path = f"{TMP_PREFIX}palette.png"
32
 
 
33
  try:
34
+ # 1) Run interpolation (frames -> TARGET_DIR/img1.png ... imgN.png)
35
  subprocess.run([
36
  "python3", "inference_img.py",
37
+ "--img", str(img_a_path), str(img_b_path),
38
+ "--exp", "4"
39
  ], check=True)
40
 
41
+ # 2) Generate palette
42
  subprocess.run([
43
+ "ffmpeg", "-y", "-r", "14", "-f", "image2",
44
+ "-i", f"{TARGET_DIR}img%d.png",
45
+ "-vf", "palettegen=stats_mode=single",
46
+ palette_path
47
  ], check=True)
48
 
49
+ # 3) Use palette to produce final GIF
50
  subprocess.run([
51
+ "ffmpeg", "-y", "-r", "14", "-f", "image2",
52
+ "-i", f"{TARGET_DIR}img%d.png",
53
+ "-i", palette_path,
54
+ "-lavfi", "paletteuse",
55
+ interpolated_path
56
  ], check=True)
57
 
58
+ return interpolated_path, interpolated_path
59
 
60
+ except subprocess.CalledProcessError as e:
61
+ raise gr.Error(f"Interpolation failed. {e}")
 
 
 
62
 
63
+ # Gradio UI
64
  with gr.Blocks(title="RIFE Interpolation") as demo:
65
  with gr.Tab("Demo"):
66
+ gr.Markdown("### Demo: Preloaded images")
67
+ input_imageA = gr.Image(type="filepath", value=str(FRAME1), label="Image A")
68
+ input_imageB = gr.Image(type="filepath", value=str(FRAME2), label="Image B")
69
+ enhance_mode = gr.Dropdown(choices=["default"], value="default", label="Mode") # placeholder to match signature
70
+ g_btn = gr.Button("Interpolate")
71
+
72
+ output_image = gr.Image(type="filepath", label="Interpolated GIF")
73
+ enhance_image_path = gr.Textbox(label="Output path", interactive=False)
74
+
75
+ g_btn.click(
76
+ fn=enhance_image,
77
+ inputs=[input_imageA, input_imageB, enhance_mode],
78
+ outputs=[output_image, enhance_image_path],
79
+ )
80
 
81
  with gr.Tab("Upload your images"):
82
+ gr.Markdown("### Upload any two images")
83
+ user_A = gr.Image(type="filepath", label="Image A")
84
+ user_B = gr.Image(type="filepath", label="Image B")
85
+ user_mode = gr.Dropdown(choices=["default"], value="default", label="Mode")
86
+ user_btn = gr.Button("Interpolate")
87
+ user_out = gr.Image(type="filepath", label="Interpolated GIF")
88
+ user_path = gr.Textbox(label="Output path", interactive=False)
89
+ user_btn.click(
90
+ fn=enhance_image,
91
+ inputs=[user_A, user_B, user_mode],
92
+ outputs=[user_out, user_path],
93
+ )
94
 
95
  demo.launch()