eduardo4547 commited on
Commit
57dd91d
·
verified ·
1 Parent(s): 20aa562

Upload 277 files

Browse files
.gitignore CHANGED
@@ -22,3 +22,21 @@ Thumbs.db
22
  # Editor directories
23
  .vscode/
24
  .idea/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  # Editor directories
23
  .vscode/
24
  .idea/
25
+
26
+ # Runtime data — never commit
27
+ backend/uploads/
28
+ backend/logs/
29
+ backend/masks/
30
+
31
+ # ML model weights
32
+ gradio-space/models/
33
+ **/*.pt
34
+ **/*.pth
35
+ **/*.bin
36
+ **/*.safetensors
37
+ **/*.gguf
38
+
39
+ # Python cache
40
+ __pycache__/
41
+ *.pyc
42
+ *.pyo
backend/services/gradio_client_service.py CHANGED
@@ -29,7 +29,8 @@ def _call_gradio_sync(image_path: Path) -> tuple[np.ndarray, int]:
29
  """
30
  from gradio_client import Client, file # type: ignore
31
 
32
- client = Client(GRADIO_SPACE_URL)
 
33
 
34
  # segment_for_backend returns (overlay_image, combined_json_str)
35
  _overlay_file, combined_json_str = client.predict(
@@ -65,18 +66,18 @@ def _call_gradio_sync(image_path: Path) -> tuple[np.ndarray, int]:
65
  return label_map, mask_count
66
 
67
 
68
- def segment_via_gradio_sync(image_path: Path) -> tuple[np.ndarray, int] | None:
69
  """
70
  Blocking call to the Gradio Space from a sync context (background task thread).
71
- Returns (label_map, mask_count) or None on failure / not configured.
72
  """
73
  if not is_gradio_enabled():
74
- return None
75
  try:
76
  return _call_gradio_sync(image_path)
77
  except Exception as exc:
78
- logger.warning("Gradio Space call failed, falling back to local SAM2: %s", exc)
79
- return None
80
 
81
 
82
  async def segment_via_gradio(image_path: Path) -> tuple[np.ndarray, int] | None:
 
29
  """
30
  from gradio_client import Client, file # type: ignore
31
 
32
+ # 300s timeout: ZeroGPU cold start + SAM2+DINO inference can take 60-120s
33
+ client = Client(GRADIO_SPACE_URL, httpx_kwargs={"timeout": 300.0})
34
 
35
  # segment_for_backend returns (overlay_image, combined_json_str)
36
  _overlay_file, combined_json_str = client.predict(
 
66
  return label_map, mask_count
67
 
68
 
69
+ def segment_via_gradio_sync(image_path: Path) -> tuple[np.ndarray, int]:
70
  """
71
  Blocking call to the Gradio Space from a sync context (background task thread).
72
+ Returns (label_map, mask_count). Raises on failure.
73
  """
74
  if not is_gradio_enabled():
75
+ raise RuntimeError("GRADIO_SPACE_URL is not configured")
76
  try:
77
  return _call_gradio_sync(image_path)
78
  except Exception as exc:
79
+ logger.error("Gradio Space call failed: %s", exc, exc_info=True)
80
+ raise RuntimeError(f"Gradio Space error: {exc}") from exc
81
 
82
 
83
  async def segment_via_gradio(image_path: Path) -> tuple[np.ndarray, int] | None:
backend/services/image_service.py CHANGED
@@ -134,13 +134,7 @@ def run_upload_job(job_id: str, content: bytes, original_name: str) -> None:
134
 
135
  image_path = UPLOAD_DIR / safe_name
136
  if is_gradio_enabled():
137
- gradio_result = segment_via_gradio_sync(image_path)
138
- if gradio_result is None:
139
- raise RuntimeError(
140
- "El Space de Gradio no respondió. "
141
- "Verifica que esté activo y que GRADIO_SPACE_URL sea correcta."
142
- )
143
- label_map, mask_count = gradio_result
144
  else:
145
  generate_label_map = _get_generate_label_map()
146
  label_map, mask_count = generate_label_map(image_rgb)
 
134
 
135
  image_path = UPLOAD_DIR / safe_name
136
  if is_gradio_enabled():
137
+ label_map, mask_count = segment_via_gradio_sync(image_path)
 
 
 
 
 
 
138
  else:
139
  generate_label_map = _get_generate_label_map()
140
  label_map, mask_count = generate_label_map(image_rgb)