pablovela5620 commited on
Commit
5fff857
1 Parent(s): 9ec56ae

chore: Refactor predict function to handle both single and multiple image inputs

Browse files
Files changed (1) hide show
  1. app.py +27 -6
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
  import spaces
3
  import torch
4
  from gradio_rerun import Rerun
@@ -17,11 +18,20 @@ model = AsymmetricCroCo3DStereo.from_pretrained(
17
 
18
 
19
  @spaces.GPU
20
- def predict(image_name_list: list[str]):
 
 
 
 
 
21
  uuid_str = str(uuid.uuid4())
22
  filename = Path(f"/tmp/gradio/{uuid_str}.rrd")
23
  rr.init(f"{uuid_str}")
24
  log_path = Path("world")
 
 
 
 
25
  optimized_results: OptimizedResult = inferece_dust3r(
26
  image_dir_or_list=image_name_list,
27
  model=model,
@@ -41,11 +51,22 @@ with gr.Blocks(
41
  ) as demo:
42
  # scene state is save so that you can change conf_thr, cam_size... without rerunning the inference
43
  gr.HTML('<h2 style="text-align: center;">Mini-DUSt3R Demo</h2>')
44
- with gr.Column():
45
- inputfiles = gr.File(file_count="multiple")
46
- rerun_viewer = Rerun(height=900)
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- run_btn = gr.Button("Run")
49
- run_btn.click(fn=predict, inputs=[inputfiles], outputs=[rerun_viewer])
50
 
51
  demo.launch()
 
1
  import gradio as gr
2
+
3
  import spaces
4
  import torch
5
  from gradio_rerun import Rerun
 
18
 
19
 
20
  @spaces.GPU
21
+ def predict(image_name_list: list[str] | str):
22
+ # check if is list or string and if not raise error
23
+ if not isinstance(image_name_list, list) and not isinstance(image_name_list, str):
24
+ raise gr.Error(
25
+ f"Input must be a list of strings or a string, got: {type(image_name_list)}"
26
+ )
27
  uuid_str = str(uuid.uuid4())
28
  filename = Path(f"/tmp/gradio/{uuid_str}.rrd")
29
  rr.init(f"{uuid_str}")
30
  log_path = Path("world")
31
+
32
+ if isinstance(image_name_list, str):
33
+ image_name_list = [image_name_list]
34
+
35
  optimized_results: OptimizedResult = inferece_dust3r(
36
  image_dir_or_list=image_name_list,
37
  model=model,
 
51
  ) as demo:
52
  # scene state is save so that you can change conf_thr, cam_size... without rerunning the inference
53
  gr.HTML('<h2 style="text-align: center;">Mini-DUSt3R Demo</h2>')
54
+ with gr.Tab(label="Single Image"):
55
+ with gr.Column():
56
+ singe_image = gr.Image(type="filepath")
57
+ run_btn_single = gr.Button("Run")
58
+ rerun_viewer_single = Rerun(height=900)
59
+ run_btn_single.click(
60
+ fn=predict, inputs=[singe_image], outputs=[rerun_viewer_single]
61
+ )
62
+ with gr.Tab(label="Multi Image"):
63
+ with gr.Column():
64
+ multi_files = gr.File(file_count="multiple")
65
+ run_btn_multi = gr.Button("Run")
66
+ rerun_viewer_multi = Rerun(height=900)
67
+ run_btn_multi.click(
68
+ fn=predict, inputs=[multi_files], outputs=[rerun_viewer_multi]
69
+ )
70
 
 
 
71
 
72
  demo.launch()