|
from fastapi import FastAPI, HTTPException |
|
from pydantic import BaseModel |
|
import gradio as gr |
|
import pandas as pd |
|
import xgboost as xgb |
|
from huggingface_hub import hf_hub_download |
|
import uvicorn |
|
|
|
|
|
model_path = hf_hub_download(repo_id="caslabs/xgboost-home-price-predictor", filename="xgboost_model.json") |
|
model = xgb.XGBRegressor() |
|
model.load_model(model_path) |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
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 |
|
|
|
|
|
@app.post("/predict") |
|
async def predict(request: PredictionRequest): |
|
data = pd.DataFrame([request.dict()]) |
|
try: |
|
predicted_price = model.predict(data)[0] |
|
return {"predicted_price": predicted_price} |
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
def gradio_predict_price(features): |
|
df = pd.DataFrame([features]) |
|
predicted_price = model.predict(df)[0] |
|
return {"predicted_price": predicted_price} |
|
|
|
|
|
iface = gr.Interface( |
|
fn=gradio_predict_price, |
|
inputs=gr.JSON(), |
|
outputs=gr.JSON(), |
|
title="Home Price Prediction API", |
|
description="Predict home price based on input features" |
|
) |
|
|
|
|
|
@app.on_event("startup") |
|
async def startup_event(): |
|
iface.launch(server_name="0.0.0.0", server_port=7860, share=False) |
|
|
|
|
|
if __name__ == "__main__": |
|
uvicorn.run(app, host="0.0.0.0", port=8000) |
|
|