Ritvik Shrivastava commited on
Commit
fe386b4
Β·
1 Parent(s): a76bfb3

fix: separate @spaces.GPU from @app .api for ZeroGPU detection

Browse files
Files changed (1) hide show
  1. app.py +11 -20
app.py CHANGED
@@ -62,22 +62,18 @@ async def health():
62
 
63
 
64
  # ── Core inference API ────────────────────────────────────────────────────────
65
- @app.api(name="analyze_defect")
66
- @spaces.GPU # GPU allocated only for this function's duration
67
- def analyze_defect(
68
- image_path: dict, # FileData from @gradio/client handle_file()
69
- language: str = "en",
70
- ) -> dict:
71
- """
72
- Primary inference endpoint.
73
 
74
- Accepts: image file + language preference
75
- Returns: full JSON defect report
76
 
77
- ZeroGPU note: GPU is requested here and released at function return.
78
- Cold-start (~30s first call) is expected; subsequent calls are fast.
79
- """
80
- # ── Load image ────────────────────────────────────────────────────────────
81
  try:
82
  img_path = image_path.get("path") or image_path.get("url")
83
  if img_path is None:
@@ -86,13 +82,8 @@ def analyze_defect(
86
  except Exception as e:
87
  return _error_response(f"Could not read image: {e}")
88
 
89
- # ── Run 3-step pipeline ───────────────────────────────────────────────────
90
  try:
91
- session = trace_logger.start_trace()
92
- report = run_gharscan_pipeline(image, language=language, trace_session=session)
93
- trace_logger.save_trace(session)
94
- return report
95
-
96
  except Exception as e:
97
  return _error_response(f"Inference failed: {e}")
98
 
 
62
 
63
 
64
  # ── Core inference API ────────────────────────────────────────────────────────
65
+ # ── Core inference API ────────────────────────────────────────────────────────
66
+ @spaces.GPU
67
+ def _gpu_inference(image: Image.Image, language: str) -> dict:
68
+ """Standalone GPU function β€” must be at module level for ZeroGPU detection."""
69
+ session = trace_logger.start_trace()
70
+ report = run_gharscan_pipeline(image, language=language, trace_session=session)
71
+ trace_logger.save_trace(session)
72
+ return report
73
 
 
 
74
 
75
+ @app.api(name="analyze_defect")
76
+ def analyze_defect(image_path: dict, language: str = "en") -> dict:
 
 
77
  try:
78
  img_path = image_path.get("path") or image_path.get("url")
79
  if img_path is None:
 
82
  except Exception as e:
83
  return _error_response(f"Could not read image: {e}")
84
 
 
85
  try:
86
+ return _gpu_inference(image, language)
 
 
 
 
87
  except Exception as e:
88
  return _error_response(f"Inference failed: {e}")
89