AItool commited on
Commit
6f026d4
·
verified ·
1 Parent(s): 8a6bc57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -81
app.py CHANGED
@@ -1,87 +1,65 @@
 
1
  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
- # Fixed input paths and output location
 
9
 
10
- FRAME1_PATH = "demo/frame1.png"
11
- FRAME2_PATH = "demo/frame2.png"
12
- TARGET_DIR = "/home/user/app/output/"
13
- #TARGET_DIR = "/home/user/app/output/"
14
- OUTPUT_GIF = "output/output.gif"
15
  PALETTE_PNG = "demo/palette.png"
16
-
17
-
18
- def generate_demo_gif(exp=2, progress=gr.Progress(track_tqdm=True)):
19
- progress(0.1, desc="Starting inference...")
20
-
21
- # Delete old output if exists
22
- try:
23
- os.remove(OUTPUT_GIF)
24
- except FileNotFoundError:
25
- pass
26
-
27
- # Build and run command
28
- cmd = [
29
- "python", "inference_img.py",
30
- "--img", FRAME1_PATH, FRAME2_PATH,
31
- "--exp", str(exp),
32
- "--model", "train_log/"
33
- ]
34
- print("Running:", " ".join(cmd))
35
- result = subprocess.run(cmd, capture_output=True, text=True)
36
-
37
- print("STDOUT:", result.stdout)
38
- print("STDERR:", result.stderr)
39
- print("Exists?", os.path.exists("output/img0.png")) # ⬅️ Add it here
40
-
41
-
42
- # Generate palette
43
- subprocess.run([
44
- "ffmpeg", "-y", "-r", "14", "-f", "image2",
45
- "-i", f"{TARGET_DIR}img%d.png",
46
- "-vf", "palettegen=stats_mode=single",
47
- PALETTE_PNG
48
- ], check=True)
49
-
50
- #!ffmpeg -r 14 -f image2 -i output/img%d.png -vf "palettegen=stats_mode=single" /content/demo/palette.png
51
- # Step 2: Generate GIF using palette
52
- subprocess.run([
53
- "ffmpeg", "-y", "-r", "14", "-f", "image2",
54
- "-i", f"{TARGET_DIR}img%d.png",
55
- "-i", PALETTE_PNG,
56
- "-lavfi", "paletteuse",
57
- OUTPUT_GIF
58
- ], check=True)
59
-
60
- #!ffmpeg -r 14 -f image2 -i output/img%d.png -i palette.png -lavfi "paletteuse" output/retro.gif
61
- subprocess.run([
62
- "ffmpeg", "-y", "-r", "14", "-f", "image2",
63
- "-i", f"{TARGET_DIR}img%d.png",
64
- "-i", PALETTE_PNG,
65
- "-lavfi", "paletteuse",
66
- OUTPUT_GIF
67
- ], check=True)
68
-
69
- # UI setup
70
- with gr.Blocks() as demo_ui:
71
- gr.Markdown("## 🎞️ Demo GIF Generator — Interpolate Two Frames")
72
-
73
- with gr.Row():
74
- gr.Image(value=FRAME1_PATH, label="Frame 1", interactive=False)
75
- gr.Image(value=FRAME2_PATH, label="Frame 2", interactive=False)
76
-
77
- exp = gr.Slider(1, 4, value=2, step=1, label="Interpolation Exponent")
78
- run_btn = gr.Button("Generate GIF")
79
- out_gif = gr.Image(label="Output GIF")
80
- status = gr.Markdown()
81
-
82
- run_btn.click(fn=generate_demo_gif, inputs=[exp], outputs=[out_gif, status])
83
-
84
-
85
- # Launch the app
86
- demo_ui.launch(ssr_mode=False)
87
-
 
1
+ # app.py
2
  import os
3
+ import uuid
4
  import subprocess
5
+ from pathlib import Path
6
  import gradio as gr
 
 
7
 
8
+ FRAME1 = Path("demo/frame1.png")
9
+ FRAME2 = Path("demo/frame2.png")
10
 
11
+ TARGET_DIR = "/home/user/app/output/" # absolute path that inference writes to
 
 
 
 
12
  PALETTE_PNG = "demo/palette.png"
13
+ OUTPUT_GIF_DIR = "output"
14
+ os.makedirs("demo", exist_ok=True)
15
+ os.makedirs(TARGET_DIR, exist_ok=True)
16
+ os.makedirs(OUTPUT_GIF_DIR, exist_ok=True)
17
+
18
+ def interpolate_image(img_a_path: str, img_b_path: str) -> tuple[str, str]:
19
+ # 1) Run inference to generate frames into TARGET_DIR/img%d.png
20
+ subprocess.run([
21
+ "python3", "inference_img.py",
22
+ "--img", str(img_a_path), str(img_b_path),
23
+ "--exp", "4"
24
+ ], check=True)
25
+
26
+ # 2) Generate palette from frames
27
+ subprocess.run([
28
+ "ffmpeg", "-y", "-r", "14", "-f", "image2",
29
+ "-i", f"{TARGET_DIR}img%d.png",
30
+ "-vf", "palettegen=stats_mode=single",
31
+ PALETTE_PNG
32
+ ], check=True)
33
+
34
+ # 3) Generate final GIF using palette
35
+ out_gif = os.path.join(OUTPUT_GIF_DIR, f"{uuid.uuid4()}.gif")
36
+ subprocess.run([
37
+ "ffmpeg", "-y", "-r", "14", "-f", "image2",
38
+ "-i", f"{TARGET_DIR}img%d.png",
39
+ "-i", PALETTE_PNG,
40
+ "-lavfi", "paletteuse",
41
+ out_gif
42
+ ], check=True)
43
+
44
+ return out_gif, out_gif
45
+
46
+ with gr.Blocks(title="RIFE Interpolation") as demo:
47
+ with gr.Tab("Demo"):
48
+ gr.Markdown("### Demo: Preloaded images")
49
+ input_imageA = gr.Image(type="filepath", value=str(FRAME1), label="Image A")
50
+ input_imageB = gr.Image(type="filepath", value=str(FRAME2), label="Image B")
51
+ run_btn = gr.Button("Interpolate")
52
+ result_img = gr.Image(type="filepath", label="Interpolated GIF")
53
+ result_path = gr.Textbox(label="Output path", interactive=False)
54
+ run_btn.click(interpolate_image, [input_imageA, input_imageB], [result_img, result_path])
55
+
56
+ with gr.Tab("Upload your images"):
57
+ gr.Markdown("### Upload any two images")
58
+ user_A = gr.Image(type="filepath", label="Image A")
59
+ user_B = gr.Image(type="filepath", label="Image B")
60
+ run_btn2 = gr.Button("Interpolate")
61
+ user_img = gr.Image(type="filepath", label="Interpolated GIF")
62
+ user_path = gr.Textbox(label="Output path", interactive=False)
63
+ run_btn2.click(interpolate_image, [user_A, user_B], [user_img, user_path])
64
+
65
+ demo.launch()