AItool commited on
Commit
f42a100
Β·
verified Β·
1 Parent(s): c516919

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -26
app.py CHANGED
@@ -5,56 +5,56 @@ 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),
24
- f"--exp={int(exp)}",
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()
 
5
  from PIL import Image
6
  import io
7
 
8
+ # Fixed input paths and output location
9
+ FRAME1_PATH = "demo/frame1.png"
10
+ FRAME2_PATH = "demo/frame2.png"
11
+ OUTPUT_GIF = "/tmp/output.gif"
12
 
13
  def generate_demo_gif(exp=2, progress=gr.Progress(track_tqdm=True)):
14
+ progress(0.1, desc="Starting inference...")
15
 
16
+ # Delete old output if exists
17
+ try:
18
+ os.remove(OUTPUT_GIF)
19
+ except FileNotFoundError:
20
+ pass
21
 
22
+ # Build and run command
23
  cmd = [
24
  "python", "inference_img.py",
25
+ "--img", FRAME1_PATH, FRAME2_PATH,
26
+ "--exp", str(exp),
27
  "--model", "train_log/"
28
  ]
29
  print("Running:", " ".join(cmd))
 
 
30
  result = subprocess.run(cmd, capture_output=True, text=True)
31
+
32
  print("STDOUT:", result.stdout)
33
  print("STDERR:", result.stderr)
34
 
35
+ # Check and display result
36
+ if result.returncode == 0 and os.path.exists(OUTPUT_GIF):
 
37
  with open(OUTPUT_GIF, "rb") as f:
38
+ gif = Image.open(io.BytesIO(f.read()))
39
+ progress(1.0, desc="GIF created!")
40
+ return gif, "βœ… Done!"
 
41
  else:
42
+ return None, "❌ Inference failed or output missing"
43
 
44
+ # UI setup
45
  with gr.Blocks() as demo_ui:
46
+ gr.Markdown("## 🎞️ Demo GIF Generator β€” Interpolate Two Frames")
47
 
48
  with gr.Row():
49
+ gr.Image(value=FRAME1_PATH, label="Frame 1", interactive=False)
50
+ gr.Image(value=FRAME2_PATH, label="Frame 2", interactive=False)
51
 
52
  exp = gr.Slider(1, 4, value=2, step=1, label="Interpolation Exponent")
53
  run_btn = gr.Button("Generate GIF")
54
+ out_gif = gr.Image(label="Output GIF")
55
  status = gr.Markdown()
56
 
57
  run_btn.click(fn=generate_demo_gif, inputs=[exp], outputs=[out_gif, status])
58
 
59
+ # Launch the app
60
  demo_ui.launch()