File size: 6,597 Bytes
9123d17
 
 
 
 
 
 
 
 
 
21f5a8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3011087
21f5a8b
9123d17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21f5a8b
9123d17
 
 
 
 
 
 
 
 
 
 
 
9411de4
9123d17
 
 
 
 
 
 
 
 
 
 
 
 
9411de4
9123d17
 
21f5a8b
9123d17
 
21f5a8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9123d17
21f5a8b
9123d17
21f5a8b
 
 
 
 
 
 
 
9123d17
21f5a8b
 
 
 
 
 
 
 
 
 
9123d17
 
21f5a8b
 
9123d17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21f5a8b
9123d17
 
 
 
21f5a8b
9123d17
 
 
 
 
 
 
 
 
 
21f5a8b
9123d17
 
 
 
 
 
 
 
 
 
 
 
 
 
21f5a8b
 
9123d17
21f5a8b
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import gradio as gr
import openai
import numpy as np
import tensorflow as tf
import keras
from PIL import Image
import requests
import json
from json import JSONEncoder
from datetime import datetime
import requests
from os import listdir
import json
import os
import cv2
import joblib
from json import JSONEncoder
from keras.optimizers import Adam
from keras.preprocessing import image
from keras.preprocessing.image import img_to_array
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.model_selection import train_test_split
from datetime import datetime
from fastapi import FastAPI, Request, Response
from sklearn.metrics import precision_score, recall_score, f1_score
import prometheus_client as prom
# Load your trained model
app = FastAPI()
DEFAULT_IMAGE_SIZE = tuple((256, 256))
N_IMAGES = 100
# Define the model and its parameters
#model = joblib.load(filename="plant_leaf_diseases_model.keras", 'utf-8')
model = tf.keras.models.load_model('cnn_model_v3.h5')



myControls = {
    "ResultControl":None,
    "Feedback":None,
    "AdditionalInfo":None
}


dataToSend = {
    "FileContent":None,
    "PlantName":None,
    "Comments":None
}

imageData = []

class NumpyEncoder(JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.ndarray) :
            return obj.tolist()
        return JSONEncoder.default(self,obj)



def uploadFile() :
    global dataToSend

    npArray = np.asarray(imageData)
    fileContent = json.dumps(npArray, cls=NumpyEncoder)
    dataToSend["FileContent"] = fileContent

    payload = json.dumps(dataToSend)


    r = requests.post("http://54.89.245.21:5005/todb", data=payload)
    return r

def saveStats(predictionStatus) :
    d = {
        'Time': str(datetime.now()),
        'PredictionStatus':None
    }

    if predictionStatus == 'Satisfied' :
        d['PredictionStatus'] = 1
    else :
        d['PredictionStatus'] = 0

    r = requests.post("http://54.89.245.21:5005/predictionstats", data=json.dumps(d))
    return r


