-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
33 lines (27 loc) · 818 Bytes
/
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import joblib
import numpy as np
import uvicorn
# Load the trained model
model = joblib.load("random_forest_model.pkl")
app = FastAPI()
class SoilData(BaseModel):
N: float
P: float
K: float
ph: float
@app.get("/")
async def read_root():
print("getting record")
return {"crop selection web page"}
@app.post("/predict/")
async def predict_crop(data: SoilData):
try:
features = np.array([[data.N, data.P, data.K, data.ph]])
prediction = model.predict(features)
return {"crop": prediction[0]}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)