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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" 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(allow_flagging="manual") 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()