Spaces:
Configuration error
Configuration error
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -31,6 +31,7 @@ except ImportError:
|
|
| 31 |
import json
|
| 32 |
import logging
|
| 33 |
import os
|
|
|
|
| 34 |
|
| 35 |
import gradio as gr
|
| 36 |
from dotenv import load_dotenv
|
|
@@ -51,8 +52,35 @@ logger = logging.getLogger(__name__)
|
|
| 51 |
# ---------------------------------------------------------------------------
|
| 52 |
IS_SPACES = os.getenv("SPACE_ID") is not None
|
| 53 |
|
|
|
|
| 54 |
if IS_SPACES:
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# ---------------------------------------------------------------------------
|
| 58 |
# Custom CSS
|
|
@@ -357,13 +385,24 @@ def ask(question: str, api_key: str) -> str:
|
|
| 357 |
# GPU-decorated functions for ZeroGPU
|
| 358 |
# ---------------------------------------------------------------------------
|
| 359 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
if IS_SPACES:
|
| 361 |
-
@
|
| 362 |
def _classify_gpu(description: str) -> dict:
|
| 363 |
"""Classify incident using the fine-tuned model on ZeroGPU."""
|
| 364 |
return classify_incident(description=description, use_hf=True)
|
| 365 |
|
| 366 |
-
@
|
| 367 |
def _ask_gpu(question: str) -> str:
|
| 368 |
"""Answer question using the fine-tuned model on ZeroGPU."""
|
| 369 |
return answer_question(question=question, use_hf=True)
|
|
|
|
| 31 |
import json
|
| 32 |
import logging
|
| 33 |
import os
|
| 34 |
+
from importlib import metadata, util
|
| 35 |
|
| 36 |
import gradio as gr
|
| 37 |
from dotenv import load_dotenv
|
|
|
|
| 52 |
# ---------------------------------------------------------------------------
|
| 53 |
IS_SPACES = os.getenv("SPACE_ID") is not None
|
| 54 |
|
| 55 |
+
spaces = None
|
| 56 |
if IS_SPACES:
|
| 57 |
+
try:
|
| 58 |
+
import spaces as _spaces
|
| 59 |
+
|
| 60 |
+
# Local `spaces/` directory can shadow the HF `spaces` package.
|
| 61 |
+
if hasattr(_spaces, "GPU"):
|
| 62 |
+
spaces = _spaces
|
| 63 |
+
else:
|
| 64 |
+
raise ImportError("Imported `spaces` module has no GPU decorator")
|
| 65 |
+
except Exception:
|
| 66 |
+
try:
|
| 67 |
+
# Load the installed HF spaces package directly from site-packages.
|
| 68 |
+
dist = metadata.distribution("spaces")
|
| 69 |
+
module_path = dist.locate_file("spaces/__init__.py")
|
| 70 |
+
spec = util.spec_from_file_location("hf_spaces_runtime", module_path)
|
| 71 |
+
if spec is None or spec.loader is None:
|
| 72 |
+
raise ImportError("Could not load spaces package spec")
|
| 73 |
+
_spaces = util.module_from_spec(spec)
|
| 74 |
+
spec.loader.exec_module(_spaces)
|
| 75 |
+
if hasattr(_spaces, "GPU"):
|
| 76 |
+
spaces = _spaces
|
| 77 |
+
else:
|
| 78 |
+
raise ImportError("Installed spaces package has no GPU decorator")
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logger.warning(
|
| 81 |
+
"HF Spaces GPU decorator unavailable (%s). Falling back to non-GPU wrappers.",
|
| 82 |
+
e,
|
| 83 |
+
)
|
| 84 |
|
| 85 |
# ---------------------------------------------------------------------------
|
| 86 |
# Custom CSS
|
|
|
|
| 385 |
# GPU-decorated functions for ZeroGPU
|
| 386 |
# ---------------------------------------------------------------------------
|
| 387 |
|
| 388 |
+
def _gpu_wrapper(duration: int):
|
| 389 |
+
"""Use HF ZeroGPU decorator when available; otherwise no-op."""
|
| 390 |
+
|
| 391 |
+
def passthrough(fn):
|
| 392 |
+
return fn
|
| 393 |
+
|
| 394 |
+
if IS_SPACES and spaces is not None and hasattr(spaces, "GPU"):
|
| 395 |
+
return spaces.GPU(duration=duration)
|
| 396 |
+
return passthrough
|
| 397 |
+
|
| 398 |
+
|
| 399 |
if IS_SPACES:
|
| 400 |
+
@_gpu_wrapper(duration=120)
|
| 401 |
def _classify_gpu(description: str) -> dict:
|
| 402 |
"""Classify incident using the fine-tuned model on ZeroGPU."""
|
| 403 |
return classify_incident(description=description, use_hf=True)
|
| 404 |
|
| 405 |
+
@_gpu_wrapper(duration=120)
|
| 406 |
def _ask_gpu(question: str) -> str:
|
| 407 |
"""Answer question using the fine-tuned model on ZeroGPU."""
|
| 408 |
return answer_question(question=question, use_hf=True)
|