File size: 1,836 Bytes
3f36ea6
 
6a535fc
d8e3db6
 
 
3f36ea6
d8e3db6
3f36ea6
155f630
670df98
 
d8e3db6
3f36ea6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a535fc
 
 
d8e3db6
3f36ea6
6a535fc
3f36ea6
 
 
6a535fc
 
 
d8e3db6
3f36ea6
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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

# Load the model from Hugging Face Hub
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 input data model for 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

# Define a prediction endpoint in FastAPI
@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))

# Define the Gradio prediction function
def gradio_predict_price(features):
    df = pd.DataFrame([features])
    predicted_price = model.predict(df)[0]
    return {"predicted_price": predicted_price}

# Set up Gradio interface
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"
)

# Launch Gradio on a separate route
@app.on_event("startup")
async def startup_event():
    iface.launch(server_name="0.0.0.0", server_port=7860, share=False)

# Run FastAPI app if this script is executed
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)