Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from fastapi import FastAPI
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
from typing import List, Optional
|
| 7 |
+
from huggingface_hub import hf_hub_download
|
| 8 |
+
from sklearn.impute import SimpleImputer
|
| 9 |
+
|
| 10 |
+
# --- Constants ---
|
| 11 |
+
REPO_ID = "DP1110/mlp-accessibility-model"
|
| 12 |
+
MODEL_FILENAME = 'mlp_regressor_model.joblib'
|
| 13 |
+
IMPUTER_FILENAME = 'simple_imputer.joblib'
|
| 14 |
+
|
| 15 |
+
FEATURE_COLUMNS = ['% ASF (Euclidean)', '% Built-Up Area', '% ASF (Network)', '% ASF from Bus Stops ', '% ASF from Bus Stops', '% ASF (Network) ']
|
| 16 |
+
|
| 17 |
+
# --- Load Model and Imputer ---
|
| 18 |
+
loaded_mlp_model = None
|
| 19 |
+
loaded_imputer = None
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME)
|
| 23 |
+
imputer_path = hf_hub_download(repo_id=REPO_ID, filename=IMPUTER_FILENAME)
|
| 24 |
+
|
| 25 |
+
loaded_mlp_model = joblib.load(model_path)
|
| 26 |
+
loaded_imputer = joblib.load(imputer_path)
|
| 27 |
+
print("Model and imputer loaded successfully!")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"Error loading model or imputer: {e}")
|
| 30 |
+
|
| 31 |
+
# --- FastAPI Application ---
|
| 32 |
+
app = FastAPI()
|
| 33 |
+
|
| 34 |
+
# --- Pydantic Input Data Model ---
|
| 35 |
+
class InputData(BaseModel):
|
| 36 |
+
perc_ASF_Euclidean: Optional[float] = None # Example: 0.5
|
| 37 |
+
perc_Built_Up_Area: Optional[float] = None # Example: 0.5
|
| 38 |
+
perc_ASF_Network: Optional[float] = None # Example: 0.5
|
| 39 |
+
perc_ASF_from_Bus_Stops_: Optional[float] = None # Example: 0.5
|
| 40 |
+
perc_ASF_from_Bus_Stops: Optional[float] = None # Example: 0.5
|
| 41 |
+
perc_ASF_Network_: Optional[float] = None # Example: 0.5
|
| 42 |
+
|
| 43 |
+
# --- Prediction Endpoint ---
|
| 44 |
+
@app.post("/predict")
|
| 45 |
+
async def predict(data: InputData):
|
| 46 |
+
if loaded_mlp_model is None or loaded_imputer is None:
|
| 47 |
+
return {"error": "Model or imputer not loaded."}
|
| 48 |
+
|
| 49 |
+
# Convert input data to pandas DataFrame
|
| 50 |
+
input_dict = data.dict()
|
| 51 |
+
|
| 52 |
+
# Reconstruct input_row with original feature names for imputer/model input
|
| 53 |
+
input_row = {}
|
| 54 |
+
for col in FEATURE_COLUMNS:
|
| 55 |
+
sanitized_col = col.replace(' ', '_').replace('-', '_').replace('%', 'perc').replace('(', '').replace(')', '')
|
| 56 |
+
input_row[col] = input_dict[sanitized_col]
|
| 57 |
+
|
| 58 |
+
input_df = pd.DataFrame([input_row])
|
| 59 |
+
|
| 60 |
+
# Ensure column order matches training features
|
| 61 |
+
input_df = input_df[FEATURE_COLUMNS]
|
| 62 |
+
|
| 63 |
+
# Impute missing values
|
| 64 |
+
input_imputed = loaded_imputer.transform(input_df)
|
| 65 |
+
input_imputed_df = pd.DataFrame(input_imputed, columns=FEATURE_COLUMNS)
|
| 66 |
+
|
| 67 |
+
# Make prediction
|
| 68 |
+
prediction = loaded_mlp_model.predict(input_imputed_df)[0]
|
| 69 |
+
|
| 70 |
+
return {"predicted_overall_accessibility_score": prediction}
|
| 71 |
+
|
| 72 |
+
# --- Health Check Endpoint ---
|
| 73 |
+
@app.get("/health")
|
| 74 |
+
async def health_check():
|
| 75 |
+
return {"status": "ok", "model_loaded": loaded_mlp_model is not None, "imputer_loaded": loaded_imputer is not None}
|
| 76 |
+
|
| 77 |
+
if __name__ == '__main__':
|
| 78 |
+
import uvicorn
|
| 79 |
+
# Run the FastAPI application using Uvicorn
|
| 80 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|