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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
toggle_css = """
|
| 2 |
<style>
|
| 3 |
/* Main container for language toggle */
|
| 4 |
.language-toggle-container {
|
| 5 |
display: flex;
|
| 6 |
-
|
| 7 |
-
|
| 8 |
margin-bottom: 20px;
|
| 9 |
padding: 10px;
|
| 10 |
background-color: rgba(255, 255, 255, 0.9);
|
|
@@ -12,15 +377,6 @@ toggle_css = """
|
|
| 12 |
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
|
| 13 |
}
|
| 14 |
|
| 15 |
-
/* Toggle title styling */
|
| 16 |
-
.toggle-title {
|
| 17 |
-
font-size: 16px;
|
| 18 |
-
font-weight: 600;
|
| 19 |
-
margin-bottom: 5px;
|
| 20 |
-
color: #2a633d;
|
| 21 |
-
align-self: center;
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
/* Toggle label styling */
|
| 25 |
.toggle-label {
|
| 26 |
font-size: 16px;
|
|
@@ -61,6 +417,9 @@ toggle_css = """
|
|
| 61 |
font-size: 14px !important;
|
| 62 |
transition: all 0.2s ease !important;
|
| 63 |
color: #555 !important;
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
|
| 66 |
.language-switcher .gr-radio input:checked + label {
|
|
@@ -92,7 +451,6 @@ toggle_css = """
|
|
| 92 |
@media (max-width: 768px) {
|
| 93 |
.language-toggle-container {
|
| 94 |
justify-content: center;
|
| 95 |
-
align-items: center;
|
| 96 |
}
|
| 97 |
|
| 98 |
.language-switcher .gr-radio label {
|
|
@@ -123,7 +481,6 @@ with gr.Blocks(theme=gr.themes.Soft(), css=toggle_css) as app:
|
|
| 123 |
"submit_btn": "Wasilisha Swali",
|
| 124 |
"clear_btn": "Futa Mazungumzo",
|
| 125 |
"toggle_label": "Lugha:",
|
| 126 |
-
"toggle_title": "Swahili/English",
|
| 127 |
"error_no_image": "Tafadhali pakia picha",
|
| 128 |
"error_processing": "Hitilafu katika kuchakata picha"
|
| 129 |
},
|
|
@@ -140,7 +497,6 @@ with gr.Blocks(theme=gr.themes.Soft(), css=toggle_css) as app:
|
|
| 140 |
"submit_btn": "Submit Question",
|
| 141 |
"clear_btn": "Clear Conversation",
|
| 142 |
"toggle_label": "Language:",
|
| 143 |
-
"toggle_title": "Swahili/English",
|
| 144 |
"error_no_image": "Please upload an image",
|
| 145 |
"error_processing": "Error processing image"
|
| 146 |
}
|
|
@@ -153,31 +509,19 @@ with gr.Blocks(theme=gr.themes.Soft(), css=toggle_css) as app:
|
|
| 153 |
# Store the current disease for context
|
| 154 |
current_disease = gr.State("")
|
| 155 |
|
| 156 |
-
# Add improved language toggle at the top
|
| 157 |
with gr.Row(elem_classes="language-toggle-container"):
|
| 158 |
-
|
| 159 |
-
toggle_title = gr.Markdown(text_dict["swahili"]["toggle_title"], elem_classes="toggle-title language-transition")
|
| 160 |
|
| 161 |
-
with gr.
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
""")
|
| 171 |
-
|
| 172 |
-
with gr.Column(elem_classes="language-switcher", scale=1):
|
| 173 |
-
language_toggle = gr.Radio(
|
| 174 |
-
["Swahili", "English"],
|
| 175 |
-
value="Swahili",
|
| 176 |
-
label="",
|
| 177 |
-
interactive=True,
|
| 178 |
-
elem_id="language-toggle"
|
| 179 |
-
)
|
| 180 |
-
|
| 181 |
# Main identification section
|
| 182 |
with gr.Row():
|
| 183 |
with gr.Column(scale=1):
|
|
@@ -208,14 +552,14 @@ with gr.Blocks(theme=gr.themes.Soft(), css=toggle_css) as app:
|
|
| 208 |
follow_up_btn = gr.Button(text_dict["swahili"]["submit_btn"], variant="primary", elem_classes="language-transition")
|
| 209 |
clear_btn = gr.Button(text_dict["swahili"]["clear_btn"], elem_classes="language-transition")
|
| 210 |
|
| 211 |
-
# Enhanced function to switch language based on toggle
|
| 212 |
def switch_language(selected_language, current_lang):
|
| 213 |
# Map the radio value to our language code
|
| 214 |
new_lang = "swahili" if selected_language == "Swahili" else "english"
|
| 215 |
|
| 216 |
# If language hasn't changed, don't update anything
|
| 217 |
if new_lang == current_lang:
|
| 218 |
-
return [current_lang] + [gr.update() for _ in range(
|
| 219 |
|
| 220 |
# Debug message to verify the language switch
|
| 221 |
print(f"Switching language from {current_lang} to {new_lang}")
|
|
@@ -241,8 +585,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=toggle_css) as app:
|
|
| 241 |
gr.update(label=text_dict[new_lang]["question_label"], placeholder=text_dict[new_lang]["question_placeholder"]),
|
| 242 |
text_dict[new_lang]["submit_btn"],
|
| 243 |
text_dict[new_lang]["clear_btn"],
|
| 244 |
-
text_dict[new_lang]["toggle_label"]
|
| 245 |
-
text_dict[new_lang]["toggle_title"]
|
| 246 |
)
|
| 247 |
|
| 248 |
# Set up event handlers
|
|
@@ -282,7 +625,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=toggle_css) as app:
|
|
| 282 |
def clear_conversation_history():
|
| 283 |
return ""
|
| 284 |
|
| 285 |
-
# Connect the language toggle with enhanced functionality
|
| 286 |
language_toggle.change(
|
| 287 |
switch_language,
|
| 288 |
inputs=[language_toggle, current_language],
|
|
@@ -298,8 +641,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=toggle_css) as app:
|
|
| 298 |
follow_up_input,
|
| 299 |
follow_up_btn,
|
| 300 |
clear_btn,
|
| 301 |
-
toggle_label
|
| 302 |
-
toggle_title
|
| 303 |
]
|
| 304 |
)
|
| 305 |
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import re
|
| 4 |
+
from fastai.vision.all import *
|
| 5 |
+
from groq import Groq
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# Load the trained model for grapevine disease classification
|
| 9 |
+
learn = load_learner('export.pkl') # Assumes you have a trained model for grapevine diseases
|
| 10 |
+
labels = learn.dls.vocab
|
| 11 |
+
|
| 12 |
+
# Initialize Groq client
|
| 13 |
+
client = Groq(
|
| 14 |
+
api_key=os.environ.get("GROQ_API_KEY"),
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Your existing functions remain the same
|
| 18 |
+
def clean_disease_name(name):
|
| 19 |
+
"""Clean disease name by removing numbers and special characters, and fix formatting"""
|
| 20 |
+
# Remove numbers and dots at the beginning
|
| 21 |
+
cleaned = re.sub(r'^\d+\.', '', name)
|
| 22 |
+
# Replace underscores with spaces
|
| 23 |
+
cleaned = cleaned.replace('_', ' ')
|
| 24 |
+
# Remove any remaining special characters
|
| 25 |
+
cleaned = re.sub(r'[^\w\s]', '', cleaned)
|
| 26 |
+
# Fix spacing
|
| 27 |
+
cleaned = ' '.join(cleaned.split())
|
| 28 |
+
return cleaned
|
| 29 |
+
|
| 30 |
+
def format_disease_info(raw_info):
|
| 31 |
+
"""Improve the formatting of disease information"""
|
| 32 |
+
# Add proper line breaks between sections and ensure consistent heading levels
|
| 33 |
+
formatted = raw_info
|
| 34 |
+
|
| 35 |
+
# Replace markdown headings with HTML headings for better control
|
| 36 |
+
formatted = re.sub(r'#+\s+(.*)', r'<h3>\1</h3>', formatted)
|
| 37 |
+
|
| 38 |
+
# Add paragraph tags for better spacing
|
| 39 |
+
formatted = re.sub(r'\n\*\s+(.*)', r'<p>• \1</p>', formatted)
|
| 40 |
+
formatted = re.sub(r'\n([^<\n].*)', r'<p>\1</p>', formatted)
|
| 41 |
+
|
| 42 |
+
# Remove any duplicate paragraph tags
|
| 43 |
+
formatted = formatted.replace('<p><p>', '<p>')
|
| 44 |
+
formatted = formatted.replace('</p></p>', '</p>')
|
| 45 |
+
|
| 46 |
+
return formatted
|
| 47 |
+
|
| 48 |
+
def get_disease_treatment(disease_name, language="swahili"):
|
| 49 |
+
"""Get direct treatment measures for the disease"""
|
| 50 |
+
clean_name = clean_disease_name(disease_name)
|
| 51 |
+
|
| 52 |
+
# Return healthy message if the prediction is healthy
|
| 53 |
+
if clean_name.lower() == "healthy" or "healthy" in clean_name.lower():
|
| 54 |
+
if language.lower() == "swahili":
|
| 55 |
+
return """
|
| 56 |
+
### Hali ya Jani: Afya nzuri
|
| 57 |
+
|
| 58 |
+
Jani hili liko katika hali nzuri ya afya. Hakuna dalili za magonjwa zinazoonekana. Endelea kutunza mizabibu yako vizuri kwa:
|
| 59 |
+
|
| 60 |
+
• Kumwagilia maji ipasavyo
|
| 61 |
+
• Kuhakikisha rutuba ya udongo
|
| 62 |
+
• Kupunguza matawi kwa usahihi
|
| 63 |
+
• Kufuatilia mara kwa mara kwa dalili za magonjwa
|
| 64 |
+
• Kudumisha hewa nzuri kuzunguka mmea
|
| 65 |
+
"""
|
| 66 |
+
else:
|
| 67 |
+
return """
|
| 68 |
+
### Leaf Status: Healthy
|
| 69 |
+
|
| 70 |
+
This leaf is in a healthy state. No signs of disease are present. Continue to maintain your vines by:
|
| 71 |
+
|
| 72 |
+
• Proper irrigation
|
| 73 |
+
• Ensuring soil fertility
|
| 74 |
+
• Appropriate pruning
|
| 75 |
+
• Regular monitoring for disease symptoms
|
| 76 |
+
• Maintaining good air circulation around the plant
|
| 77 |
+
"""
|
| 78 |
+
|
| 79 |
+
prompt = f"""
|
| 80 |
+
Provide a concise, practical list of DIRECT treatment measures for {clean_name} disease in grapevines.
|
| 81 |
+
|
| 82 |
+
Format your response as a bulleted list with 5-7 specific actions a vineyard manager should take IMMEDIATELY to treat the disease.
|
| 83 |
+
|
| 84 |
+
For each treatment measure:
|
| 85 |
+
1. Start with an action verb
|
| 86 |
+
2. Be specific about products/techniques to use
|
| 87 |
+
3. Include application rates/timing when relevant
|
| 88 |
+
|
| 89 |
+
The response should be in {"Swahili" if language.lower() == "swahili" else "English"}.
|
| 90 |
+
|
| 91 |
+
Begin with a short title: "HATUA ZA MATIBABU:" (if Swahili) or "TREATMENT MEASURES:" (if English)
|
| 92 |
+
"""
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
chat_completion = client.chat.completions.create(
|
| 96 |
+
messages=[
|
| 97 |
+
{
|
| 98 |
+
"role": "user",
|
| 99 |
+
"content": prompt,
|
| 100 |
+
}
|
| 101 |
+
],
|
| 102 |
+
model="llama-3.3-70b-versatile",
|
| 103 |
+
)
|
| 104 |
+
return chat_completion.choices[0].message.content
|
| 105 |
+
except Exception as e:
|
| 106 |
+
error_msg = "Hitilafu kupata taarifa za matibabu" if language.lower() == "swahili" else "Error fetching treatment information"
|
| 107 |
+
return f"{error_msg}: {str(e)}"
|
| 108 |
+
|
| 109 |
+
def get_disease_info(disease_name, language="swahili"):
|
| 110 |
+
"""Get detailed information about a grapevine disease using Groq API"""
|
| 111 |
+
clean_name = clean_disease_name(disease_name)
|
| 112 |
+
|
| 113 |
+
# Return healthy message if the prediction is healthy
|
| 114 |
+
if clean_name.lower() == "healthy" or "healthy" in clean_name.lower():
|
| 115 |
+
if language.lower() == "swahili":
|
| 116 |
+
return """
|
| 117 |
+
# Hali ya Jani: Afya nzuri
|
| 118 |
+
|
| 119 |
+
Jani hili liko katika hali nzuri ya afya. Hakuna dalili za magonjwa zinazoonekana. Hii ni ishara nzuri kwamba:
|
| 120 |
+
|
| 121 |
+
- Mizabibu yako inapata virutubisho vya kutosha
|
| 122 |
+
- Umwagiliaji wa maji uko katika kiasi cha kutosha
|
| 123 |
+
- Hali ya hewa katika shamba lako ni nzuri
|
| 124 |
+
- Hakuna mashambulizi ya wadudu waharibifu
|
| 125 |
+
|
| 126 |
+
Endelea kudumisha utunzaji mzuri wa mizabibu yako ili kuhakikisha uzalishaji bora wa zabibu.
|
| 127 |
+
"""
|
| 128 |
+
else:
|
| 129 |
+
return """
|
| 130 |
+
# Leaf Status: Healthy
|
| 131 |
+
|
| 132 |
+
This leaf is in a healthy state. No signs of disease are present. This is a good indication that:
|
| 133 |
+
|
| 134 |
+
- Your vines are receiving adequate nutrition
|
| 135 |
+
- Irrigation levels are appropriate
|
| 136 |
+
- Climate conditions in your vineyard are favorable
|
| 137 |
+
- There are no pest invasions
|
| 138 |
+
|
| 139 |
+
Continue to maintain good vineyard practices to ensure optimal grape production.
|
| 140 |
+
"""
|
| 141 |
+
|
| 142 |
+
lang = "SWAHILI" if language.lower() == "swahili" else "ENGLISH"
|
| 143 |
+
|
| 144 |
+
prompt = f"""
|
| 145 |
+
Provide detailed information IN {lang} about {clean_name} disease in grapevines.
|
| 146 |
+
|
| 147 |
+
Structure your response like this:
|
| 148 |
+
1. A brief introduction about what this disease is (1-2 sentences)
|
| 149 |
+
2. Main symptoms and how to identify the disease (short paragraph)
|
| 150 |
+
3. FOCUS EXTENSIVELY on the treatment methods and prevention strategies (this should be the longest section)
|
| 151 |
+
4. Conclusion with practical advice for vineyard managers
|
| 152 |
+
|
| 153 |
+
Format your response in markdown for better readability.
|
| 154 |
+
Make sure the entire response is in {lang}.
|
| 155 |
+
"""
|
| 156 |
+
|
| 157 |
+
try:
|
| 158 |
+
chat_completion = client.chat.completions.create(
|
| 159 |
+
messages=[
|
| 160 |
+
{
|
| 161 |
+
"role": "user",
|
| 162 |
+
"content": prompt,
|
| 163 |
+
}
|
| 164 |
+
],
|
| 165 |
+
model="llama-3.3-70b-versatile",
|
| 166 |
+
)
|
| 167 |
+
return chat_completion.choices[0].message.content
|
| 168 |
+
except Exception as e:
|
| 169 |
+
error_msg = "Hitilafu kupata taarifa" if language.lower() == "swahili" else "Error fetching information"
|
| 170 |
+
return f"{error_msg}: {str(e)}"
|
| 171 |
+
|
| 172 |
+
def predict_and_get_info(img, language="swahili"):
|
| 173 |
+
"""Predict grapevine disease and get detailed information"""
|
| 174 |
+
# Process the image
|
| 175 |
+
img = PILImage.create(img)
|
| 176 |
+
|
| 177 |
+
# Get prediction
|
| 178 |
+
pred, pred_idx, probs = learn.predict(img)
|
| 179 |
+
|
| 180 |
+
# Get top 5 predictions (or all if less than 5)
|
| 181 |
+
num_classes = min(5, len(labels))
|
| 182 |
+
top_indices = probs.argsort(descending=True)[:num_classes]
|
| 183 |
+
top_probs = probs[top_indices]
|
| 184 |
+
top_labels = [labels[i] for i in top_indices]
|
| 185 |
+
|
| 186 |
+
# Format as dictionary with cleaned names for display
|
| 187 |
+
prediction_results = {clean_disease_name(top_labels[i]): float(top_probs[i]) for i in range(num_classes)}
|
| 188 |
+
|
| 189 |
+
# Get top prediction (original format for info retrieval)
|
| 190 |
+
top_disease = str(pred)
|
| 191 |
+
# Also keep a clean version for display
|
| 192 |
+
clean_top_disease = clean_disease_name(top_disease)
|
| 193 |
+
|
| 194 |
+
# Check if healthy
|
| 195 |
+
is_healthy = clean_top_disease.lower() == "healthy" or "healthy" in clean_top_disease.lower()
|
| 196 |
+
|
| 197 |
+
# Get detailed information about the top predicted disease
|
| 198 |
+
disease_info = get_disease_info(top_disease, language)
|
| 199 |
+
formatted_info = format_disease_info(disease_info)
|
| 200 |
+
|
| 201 |
+
# Get direct treatment measures
|
| 202 |
+
treatment_measures = get_disease_treatment(top_disease, language)
|
| 203 |
+
formatted_treatment = format_disease_info(treatment_measures)
|
| 204 |
+
|
| 205 |
+
# Create title text based on language and health status
|
| 206 |
+
if language.lower() == "swahili":
|
| 207 |
+
title_text = "Jani Zuri na Afya Njema" if is_healthy else f"Ugonjwa: {clean_top_disease}"
|
| 208 |
+
advice_title = "Ushauri wa Haraka kwa Wakulima"
|
| 209 |
+
advice_text = "Kumbuka: Fuata maelekezo ya kitaalamu na tumia dawa zilizoidhinishwa tu. Wasiliana na mtaalamu wa kilimo kwa ushauri zaidi."
|
| 210 |
+
treatment_title = "Hatua za Matibabu"
|
| 211 |
+
else:
|
| 212 |
+
title_text = "Healthy Leaf" if is_healthy else f"Disease: {clean_top_disease}"
|
| 213 |
+
advice_title = "Quick Advice for Vineyard Managers"
|
| 214 |
+
advice_text = "Remember: Follow professional instructions and use only approved treatments. Contact an agricultural expert for more advice."
|
| 215 |
+
treatment_title = "Treatment Measures"
|
| 216 |
+
|
| 217 |
+
# Create well-formatted display with improved fonts and styling
|
| 218 |
+
custom_css = """
|
| 219 |
+
<style>
|
| 220 |
+
.disease-container {
|
| 221 |
+
font-family: 'Nunito', 'Segoe UI', sans-serif;
|
| 222 |
+
padding: 20px;
|
| 223 |
+
max-width: 100%;
|
| 224 |
+
background-color: #ffffff;
|
| 225 |
+
border-radius: 12px;
|
| 226 |
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
|
| 227 |
+
}
|
| 228 |
+
.disease-title {
|
| 229 |
+
color: #2a633d;
|
| 230 |
+
font-size: 28px;
|
| 231 |
+
font-weight: 700;
|
| 232 |
+
margin-bottom: 20px;
|
| 233 |
+
padding-bottom: 10px;
|
| 234 |
+
border-bottom: 2px solid #e0e0e0;
|
| 235 |
+
}
|
| 236 |
+
.info-container {
|
| 237 |
+
line-height: 1.8;
|
| 238 |
+
font-size: 16px;
|
| 239 |
+
color: #333333;
|
| 240 |
+
}
|
| 241 |
+
.info-container h3 {
|
| 242 |
+
color: #2a633d;
|
| 243 |
+
font-size: 22px;
|
| 244 |
+
font-weight: 600;
|
| 245 |
+
margin-top: 25px;
|
| 246 |
+
margin-bottom: 15px;
|
| 247 |
+
padding-bottom: 8px;
|
| 248 |
+
border-bottom: 1px solid #e0e0e0;
|
| 249 |
+
}
|
| 250 |
+
.info-container p {
|
| 251 |
+
margin-bottom: 15px;
|
| 252 |
+
text-align: justify;
|
| 253 |
+
}
|
| 254 |
+
.treatment-section {
|
| 255 |
+
background-color: #f1f8e9;
|
| 256 |
+
padding: 20px;
|
| 257 |
+
border-radius: 8px;
|
| 258 |
+
margin: 25px 0;
|
| 259 |
+
border-left: 5px solid #558b2f;
|
| 260 |
+
}
|
| 261 |
+
.treatment-section h3 {
|
| 262 |
+
color: #33691e;
|
| 263 |
+
border-bottom: none;
|
| 264 |
+
margin-top: 0;
|
| 265 |
+
}
|
| 266 |
+
.direct-treatment {
|
| 267 |
+
background-color: #e8f5e9;
|
| 268 |
+
padding: 20px;
|
| 269 |
+
border-radius: 8px;
|
| 270 |
+
margin: 25px 0;
|
| 271 |
+
border-left: 5px solid #2e7d32;
|
| 272 |
+
}
|
| 273 |
+
.direct-treatment h3 {
|
| 274 |
+
color: #2e7d32;
|
| 275 |
+
border-bottom: none;
|
| 276 |
+
margin-top: 0;
|
| 277 |
+
}
|
| 278 |
+
.healthy-state {
|
| 279 |
+
background-color: #e8f5e9;
|
| 280 |
+
color: #2e7d32;
|
| 281 |
+
padding: 15px;
|
| 282 |
+
border-radius: 8px;
|
| 283 |
+
text-align: center;
|
| 284 |
+
font-weight: bold;
|
| 285 |
+
font-size: 18px;
|
| 286 |
+
margin: 15px 0;
|
| 287 |
+
}
|
| 288 |
+
/* Ensure good readability for text */
|
| 289 |
+
body {
|
| 290 |
+
font-feature-settings: "kern", "liga", "clig", "calt";
|
| 291 |
+
text-rendering: optimizeLegibility;
|
| 292 |
+
}
|
| 293 |
+
</style>
|
| 294 |
+
"""
|
| 295 |
+
|
| 296 |
+
# Healthy banner HTML if needed
|
| 297 |
+
healthy_banner = ""
|
| 298 |
+
if is_healthy:
|
| 299 |
+
if language.lower() == "swahili":
|
| 300 |
+
healthy_banner = '<div class="healthy-state">✓ Jani hili liko katika hali nzuri ya afya</div>'
|
| 301 |
+
else:
|
| 302 |
+
healthy_banner = '<div class="healthy-state">✓ This leaf is in a healthy state</div>'
|
| 303 |
+
|
| 304 |
+
combined_info = f"""
|
| 305 |
+
{custom_css}
|
| 306 |
+
<div class="disease-container">
|
| 307 |
+
<h2 class="disease-title">{title_text}</h2>
|
| 308 |
+
{healthy_banner}
|
| 309 |
+
|
| 310 |
+
<div class="info-container">
|
| 311 |
+
{formatted_info}
|
| 312 |
+
</div>
|
| 313 |
+
|
| 314 |
+
<div class="direct-treatment">
|
| 315 |
+
<h3>{treatment_title}</h3>
|
| 316 |
+
{formatted_treatment}
|
| 317 |
+
</div>
|
| 318 |
+
|
| 319 |
+
<div class="treatment-section">
|
| 320 |
+
<h3>{advice_title}</h3>
|
| 321 |
+
<p>{advice_text}</p>
|
| 322 |
+
</div>
|
| 323 |
+
</div>
|
| 324 |
+
"""
|
| 325 |
+
|
| 326 |
+
return prediction_results, combined_info, clean_top_disease
|
| 327 |
+
|
| 328 |
+
def follow_up_question(question, disease_name, language="swahili"):
|
| 329 |
+
"""Allow vineyard managers to ask follow-up questions about the identified disease"""
|
| 330 |
+
if not question.strip() or not disease_name:
|
| 331 |
+
return "Tafadhali tambua ugonjwa wa mizabibu kwanza na uulize swali mahususi kuuhusu." if language.lower() == "swahili" else "Please identify a grapevine disease first and ask a specific question about it."
|
| 332 |
+
|
| 333 |
+
lang = "SWAHILI" if language.lower() == "swahili" else "ENGLISH"
|
| 334 |
+
|
| 335 |
+
prompt = f"""
|
| 336 |
+
The vineyard manager is asking about {disease_name} disease: "{question}"
|
| 337 |
+
|
| 338 |
+
Provide a detailed answer IN {lang}, focusing on accurate plant pathology information.
|
| 339 |
+
If the question relates to treatment options, preventive measures, or climate change impacts, emphasize those aspects in your response.
|
| 340 |
+
|
| 341 |
+
IMPORTANT:
|
| 342 |
+
- Do not repeat basic introductory information about the disease that would have already been provided
|
| 343 |
+
- Directly answer the specific question asked
|
| 344 |
+
- The entire response must be in {lang}
|
| 345 |
+
- Focus on practical advice whenever possible
|
| 346 |
+
|
| 347 |
+
Format your response in markdown for better readability.
|
| 348 |
+
"""
|
| 349 |
+
|
| 350 |
+
try:
|
| 351 |
+
chat_completion = client.chat.completions.create(
|
| 352 |
+
messages=[
|
| 353 |
+
{
|
| 354 |
+
"role": "user",
|
| 355 |
+
"content": prompt,
|
| 356 |
+
}
|
| 357 |
+
],
|
| 358 |
+
model="llama-3.3-70b-versatile",
|
| 359 |
+
)
|
| 360 |
+
return chat_completion.choices[0].message.content
|
| 361 |
+
except Exception as e:
|
| 362 |
+
error_msg = "Hitilafu kupata taarifa" if language.lower() == "swahili" else "Error fetching information"
|
| 363 |
+
return f"{error_msg}: {str(e)}"
|
| 364 |
+
|
| 365 |
+
# Enhanced CSS for styling the improved language toggle
|
| 366 |
toggle_css = """
|
| 367 |
<style>
|
| 368 |
/* Main container for language toggle */
|
| 369 |
.language-toggle-container {
|
| 370 |
display: flex;
|
| 371 |
+
align-items: center;
|
| 372 |
+
justify-content: flex-end;
|
| 373 |
margin-bottom: 20px;
|
| 374 |
padding: 10px;
|
| 375 |
background-color: rgba(255, 255, 255, 0.9);
|
|
|
|
| 377 |
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
|
| 378 |
}
|
| 379 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
/* Toggle label styling */
|
| 381 |
.toggle-label {
|
| 382 |
font-size: 16px;
|
|
|
|
| 417 |
font-size: 14px !important;
|
| 418 |
transition: all 0.2s ease !important;
|
| 419 |
color: #555 !important;
|
| 420 |
+
display: flex !important;
|
| 421 |
+
align-items: center !important;
|
| 422 |
+
justify-content: center !important;
|
| 423 |
}
|
| 424 |
|
| 425 |
.language-switcher .gr-radio input:checked + label {
|
|
|
|
| 451 |
@media (max-width: 768px) {
|
| 452 |
.language-toggle-container {
|
| 453 |
justify-content: center;
|
|
|
|
| 454 |
}
|
| 455 |
|
| 456 |
.language-switcher .gr-radio label {
|
|
|
|
| 481 |
"submit_btn": "Wasilisha Swali",
|
| 482 |
"clear_btn": "Futa Mazungumzo",
|
| 483 |
"toggle_label": "Lugha:",
|
|
|
|
| 484 |
"error_no_image": "Tafadhali pakia picha",
|
| 485 |
"error_processing": "Hitilafu katika kuchakata picha"
|
| 486 |
},
|
|
|
|
| 497 |
"submit_btn": "Submit Question",
|
| 498 |
"clear_btn": "Clear Conversation",
|
| 499 |
"toggle_label": "Language:",
|
|
|
|
| 500 |
"error_no_image": "Please upload an image",
|
| 501 |
"error_processing": "Error processing image"
|
| 502 |
}
|
|
|
|
| 509 |
# Store the current disease for context
|
| 510 |
current_disease = gr.State("")
|
| 511 |
|
| 512 |
+
# Add improved language toggle at the top
|
| 513 |
with gr.Row(elem_classes="language-toggle-container"):
|
| 514 |
+
toggle_label = gr.Markdown(text_dict["swahili"]["toggle_label"], elem_classes="toggle-label language-transition")
|
|
|
|
| 515 |
|
| 516 |
+
with gr.Column(elem_classes="language-switcher", scale=1):
|
| 517 |
+
language_toggle = gr.Radio(
|
| 518 |
+
["Swahili", "English"],
|
| 519 |
+
value="Swahili",
|
| 520 |
+
label="",
|
| 521 |
+
interactive=True,
|
| 522 |
+
elem_id="language-toggle"
|
| 523 |
+
)
|
| 524 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
# Main identification section
|
| 526 |
with gr.Row():
|
| 527 |
with gr.Column(scale=1):
|
|
|
|
| 552 |
follow_up_btn = gr.Button(text_dict["swahili"]["submit_btn"], variant="primary", elem_classes="language-transition")
|
| 553 |
clear_btn = gr.Button(text_dict["swahili"]["clear_btn"], elem_classes="language-transition")
|
| 554 |
|
| 555 |
+
# Enhanced function to switch language based on toggle
|
| 556 |
def switch_language(selected_language, current_lang):
|
| 557 |
# Map the radio value to our language code
|
| 558 |
new_lang = "swahili" if selected_language == "Swahili" else "english"
|
| 559 |
|
| 560 |
# If language hasn't changed, don't update anything
|
| 561 |
if new_lang == current_lang:
|
| 562 |
+
return [current_lang] + [gr.update() for _ in range(11)]
|
| 563 |
|
| 564 |
# Debug message to verify the language switch
|
| 565 |
print(f"Switching language from {current_lang} to {new_lang}")
|
|
|
|
| 585 |
gr.update(label=text_dict[new_lang]["question_label"], placeholder=text_dict[new_lang]["question_placeholder"]),
|
| 586 |
text_dict[new_lang]["submit_btn"],
|
| 587 |
text_dict[new_lang]["clear_btn"],
|
| 588 |
+
text_dict[new_lang]["toggle_label"]
|
|
|
|
| 589 |
)
|
| 590 |
|
| 591 |
# Set up event handlers
|
|
|
|
| 625 |
def clear_conversation_history():
|
| 626 |
return ""
|
| 627 |
|
| 628 |
+
# Connect the language toggle with enhanced functionality
|
| 629 |
language_toggle.change(
|
| 630 |
switch_language,
|
| 631 |
inputs=[language_toggle, current_language],
|
|
|
|
| 641 |
follow_up_input,
|
| 642 |
follow_up_btn,
|
| 643 |
clear_btn,
|
| 644 |
+
toggle_label
|
|
|
|
| 645 |
]
|
| 646 |
)
|
| 647 |
|