Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -120,6 +120,43 @@ def process_speech(input_language, audio_input):
|
|
| 120 |
except Exception as e :
|
| 121 |
return f"{e}"
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
def save_image(image_input, output_dir="saved_images"):
|
| 124 |
if not os.path.exists(output_dir):
|
| 125 |
os.makedirs(output_dir)
|
|
@@ -376,6 +413,10 @@ def process_and_query(input_language=None, audio_input=None, image_input=None, t
|
|
| 376 |
sources_info = vectara_response.get('sources', [])
|
| 377 |
|
| 378 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
# Format Vectara response in Markdown
|
| 380 |
markdown_output = "### Vectara Response Summary\n"
|
| 381 |
markdown_output += f"* **Summary**: {summary}\n"
|
|
@@ -383,9 +424,15 @@ def process_and_query(input_language=None, audio_input=None, image_input=None, t
|
|
| 383 |
for source in sources_info:
|
| 384 |
markdown_output += f"* {source}\n"
|
| 385 |
|
| 386 |
-
# Process the summary with
|
| 387 |
final_response = process_summary_with_stablemed(summary)
|
| 388 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
# Evaluate hallucination
|
| 390 |
hallucination_label = evaluate_hallucination(final_response, summary)
|
| 391 |
|
|
@@ -395,10 +442,10 @@ def process_and_query(input_language=None, audio_input=None, image_input=None, t
|
|
| 395 |
markdown_output += "\n### Hallucination Evaluation\n"
|
| 396 |
markdown_output += f"* **Label**: {hallucination_label}\n"
|
| 397 |
|
| 398 |
-
return markdown_output
|
| 399 |
|
| 400 |
except Exception as e:
|
| 401 |
-
return f"Error occurred during processing: {e}. No hallucination evaluation."
|
| 402 |
|
| 403 |
|
| 404 |
|
|
@@ -524,9 +571,10 @@ languages = [
|
|
| 524 |
"Zulu"
|
| 525 |
]
|
| 526 |
|
|
|
|
| 527 |
def clear():
|
| 528 |
-
# Return default values
|
| 529 |
-
return "English", None, None, "",
|
| 530 |
|
| 531 |
|
| 532 |
|
|
@@ -550,23 +598,26 @@ def create_interface():
|
|
| 550 |
|
| 551 |
with gr.Accordion("MultiMed", open=False) as multimend_accordion:
|
| 552 |
text_input = gr.Textbox(label="Use Text", lines=3, placeholder="I have had a sore throat and phlegm for a few days and now my cough has gotten worse!")
|
| 553 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 554 |
|
| 555 |
-
text_button = gr.Button("Use MultiMed")
|
| 556 |
-
text_button.click(process_and_query, inputs=[input_language, audio_input, image_input, text_input], outputs=[text_output])
|
| 557 |
-
gr.Examples([
|
| 558 |
-
["What is the proper treatment for buccal herpes?"],
|
| 559 |
-
["Male, 40 presenting with swollen glands and a rash"],
|
| 560 |
-
["How does cellular metabolism work TCA cycle"],
|
| 561 |
-
["What special care must be provided to children with chicken pox?"],
|
| 562 |
-
["When and how often should I wash my hands?"],
|
| 563 |
-
["بکل ہرپس کا صحیح علاج کیا ہے؟"],
|
| 564 |
-
["구강 헤르페스의 적절한 치료법은 무엇입니까?"],
|
| 565 |
-
["Je, ni matibabu gani sahihi kwa herpes ya buccal?"],
|
| 566 |
-
],inputs=[text_input])
|
| 567 |
|
|
|
|
|
|
|
|
|
|
| 568 |
clear_button = gr.Button("Clear")
|
| 569 |
-
clear_button.click(clear, inputs=[], outputs=[input_language, audio_input, image_input,
|
| 570 |
|
| 571 |
return iface
|
| 572 |
|
|
|
|
| 120 |
except Exception as e :
|
| 121 |
return f"{e}"
|
| 122 |
|
| 123 |
+
def translate_text(input_text, input_language):
|
| 124 |
+
"""
|
| 125 |
+
Translate text from one language to another.
|
| 126 |
+
"""
|
| 127 |
+
try:
|
| 128 |
+
text_translation_result = client.predict(
|
| 129 |
+
task="T2TT (Text to Text translation)",
|
| 130 |
+
input_text=input_text,
|
| 131 |
+
source_language=Auto-detect, # Auto-detect or specified language
|
| 132 |
+
target_language=input_language,
|
| 133 |
+
api_name="/run_text_translation" # Hypothetical API endpoint for text translation
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
translated_text = text_translation_result['translated_text']
|
| 137 |
+
return translated_text
|
| 138 |
+
except Exception as e:
|
| 139 |
+
return f"An error occurred during translation: {e}"
|
| 140 |
+
|
| 141 |
+
def convert_text_to_speech(input_text, input_language):
|
| 142 |
+
"""
|
| 143 |
+
Convert text to speech in the specified language.
|
| 144 |
+
"""
|
| 145 |
+
try:
|
| 146 |
+
text_to_speech_result = client.predict(
|
| 147 |
+
task="T2ST (Text to Speech translation)",
|
| 148 |
+
input_text=input_text,
|
| 149 |
+
language=input_language,
|
| 150 |
+
api_name="/run_text_to_speech" # Hypothetical API endpoint for text-to-speech
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
# Assuming the API returns a file path or similar identifier for the audio
|
| 154 |
+
audio_file = text_to_speech_result['audio_file']
|
| 155 |
+
return audio_file
|
| 156 |
+
except Exception as e:
|
| 157 |
+
return f"An error occurred during text-to-speech conversion: {e}"
|
| 158 |
+
|
| 159 |
+
|
| 160 |
def save_image(image_input, output_dir="saved_images"):
|
| 161 |
if not os.path.exists(output_dir):
|
| 162 |
os.makedirs(output_dir)
|
|
|
|
| 413 |
sources_info = vectara_response.get('sources', [])
|
| 414 |
|
| 415 |
|
| 416 |
+
# Convert translated text to speech
|
| 417 |
+
audio_file_path = convert_text_to_speech(translated_response, input_language)
|
| 418 |
+
|
| 419 |
+
|
| 420 |
# Format Vectara response in Markdown
|
| 421 |
markdown_output = "### Vectara Response Summary\n"
|
| 422 |
markdown_output += f"* **Summary**: {summary}\n"
|
|
|
|
| 424 |
for source in sources_info:
|
| 425 |
markdown_output += f"* {source}\n"
|
| 426 |
|
| 427 |
+
# Process the summary with Stablemed
|
| 428 |
final_response = process_summary_with_stablemed(summary)
|
| 429 |
|
| 430 |
+
# Translate the final response
|
| 431 |
+
translated_response = translate_text(final_response, input_language)
|
| 432 |
+
|
| 433 |
+
# Convert translated text to speech
|
| 434 |
+
audio_file_path = convert_text_to_speech(translated_response, input_language)
|
| 435 |
+
|
| 436 |
# Evaluate hallucination
|
| 437 |
hallucination_label = evaluate_hallucination(final_response, summary)
|
| 438 |
|
|
|
|
| 442 |
markdown_output += "\n### Hallucination Evaluation\n"
|
| 443 |
markdown_output += f"* **Label**: {hallucination_label}\n"
|
| 444 |
|
| 445 |
+
return markdown_output, audio_file_path
|
| 446 |
|
| 447 |
except Exception as e:
|
| 448 |
+
return f"Error occurred during processing: {e}. No hallucination evaluation.", None
|
| 449 |
|
| 450 |
|
| 451 |
|
|
|
|
| 571 |
"Zulu"
|
| 572 |
]
|
| 573 |
|
| 574 |
+
|
| 575 |
def clear():
|
| 576 |
+
# Return default values for each component
|
| 577 |
+
return "English", None, None, "", None
|
| 578 |
|
| 579 |
|
| 580 |
|
|
|
|
| 598 |
|
| 599 |
with gr.Accordion("MultiMed", open=False) as multimend_accordion:
|
| 600 |
text_input = gr.Textbox(label="Use Text", lines=3, placeholder="I have had a sore throat and phlegm for a few days and now my cough has gotten worse!")
|
| 601 |
+
|
| 602 |
+
gr.Examples([
|
| 603 |
+
["What is the proper treatment for buccal herpes?"],
|
| 604 |
+
["I have had a sore throat and hoarse voice for serveral days and now a strong cough recently "],
|
| 605 |
+
["How does cellular metabolism work TCA cycle"],
|
| 606 |
+
["What special care must be provided to children with chicken pox?"],
|
| 607 |
+
["When and how often should I wash my hands?"],
|
| 608 |
+
["بکل ہرپس کا صحیح علاج کیا ہے؟"],
|
| 609 |
+
["구강 헤르페스의 적절한 치료법은 무엇입니까?"],
|
| 610 |
+
["Je, ni matibabu gani sahihi kwa herpes ya buccal?"],
|
| 611 |
+
],inputs=[text_input])
|
| 612 |
+
text_output = gr.Markdown(label="MultiMed") # Markdown component for text
|
| 613 |
+
audio_output = gr.Audio(label="Audio Out", type="filepath")
|
| 614 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
|
| 616 |
+
text_button = gr.Button("Use MultiMed")
|
| 617 |
+
text_button.click(process_and_query, inputs=[input_language, audio_input, image_input, text_input], outputs=[text_output, audio_output])
|
| 618 |
+
|
| 619 |
clear_button = gr.Button("Clear")
|
| 620 |
+
clear_button.click(clear, inputs=[], outputs=[input_language, audio_input, image_input, text_output, audio_output])
|
| 621 |
|
| 622 |
return iface
|
| 623 |
|