Update app.py
Browse files
app.py
CHANGED
@@ -13,14 +13,14 @@ from gtts import gTTS
|
|
13 |
from googletrans import Translator
|
14 |
import google.generativeai as genai # Import Gemini API
|
15 |
|
16 |
-
#
|
17 |
GEMINI_API_KEY = os.getenv("GOOGLE_API_KEY")
|
18 |
genai.configure(api_key=GEMINI_API_KEY)
|
19 |
|
20 |
-
#
|
21 |
yolo_model = YOLO("models/best.pt")
|
22 |
|
23 |
-
#
|
24 |
if "conversation_history" not in st.session_state:
|
25 |
st.session_state.conversation_history = {}
|
26 |
|
@@ -66,7 +66,7 @@ def generate_gemini_response(disease_list, user_context="", conversation_history
|
|
66 |
except Exception as e:
|
67 |
return f"Error connecting to Gemini API: {str(e)}"
|
68 |
|
69 |
-
#
|
70 |
def inference(image):
|
71 |
"""Detect crop diseases in the given image."""
|
72 |
results = yolo_model(image, conf=0.4)
|
@@ -81,7 +81,7 @@ def inference(image):
|
|
81 |
|
82 |
return infer, detected_classes, class_names
|
83 |
|
84 |
-
#
|
85 |
def text_to_speech(text, language="en"):
|
86 |
"""Convert text to speech using gTTS."""
|
87 |
try:
|
@@ -130,14 +130,14 @@ st.subheader("π Provide Initial Context or Ask a Question")
|
|
130 |
# Generalized example prompts for easier input
|
131 |
example_prompts = {
|
132 |
"Select an example...": "",
|
133 |
-
"
|
134 |
-
"
|
135 |
-
"
|
136 |
-
"
|
137 |
-
"
|
138 |
-
"
|
139 |
-
"
|
140 |
-
"
|
141 |
}
|
142 |
|
143 |
# Dropdown menu for selecting an example
|
@@ -194,13 +194,22 @@ if uploaded_file:
|
|
194 |
st.write("**AI:**", entry["response"])
|
195 |
|
196 |
# Convert diagnosis to speech if enabled
|
|
|
|
|
197 |
if tts_enabled:
|
198 |
if st.button("π Listen to Diagnosis"):
|
199 |
with st.spinner("Generating audio... π΅"):
|
200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
if audio_bytes:
|
202 |
st.audio(audio_bytes, format="audio/mp3")
|
203 |
|
|
|
204 |
else:
|
205 |
st.write("β No crop disease detected.")
|
206 |
|
|
|
13 |
from googletrans import Translator
|
14 |
import google.generativeai as genai # Import Gemini API
|
15 |
|
16 |
+
# Configuring Google Gemini API
|
17 |
GEMINI_API_KEY = os.getenv("GOOGLE_API_KEY")
|
18 |
genai.configure(api_key=GEMINI_API_KEY)
|
19 |
|
20 |
+
# Loading YOLO model for crop disease detection
|
21 |
yolo_model = YOLO("models/best.pt")
|
22 |
|
23 |
+
# Initializing conversation history if not set
|
24 |
if "conversation_history" not in st.session_state:
|
25 |
st.session_state.conversation_history = {}
|
26 |
|
|
|
66 |
except Exception as e:
|
67 |
return f"Error connecting to Gemini API: {str(e)}"
|
68 |
|
69 |
+
# Performing inference using YOLO
|
70 |
def inference(image):
|
71 |
"""Detect crop diseases in the given image."""
|
72 |
results = yolo_model(image, conf=0.4)
|
|
|
81 |
|
82 |
return infer, detected_classes, class_names
|
83 |
|
84 |
+
# Converting text to chosen language speech
|
85 |
def text_to_speech(text, language="en"):
|
86 |
"""Convert text to speech using gTTS."""
|
87 |
try:
|
|
|
130 |
# Generalized example prompts for easier input
|
131 |
example_prompts = {
|
132 |
"Select an example...": "",
|
133 |
+
"General Plant Health Issue": "My plant leaves are wilting and turning yellow. Is this a disease or a nutrient deficiency?",
|
134 |
+
"Leaf Spots and Discoloration": "I see dark spots on my crop leaves. Could this be a fungal or bacterial infection?",
|
135 |
+
"Leaves Drying or Curling": "The leaves on my plants are curling and drying up. What could be causing this?",
|
136 |
+
"Pest or Disease?": "I noticed tiny insects on my plants along with some leaf damage. Could this be a pest problem or a disease?",
|
137 |
+
"Overwatering or Root Rot?": "My plant leaves are turning brown and mushy. Is this due to overwatering or a root infection?",
|
138 |
+
"Poor Crop Growth": "My crops are growing very slowly and seem weak. Could this be due to soil problems or disease?",
|
139 |
+
"Weather and Disease Connection": "It has been raining a lot, and now my plants have mold. Could the weather be causing a fungal disease?",
|
140 |
+
"Regional Disease Concern": "I'm in a humid area and my crops often get infected. What are common diseases for this climate?",
|
141 |
}
|
142 |
|
143 |
# Dropdown menu for selecting an example
|
|
|
194 |
st.write("**AI:**", entry["response"])
|
195 |
|
196 |
# Convert diagnosis to speech if enabled
|
197 |
+
from googletrans import Translator
|
198 |
+
|
199 |
if tts_enabled:
|
200 |
if st.button("π Listen to Diagnosis"):
|
201 |
with st.spinner("Generating audio... π΅"):
|
202 |
+
# Translate the diagnosis to the target language
|
203 |
+
translator = Translator()
|
204 |
+
translated_text = translator.translate(diagnosis, dest=language).text
|
205 |
+
# Filter out unwanted characters like '#' and '*'
|
206 |
+
filtered_text = ''.join(c for c in translated_text if c not in ['#', '*'])
|
207 |
+
# Now process the translated text for TTS
|
208 |
+
audio_bytes = text_to_speech(filtered_text, language)
|
209 |
if audio_bytes:
|
210 |
st.audio(audio_bytes, format="audio/mp3")
|
211 |
|
212 |
+
|
213 |
else:
|
214 |
st.write("β No crop disease detected.")
|
215 |
|