-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.py
55 lines (43 loc) · 1.29 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
from pydantic import BaseModel
from fastapi import FastAPI, Request
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from datetime import date
from utils import pred_crop, pred_rainfall, pred_temp_hum
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def root():
return {"message": "Hello World"}
class Inputs(BaseModel):
nitrogen: float
phosphorous: float
potassium: float
ph: float
state: str
district: str
month: str
@app.post("/predict/")
async def predict(inputs: Inputs):
# print(inputs)
nitrogen = inputs.nitrogen
phosphorous = inputs.phosphorous
potassium = inputs.potassium
state = inputs.state
district = inputs.district
month = inputs.month
ph = inputs.ph
try:
rainfall = pred_rainfall.get_rainfall(state, district, month)
temperature, humidity = pred_temp_hum.get_temp_hum(district)
prediction = pred_crop.predict_crop(
nitrogen, phosphorous, potassium, temperature, humidity, ph, rainfall)
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
return {"result": prediction[0]}