Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import xgboost as xgb
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
|
6 |
+
# Download and load the model from the Hugging Face repository
|
7 |
+
model_path = hf_hub_download(
|
8 |
+
repo_id='caslabs/xgboost-home-price-predictor',
|
9 |
+
filename='xgboost_model.json',
|
10 |
+
repo_type='model'
|
11 |
+
)
|
12 |
+
|
13 |
+
loaded_model = xgb.XGBRegressor()
|
14 |
+
loaded_model.load_model(model_path)
|
15 |
+
|
16 |
+
# Define the prediction function
|
17 |
+
def predict_price(data):
|
18 |
+
# Parse the input data
|
19 |
+
input_data = pd.DataFrame([data])
|
20 |
+
|
21 |
+
# Make a prediction
|
22 |
+
predicted_price = loaded_model.predict(input_data)[0]
|
23 |
+
return {"predicted_price": round(predicted_price, 2)}
|
24 |
+
|
25 |
+
# Define the API interface with Gradio
|
26 |
+
iface = gr.Interface(
|
27 |
+
fn=predict_price,
|
28 |
+
inputs=gr.JSON(), # Accept JSON input
|
29 |
+
outputs=gr.JSON(), # Return JSON output
|
30 |
+
title="Home Price Prediction API",
|
31 |
+
description="Provide property details in JSON format, and the model will return the predicted adjusted sale price."
|
32 |
+
)
|
33 |
+
|
34 |
+
iface.launch()
|