Spaces:
Paused
Paused
Commit ·
eaf3f5c
1
Parent(s): 8773eb3
minors updates
Browse files- pages/main.py +27 -190
pages/main.py
CHANGED
|
@@ -57,6 +57,16 @@ from core.core import init_process_mode
|
|
| 57 |
from core.moderation import api_moderation_openai_text
|
| 58 |
from core.audio_isolation import isolate_audio
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
def hash_file(file):
|
| 61 |
hasher = hashlib.md5()
|
| 62 |
buf = file.read()
|
|
@@ -419,14 +429,17 @@ def main_page():
|
|
| 419 |
st.session_state.ui_audio_input_disabled = False
|
| 420 |
st.session_state.ui_filesuploader_disabled = False
|
| 421 |
|
| 422 |
-
|
|
|
|
|
|
|
| 423 |
# Interface utilisateur pour le chat textuel
|
| 424 |
st.session_state.user_input = st.chat_input(
|
| 425 |
get_translation("entrez_message"),
|
| 426 |
disabled=st.session_state.ui_chat_input_disabled
|
| 427 |
)
|
| 428 |
|
| 429 |
-
|
|
|
|
| 430 |
# Interface utilisateur pour l'upload de fichiers
|
| 431 |
st.session_state.uploaded_files = st.file_uploader(
|
| 432 |
"Choose files to upload",
|
|
@@ -436,7 +449,8 @@ def main_page():
|
|
| 436 |
disabled=st.session_state.ui_filesuploader_disabled
|
| 437 |
)
|
| 438 |
|
| 439 |
-
|
|
|
|
| 440 |
# Interface utilisateur pour l'enregistrement audio
|
| 441 |
st.audio_input(
|
| 442 |
"Record a voice message",
|
|
@@ -445,197 +459,20 @@ def main_page():
|
|
| 445 |
disabled=st.session_state.ui_audio_input_disabled
|
| 446 |
)
|
| 447 |
|
| 448 |
-
if st.session_state.user_input:
|
| 449 |
-
#st.session_state.ui_chat_input_disabled = True
|
| 450 |
-
#st.session_state.ui_audio_input_disabled = True
|
| 451 |
-
#st.session_state.ui_filesuploader_disabled = True
|
| 452 |
-
|
| 453 |
-
# Appeler la fonction de modération
|
| 454 |
-
moderation_result = api_moderation_openai_text(st.session_state.user_input)
|
| 455 |
-
if moderation_result.get("flagged"):
|
| 456 |
-
st.error("Votre message a été jugé inapproprié et ne peut pas être traité.")
|
| 457 |
-
return # Arrêter le traitement si le message est inapproprié
|
| 458 |
-
elif "error" in moderation_result:
|
| 459 |
-
st.error(moderation_result["error"])
|
| 460 |
-
return # Gérer les erreurs de modération
|
| 461 |
-
|
| 462 |
-
# Réinitialiser l'état précédent
|
| 463 |
-
st.session_state.full_response = ""
|
| 464 |
-
|
| 465 |
-
with st.chat_message("user", avatar="👤"):
|
| 466 |
-
st.markdown(st.session_state.user_input)
|
| 467 |
-
|
| 468 |
-
# Traitement du message texte de l'utilisateur
|
| 469 |
-
if st.session_state.language_detected is None:
|
| 470 |
-
st.session_state.language_detected = detect_language(
|
| 471 |
-
input_text = st.session_state.user_input,
|
| 472 |
-
temperature = 0.01,
|
| 473 |
-
context_window = 512,
|
| 474 |
-
model="gpt-4o"
|
| 475 |
-
)
|
| 476 |
-
|
| 477 |
-
st.session_state.audio_list = []
|
| 478 |
-
|
| 479 |
-
for cursor_selected_lang in st.session_state.selected_languages:
|
| 480 |
-
st.session_state.target_language = cursor_selected_lang["iso-639-1"]
|
| 481 |
-
target_language_name = cursor_selected_lang["language"]
|
| 482 |
-
|
| 483 |
-
# Réinitialiser les messages avant de traiter une nouvelle entrée
|
| 484 |
-
st.session_state.messages = []
|
| 485 |
-
st.session_state.full_response = ""
|
| 486 |
-
|
| 487 |
-
# Initialisation du mode de traitement pour la langue cible actuelle
|
| 488 |
-
st.session_state.system_prompt, st.session_state.operation_prompt = init_process_mode(from_lang=
|
| 489 |
-
(
|
| 490 |
-
st.session_state.language_detected if "language_detected" in st.session_state.language_detected else convert_language_name_to_iso6391(
|
| 491 |
-
st.session_state.interface_language
|
| 492 |
-
)
|
| 493 |
-
),
|
| 494 |
-
to_lang=st.session_state.target_language
|
| 495 |
-
)
|
| 496 |
-
# display error with st.error ; if (st.session_state.system_prompt, st.session_state.operation_prompt) is "", "" or None, None, and raise error
|
| 497 |
-
if (not st.session_state.system_prompt) or (not st.session_state.operation_prompt):
|
| 498 |
-
st.error("Erreur : Les prompts système ou d'opération sont vides.")
|
| 499 |
-
raise ValueError("Les prompts système ou d'opération ne peuvent pas être vides.")
|
| 500 |
-
|
| 501 |
-
with st.status(f'({target_language_name}) - {get_translation("traduction_en_cours")}', expanded=True) as response_status:
|
| 502 |
-
with st.chat_message("assistant", avatar="👻"):
|
| 503 |
-
message_placeholder = st.empty()
|
| 504 |
-
st.session_state.response_generator = process_message(
|
| 505 |
-
st.session_state.user_input,
|
| 506 |
-
st.session_state.operation_prompt,
|
| 507 |
-
st.session_state.system_prompt
|
| 508 |
-
)
|
| 509 |
-
|
| 510 |
-
response_status.update(label=f'({target_language_name}) - {get_translation("traduction_en_cours")}', state="running", expanded=True)
|
| 511 |
-
for response_chunk in st.session_state.response_generator:
|
| 512 |
-
message_placeholder.markdown(response_chunk)
|
| 513 |
-
|
| 514 |
-
st.session_state.end_response = st.session_state.response_generator.close() # Obtenir la réponse complète à la fin
|
| 515 |
-
if st.session_state.full_response != "":
|
| 516 |
-
message_placeholder.markdown(st.session_state.full_response)
|
| 517 |
-
|
| 518 |
-
if st.session_state.enable_tts_for_input_from_text_field:
|
| 519 |
-
response_status.update(label=f'({target_language_name}) - {get_translation("traduction_terminee")} ; {get_translation("synthese_vocale_en_cours")}', state="running", expanded=False)
|
| 520 |
-
st.session_state.tts_audio, st.session_state.tts_duration = process_tts_message(st.session_state.full_response)
|
| 521 |
-
del st.session_state.full_response
|
| 522 |
-
if st.session_state.tts_audio:
|
| 523 |
-
st.audio(base64.b64decode(st.session_state.tts_audio.encode()), format="audio/mp3", autoplay=False)
|
| 524 |
-
st.session_state.audio_list.append((base64.b64decode(st.session_state.tts_audio.encode()), st.session_state.tts_duration))
|
| 525 |
-
response_status.update(label=f'({target_language_name}) - {get_translation("traduction_terminee")} ; {get_translation("synthese_vocale_terminee")}', state="complete", expanded=False)
|
| 526 |
-
else:
|
| 527 |
-
response_status.update(label=f'({target_language_name}) - {get_translation("erreur_synthese_vocale")}', state="error", expanded=False)
|
| 528 |
-
|
| 529 |
-
else:
|
| 530 |
-
response_status.update(label=f'({target_language_name}) - {get_translation("traduction_terminee")}', state="complete", expanded=False)
|
| 531 |
-
else:
|
| 532 |
-
response_status.update(label=f'({target_language_name}) - {get_translation("erreur_traduction")}', state="error", expanded=False)
|
| 533 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
|
|
|
|
| 535 |
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
#st.session_state.ui_audio_input_disabled = True
|
| 539 |
-
#st.session_state.ui_filesuploader_disabled = True
|
| 540 |
-
|
| 541 |
-
with st.status(f"{get_translation('concatenation_audio_en_cours')}", expanded=False) as audio_status:
|
| 542 |
-
audio_status.update(label=f"{get_translation('concatenation_audio_en_cours')}", state="running", expanded=False)
|
| 543 |
-
try:
|
| 544 |
-
st.session_state.final_audio = concatenate_audio_files(st.session_state.audio_list)
|
| 545 |
-
with st.container(border=True):
|
| 546 |
-
|
| 547 |
-
# Générer un nom de fichier unique
|
| 548 |
-
st.session_state.timestamp = time.strftime("%Y%m%d-%H%M%S")
|
| 549 |
-
st.session_state.langues = "_".join([lang["iso-639-1"] for lang in st.session_state.selected_languages])
|
| 550 |
-
st.session_state.nom_fichier = f"reponse_audio_{st.session_state.langues}_{st.session_state.timestamp}.mp3"
|
| 551 |
-
|
| 552 |
-
st.audio(st.session_state.final_audio, format="audio/mp3", autoplay=st.session_state.autoplay_tts)
|
| 553 |
-
|
| 554 |
-
st.download_button(
|
| 555 |
-
label=f"📥 {get_translation('telecharger_audio')}",
|
| 556 |
-
data=st.session_state.final_audio,
|
| 557 |
-
file_name=st.session_state.nom_fichier,
|
| 558 |
-
mime="audio/mp3",
|
| 559 |
-
use_container_width=True,
|
| 560 |
-
type="primary",
|
| 561 |
-
key=f"download_button_{st.session_state.langues}_{st.session_state.timestamp}",
|
| 562 |
-
)
|
| 563 |
-
|
| 564 |
-
audio_status.update(label=f"{get_translation('concatenation_audio_terminee')}", state="complete", expanded=True)
|
| 565 |
-
except Exception as e:
|
| 566 |
-
st.error(f"{get_translation('erreur_concatenation_audio')} : {str(e)}")
|
| 567 |
-
|
| 568 |
-
audio_status.update(label=f"{get_translation('erreur_concatenation_audio')} : {str(e)}", state="error", expanded=True)
|
| 569 |
-
#finally:
|
| 570 |
-
# st.session_state.ui_chat_input_disabled = False
|
| 571 |
-
# st.session_state.ui_audio_input_disabled = False
|
| 572 |
-
# st.session_state.ui_filesuploader_disabled = False
|
| 573 |
-
|
| 574 |
-
if st.session_state.uploaded_files is not None:
|
| 575 |
-
#st.session_state.ui_chat_input_disabled = True
|
| 576 |
-
#st.session_state.ui_audio_input_disabled = True
|
| 577 |
-
#st.session_state.ui_filesuploader_disabled = True
|
| 578 |
-
|
| 579 |
-
new_uploaded_files_hashes = []
|
| 580 |
-
for file in st.session_state.uploaded_files:
|
| 581 |
-
uploaded_file_hash = hash_file(file)
|
| 582 |
-
new_uploaded_files_hashes.append(uploaded_file_hash)
|
| 583 |
-
if uploaded_file_hash not in st.session_state.changed_uploaded_files.keys():
|
| 584 |
-
uploaded_file_name = file.name
|
| 585 |
-
uploaded_file_type = os.path.splitext(file.name)[1].lstrip('.')
|
| 586 |
-
st.session_state.changed_uploaded_files[uploaded_file_hash] = {
|
| 587 |
-
"name": uploaded_file_name,
|
| 588 |
-
"type": uploaded_file_type,
|
| 589 |
-
"bytes_data": file.read()
|
| 590 |
-
}
|
| 591 |
-
if uploaded_file_type in ["wav", "mp3"]:
|
| 592 |
-
audio = AudioSegment.from_wav(io.BytesIO(st.session_state.changed_uploaded_files[uploaded_file_hash]["bytes_data"]))
|
| 593 |
-
with tempfile.NamedTemporaryFile(suffix=f".{uploaded_file_type}", delete=False) as tmp_file:
|
| 594 |
-
audio.export(tmp_file, format=uploaded_file_type)
|
| 595 |
-
tmp_file.close()
|
| 596 |
-
st.session_state.changed_uploaded_files[uploaded_file_hash]["audio_transcription"] = transcribe_audio(
|
| 597 |
-
filepath=tmp_file.name
|
| 598 |
-
)
|
| 599 |
-
audio = AudioSegment.empty()
|
| 600 |
-
|
| 601 |
-
changed_uploaded_files = {}
|
| 602 |
-
for file_hash in st.session_state.changed_uploaded_files.keys():
|
| 603 |
-
if file_hash in new_uploaded_files_hashes:
|
| 604 |
-
changed_uploaded_files[file_hash] = st.session_state.changed_uploaded_files[file_hash]
|
| 605 |
-
st.session_state.changed_uploaded_files = changed_uploaded_files
|
| 606 |
-
|
| 607 |
-
for key, value in st.session_state.changed_uploaded_files.items():
|
| 608 |
-
if key in new_uploaded_files_hashes:
|
| 609 |
-
with st.container(border=True):
|
| 610 |
-
if value["type"] in ["txt"]:
|
| 611 |
-
st.write(f"**filename:** {value['name']}")
|
| 612 |
-
st.write(f"**filetype:** {value['type']}")
|
| 613 |
-
file_content = value["bytes_data"].decode()
|
| 614 |
-
with st.expander("View file content"):
|
| 615 |
-
st.text_area( f"Edit text - {value["name"]}",
|
| 616 |
-
value=file_content,
|
| 617 |
-
key=value["name"],
|
| 618 |
-
on_change=callback_change_edited_text,
|
| 619 |
-
args=(key, value, value["name"])
|
| 620 |
-
)
|
| 621 |
-
elif value["type"] in ["wav", "mp3"]:
|
| 622 |
-
st.write(f"**Filename:** {value['name']}")
|
| 623 |
-
audio = AudioSegment.from_file(io.BytesIO(value["bytes_data"]))
|
| 624 |
-
st.write(f"Frame rate: {audio.frame_rate}, Frame width: {audio.frame_width}, Duration: {audio.duration_seconds} seconds")
|
| 625 |
-
st.audio(value["bytes_data"],
|
| 626 |
-
format=f"audio/{value['type']}",
|
| 627 |
-
autoplay=False
|
| 628 |
-
)
|
| 629 |
-
if "audio_transcription" in value.keys():
|
| 630 |
-
audio_file_transcription = value["audio_transcription"]
|
| 631 |
-
with st.expander("View audio transcription"):
|
| 632 |
-
st.text_area( f"Edit text - {value['name']}",
|
| 633 |
-
value=audio_file_transcription,
|
| 634 |
-
key=value["name"],
|
| 635 |
-
on_change=callback_change_edited_text,
|
| 636 |
-
args=(key, value, value["name"])
|
| 637 |
-
)
|
| 638 |
|
|
|
|
|
|
|
| 639 |
|
| 640 |
main_page()
|
| 641 |
|
|
|
|
| 57 |
from core.moderation import api_moderation_openai_text
|
| 58 |
from core.audio_isolation import isolate_audio
|
| 59 |
|
| 60 |
+
def user_message_moderation_check(user_message: Optional[str] = st.session_state.user_input if isinstance(st.session_state.user_input, str) else None):
|
| 61 |
+
# Appeler la fonction de modération
|
| 62 |
+
moderation_result = api_moderation_openai_text(user_message)
|
| 63 |
+
if moderation_result.get("flagged"):
|
| 64 |
+
st.error("Votre message a été jugé inapproprié et ne peut pas être traité.")
|
| 65 |
+
return # Arrêter le traitement si le message est inapproprié
|
| 66 |
+
elif "error" in moderation_result:
|
| 67 |
+
st.error(moderation_result["error"])
|
| 68 |
+
return # Gérer les erreurs de modération
|
| 69 |
+
|
| 70 |
def hash_file(file):
|
| 71 |
hasher = hashlib.md5()
|
| 72 |
buf = file.read()
|
|
|
|
| 429 |
st.session_state.ui_audio_input_disabled = False
|
| 430 |
st.session_state.ui_filesuploader_disabled = False
|
| 431 |
|
| 432 |
+
|
| 433 |
+
with chat_input_tabs1:
|
| 434 |
+
with st.container(border=True):
|
| 435 |
# Interface utilisateur pour le chat textuel
|
| 436 |
st.session_state.user_input = st.chat_input(
|
| 437 |
get_translation("entrez_message"),
|
| 438 |
disabled=st.session_state.ui_chat_input_disabled
|
| 439 |
)
|
| 440 |
|
| 441 |
+
with chat_input_tabs3:
|
| 442 |
+
with st.container(border=True):
|
| 443 |
# Interface utilisateur pour l'upload de fichiers
|
| 444 |
st.session_state.uploaded_files = st.file_uploader(
|
| 445 |
"Choose files to upload",
|
|
|
|
| 449 |
disabled=st.session_state.ui_filesuploader_disabled
|
| 450 |
)
|
| 451 |
|
| 452 |
+
with chat_input_tabs2:
|
| 453 |
+
with st.container(border=True):
|
| 454 |
# Interface utilisateur pour l'enregistrement audio
|
| 455 |
st.audio_input(
|
| 456 |
"Record a voice message",
|
|
|
|
| 459 |
disabled=st.session_state.ui_audio_input_disabled
|
| 460 |
)
|
| 461 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
|
| 463 |
+
with st.container(border=True):
|
| 464 |
+
if st.session_state.user_input:
|
| 465 |
+
#st.session_state.ui_chat_input_disabled = True
|
| 466 |
+
#st.session_state.ui_audio_input_disabled = True
|
| 467 |
+
#st.session_state.ui_filesuploader_disabled = True
|
| 468 |
|
| 469 |
+
user_message_moderation_check(st.session_state.user_input)
|
| 470 |
|
| 471 |
+
# Réinitialiser l'état précédent
|
| 472 |
+
st.session_state.full_response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
|
| 474 |
+
with st.chat_message("user", avatar="👤"):
|
| 475 |
+
st.markdown(st.session_state.user_input)
|
| 476 |
|
| 477 |
main_page()
|
| 478 |
|