Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- __pycache__/handler.cpython-313.pyc +0 -0
- handler.py +25 -12
__pycache__/handler.cpython-313.pyc
ADDED
|
Binary file (5.42 kB). View file
|
|
|
handler.py
CHANGED
|
@@ -19,10 +19,10 @@ except ImportError:
|
|
| 19 |
sys.path.append('.')
|
| 20 |
from text_features import TextFeatureExtractor
|
| 21 |
|
| 22 |
-
# Initialize global extractor
|
| 23 |
-
print("[INFO] Initializing Global TextFeatureExtractor...")
|
| 24 |
-
# Preload models to avoid first-request latency in the Space runtime.
|
| 25 |
-
extractor = TextFeatureExtractor(use_intent_model=True, preload=True)
|
| 26 |
|
| 27 |
|
| 28 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ #
|
|
@@ -36,6 +36,7 @@ from pydantic import BaseModel
|
|
| 36 |
from typing import Optional, List, Dict
|
| 37 |
import traceback
|
| 38 |
import numpy as np
|
|
|
|
| 39 |
|
| 40 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ #
|
| 41 |
# Constants & Defaults
|
|
@@ -50,9 +51,21 @@ DEFAULT_TEXT_FEATURES = {
|
|
| 50 |
}
|
| 51 |
|
| 52 |
app = FastAPI(title="Text Feature Extraction API", version="1.0.0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
app.add_middleware(
|
| 54 |
CORSMiddleware,
|
| 55 |
-
allow_origins=
|
|
|
|
|
|
|
| 56 |
allow_methods=["*"], allow_headers=["*"],
|
| 57 |
)
|
| 58 |
|
|
@@ -83,13 +96,13 @@ async def root():
|
|
| 83 |
}
|
| 84 |
|
| 85 |
|
| 86 |
-
@app.get("/health")
|
| 87 |
-
async def health():
|
| 88 |
-
return {
|
| 89 |
-
"status": "healthy",
|
| 90 |
-
"intent_model_loaded": extractor.use_intent_model,
|
| 91 |
-
"models_preloaded": True,
|
| 92 |
-
}
|
| 93 |
|
| 94 |
|
| 95 |
@app.post("/extract-text-features")
|
|
|
|
| 19 |
sys.path.append('.')
|
| 20 |
from text_features import TextFeatureExtractor
|
| 21 |
|
| 22 |
+
# Initialize global extractor
|
| 23 |
+
print("[INFO] Initializing Global TextFeatureExtractor...")
|
| 24 |
+
# Preload models to avoid first-request latency in the Space runtime.
|
| 25 |
+
extractor = TextFeatureExtractor(use_intent_model=True, preload=True)
|
| 26 |
|
| 27 |
|
| 28 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ #
|
|
|
|
| 36 |
from typing import Optional, List, Dict
|
| 37 |
import traceback
|
| 38 |
import numpy as np
|
| 39 |
+
import os
|
| 40 |
|
| 41 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ #
|
| 42 |
# Constants & Defaults
|
|
|
|
| 51 |
}
|
| 52 |
|
| 53 |
app = FastAPI(title="Text Feature Extraction API", version="1.0.0")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def _cors_origins_from_env() -> list[str]:
|
| 57 |
+
raw = (os.getenv("ALLOWED_ORIGINS") or "").strip()
|
| 58 |
+
if not raw:
|
| 59 |
+
return ["*"]
|
| 60 |
+
return [o.strip() for o in raw.split(",") if o.strip()]
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
_cors_origins = _cors_origins_from_env()
|
| 64 |
app.add_middleware(
|
| 65 |
CORSMiddleware,
|
| 66 |
+
allow_origins=_cors_origins,
|
| 67 |
+
# Browsers reject: Access-Control-Allow-Origin="*" with credentials=true.
|
| 68 |
+
allow_credentials=("*" not in _cors_origins),
|
| 69 |
allow_methods=["*"], allow_headers=["*"],
|
| 70 |
)
|
| 71 |
|
|
|
|
| 96 |
}
|
| 97 |
|
| 98 |
|
| 99 |
+
@app.get("/health")
|
| 100 |
+
async def health():
|
| 101 |
+
return {
|
| 102 |
+
"status": "healthy",
|
| 103 |
+
"intent_model_loaded": extractor.use_intent_model,
|
| 104 |
+
"models_preloaded": True,
|
| 105 |
+
}
|
| 106 |
|
| 107 |
|
| 108 |
@app.post("/extract-text-features")
|