Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException, Header
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
import joblib
|
|
@@ -7,7 +8,14 @@ import requests
|
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
from prometheus_client import Counter, Histogram, Gauge, generate_latest
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Environment variables
|
| 13 |
API_KEY = os.getenv("API_KEY", "test-key-123")
|
|
@@ -19,18 +27,10 @@ REQS = Counter("pred_requests_total", "Total prediction requests")
|
|
| 19 |
LAT = Histogram("pred_request_latency_seconds", "Request latency")
|
| 20 |
LATEST = Gauge("latest_prediction", "Last predicted value")
|
| 21 |
|
| 22 |
-
# Global variables
|
| 23 |
-
model = None
|
| 24 |
-
encoders = {}
|
| 25 |
-
scaler = None
|
| 26 |
-
feature_columns = []
|
| 27 |
-
categorical_columns = []
|
| 28 |
-
boolean_columns = []
|
| 29 |
-
loaded = False
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
global model, encoders, scaler, feature_columns, categorical_columns, boolean_columns, loaded
|
| 35 |
|
| 36 |
print("๐ Starting FastAPI application...")
|
|
@@ -66,7 +66,15 @@ async def load_model():
|
|
| 66 |
print(f" Model load error: {ex}")
|
| 67 |
loaded = False
|
| 68 |
|
| 69 |
-
print(" FastAPI app initialized")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
|
| 72 |
@app.get("/")
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException, Header
|
| 2 |
+
from contextlib import asynccontextmanager
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
| 5 |
import joblib
|
|
|
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
from prometheus_client import Counter, Histogram, Gauge, generate_latest
|
| 10 |
|
| 11 |
+
# Global variables
|
| 12 |
+
model = None
|
| 13 |
+
encoders = {}
|
| 14 |
+
scaler = None
|
| 15 |
+
feature_columns = []
|
| 16 |
+
categorical_columns = []
|
| 17 |
+
boolean_columns = []
|
| 18 |
+
loaded = False
|
| 19 |
|
| 20 |
# Environment variables
|
| 21 |
API_KEY = os.getenv("API_KEY", "test-key-123")
|
|
|
|
| 27 |
LAT = Histogram("pred_request_latency_seconds", "Request latency")
|
| 28 |
LATEST = Gauge("latest_prediction", "Last predicted value")
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
@asynccontextmanager
|
| 32 |
+
async def lifespan(app: FastAPI):
|
| 33 |
+
# Startup
|
| 34 |
global model, encoders, scaler, feature_columns, categorical_columns, boolean_columns, loaded
|
| 35 |
|
| 36 |
print("๐ Starting FastAPI application...")
|
|
|
|
| 66 |
print(f" Model load error: {ex}")
|
| 67 |
loaded = False
|
| 68 |
|
| 69 |
+
print(" FastAPI app initialized and ready!")
|
| 70 |
+
|
| 71 |
+
yield # Server runs here
|
| 72 |
+
|
| 73 |
+
# Shutdown (cleanup if needed)
|
| 74 |
+
print("๐ Shutting down...")
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
app = FastAPI(title="Loan Approval API", version="1.0", lifespan=lifespan)
|
| 78 |
|
| 79 |
|
| 80 |
@app.get("/")
|