ammyp commited on
Commit
b34a35e
·
1 Parent(s): bf22acb

Upload 2 files

Browse files
Files changed (2) hide show
  1. api.py +107 -0
  2. requirements.txt +10 -0
api.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI,UploadFile,File
2
+ from pydantic import BaseModel
3
+ import pickle
4
+ import json
5
+ import pandas as pd
6
+ from tensorflow.keras.models import load_model
7
+ from tensorflow.keras.preprocessing import image
8
+ from tensorflow.keras.applications.inception_v3 import preprocess_input
9
+ import numpy as np
10
+ import os
11
+ import gdown
12
+ import lightgbm as lgb
13
+ from PIL import Image
14
+
15
+ CHUNK_SIZE = 1024
16
+
17
+ app = FastAPI(
18
+ title='Farmer Buddy API',
19
+ description='API for Farmer Buddy App',
20
+ )
21
+
22
+ class crop_recommend_input(BaseModel):
23
+ N : int
24
+ P : int
25
+ K : int
26
+ temperature : float
27
+ humidity : float
28
+ ph : float
29
+ rainfall : float
30
+
31
+ class crop_yield_input(BaseModel):
32
+ State_Name : str
33
+ District_Name : str
34
+ Season : str
35
+ Crop : str
36
+ Area : float
37
+ Production : float
38
+ id = "1AWo5bjBSjtVRZlTcdvF1MHAXfvAgFrny"
39
+ output = "modelcrops.zip"
40
+ gdown.download(id=id, output=output, quiet=False)
41
+ from zipfile import ZipFile
42
+ with ZipFile("modelcrops.zip", 'r') as zObject:
43
+ zObject.extractall(
44
+ path="")
45
+
46
+ os.remove(str("modelcrops.zip"))
47
+ crop_recommend_ml = pickle.load(open('CropRecommendationSystem','rb'))
48
+ crop_yield_ml = pickle.load(open('CropYieldPrediction.pkl','rb'))
49
+ crop_disease_ml=load_model('CropDiseaseDetection.h5')
50
+
51
+ @app.post('/croprecommend')
52
+ def croprecommend(input_parameters : crop_recommend_input):
53
+
54
+ input_data = input_parameters.json()
55
+ input_dictionary = json.loads(input_data)
56
+ N = input_dictionary['N']
57
+ P = input_dictionary['P']
58
+ K = input_dictionary['K']
59
+ temperature = input_dictionary['temperature']
60
+ humidity = input_dictionary['humidity']
61
+ ph = input_dictionary['ph']
62
+ rainfall = input_dictionary['rainfall']
63
+ input_list = [N, P, K, temperature, humidity, ph, rainfall]
64
+ prediction = crop_recommend_ml.predict([input_list])
65
+ print(prediction[0])
66
+ return {"crop":str(prediction[0])}
67
+
68
+ @app.post('/cropyield')
69
+ def cropyield(input_parameters : crop_yield_input):
70
+
71
+ input_data = input_parameters.json()
72
+ input_dictionary = json.loads(input_data)
73
+ State_Name = input_dictionary['State_Name']
74
+ District_Name = input_dictionary['District_Name']
75
+ Season = input_dictionary['Season']
76
+ Crop = input_dictionary['Crop']
77
+ Area = input_dictionary['Area']
78
+ Production = input_dictionary['Production']
79
+ input_list = [State_Name, District_Name, Season, Crop, Area, Production]
80
+ # df = pd.DataFrame([['Chhattisgarh', 'BEMETARA', 'Rabi' ,'Potato', 3.0 ,20.0]], columns=['State_Name', 'District_Name', 'Season', 'Crop', 'Area' ,'Production'])
81
+ df = pd.DataFrame([input_list], columns=['State_Name', 'District_Name', 'Season', 'Crop', 'Area' ,'Production'])
82
+ prediction = crop_yield_ml.predict(df)
83
+ return {"yield":float(prediction[0])}
84
+
85
+ @app.post('/cropdisease')
86
+ async def cropdisease(file: UploadFile = File(...)):
87
+ try:
88
+ contents = file.file.read()
89
+ with open(file.filename, 'wb') as f:
90
+ f.write(contents)
91
+ except Exception:
92
+ return {"message": "There was an error uploading the file"}
93
+ finally:
94
+ file.file.close()
95
+ classes = ['Potato___Early_blight', 'Tomato_healthy', 'Tomato__Target_Spot', 'Tomato__Tomato_mosaic_virus', 'Tomato_Septoria_leaf_spot', 'Tomato_Bacterial_spot', 'Tomato_Spider_mites_Two_spotted_spider_mite', 'Tomato_Early_blight', 'Tomato_Late_blight', 'Pepper__bell___healthy', 'Tomato__Tomato_YellowLeaf__Curl_Virus', 'Potato___healthy', 'Tomato_Leaf_Mold', 'Potato___Late_blight', 'Pepper__bell___Bacterial_spot']
96
+ img=image.load_img(str(file.filename),target_size=(224,224))
97
+ x=image.img_to_array(img)
98
+ x=x/255
99
+ x=np.expand_dims(x,axis=0)
100
+ img_data=preprocess_input(x)
101
+ prediction = crop_disease_ml.predict(img_data)
102
+ predictions = list(prediction[0])
103
+ max_num = max(predictions)
104
+ index = predictions.index(max_num)
105
+ print(classes[index])
106
+ os.remove(str(file.filename))
107
+ return {"disease":classes[index]}
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ pydantic
3
+ pandas
4
+ tensorflow
5
+ numpy
6
+ uvicorn
7
+ lightgbm
8
+ gdown
9
+ python-multipart
10
+ Pillow