Spaces:
Sleeping
Sleeping
Commit ·
ab70132
1
Parent(s): 175aef4
perf: cache SAP model and fix main.py imports/sys.path
Browse files- webapp/benchmark.py +17 -7
- webapp/main.py +9 -5
webapp/benchmark.py
CHANGED
|
@@ -74,6 +74,9 @@ class _SAPModel:
|
|
| 74 |
Tries the real SAP RPT-1 OSS via HuggingFace; falls back to k-NN simulator
|
| 75 |
if the package is not installed or authentication fails.
|
| 76 |
"""
|
|
|
|
|
|
|
|
|
|
| 77 |
def __init__(self, task):
|
| 78 |
self.task = task
|
| 79 |
self._real = False
|
|
@@ -81,14 +84,21 @@ class _SAPModel:
|
|
| 81 |
|
| 82 |
if HF_TOKEN:
|
| 83 |
try:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
self._model = SAP_RPT_OSS_Classifier(max_context_size=2048, bagging=1)
|
| 89 |
else:
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
except Exception:
|
| 93 |
self._init_sim()
|
| 94 |
else:
|
|
|
|
| 74 |
Tries the real SAP RPT-1 OSS via HuggingFace; falls back to k-NN simulator
|
| 75 |
if the package is not installed or authentication fails.
|
| 76 |
"""
|
| 77 |
+
# Class-level cache to avoid reloading Transformer weights on every fold
|
| 78 |
+
_shared_model = None
|
| 79 |
+
|
| 80 |
def __init__(self, task):
|
| 81 |
self.task = task
|
| 82 |
self._real = False
|
|
|
|
| 84 |
|
| 85 |
if HF_TOKEN:
|
| 86 |
try:
|
| 87 |
+
# Reuse shared model if available
|
| 88 |
+
if _SAPModel._shared_model is not None and _SAPModel._shared_model.task == task:
|
| 89 |
+
self._model = _SAPModel._shared_model._model
|
| 90 |
+
self._real = True
|
|
|
|
| 91 |
else:
|
| 92 |
+
from huggingface_hub import login
|
| 93 |
+
login(token=HF_TOKEN, add_to_git_credential=False)
|
| 94 |
+
from sap_rpt_oss import SAP_RPT_OSS_Classifier, SAP_RPT_OSS_Regressor
|
| 95 |
+
if task == "classification":
|
| 96 |
+
self._model = SAP_RPT_OSS_Classifier(max_context_size=2048, bagging=1)
|
| 97 |
+
else:
|
| 98 |
+
self._model = SAP_RPT_OSS_Regressor(max_context_size=2048, bagging=1)
|
| 99 |
+
self._real = True
|
| 100 |
+
# Store in class-level cache
|
| 101 |
+
_SAPModel._shared_model = self
|
| 102 |
except Exception:
|
| 103 |
self._init_sim()
|
| 104 |
else:
|
webapp/main.py
CHANGED
|
@@ -1,19 +1,23 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
import io, os
|
| 6 |
-
from pathlib import Path
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
# Load .env before anything else so HF_TOKEN is available to benchmark.py
|
| 10 |
-
load_dotenv(
|
| 11 |
|
| 12 |
import pandas as pd
|
|
|
|
| 13 |
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 14 |
from fastapi.responses import JSONResponse
|
| 15 |
from fastapi.staticfiles import StaticFiles
|
| 16 |
|
|
|
|
| 17 |
try:
|
| 18 |
from benchmark import run_benchmark, infer_task
|
| 19 |
except ImportError:
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
# Add both root and webapp directory to sys.path to resolve all import issues
|
| 4 |
+
BASE_DIR = Path(__file__).resolve().parent.parent
|
| 5 |
+
sys.path.insert(0, str(BASE_DIR))
|
| 6 |
+
sys.path.insert(0, str(BASE_DIR / "webapp"))
|
| 7 |
|
| 8 |
import io, os
|
|
|
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
|
| 11 |
# Load .env before anything else so HF_TOKEN is available to benchmark.py
|
| 12 |
+
load_dotenv(BASE_DIR / "webapp" / ".env")
|
| 13 |
|
| 14 |
import pandas as pd
|
| 15 |
+
import numpy as np
|
| 16 |
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 17 |
from fastapi.responses import JSONResponse
|
| 18 |
from fastapi.staticfiles import StaticFiles
|
| 19 |
|
| 20 |
+
# Now we can import benchmark reliably
|
| 21 |
try:
|
| 22 |
from benchmark import run_benchmark, infer_task
|
| 23 |
except ImportError:
|