Jeff28 commited on
Commit
fa0fe07
·
verified ·
1 Parent(s): 00266c9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ from tensorflow.keras.preprocessing import image
5
+ from huggingface_hub import InferenceClient
6
+
7
+ # Load the trained disease detection model
8
+ model = tf.keras.models.load_model("Tomato_Leaf_Disease_Model.h5")
9
+
10
+ # Disease categories
11
+ class_labels = [
12
+ "Tomato Bacterial Spot",
13
+ "Tomato Early Blight",
14
+ "Tomato Late Blight",
15
+ "Tomato Mosaic Virus",
16
+ "Tomato Yellow Leaf Curl Virus"
17
+ ]
18
+
19
+ # AI Assistant (Zephyr-7B)
20
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
21
+
22
+ # Image preprocessing
23
+ def preprocess(img):
24
+ img = img.resize((224, 224)) # Resize for the model
25
+ img = image.img_to_array(img) / 255.0 # Normalize
26
+ img = np.expand_dims(img, axis=0) # Add batch dimension
27
+ return img
28
+
29
+ # Disease detection function
30
+ def detect_disease(img):
31
+ img = preprocess(img)
32
+ prediction = model.predict(img)
33
+ class_idx = np.argmax(prediction) # Class with highest probability
34
+ confidence = np.max(prediction) * 100 # Confidence score
35
+ disease_name = class_labels[class_idx]
36
+ return f"{disease_name} (Confidence: {confidence:.2f}%)", disease_name
37
+
38
+ # AI Assistant function
39
+ def get_disease_advice(disease_name):
40
+ prompt = f"A farmer detected {disease_name} on their tomato plant. How should they manage it?"
41
+
42
+ messages = [{"role": "system", "content": "You are an expert agricultural advisor."}]
43
+ messages.append({"role": "user", "content": prompt})
44
+
45
+ response = ""
46
+ for message in client.chat_completion(messages, max_tokens=512, stream=True, temperature=0.7, top_p=0.95):
47
+ token = message.choices[0].delta.content
48
+ response += token
49
+
50
+ return response
51
+
52
+ # Combined function
53
+ def diagnose_and_advise(image):
54
+ detected_disease, disease_name = detect_disease(image)
55
+
56
+ if "Healthy" in detected_disease:
57
+ return detected_disease, "Your tomato plant looks healthy! 😊"
58
+
59
+ advice = get_disease_advice(disease_name)
60
+ return detected_disease, advice
61
+
62
+ # AI Chatbot for General Questions
63
+ def chatbot_response(user_message, history, system_message, max_tokens, temperature, top_p):
64
+ messages = [{"role": "system", "content": system_message}]
65
+
66
+ for val in history:
67
+ if val[0]:
68
+ messages.append({"role": "user", "content": val[0]})
69
+ if val[1]:
70
+ messages.append({"role": "assistant", "content": val[1]})
71
+
72
+ messages.append({"role": "user", "content": user_message})
73
+
74
+ response = ""
75
+ for message in client.chat_completion(messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p):
76
+ token = message.choices[0].delta.content
77
+ response += token
78
+
79
+ return response
80
+
81
+ # Gradio UI
82
+ with gr.Blocks() as demo:
83
+ gr.Markdown("# 🍅 Tomato Sentry: Disease Detection & AI Assistant")
84
+
85
+ with gr.Row():
86
+ with gr.Column():
87
+ gr.Markdown("### 📸 Disease Detection")
88
+ image_input = gr.Image(type="pil", label="Upload a Tomato Leaf Image")
89
+ detect_button = gr.Button("Detect Disease")
90
+ disease_output = gr.Textbox(label="Detected Disease & Confidence")
91
+ advice_output = gr.Textbox(label="AI Farming Advice")
92
+
93
+ with gr.Column():
94
+ gr.Markdown("### 🤖 AI Chatbot for Farmers")
95
+ user_input = gr.Textbox(label="Ask a farming question")
96
+ chat_button = gr.Button("Get Advice")
97
+ chat_output = gr.Textbox(label="AI Response")
98
+
99
+ detect_button.click(diagnose_and_advise, inputs=image_input, outputs=[disease_output, advice_output])
100
+ chat_button.click(chatbot_response, inputs=[user_input, [], "You are a farming expert.", 512, 0.7, 0.95], outputs=chat_output)
101
+
102
+ demo.launch()