-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
87 lines (72 loc) · 2.41 KB
/
app.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import logging
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import joblib
import numpy as np
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import joblib
import numpy as np
app = FastAPI()
# Add the CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Rest of your code...
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load the trained model and scaler
model = joblib.load('linear_regression_model.pkl')
scaler = joblib.load('scaler.pkl')
# In-memory storage for house features
houses = {}
class HouseFeatures(BaseModel):
MedInc: float
HouseAge: float
AveRooms: float
AveBedrms: float
Population: float
AveOccup: float
Latitude: float
Longitude: float
@app.get('/house/{house_id}')
async def get_house(house_id: int):
if house_id not in houses:
raise HTTPException(status_code=404, detail="House not found")
return houses[house_id]
@app.put('/house/{house_id}')
async def update_house(house_id: int, house_features: HouseFeatures):
houses[house_id] = house_features
return {"status": "House features updated", "house_id": house_id}
@app.delete('/house/{house_id}')
async def delete_house(house_id: int):
if house_id not in houses:
raise HTTPException(status_code=404, detail="House not found")
del houses[house_id]
return {"status": "House features deleted", "house_id": house_id}
@app.post('/predict')
async def predict_house_price(house_features: HouseFeatures):
try:
# Scale the input features
scaled_features = scaler.transform(np.array([[
house_features.MedInc,
house_features.HouseAge,
house_features.AveRooms,
house_features.AveBedrms,
house_features.Population,
house_features.AveOccup,
house_features.Latitude,
house_features.Longitude
]]))
# Make the prediction
predicted_price = model.predict(scaled_features)[0]
return {'predicted_price': predicted_price}
except Exception as e:
logger.error(f"Error while making prediction: {e}")
return {'error': str(e)}