Update app.py
Browse files
app.py
CHANGED
@@ -9,8 +9,6 @@ import tempfile
|
|
9 |
import base64
|
10 |
import requests
|
11 |
from datetime import datetime
|
12 |
-
from gtts import gTTS
|
13 |
-
from googletrans import Translator
|
14 |
import google.generativeai as genai # Import Gemini API
|
15 |
|
16 |
# Configuring Google Gemini API
|
@@ -103,22 +101,7 @@ def inference(image, conf_threshold=0.5):
|
|
103 |
return infer, detected_classes, class_names, confidence_scores
|
104 |
|
105 |
|
106 |
-
# Converting text to chosen language speech
|
107 |
-
def text_to_speech(text, language="en"):
|
108 |
-
"""Convert text to speech using gTTS."""
|
109 |
-
try:
|
110 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
111 |
-
tts = gTTS(text=text, lang=language, slow=False)
|
112 |
-
tts.save(temp_audio.name)
|
113 |
-
|
114 |
-
with open(temp_audio.name, "rb") as audio_file:
|
115 |
-
audio_bytes = audio_file.read()
|
116 |
|
117 |
-
os.unlink(temp_audio.name)
|
118 |
-
return audio_bytes
|
119 |
-
except Exception as e:
|
120 |
-
st.error(f"Error generating speech: {str(e)}")
|
121 |
-
return None
|
122 |
|
123 |
# Initialize Streamlit UI
|
124 |
st.title("AI-Powered Crop Disease Detection & Diagnosis System")
|
@@ -132,14 +115,7 @@ with st.sidebar:
|
|
132 |
|
133 |
confidence_threshold = st.slider("Detection Confidence Threshold", 0.0, 1.0, 0.4)
|
134 |
|
135 |
-
|
136 |
-
tts_enabled = st.checkbox("Enable Text-to-Speech", value=True)
|
137 |
-
language = st.selectbox("Speech Language", options=["en", "ne", "hi", "bn"], format_func=lambda x: {
|
138 |
-
"en": "English",
|
139 |
-
"ne": "Nepali",
|
140 |
-
"hi": "Hindi",
|
141 |
-
"bn": "Bengali"
|
142 |
-
}[x])
|
143 |
|
144 |
if st.button("Clear Conversation History"):
|
145 |
st.session_state.conversation_history = {}
|
@@ -222,19 +198,7 @@ if uploaded_file:
|
|
222 |
for i, entry in enumerate(st.session_state.conversation_history[file_id]):
|
223 |
with st.expander(f"Q{i+1}: {entry['question'][:50]}..."):
|
224 |
st.write("**User:**", entry["question"])
|
225 |
-
st.write("**AI:**", entry["response"])
|
226 |
-
|
227 |
-
# Convert diagnosis to speech if enabled
|
228 |
-
|
229 |
-
translator = Translator()
|
230 |
-
if tts_enabled:
|
231 |
-
if st.button("π Listen to Diagnosis"):
|
232 |
-
with st.spinner("Generating audio... π΅"):
|
233 |
-
if language != "en":
|
234 |
-
diagnosis = translator.translate(diagnosis, dest=language)
|
235 |
-
audio_bytes = text_to_speech(diagnosis, language)
|
236 |
-
if audio_bytes:
|
237 |
-
st.audio(audio_bytes, format="audio/mp3")
|
238 |
else:
|
239 |
st.write("β
No high-confidence diseases detected.")
|
240 |
|
@@ -247,5 +211,4 @@ st.markdown("""
|
|
247 |
3. The system detects the disease using AI.
|
248 |
4. Gemini generates a diagnosis with symptoms and treatments.
|
249 |
5. Ask follow-up questions, and the AI will remember previous responses.
|
250 |
-
6. Optionally, listen to the AI-generated diagnosis.
|
251 |
""")
|
|
|
9 |
import base64
|
10 |
import requests
|
11 |
from datetime import datetime
|
|
|
|
|
12 |
import google.generativeai as genai # Import Gemini API
|
13 |
|
14 |
# Configuring Google Gemini API
|
|
|
101 |
return infer, detected_classes, class_names, confidence_scores
|
102 |
|
103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
# Initialize Streamlit UI
|
107 |
st.title("AI-Powered Crop Disease Detection & Diagnosis System")
|
|
|
115 |
|
116 |
confidence_threshold = st.slider("Detection Confidence Threshold", 0.0, 1.0, 0.4)
|
117 |
|
118 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
if st.button("Clear Conversation History"):
|
121 |
st.session_state.conversation_history = {}
|
|
|
198 |
for i, entry in enumerate(st.session_state.conversation_history[file_id]):
|
199 |
with st.expander(f"Q{i+1}: {entry['question'][:50]}..."):
|
200 |
st.write("**User:**", entry["question"])
|
201 |
+
st.write("**AI:**", entry["response"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
else:
|
203 |
st.write("β
No high-confidence diseases detected.")
|
204 |
|
|
|
211 |
3. The system detects the disease using AI.
|
212 |
4. Gemini generates a diagnosis with symptoms and treatments.
|
213 |
5. Ask follow-up questions, and the AI will remember previous responses.
|
|
|
214 |
""")
|