-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlapi.py
48 lines (41 loc) · 1.4 KB
/
mlapi.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
# Libraries
from fastapi import FastAPI
import pickle
import numpy as np
import requests
import gdown
# Custom Files
from customData import *
from encoding import *
# Initializing the app
app = FastAPI()
# Old plan path = "ml_files/model.pkl"
#https://drive.google.com/file/d/1RAfFlqyxWkBCTk-eF1NtJ55bnBTcXy6t/view?usp=sharing
url = "https://drive.google.com/uc?id=1RAfFlqyxWkBCTk-eF1NtJ55bnBTcXy6t"
output = "ml_files/output.pkl"
gdown.download(url, output, quiet=False)
with open("ml_files/output.pkl", "rb") as f:
model = pickle.load(f)
def pipeline(item: InputData) -> EncodedData:
# return {
# "Year" : [item.Year],
# "Trimester" : [item.Trimester],
# "District" : [dist_encoding[item.District]],
# "Neighbourhood" : [neigh_encoding[item.Neighbourhood]],
# "Average _rent" : [rent_encoding[item.Average_rent]]
# }
return np.array([
item.Year, \
item.Trimester, \
dist_encoding[item.District], \
neigh_encoding[item.Neighbourhood], \
rent_encoding[item.Average_rent]
])
@app.get('/')
async def scoring_endpoint(item : InputData):
newData = pipeline(item)
newData = newData.reshape((1, -1))
yhat = model.predict(newData)[0]
yhat = round(yhat, 2)
pred_text = '€ ' + str(yhat)
return {'prediction' : pred_text}