-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
46 lines (36 loc) · 1.04 KB
/
main.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
# 1. Library imports
import uvicorn
import joblib
import numpy as np
from fastapi import FastAPI
from pydantic import BaseModel
from starlette.middleware.cors import CORSMiddleware
class Rent(BaseModel):
rooms: int
location: str
# 2. Create app and model objects
app = FastAPI()
model = joblib.load("joblib_model.sav")
# 3. Expose the prediction functionality, make a prediction from the passed
@app.get("/")
def read_root():
return {"Hello": "Stutern"}
@app.post("/predict/")
def predict_rent(data: Rent):
data = data.dict()
rooms = data['rooms']
location = data['location']
result = np.exp(model.predict([rooms, location]))
result = np.round(result, 2)
return {'Rent': result}
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
#allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 4. Run the API with uvicorn
# Will run on http://127.0.0.1:8000
if __name__ == '__main__':
uvicorn.run(app, host='127.0.0.1', port=8000)