Tremick commited on
Commit
275bc8b
·
1 Parent(s): ddbf47c

Fix ZeroGPU: switch to demo.launch() + demo.app, drop gr.mount_gradio_app

Browse files
Files changed (1) hide show
  1. app.py +34 -34
app.py CHANGED
@@ -5,7 +5,7 @@ import numpy as np
5
  from PIL import Image, UnidentifiedImageError
6
  import torch
7
  import gradio as gr
8
- from fastapi import FastAPI, File, UploadFile, HTTPException
9
  from fastapi.responses import Response
10
  from transformers import DepthProImageProcessor, DepthProForDepthEstimation
11
 
@@ -42,8 +42,8 @@ def _run_depth_inference(image: Image.Image):
42
 
43
 
44
  # =====================================================================
45
- # 3. Gradio function - decorated with @spaces.GPU so ZeroGPU allocates
46
- # a real GPU for this call.
47
  # =====================================================================
48
  @spaces.GPU(duration=120)
49
  def create_3d_point_cloud(input_pil_image):
@@ -104,17 +104,39 @@ def create_3d_point_cloud(input_pil_image):
104
 
105
 
106
  # =====================================================================
107
- # 4. FastAPI backend for the REST /get_metric_depth endpoint
 
108
  # =====================================================================
109
- fastapi_app = FastAPI(title="True Metric ToF Server")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
 
112
- @fastapi_app.get("/status")
 
 
 
 
113
  def status():
114
  return {"status": "running", "device": str(device)}
115
 
116
 
117
- @fastapi_app.post("/get_metric_depth")
118
  async def get_metric_depth(file: UploadFile = File(...)):
119
  if not (file.content_type or "").startswith("image/"):
120
  raise HTTPException(status_code=400, detail="Uploaded file must be an image.")
@@ -140,31 +162,9 @@ async def get_metric_depth(file: UploadFile = File(...)):
140
 
141
 
142
  # =====================================================================
143
- # 5. Gradio UI
144
- # =====================================================================
145
- with gr.Blocks(title="Software-ToF 3D Cloud Visualizer") as demo:
146
- gr.Markdown("# 📷 Software-ToF Camera Simulation")
147
- gr.Markdown(
148
- "Upload any image to calculate absolute metric depths and generate "
149
- "an interactive 3D Point Cloud visualization."
150
- )
151
-
152
- with gr.Row():
153
- with gr.Column():
154
- input_img = gr.Image(type="pil", label="Input Scene Photo")
155
- submit_btn = gr.Button("Generate ToF 3D Structure", variant="primary")
156
- with gr.Column():
157
- output_3d = gr.Model3D(
158
- label="Interactive 3D Point Cloud Canvas",
159
- display_mode="point_cloud",
160
- )
161
-
162
- submit_btn.click(fn=create_3d_point_cloud, inputs=[input_img], outputs=[output_3d])
163
-
164
-
165
- # =====================================================================
166
- # 6. Mount Gradio onto FastAPI.
167
- # Hugging Face (sdk: gradio) auto-discovers this `app` variable and
168
- # serves it via uvicorn on port 7860 - no manual uvicorn.run() needed.
169
  # =====================================================================
170
- app = gr.mount_gradio_app(fastapi_app, demo, path="/")
 
 
5
  from PIL import Image, UnidentifiedImageError
6
  import torch
7
  import gradio as gr
8
+ from fastapi import File, UploadFile, HTTPException
9
  from fastapi.responses import Response
10
  from transformers import DepthProImageProcessor, DepthProForDepthEstimation
11
 
 
42
 
43
 
44
  # =====================================================================
45
+ # 3. Gradio function - @spaces.GPU tells ZeroGPU to allocate a GPU here.
46
+ # This MUST be bound to a Gradio event handler for ZeroGPU to detect it.
47
  # =====================================================================
48
  @spaces.GPU(duration=120)
49
  def create_3d_point_cloud(input_pil_image):
 
104
 
105
 
106
  # =====================================================================
107
+ # 4. Gradio UI - uses demo.launch() (NOT gr.mount_gradio_app) so that
108
+ # ZeroGPU can properly detect the @spaces.GPU decorated function above.
109
  # =====================================================================
110
+ with gr.Blocks(title="Software-ToF 3D Cloud Visualizer") as demo:
111
+ gr.Markdown("# 📷 Software-ToF Camera Simulation")
112
+ gr.Markdown(
113
+ "Upload any image to calculate absolute metric depths and generate "
114
+ "an interactive 3D Point Cloud visualization."
115
+ )
116
+
117
+ with gr.Row():
118
+ with gr.Column():
119
+ input_img = gr.Image(type="pil", label="Input Scene Photo")
120
+ submit_btn = gr.Button("Generate ToF 3D Structure", variant="primary")
121
+ with gr.Column():
122
+ output_3d = gr.Model3D(
123
+ label="Interactive 3D Point Cloud Canvas",
124
+ display_mode="point_cloud",
125
+ )
126
+
127
+ submit_btn.click(fn=create_3d_point_cloud, inputs=[input_img], outputs=[output_3d])
128
 
129
 
130
+ # =====================================================================
131
+ # 5. Add the REST /get_metric_depth endpoint via Gradio's internal
132
+ # FastAPI app - this avoids gr.mount_gradio_app which breaks ZeroGPU.
133
+ # =====================================================================
134
+ @demo.app.get("/status")
135
  def status():
136
  return {"status": "running", "device": str(device)}
137
 
138
 
139
+ @demo.app.post("/get_metric_depth")
140
  async def get_metric_depth(file: UploadFile = File(...)):
141
  if not (file.content_type or "").startswith("image/"):
142
  raise HTTPException(status_code=400, detail="Uploaded file must be an image.")
 
162
 
163
 
164
  # =====================================================================
165
+ # 6. Launch - Hugging Face auto-detects the `demo` variable when
166
+ # sdk: gradio is set and launches it. The explicit launch() call
167
+ # here ensures it also works when run locally.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  # =====================================================================
169
+ if __name__ == "__main__":
170
+ demo.launch()