Spaces:
Sleeping
Sleeping
pip install gradio fastapi uvicorn | |
import gradio as gr | |
import pandas as pd | |
from huggingface_hub import hf_hub_download | |
import joblib | |
from fastapi import FastAPI | |
from gradio.routes import Route | |
# Load the model | |
repo_id = "rmaitest/mlmodel2" | |
model_file = "house_price_model.pkl" # Adjust as necessary | |
# Download and load the model | |
model_path = hf_hub_download(repo_id, model_file) | |
model = joblib.load(model_path) | |
# Define the prediction function | |
def predict_price(size, bedrooms, age): | |
# Create a DataFrame from the input | |
input_data = pd.DataFrame({ | |
'Size (sq ft)': [size], | |
'Number of Bedrooms': [bedrooms], | |
'Age of House (years)': [age] | |
}) | |
# Make prediction | |
prediction = model.predict(input_data) | |
return prediction[0] | |
# Define the Gradio interface | |
iface = gr.Interface( | |
fn=predict_price, | |
inputs=[ | |
gr.Number(label="Size (sq ft)"), | |
gr.Number(label="Number of Bedrooms"), | |
gr.Number(label="Age of House (years)") | |
], | |
outputs=gr.Number(label="Predicted Price ($)"), | |
title="House Price Prediction", | |
description="Enter the size, number of bedrooms, and age of the house to get the predicted price." | |
) | |
# Create FastAPI app | |
app = FastAPI() | |
# Create a route for the /predict API endpoint | |
async def predict(size: float, bedrooms: int, age: int): | |
# Call the Gradio function manually for the API route | |
return iface.fn(size, bedrooms, age) | |
# Launch Gradio interface (only for UI purposes, if needed) | |
if __name__ == "__main__": | |
iface.launch(share=True) | |