Spaces:
Runtime error
Runtime error
Commit
·
9123d17
1
Parent(s):
5887f48
gradio app changes
Browse files- gradio_app.py +146 -0
gradio_app.py
ADDED
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
+
import keras
|
6 |
+
from PIL import Image
|
7 |
+
import requests
|
8 |
+
import json
|
9 |
+
from json import JSONEncoder
|
10 |
+
from datetime import datetime
|
11 |
+
|
12 |
+
|
13 |
+
myControls = {
|
14 |
+
"ResultControl":None,
|
15 |
+
"Feedback":None,
|
16 |
+
"AdditionalInfo":None
|
17 |
+
}
|
18 |
+
|
19 |
+
|
20 |
+
dataToSend = {
|
21 |
+
"FileContent":None,
|
22 |
+
"PlantName":None,
|
23 |
+
"Comments":None
|
24 |
+
}
|
25 |
+
|
26 |
+
imageData = []
|
27 |
+
|
28 |
+
class NumpyEncoder(JSONEncoder):
|
29 |
+
def default(self, obj):
|
30 |
+
if isinstance(obj, np.ndarray) :
|
31 |
+
return obj.tolist()
|
32 |
+
return JSONEncoder.default(self,obj)
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
def uploadFile() :
|
37 |
+
global dataToSend
|
38 |
+
|
39 |
+
npArray = np.asarray(imageData)
|
40 |
+
fileContent = json.dumps(npArray, cls=NumpyEncoder)
|
41 |
+
dataToSend["FileContent"] = fileContent
|
42 |
+
|
43 |
+
payload = json.dumps(dataToSend)
|
44 |
+
|
45 |
+
|
46 |
+
r = requests.post("http://127.0.0.1:5000/todb", data=payload)
|
47 |
+
return r
|
48 |
+
|
49 |
+
def saveStats(predictionStatus) :
|
50 |
+
d = {
|
51 |
+
'Time': str(datetime.now()),
|
52 |
+
'PredictionStatus':None
|
53 |
+
}
|
54 |
+
|
55 |
+
if predictionStatus == 'Satisfied' :
|
56 |
+
d['PredictionStatus'] = 1
|
57 |
+
else :
|
58 |
+
d['PredictionStatus'] = 0
|
59 |
+
|
60 |
+
r = requests.post("http://127.0.0.1:5000/predictionstats", data=json.dumps(d))
|
61 |
+
return r
|
62 |
+
|
63 |
+
|
64 |
+
def predict(imageToProcess):
|
65 |
+
global imageData
|
66 |
+
reply = "Nothing to display"
|
67 |
+
try :
|
68 |
+
|
69 |
+
openai.api_key = "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"
|
70 |
+
message = "Mango Plant Diseases"
|
71 |
+
if message:
|
72 |
+
messages = []
|
73 |
+
messages.append(
|
74 |
+
{"role": "user", "content": message},
|
75 |
+
)
|
76 |
+
chat = openai.ChatCompletion.create(
|
77 |
+
model="gpt-3.5-turbo", messages=messages
|
78 |
+
)
|
79 |
+
|
80 |
+
reply = chat.choices[0].message.content
|
81 |
+
except :
|
82 |
+
pass
|
83 |
+
|
84 |
+
imageData = imageToProcess
|
85 |
+
print("Image Dimensions", imageData.height, imageData.width)
|
86 |
+
|
87 |
+
return ["No Disease", reply]
|
88 |
+
|
89 |
+
def submitFeedback(correctOrWrong, plantName, userData):
|
90 |
+
global dataToSend
|
91 |
+
|
92 |
+
print(correctOrWrong)
|
93 |
+
|
94 |
+
|
95 |
+
if correctOrWrong == "Not Satisfied" :
|
96 |
+
dataToSend["PlantName"] = plantName
|
97 |
+
dataToSend["Comments"] = userData
|
98 |
+
dataToSend["FileContent"] = json.dumps(np.asarray(imageData).tolist())
|
99 |
+
r = uploadFile()
|
100 |
+
|
101 |
+
if r != None :
|
102 |
+
res = json.loads(r.text)
|
103 |
+
gr.Warning("Data Submitted for learning :" + res["Status"])
|
104 |
+
else :
|
105 |
+
gr.Error("Failed to upload the file for learning")
|
106 |
+
|
107 |
+
saveStats(correctOrWrong)
|
108 |
+
|
109 |
+
with gr.Blocks(allow_flagging="manual") as app :
|
110 |
+
|
111 |
+
gr.Markdown(
|
112 |
+
"""
|
113 |
+
# AI based plant Disease Detection Application
|
114 |
+
|
115 |
+
"""
|
116 |
+
)
|
117 |
+
myControls["ImageInput"] = gr.Image(type="pil")
|
118 |
+
|
119 |
+
controls = []
|
120 |
+
|
121 |
+
myControls["ResultControl"] = gr.Textbox(label='Possible Disease could be ')
|
122 |
+
myControls["AdditionalInfo"] = gr.TextArea(label='Additional Info')
|
123 |
+
controls.append(myControls["ResultControl"])
|
124 |
+
controls.append(myControls["AdditionalInfo"])
|
125 |
+
|
126 |
+
|
127 |
+
predictBtn = gr.Button(value='Predict')
|
128 |
+
predictBtn.click(predict, inputs=[myControls["ImageInput"]], outputs=controls)
|
129 |
+
|
130 |
+
|
131 |
+
gr.Markdown()
|
132 |
+
|
133 |
+
myControls["PredictionSelection"] = gr.Radio(["Satisfied", "Not Satisfied"], label="Feedback", info="Are you satisfied with the prediction?")
|
134 |
+
#myControls["Feedback"] = gr.Checkbox(label="Is prediction wrong? If so, please provide the proper classification")
|
135 |
+
myControls["PlantName"] = gr.Textbox(label='Specify the name of the plant')
|
136 |
+
myControls["UserInput"] = gr.Textbox(label='What is the correct classification?')
|
137 |
+
feedbackBtn = gr.Button(value='Submit Feedback')
|
138 |
+
feedbackBtn.click(submitFeedback, inputs =[myControls["PredictionSelection"], myControls["PlantName"], myControls["UserInput"]])
|
139 |
+
|
140 |
+
|
141 |
+
|
142 |
+
|
143 |
+
|
144 |
+
|
145 |
+
|
146 |
+
app.queue().launch()
|