forked from PiyushKarmhe/Fertilizer_Recommender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
61 lines (44 loc) · 1.55 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from fastapi import FastAPI, Request
import os
import pickle
from pydantic import BaseModel
app = FastAPI()
# Defining a Pydantic model
class FeaturesRequest(BaseModel):
feature1: str
feature2: str
feature3: int
feature4: int
feature5: int
# Get the directory of the current script
script_dir = os.path.dirname(os.path.realpath(__file__))
# Loading Model
file_path = os.path.join(script_dir, "save/rf_pipeline.pkl")
with open(file_path, "rb") as model_file:
model = pickle.load(model_file)
# Loading Encode
file_path = os.path.join(script_dir, "save/Encode.pkl")
with open(file_path, "rb") as Encode_file:
Encode = pickle.load(Encode_file)
print("Model : \n",model)
print("Encode : \n",Encode)
def preProcess(data):
data[0] = Encode["Encoders"]["Soil Type"].transform([data[0]])[0]
data[1] = Encode["Encoders"]["Crop Type"].transform([data[1]])[0]
return data
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.post("/predict")
async def predict(request: Request, features_request: FeaturesRequest):
# features = ["Sandy","Maize",36,0,0]
features = [features_request.feature1, features_request.feature2,
features_request.feature3, features_request.feature4,
features_request.feature5]
features = preProcess(features)
print(features)
prediction = model.predict([features])[0]
prediction = Encode["InvertEncodings"]["Fertilizer Name"][prediction]
print("Prediction : ",prediction)
print("\n---Sent---")
return {"prediction": prediction}