Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,6 @@ import requests
|
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
from prometheus_client import Counter, Histogram, Gauge, generate_latest
|
| 9 |
|
| 10 |
-
print("🚀 Starting FastAPI application...")
|
| 11 |
-
|
| 12 |
app = FastAPI(title="Loan Approval API", version="1.0")
|
| 13 |
|
| 14 |
# Environment variables
|
|
@@ -16,9 +14,6 @@ API_KEY = os.getenv("API_KEY", "test-key-123")
|
|
| 16 |
HF_MODEL_REPO = os.getenv("HF_MODEL_REPO")
|
| 17 |
PROM_PUSHGATEWAY = os.getenv("PROM_PUSHGATEWAY")
|
| 18 |
|
| 19 |
-
print(f"API_KEY: {'Set' if API_KEY else 'Not set'}")
|
| 20 |
-
print(f"HF_MODEL_REPO: {HF_MODEL_REPO}")
|
| 21 |
-
|
| 22 |
# Prometheus metrics
|
| 23 |
REQS = Counter("pred_requests_total", "Total prediction requests")
|
| 24 |
LAT = Histogram("pred_request_latency_seconds", "Request latency")
|
|
@@ -33,37 +28,45 @@ categorical_columns = []
|
|
| 33 |
boolean_columns = []
|
| 34 |
loaded = False
|
| 35 |
|
| 36 |
-
# Load model on startup
|
| 37 |
-
if not HF_MODEL_REPO:
|
| 38 |
-
print("⚠️ WARNING: HF_MODEL_REPO not set. Using mock mode.")
|
| 39 |
-
loaded = False
|
| 40 |
-
else:
|
| 41 |
-
try:
|
| 42 |
-
print(f" Downloading model from {HF_MODEL_REPO}...")
|
| 43 |
-
|
| 44 |
-
m = hf_hub_download(repo_id=HF_MODEL_REPO, filename="best_model.joblib")
|
| 45 |
-
e = hf_hub_download(repo_id=HF_MODEL_REPO, filename="models/encoders.joblib")
|
| 46 |
-
s = hf_hub_download(repo_id=HF_MODEL_REPO, filename="models/scaler.joblib")
|
| 47 |
-
f = hf_hub_download(repo_id=HF_MODEL_REPO, filename="models/feature_columns.joblib")
|
| 48 |
-
c = hf_hub_download(repo_id=HF_MODEL_REPO, filename="models/categorical_columns.joblib")
|
| 49 |
-
b = hf_hub_download(repo_id=HF_MODEL_REPO, filename="models/boolean_columns.joblib")
|
| 50 |
-
|
| 51 |
-
print(" Loading artifacts...")
|
| 52 |
-
model = joblib.load(m)
|
| 53 |
-
encoders = joblib.load(e)
|
| 54 |
-
scaler = joblib.load(s)
|
| 55 |
-
feature_columns = joblib.load(f)
|
| 56 |
-
categorical_columns = joblib.load(c)
|
| 57 |
-
boolean_columns = joblib.load(b)
|
| 58 |
-
loaded = True
|
| 59 |
-
|
| 60 |
-
print(" Model loaded successfully!")
|
| 61 |
-
print(f" Features: {len(feature_columns)}")
|
| 62 |
-
except Exception as ex:
|
| 63 |
-
print(f" Model load error: {ex}")
|
| 64 |
-
loaded = False
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
@app.get("/")
|
|
|
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
from prometheus_client import Counter, Histogram, Gauge, generate_latest
|
| 9 |
|
|
|
|
|
|
|
| 10 |
app = FastAPI(title="Loan Approval API", version="1.0")
|
| 11 |
|
| 12 |
# Environment variables
|
|
|
|
| 14 |
HF_MODEL_REPO = os.getenv("HF_MODEL_REPO")
|
| 15 |
PROM_PUSHGATEWAY = os.getenv("PROM_PUSHGATEWAY")
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
# Prometheus metrics
|
| 18 |
REQS = Counter("pred_requests_total", "Total prediction requests")
|
| 19 |
LAT = Histogram("pred_request_latency_seconds", "Request latency")
|
|
|
|
| 28 |
boolean_columns = []
|
| 29 |
loaded = False
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
@app.on_event("startup")
|
| 33 |
+
async def load_model():
|
| 34 |
+
global model, encoders, scaler, feature_columns, categorical_columns, boolean_columns, loaded
|
| 35 |
+
|
| 36 |
+
print("🚀 Starting FastAPI application...")
|
| 37 |
+
print(f"API_KEY: {'Set' if API_KEY else 'Not set'}")
|
| 38 |
+
print(f"HF_MODEL_REPO: {HF_MODEL_REPO}")
|
| 39 |
+
|
| 40 |
+
if not HF_MODEL_REPO:
|
| 41 |
+
print("⚠️ WARNING: HF_MODEL_REPO not set. Using mock mode.")
|
| 42 |
+
loaded = False
|
| 43 |
+
else:
|
| 44 |
+
try:
|
| 45 |
+
print(f" Downloading model from {HF_MODEL_REPO}...")
|
| 46 |
+
|
| 47 |
+
m = hf_hub_download(repo_id=HF_MODEL_REPO, filename="best_model.joblib")
|
| 48 |
+
e = hf_hub_download(repo_id=HF_MODEL_REPO, filename="models/encoders.joblib")
|
| 49 |
+
s = hf_hub_download(repo_id=HF_MODEL_REPO, filename="models/scaler.joblib")
|
| 50 |
+
f = hf_hub_download(repo_id=HF_MODEL_REPO, filename="models/feature_columns.joblib")
|
| 51 |
+
c = hf_hub_download(repo_id=HF_MODEL_REPO, filename="models/categorical_columns.joblib")
|
| 52 |
+
b = hf_hub_download(repo_id=HF_MODEL_REPO, filename="models/boolean_columns.joblib")
|
| 53 |
+
|
| 54 |
+
print(" Loading artifacts...")
|
| 55 |
+
model = joblib.load(m)
|
| 56 |
+
encoders = joblib.load(e)
|
| 57 |
+
scaler = joblib.load(s)
|
| 58 |
+
feature_columns = joblib.load(f)
|
| 59 |
+
categorical_columns = joblib.load(c)
|
| 60 |
+
boolean_columns = joblib.load(b)
|
| 61 |
+
loaded = True
|
| 62 |
+
|
| 63 |
+
print(" Model loaded successfully!")
|
| 64 |
+
print(f" Features: {len(feature_columns)}")
|
| 65 |
+
except Exception as ex:
|
| 66 |
+
print(f" Model load error: {ex}")
|
| 67 |
+
loaded = False
|
| 68 |
+
|
| 69 |
+
print(" FastAPI app initialized")
|
| 70 |
|
| 71 |
|
| 72 |
@app.get("/")
|