Spaces:
Sleeping
Sleeping
tyagipulkit
commited on
Commit
•
6e80ca6
1
Parent(s):
849985d
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
!pip install -q -U google-generativeai
|
2 |
+
|
3 |
+
import tensorflow as tf
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
from numpy import asarray
|
7 |
+
import gradio as gr
|
8 |
+
import google.generativeai as genai
|
9 |
+
#import cv2
|
10 |
+
|
11 |
+
print("Dependency Imported Successfully!")
|
12 |
+
|
13 |
+
|
14 |
+
GOOGLE_API_KEY = "AIzaSyA4pL0voDE0py8q8iXtQNQRYYMx_UdFeLk"
|
15 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
16 |
+
model = genai.GenerativeModel('gemini-pro')
|
17 |
+
|
18 |
+
|
19 |
+
#kindly add model path here (Save model in your drive and copy path and paste it below)
|
20 |
+
model_path="./Models/"
|
21 |
+
|
22 |
+
sugarcane_model = tf.keras.models.load_model(f"{model_path}sugracane.h5")
|
23 |
+
tomato_model = tf.keras.models.load_model(f"{model_path}Tomato.h5")
|
24 |
+
corn_model = tf.keras.models.load_model(f"{model_path}Corn.h5")
|
25 |
+
potato_model = tf.keras.models.load_model(f"{model_path}Potato.h5")
|
26 |
+
rice_model = tf.keras.models.load_model(f"{model_path}Rice.h5")
|
27 |
+
wheat_model = tf.keras.models.load_model(f"{model_path}Wheat.h5")
|
28 |
+
|
29 |
+
|
30 |
+
print("Models Imported Successfully!")
|
31 |
+
|
32 |
+
pred_class = "Plant"
|
33 |
+
|
34 |
+
|
35 |
+
def predict(model,class_name,img):
|
36 |
+
global pred_class
|
37 |
+
img = tf.image.resize(img, (256, 256))
|
38 |
+
img_array = tf.keras.preprocessing.image.img_to_array(img)
|
39 |
+
img_array = tf.expand_dims(img_array, 0)
|
40 |
+
predictions = model.predict(img_array)
|
41 |
+
predicted_class = class_name[np.argmax(predictions[0])]
|
42 |
+
confidence = round(100 * (np.max(predictions[0])), 2)
|
43 |
+
pred_class = predicted_class
|
44 |
+
return predicted_class,f"{confidence} %"
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
+
def generate_output(crop,Leaf_Image):
|
50 |
+
if crop=="Sugarcane":
|
51 |
+
sugarcane_classes = ['Sugarcane___Bacterial_Blight', 'Sugarcane___Healthy', 'Sugarcane___Red_Rot']
|
52 |
+
#sugarcane_model = tf.keras.models.load_model(f"{model_path}sugracane.h5")
|
53 |
+
return predict(sugarcane_model,sugarcane_classes,Leaf_Image)
|
54 |
+
|
55 |
+
if crop=="Tomato":
|
56 |
+
tomato_classes = ['Tomato___Bacterial_spot','Tomato___Early_blight','Tomato___Late_blight','Tomato___healthy']
|
57 |
+
#tomato_model = tf.keras.models.load_model(f"{model_path}Tomato.h5")
|
58 |
+
return predict(tomato_model,tomato_classes,Leaf_Image)
|
59 |
+
|
60 |
+
if crop=="Corn":
|
61 |
+
corn_classes = ['Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot','Corn_(maize)___Common_rust_','Corn_(maize)___Northern_Leaf_Blight','Corn_(maize)___healthy']
|
62 |
+
#corn_model = tf.keras.models.load_model(f"{model_path}Corn.h5")
|
63 |
+
return predict(corn_model,corn_classes,Leaf_Image)
|
64 |
+
|
65 |
+
if crop=="Potato":
|
66 |
+
potato_classes = ['Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy']
|
67 |
+
#potato_model = tf.keras.models.load_model(f"{model_path}Potato.h5")
|
68 |
+
return predict(potato_model,potato_classes,Leaf_Image)
|
69 |
+
|
70 |
+
if crop=="Rice":
|
71 |
+
rice_classes = ['Rice___Brown_Spot', 'Rice___Healthy', 'Rice___Hispa', 'Rice___Leaf_Blast']
|
72 |
+
#rice_model = tf.keras.models.load_model(f"{model_path}Rice.h5")
|
73 |
+
return predict(rice_model,rice_classes,Leaf_Image)
|
74 |
+
|
75 |
+
if crop=="Wheat":
|
76 |
+
wheat_classes = ['Wheat___Brown_Rust', 'Wheat___Healthy', 'Wheat___Yellow_Rust']
|
77 |
+
#wheat_model = tf.keras.models.load_model(f"{model_path}Wheat.h5")
|
78 |
+
return predict(wheat_model,wheat_classes,Leaf_Image)
|
79 |
+
|
80 |
+
|
81 |
+
def transform_history(history):
|
82 |
+
new_history = []
|
83 |
+
new_history.append({"parts": [{"text": "You are a Agriculture experts named PlantDoc, that specializes in Plant Diseases"}], "role": "user"})
|
84 |
+
new_history.append({"parts": [{"text": "ok"}], "role": "model"})
|
85 |
+
print(history)
|
86 |
+
for chat in history:
|
87 |
+
new_history.append({"parts": [{"text": chat[0]}], "role": "user"})
|
88 |
+
new_history.append({"parts": [{"text": chat[1]}], "role": "model"})
|
89 |
+
return new_history
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
def response(message, history):
|
94 |
+
global chat
|
95 |
+
# The history will be the same as in Gradio, the 'Undo' and 'Clear' buttons will work correctly.
|
96 |
+
chat.history = transform_history(history)
|
97 |
+
response = chat.send_message(message)
|
98 |
+
response.resolve()
|
99 |
+
|
100 |
+
# Each character of the answer is displayed
|
101 |
+
for i in range(len(response.text)):
|
102 |
+
time.sleep(0.01)
|
103 |
+
yield response.text[: i+1]
|
104 |
+
css = """
|
105 |
+
#textbox {height: 700px;}
|
106 |
+
"""
|
107 |
+
|
108 |
+
with gr.Blocks(css=css) as demo:
|
109 |
+
with gr.Row():
|
110 |
+
with gr.Column():
|
111 |
+
leaf_img = gr.Image(type="numpy",label="Upload Plant's Leaf Image")
|
112 |
+
with gr.Column():
|
113 |
+
crop = gr.Radio(["Potato", "Tomato", "Wheat","Rice","Corn","Sugarcane"], label="Crop", info="Please Select a Crop?")
|
114 |
+
disease = gr.Textbox(label="Disease Predicted :- ")
|
115 |
+
confi = gr.Textbox(label="Level of Condidence:- ")
|
116 |
+
with gr.Row():
|
117 |
+
with gr.Column(elem_id="textbox"):
|
118 |
+
greet_btn = gr.Button("Predict")
|
119 |
+
greet_btn.click(fn=generate_output, inputs=[crop,leaf_img], outputs=[disease,confi], api_name="predict")
|
120 |
+
gr.ChatInterface(fn=response,examples=["Give me more Info about this disease!", "How to treat this Plant disease!"], title="Talk to PlantDoc(AI Expert).")
|
121 |
+
if __name__ == "__main__":
|
122 |
+
chat = model.start_chat(history=[])
|
123 |
+
demo.launch()
|