Spaces:
Runtime error
Runtime error
File size: 3,846 Bytes
9123d17 bf5a555 9123d17 3b4f3a7 9123d17 |
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 |
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
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://127.0.0.1:5000/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://127.0.0.1:5000/predictionstats", data=json.dumps(d))
return r
def predict(imageToProcess):
global imageData
reply = "Nothing to display"
try :
#openai.api_key = "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"
openai_key = "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"
message = "Mango Plant Diseases"
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 :
pass
imageData = imageToProcess
print("Image Dimensions", imageData.height, imageData.width)
return ["No Disease", 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 app :
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()
|