This repository was archived by the owner on Jan 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
49 lines (42 loc) · 1.17 KB
/
Copy pathmain.py
File metadata and controls
49 lines (42 loc) · 1.17 KB
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
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import numpy as np
# Load model & scaler
model = joblib.load("models/house_price_model.pkl")
scaler = joblib.load("models/scaler.pkl")
app = FastAPI(
title="House Price Prediction API",
description="A FastAPI-powered machine learning API that serves house price predictions using a pre-trained regression model and scaled input features."
)
# Input schema
class HouseData(BaseModel):
CRIM: float
ZN: float
INDUS: float
CHAS: int
NOX: float
RM: float
AGE: float
DIS: float
RAD: int
TAX: float
PTRATIO: float
B: float
LSTAT: float
@app.get("/")
def home():
return {"message": "House Price Prediction API is running"}
@app.post("/predict")
def predict_price(data: HouseData):
input_data = np.array([[
data.CRIM, data.ZN, data.INDUS, data.CHAS,
data.NOX, data.RM, data.AGE, data.DIS,
data.RAD, data.TAX, data.PTRATIO,
data.B, data.LSTAT
]])
input_scaled = scaler.transform(input_data)
prediction = model.predict(input_scaled)
return {
"predicted_house_price": round(float(prediction[0]), 2)
}