Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
@
|
| 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
|
| 135 |
"""
|
| 136 |
-
print(f"[gpu]
|
| 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")
|
|
|