Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, HTTPException | |
from pydantic import BaseModel | |
import pandas as pd | |
import xgboost as xgb | |
from huggingface_hub import hf_hub_download | |
# Download and load the model from the correct repo | |
model_path = hf_hub_download(repo_id="caslabs/xgboost-home-price-predictor", filename="xgboost_model.json") | |
model = xgb.XGBRegressor() | |
model.load_model(model_path) | |
# Initialize FastAPI app | |
app = FastAPI() | |
# Define the expected input format | |
class PredictionRequest(BaseModel): | |
Site_Area_sqft: float | |
Actual_Age_Years: int | |
Total_Rooms: int | |
Bedrooms: int | |
Bathrooms: float | |
Gross_Living_Area_sqft: float | |
Design_Style_Code: int | |
Condition_Code: int | |
Energy_Efficient_Code: int | |
Garage_Carport_Code: int | |
# Define the /predict route | |
async def predict(request: PredictionRequest): | |
# Convert the input data to a DataFrame | |
data = pd.DataFrame([request.dict()]) | |
# Make a prediction | |
try: | |
predicted_price = model.predict(data)[0] | |
return {"predicted_price": predicted_price} | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |