pablovela5620 commited on
Commit
24ad251
1 Parent(s): 64f8593

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +34 -119
app.py CHANGED
@@ -1,125 +1,40 @@
1
- import gradio as gr
2
-
3
- import spaces
4
- import torch
5
- from gradio_rerun import Rerun
6
- import rerun as rr
7
- import rerun.blueprint as rrb
8
  from pathlib import Path
9
- import uuid
10
-
11
- from mini_dust3r.api import OptimizedResult, inferece_dust3r, log_optimized_result
12
- from mini_dust3r.model import AsymmetricCroCo3DStereo
13
 
14
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
15
- model = AsymmetricCroCo3DStereo.from_pretrained(
16
- "naver/DUSt3R_ViTLarge_BaseDecoder_512_dpt"
17
- ).to(DEVICE)
18
 
19
 
20
- def create_blueprint(image_name_list: list[str], log_path: Path) -> rrb.Blueprint:
21
- # dont show 2d views if there are more than 4 images as to not clutter the view
22
- if len(image_name_list) > 4:
23
- blueprint = rrb.Blueprint(
24
- rrb.Horizontal(
25
- rrb.Spatial3DView(origin=f"{log_path}"),
26
- ),
27
- collapse_panels=True,
 
28
  )
29
- else:
30
- blueprint = rrb.Blueprint(
31
- rrb.Horizontal(
32
- contents=[
33
- rrb.Spatial3DView(origin=f"{log_path}"),
34
- rrb.Vertical(
35
- contents=[
36
- rrb.Spatial2DView(
37
- origin=f"{log_path}/camera_{i}/pinhole/",
38
- contents=[
39
- "+ $origin/**",
40
- ],
41
- )
42
- for i in range(len(image_name_list))
43
- ]
44
- ),
45
- ],
46
- column_shares=[3, 1],
47
- ),
48
- collapse_panels=True,
49
- )
50
- return blueprint
51
-
52
-
53
- @spaces.GPU
54
- def predict(image_name_list: list[str] | str):
55
- # check if is list or string and if not raise error
56
- if not isinstance(image_name_list, list) and not isinstance(image_name_list, str):
57
- raise gr.Error(
58
- f"Input must be a list of strings or a string, got: {type(image_name_list)}"
59
  )
60
- uuid_str = str(uuid.uuid4())
61
- filename = Path(f"/tmp/gradio/{uuid_str}.rrd")
62
- rr.init(f"{uuid_str}")
63
- log_path = Path("world")
64
-
65
- if isinstance(image_name_list, str):
66
- image_name_list = [image_name_list]
67
-
68
- optimized_results: OptimizedResult = inferece_dust3r(
69
- image_dir_or_list=image_name_list,
70
- model=model,
71
- device=DEVICE,
72
- batch_size=1,
73
- )
74
-
75
- blueprint: rrb.Blueprint = create_blueprint(image_name_list, log_path)
76
- rr.send_blueprint(blueprint)
77
-
78
- rr.set_time_sequence("sequence", 0)
79
- log_optimized_result(optimized_results, log_path)
80
- rr.save(filename.as_posix())
81
- return filename.as_posix()
82
-
83
-
84
- with gr.Blocks(
85
- css=""".gradio-container {margin: 0 !important; min-width: 100%};""",
86
- title="Mini-DUSt3R Demo",
87
- ) as demo:
88
- # scene state is save so that you can change conf_thr, cam_size... without rerunning the inference
89
- gr.HTML('<h2 style="text-align: center;">Mini-DUSt3R Demo</h2>')
90
- gr.HTML(
91
- '<p style="text-align: center;">Unofficial DUSt3R demo using the mini-dust3r pip package</p>'
92
- )
93
- gr.HTML(
94
- '<p style="text-align: center;">More info <a href="https://github.com/pablovela5620/mini-dust3r">here</a></p>'
95
- )
96
- with gr.Tab(label="Single Image"):
97
- with gr.Column():
98
- single_image = gr.Image(type="filepath", height=300)
99
- run_btn_single = gr.Button("Run")
100
- rerun_viewer_single = Rerun(height=900)
101
- run_btn_single.click(
102
- fn=predict, inputs=[single_image], outputs=[rerun_viewer_single]
103
- )
104
-
105
- example_single_dir = Path("examples/single_image")
106
- example_single_files = sorted(example_single_dir.glob("*.png"))
107
-
108
- examples_single = gr.Examples(
109
- examples=example_single_files,
110
- inputs=[single_image],
111
- outputs=[rerun_viewer_single],
112
- fn=predict,
113
- cache_examples="lazy",
114
- )
115
- with gr.Tab(label="Multi Image"):
116
- with gr.Column():
117
- multi_files = gr.File(file_count="multiple")
118
- run_btn_multi = gr.Button("Run")
119
- rerun_viewer_multi = Rerun(height=900)
120
- run_btn_multi.click(
121
- fn=predict, inputs=[multi_files], outputs=[rerun_viewer_multi]
122
- )
123
-
124
-
125
- demo.launch()
 
1
+ import subprocess
 
 
 
 
 
 
2
  from pathlib import Path
 
 
 
 
3
 
4
+ PIXI_PATH = Path("/home/user/.pixi/bin/pixi")
5
+ PIXI_VERSION = "0.32.1"
 
 
6
 
7
 
8
+ def check_and_install_pixi() -> None:
9
+ try:
10
+ subprocess.check_call(f"{PIXI_PATH} --version", shell=True)
11
+ except subprocess.CalledProcessError:
12
+ print("pixi not found. Installing pixi...")
13
+ # Install pixi using the provided installation script
14
+ subprocess.check_call(
15
+ f"PIXI_VERSION=v{PIXI_VERSION} curl -fsSL https://pixi.sh/install.sh | bash",
16
+ shell=True,
17
  )
18
+ subprocess.check_call(
19
+ f"{PIXI_PATH} self-update --version {PIXI_VERSION}", shell=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  )
21
+ subprocess.check_call(f"{PIXI_PATH} --version", shell=True)
22
+
23
+
24
+ def run_command(command: str) -> None:
25
+ try:
26
+ subprocess.check_call(command, shell=True)
27
+ except subprocess.CalledProcessError as e:
28
+ print(f"run command {command}. Error: {e}")
29
+
30
+
31
+ if __name__ == "__main__":
32
+ check_and_install_pixi()
33
+ # install lsof
34
+ run_command(command=f"{PIXI_PATH} global install lsof")
35
+ # kill anything running on port 7860
36
+ run_command(command=f"{PIXI_PATH.parent}/lsof -t -i:7860 | xargs -r kill")
37
+ # clean current environment
38
+ run_command(command=f"{PIXI_PATH} clean")
39
+ # run spaces app
40
+ run_command(command=f"{PIXI_PATH} run -e spaces app")