Updated app.py with Groq API integration for improved AI assistant
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import tensorflow as tf
|
|
| 4 |
from tensorflow.keras.preprocessing import image
|
| 5 |
import gradio as gr
|
| 6 |
import requests
|
|
|
|
| 7 |
|
| 8 |
# Suppress TensorFlow warnings
|
| 9 |
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
|
@@ -299,61 +300,52 @@ def detect_disease_scaled(img, scaling_method, temperature, min_conf, max_conf):
|
|
| 299 |
raw_text = f"Raw Confidence: {raw_confidence:.2f}%"
|
| 300 |
return result, raw_text, ai_response
|
| 301 |
|
| 302 |
-
# Gradio UI
|
| 303 |
-
with gr.Blocks(
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
with gr.Column():
|
| 311 |
-
image_input = gr.Image(type="pil", label="Upload a Tomato Leaf Image")
|
| 312 |
-
|
| 313 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 314 |
-
scaling_method = gr.Radio(
|
| 315 |
-
["Temperature Scaling", "Min-Max Normalization"],
|
| 316 |
-
label="Confidence Scaling Method",
|
| 317 |
-
value="Temperature Scaling"
|
| 318 |
-
)
|
| 319 |
-
temperature_slider = gr.Slider(0.5, 2.0, step=0.1, label="Temperature", value=1.0)
|
| 320 |
-
min_conf_slider = gr.Slider(0, 100, step=1, label="Min Confidence", value=20)
|
| 321 |
-
max_conf_slider = gr.Slider(0, 100, step=1, label="Max Confidence", value=90)
|
| 322 |
-
|
| 323 |
-
detect_button = gr.Button("Detect Disease", variant="primary")
|
| 324 |
-
|
| 325 |
-
with gr.Column():
|
| 326 |
-
disease_output = gr.Textbox(label="Detected Disease & Adjusted Confidence", interactive=False)
|
| 327 |
-
raw_confidence_output = gr.Textbox(label="Raw Confidence", interactive=False)
|
| 328 |
-
ai_response_output = gr.Markdown(label="AI Assistant's Analysis & Recommendations")
|
| 329 |
-
|
| 330 |
-
# Expert Chat Tab
|
| 331 |
-
with gr.TabItem("Chat with Expert"):
|
| 332 |
-
gr.Markdown("# π¬ Chat with Agricultural Expert")
|
| 333 |
-
gr.Markdown("Ask any questions about tomato farming, diseases, or agricultural practices.")
|
| 334 |
-
|
| 335 |
-
chatbot = gr.Chatbot(
|
| 336 |
-
label="Chat History",
|
| 337 |
-
height=400,
|
| 338 |
-
bubble_full_width=False,
|
| 339 |
-
show_copy_button=True
|
| 340 |
-
)
|
| 341 |
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
label="
|
| 345 |
-
|
| 346 |
-
lines=2
|
| 347 |
)
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
|
| 358 |
# Set up event handlers
|
| 359 |
detect_button.click(
|
|
|
|
| 4 |
from tensorflow.keras.preprocessing import image
|
| 5 |
import gradio as gr
|
| 6 |
import requests
|
| 7 |
+
import json
|
| 8 |
|
| 9 |
# Suppress TensorFlow warnings
|
| 10 |
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
|
|
|
| 300 |
raw_text = f"Raw Confidence: {raw_confidence:.2f}%"
|
| 301 |
return result, raw_text, ai_response
|
| 302 |
|
| 303 |
+
# Simplified Gradio UI for better compatibility
|
| 304 |
+
with gr.Blocks() as demo:
|
| 305 |
+
gr.Markdown("# π
EvSentry8: Tomato Disease Detection with AI Assistant")
|
| 306 |
+
|
| 307 |
+
with gr.Tab("Disease Detection"):
|
| 308 |
+
with gr.Row():
|
| 309 |
+
with gr.Column():
|
| 310 |
+
image_input = gr.Image(type="pil", label="Upload a Tomato Leaf Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
+
scaling_method = gr.Radio(
|
| 313 |
+
["Temperature Scaling", "Min-Max Normalization"],
|
| 314 |
+
label="Confidence Scaling Method",
|
| 315 |
+
value="Temperature Scaling"
|
|
|
|
| 316 |
)
|
| 317 |
+
temperature_slider = gr.Slider(0.5, 2.0, step=0.1, label="Temperature", value=1.0)
|
| 318 |
+
min_conf_slider = gr.Slider(0, 100, step=1, label="Min Confidence", value=20)
|
| 319 |
+
max_conf_slider = gr.Slider(0, 100, step=1, label="Max Confidence", value=90)
|
| 320 |
+
|
| 321 |
+
detect_button = gr.Button("Detect Disease")
|
| 322 |
+
|
| 323 |
+
with gr.Column():
|
| 324 |
+
disease_output = gr.Textbox(label="Detected Disease & Adjusted Confidence")
|
| 325 |
+
raw_confidence_output = gr.Textbox(label="Raw Confidence")
|
| 326 |
+
ai_response_output = gr.Markdown(label="AI Assistant's Analysis & Recommendations")
|
| 327 |
+
|
| 328 |
+
with gr.Tab("Chat with Expert"):
|
| 329 |
+
gr.Markdown("# π¬ Chat with Agricultural Expert")
|
| 330 |
+
gr.Markdown("Ask any questions about tomato farming, diseases, or agricultural practices.")
|
| 331 |
+
|
| 332 |
+
chatbot = gr.Chatbot(height=400)
|
| 333 |
+
|
| 334 |
+
with gr.Row():
|
| 335 |
+
chat_input = gr.Textbox(
|
| 336 |
+
label="Your Question",
|
| 337 |
+
placeholder="Ask about tomato farming, diseases, or agricultural practices...",
|
| 338 |
+
lines=2
|
| 339 |
+
)
|
| 340 |
+
chat_button = gr.Button("Send")
|
| 341 |
+
|
| 342 |
+
gr.Markdown("""
|
| 343 |
+
### Example Questions:
|
| 344 |
+
- How do I identify tomato bacterial spot?
|
| 345 |
+
- What's the best way to prevent late blight?
|
| 346 |
+
- How often should I water my tomato plants?
|
| 347 |
+
- What are the signs of nutrient deficiency in tomatoes?
|
| 348 |
+
""")
|
| 349 |
|
| 350 |
# Set up event handlers
|
| 351 |
detect_button.click(
|