Spaces:
Runtime error

h-rand commited on
Commit
3cb0fbb
·
verified ·
1 Parent(s): 0522fe3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -7
app.py CHANGED
@@ -8,7 +8,6 @@ import uuid
8
  from typing import Dict, Optional
9
 
10
  import gradio as gr
11
- import spaces
12
  from fastapi import (
13
  Depends, FastAPI, File, Header, HTTPException, Query, UploadFile,
14
  )
@@ -16,9 +15,26 @@ from fastapi.responses import FileResponse
16
  from gradio.routes import mount_gradio_app
17
  from starlette.background import BackgroundTask
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  # ---------------------------------------------------------------------------
21
- # Configuration
22
  # ---------------------------------------------------------------------------
23
  SEPARATE_API_KEY = os.environ.get("SEPARATE_API_KEY", "").strip()
24
 
@@ -126,14 +142,14 @@ async def _do_separation(
126
  return await _find_model_output(output_dir)
127
 
128
 
129
- @spaces.GPU(duration=120)
130
  async def _separate_with_gpu(
131
  input_path: str, tmpdir: str, request_id: Optional[str] = None,
132
  ) -> str:
133
  """
134
- Exécute la séparation sous ZeroGPU grâce au décorateur @spaces.GPU.
135
  """
136
- print(f"[gpu] ZeroGPU active for ({request_id or 'ui'})", flush=True)
137
  return await _do_separation(input_path, tmpdir, request_id)
138
 
139
 
@@ -282,5 +298,4 @@ with gr.Blocks(title="Mousika - Cloud Separator") as demo:
282
  # ---------------------------------------------------------------------------
283
  # Montage : Gradio sur /ui, API REST à la racine
284
  # ---------------------------------------------------------------------------
285
- app = mount_gradio_app(app, demo, path="/ui")
286
-
 
8
  from typing import Dict, Optional
9
 
10
  import gradio as gr
 
11
  from fastapi import (
12
  Depends, FastAPI, File, Header, HTTPException, Query, UploadFile,
13
  )
 
15
  from gradio.routes import mount_gradio_app
16
  from starlette.background import BackgroundTask
17
 
18
+ # ---------------------------------------------------------------------------
19
+ # Configuration ZeroGPU sécurisée (évite le crash si hors ZeroGPU / CPU)
20
+ # ---------------------------------------------------------------------------
21
+ try:
22
+ import spaces
23
+ def gpu_decorator(duration=120):
24
+ def decorator(func):
25
+ try:
26
+ return spaces.GPU(duration=duration)(func)
27
+ except Exception:
28
+ return func
29
+ return decorator
30
+ except ImportError:
31
+ def gpu_decorator(duration=120):
32
+ def decorator(func):
33
+ return func
34
+ return decorator
35
 
36
  # ---------------------------------------------------------------------------
37
+ # Configuration générale
38
  # ---------------------------------------------------------------------------
39
  SEPARATE_API_KEY = os.environ.get("SEPARATE_API_KEY", "").strip()
40
 
 
142
  return await _find_model_output(output_dir)
143
 
144
 
145
+ @gpu_decorator(duration=120)
146
  async def _separate_with_gpu(
147
  input_path: str, tmpdir: str, request_id: Optional[str] = None,
148
  ) -> str:
149
  """
150
+ Exécute la séparation sous ZeroGPU (si disponible) ou fallback transparent.
151
  """
152
+ print(f"[gpu] Traitement en cours pour ({request_id or 'ui'})", flush=True)
153
  return await _do_separation(input_path, tmpdir, request_id)
154
 
155
 
 
298
  # ---------------------------------------------------------------------------
299
  # Montage : Gradio sur /ui, API REST à la racine
300
  # ---------------------------------------------------------------------------
301
+ app = mount_gradio_app(app, demo, path="/ui")