Update app.py
Browse files
app.py
CHANGED
|
@@ -362,8 +362,28 @@ def follow_up_question(question, disease_name, language="swahili"):
|
|
| 362 |
error_msg = "Hitilafu kupata taarifa" if language.lower() == "swahili" else "Error fetching information"
|
| 363 |
return f"{error_msg}: {str(e)}"
|
| 364 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
# Create the Gradio interface
|
| 366 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
|
|
|
|
|
|
|
|
| 367 |
# State for current language
|
| 368 |
current_language = gr.State("swahili")
|
| 369 |
|
|
@@ -381,7 +401,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 381 |
"question_placeholder": "Mfano: Je, ni hatua gani bora za kuzuia ugonjwa huu?",
|
| 382 |
"submit_btn": "Wasilisha Swali",
|
| 383 |
"clear_btn": "Futa Mazungumzo",
|
| 384 |
-
"
|
| 385 |
"error_no_image": "Tafadhali pakia picha",
|
| 386 |
"error_processing": "Hitilafu katika kuchakata picha"
|
| 387 |
},
|
|
@@ -397,7 +417,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 397 |
"question_placeholder": "Example: What are the best preventative measures for this disease?",
|
| 398 |
"submit_btn": "Submit Question",
|
| 399 |
"clear_btn": "Clear Conversation",
|
| 400 |
-
"
|
| 401 |
"error_no_image": "Please upload an image",
|
| 402 |
"error_processing": "Error processing image"
|
| 403 |
}
|
|
@@ -410,12 +430,15 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 410 |
# Store the current disease for context
|
| 411 |
current_disease = gr.State("")
|
| 412 |
|
| 413 |
-
# Add
|
| 414 |
-
with gr.Row():
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
|
|
|
|
|
|
|
|
|
| 419 |
)
|
| 420 |
|
| 421 |
# Main identification section
|
|
@@ -447,13 +470,19 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 447 |
follow_up_btn = gr.Button(text_dict["swahili"]["submit_btn"], variant="primary")
|
| 448 |
clear_btn = gr.Button(text_dict["swahili"]["clear_btn"])
|
| 449 |
|
| 450 |
-
# Function to switch language
|
| 451 |
-
def switch_language(current_lang):
|
| 452 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
|
| 454 |
-
# Debug message to verify the language switch
|
| 455 |
print(f"Switching language from {current_lang} to {new_lang}")
|
| 456 |
|
|
|
|
| 457 |
return (
|
| 458 |
new_lang,
|
| 459 |
text_dict[new_lang]["title"],
|
|
@@ -466,8 +495,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 466 |
text_dict[new_lang]["question_label"],
|
| 467 |
text_dict[new_lang]["question_placeholder"],
|
| 468 |
text_dict[new_lang]["submit_btn"],
|
| 469 |
-
text_dict[new_lang]["clear_btn"]
|
| 470 |
-
text_dict[new_lang]["lang_btn"]
|
| 471 |
)
|
| 472 |
|
| 473 |
# Set up event handlers
|
|
@@ -507,10 +535,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 507 |
def clear_conversation_history():
|
| 508 |
return ""
|
| 509 |
|
| 510 |
-
# Connect the language
|
| 511 |
-
|
| 512 |
switch_language,
|
| 513 |
-
inputs=[current_language],
|
| 514 |
outputs=[
|
| 515 |
current_language,
|
| 516 |
title_md,
|
|
@@ -523,8 +551,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 523 |
follow_up_input,
|
| 524 |
follow_up_input,
|
| 525 |
follow_up_btn,
|
| 526 |
-
clear_btn
|
| 527 |
-
lang_btn
|
| 528 |
]
|
| 529 |
)
|
| 530 |
|
|
|
|
| 362 |
error_msg = "Hitilafu kupata taarifa" if language.lower() == "swahili" else "Error fetching information"
|
| 363 |
return f"{error_msg}: {str(e)}"
|
| 364 |
|
| 365 |
+
# CSS for styling the language toggle
|
| 366 |
+
toggle_css = """
|
| 367 |
+
<style>
|
| 368 |
+
.language-toggle-container {
|
| 369 |
+
display: flex;
|
| 370 |
+
align-items: center;
|
| 371 |
+
justify-content: center;
|
| 372 |
+
margin-bottom: 20px;
|
| 373 |
+
}
|
| 374 |
+
.toggle-label {
|
| 375 |
+
font-size: 16px;
|
| 376 |
+
font-weight: 600;
|
| 377 |
+
margin-right: 10px;
|
| 378 |
+
}
|
| 379 |
+
</style>
|
| 380 |
+
"""
|
| 381 |
+
|
| 382 |
# Create the Gradio interface
|
| 383 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 384 |
+
# Add the CSS for the toggle
|
| 385 |
+
gr.HTML(toggle_css)
|
| 386 |
+
|
| 387 |
# State for current language
|
| 388 |
current_language = gr.State("swahili")
|
| 389 |
|
|
|
|
| 401 |
"question_placeholder": "Mfano: Je, ni hatua gani bora za kuzuia ugonjwa huu?",
|
| 402 |
"submit_btn": "Wasilisha Swali",
|
| 403 |
"clear_btn": "Futa Mazungumzo",
|
| 404 |
+
"toggle_label": "Lugha/Language:",
|
| 405 |
"error_no_image": "Tafadhali pakia picha",
|
| 406 |
"error_processing": "Hitilafu katika kuchakata picha"
|
| 407 |
},
|
|
|
|
| 417 |
"question_placeholder": "Example: What are the best preventative measures for this disease?",
|
| 418 |
"submit_btn": "Submit Question",
|
| 419 |
"clear_btn": "Clear Conversation",
|
| 420 |
+
"toggle_label": "Language/Lugha:",
|
| 421 |
"error_no_image": "Please upload an image",
|
| 422 |
"error_processing": "Error processing image"
|
| 423 |
}
|
|
|
|
| 430 |
# Store the current disease for context
|
| 431 |
current_disease = gr.State("")
|
| 432 |
|
| 433 |
+
# Add language toggle at the top
|
| 434 |
+
with gr.Row(elem_classes="language-toggle-container"):
|
| 435 |
+
gr.Markdown(text_dict["swahili"]["toggle_label"], elem_classes="toggle-label")
|
| 436 |
+
language_toggle = gr.Radio(
|
| 437 |
+
["Swahili", "English"],
|
| 438 |
+
value="Swahili",
|
| 439 |
+
label="",
|
| 440 |
+
interactive=True,
|
| 441 |
+
elem_id="language-toggle"
|
| 442 |
)
|
| 443 |
|
| 444 |
# Main identification section
|
|
|
|
| 470 |
follow_up_btn = gr.Button(text_dict["swahili"]["submit_btn"], variant="primary")
|
| 471 |
clear_btn = gr.Button(text_dict["swahili"]["clear_btn"])
|
| 472 |
|
| 473 |
+
# Function to switch language based on toggle
|
| 474 |
+
def switch_language(selected_language, current_lang):
|
| 475 |
+
# Map the radio value to our language code
|
| 476 |
+
new_lang = "swahili" if selected_language == "Swahili" else "english"
|
| 477 |
+
|
| 478 |
+
# If language hasn't changed, don't update anything
|
| 479 |
+
if new_lang == current_lang:
|
| 480 |
+
return [current_lang] + [gr.update() for _ in range(11)]
|
| 481 |
|
| 482 |
+
# Debug message to verify the language switch
|
| 483 |
print(f"Switching language from {current_lang} to {new_lang}")
|
| 484 |
|
| 485 |
+
# Return updated values
|
| 486 |
return (
|
| 487 |
new_lang,
|
| 488 |
text_dict[new_lang]["title"],
|
|
|
|
| 495 |
text_dict[new_lang]["question_label"],
|
| 496 |
text_dict[new_lang]["question_placeholder"],
|
| 497 |
text_dict[new_lang]["submit_btn"],
|
| 498 |
+
text_dict[new_lang]["clear_btn"]
|
|
|
|
| 499 |
)
|
| 500 |
|
| 501 |
# Set up event handlers
|
|
|
|
| 535 |
def clear_conversation_history():
|
| 536 |
return ""
|
| 537 |
|
| 538 |
+
# Connect the language toggle
|
| 539 |
+
language_toggle.change(
|
| 540 |
switch_language,
|
| 541 |
+
inputs=[language_toggle, current_language],
|
| 542 |
outputs=[
|
| 543 |
current_language,
|
| 544 |
title_md,
|
|
|
|
| 551 |
follow_up_input,
|
| 552 |
follow_up_input,
|
| 553 |
follow_up_btn,
|
| 554 |
+
clear_btn
|
|
|
|
| 555 |
]
|
| 556 |
)
|
| 557 |
|