def predict(imageToProcess):
    global imageData
    imagelist = []
    if imageToProcess == None :
        gr.Error("No image is given to process")
        return [None, None]

    imageToProcess.save("./temp/test.jpg")
    image_array = convert_image_to_array("./temp/test.jpg")
    imagelist.append(image_array)
    np_image = np.array(image_array, dtype=np.float16) / 225.0
    print(np_image.shape)
    prediction = model.predict(np_image.reshape(1,256, 256, 3))
    leaf_class_category_mappings = {0:"Apple Apple scab", 1:"Apple Black rot", 2:"Apple Cedar apple rust",3:"Apple healthy", 4:"Background without leaves", 5:"Blueberry healthy", 6:"Cherry Powdery mildew", 7:"Cherry healthy",8:"Corn Cercospora leaf spotGray leaf spot", 9:"Corn Common rust", 10:"Corn Northern Leaf Blight", 11:"Corn healthy",12:"Grape Black rot", 13:"Grape Esca (Black Measles)", 14:"Grape Leaf blight (Isariopsis Leaf Spot)", 15:"Grape healthy",16:"Orange Haunglongbing (Citrus greening)", 17:"Peach Bacterial spot", 18:"Peach healthy",19:"Pepper  bell Bacterial spot",20:"Pepper  bell healthy", 21:"Potato Early blight", 22:"Potato Late blight", 23:"Potato healthy", 24:"Raspberry healthy",25:"Soybean healthy", 26:"Squash Powdery mildew", 27:"Strawberry Leaf scorch", 28:"Strawberry healthy", 29:"Tomato Bacterial spot",30:"Tomato Early blight", 31:"Tomato Late blight", 32:"Tomato Leaf Mold", 33:"Tomato Septoria leaf spot", 34:"Tomato Spider mitesTwo-spotted spider mite",35:"Tomato Target Spot", 36:"Tomato Tomato Yellow Leaf Curl Virus", 37:"Tomato Tomato mosaic virus", 38:"Tomato healthy"}
    print(prediction)
    #return ["leaf_class_category_mappings[np.argmax(prediction)]"
    #return [leaf_class_category_mappings[np.argmax(prediction)], "none"]
    predicteddisease = leaf_class_category_mappings[np.argmax(prediction)]
    reply = "Nothing to display"

    try :
        key1="sk"
        key2="-ico1dAjc3rZL3"
        key3="ssVVc4LT3BlbkFJwKTdY4IeCkMtZehqRpSU"

        openai.api_key = key1+key2+key3
        message = "What is "+predicteddisease+" and how to treat the disease"

        if message:
            messages = []
            messages.append(
                {"role": "user", "content": message},
            )
            chat = openai.ChatCompletion.create(
                model="gpt-3.5-turbo", messages=messages
            )

        reply = chat.choices[0].message.content
    except Exception as ex :
        print(ex)


    imageData = imageToProcess
    return [predicteddisease, reply]

def submitFeedback(correctOrWrong, plantName, userData):
    global dataToSend

    print(correctOrWrong)


    if correctOrWrong == "Not Satisfied" :
        dataToSend["PlantName"] = plantName
        dataToSend["Comments"] = userData
        dataToSend["FileContent"] = json.dumps(np.asarray(imageData).tolist())
        r = uploadFile()

        if r != None :
            res = json.loads(r.text)
            gr.Warning("Data Submitted for learning :" + res["Status"])
        else :
            gr.Error("Failed to upload the file for learning")

    saveStats(correctOrWrong)

with gr.Blocks() as gradioapp :

    gr.Markdown(
    """
        # AI based plant Disease Detection Application

    """
    )
    myControls["ImageInput"] = gr.Image(type="pil")

    controls = []

    myControls["ResultControl"] = gr.Textbox(label='Possible Disease could be ')
    myControls["AdditionalInfo"] = gr.TextArea(label='Additional Info')
    controls.append(myControls["ResultControl"])
    controls.append(myControls["AdditionalInfo"])


    predictBtn = gr.Button(value='Predict')
    predictBtn.click(predict, inputs=[myControls["ImageInput"]], outputs=controls)


    gr.Markdown()

    myControls["PredictionSelection"] = gr.Radio(["Satisfied", "Not Satisfied"], label="Feedback", info="Are you satisfied with the prediction?")
    #myControls["Feedback"] = gr.Checkbox(label="Is prediction wrong? If so, please provide the proper classification")
    myControls["PlantName"] = gr.Textbox(label='Specify the name of the plant')
    myControls["UserInput"] = gr.Textbox(label='What is the correct classification?')
    feedbackBtn = gr.Button(value='Submit Feedback')
    feedbackBtn.click(submitFeedback, inputs =[myControls["PredictionSelection"], myControls["PlantName"], myControls["UserInput"]])

    #app.queue().launch()
    gradioapp = gr.mount_gradio_app(app, gradioapp, path="/")

if __name__ == "__main__":
    # Use this for debugging purposes only
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8001)   # Ref: https://www.gradio.app/docs/interface