Spaces:
Paused
Paused
rick
commited on
minors updates
Browse files- pages/main.py +137 -113
pages/main.py
CHANGED
|
@@ -3,6 +3,7 @@ import base64
|
|
| 3 |
import io
|
| 4 |
import json
|
| 5 |
import os
|
|
|
|
| 6 |
import re
|
| 7 |
import tempfile
|
| 8 |
import time
|
|
@@ -17,9 +18,12 @@ from typing import Union
|
|
| 17 |
from io import BytesIO
|
| 18 |
from copy import deepcopy
|
| 19 |
|
|
|
|
| 20 |
# Third-party libraries
|
| 21 |
import requests
|
| 22 |
import streamlit as st
|
|
|
|
|
|
|
| 23 |
#from audiorecorder import audiorecorder
|
| 24 |
from openai import OpenAI
|
| 25 |
from pydub import AudioSegment
|
|
@@ -53,6 +57,23 @@ from core.moderation import api_moderation_openai_text
|
|
| 53 |
|
| 54 |
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# Au début du fichier, après les imports
|
| 57 |
st.set_page_config(
|
| 58 |
page_title=f"DEMORRHA - (v{__version__})",
|
|
@@ -309,126 +330,130 @@ def main_page():
|
|
| 309 |
format_func=lambda lang: f"{LANGUAGES_EMOJI.get(lang, '')} {lang}"
|
| 310 |
)
|
| 311 |
|
| 312 |
-
|
| 313 |
-
#
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
)
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
# Appeler la fonction de modération
|
| 322 |
-
moderation_result = api_moderation_openai_text(st.session_state.user_input)
|
| 323 |
-
if moderation_result.get("flagged"):
|
| 324 |
-
st.error("Votre message a été jugé inapproprié et ne peut pas être traité.")
|
| 325 |
-
return # Arrêter le traitement si le message est inapproprié
|
| 326 |
-
elif "error" in moderation_result:
|
| 327 |
-
st.error(moderation_result["error"])
|
| 328 |
-
return # Gérer les erreurs de modération
|
| 329 |
-
|
| 330 |
-
# Réinitialiser l'état précédent
|
| 331 |
-
st.session_state.full_response = ""
|
| 332 |
-
|
| 333 |
-
with st.chat_message("user", avatar="👤"):
|
| 334 |
-
st.markdown(st.session_state.user_input)
|
| 335 |
-
|
| 336 |
-
# Traitement du message texte de l'utilisateur
|
| 337 |
-
if st.session_state.language_detected is None:
|
| 338 |
-
st.session_state.language_detected = detect_language(
|
| 339 |
-
input_text = st.session_state.user_input,
|
| 340 |
-
temperature = 0.01,
|
| 341 |
-
context_window = 512,
|
| 342 |
-
model="gpt-4o"
|
| 343 |
-
)
|
| 344 |
|
| 345 |
-
st.
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
)
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
|
|
|
|
|
|
|
|
|
| 368 |
|
| 369 |
-
|
| 370 |
-
with st.chat_message("assistant", avatar="👻"):
|
| 371 |
-
message_placeholder = st.empty()
|
| 372 |
-
st.session_state.response_generator = process_message(
|
| 373 |
-
st.session_state.user_input,
|
| 374 |
-
st.session_state.operation_prompt,
|
| 375 |
-
st.session_state.system_prompt
|
| 376 |
-
)
|
| 377 |
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
response_status.update(label=f'({target_language_name}) - {get_translation("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
|
|
|
|
|
|
|
| 397 |
else:
|
| 398 |
-
response_status.update(label=f'({target_language_name}) - {get_translation("
|
| 399 |
-
else:
|
| 400 |
-
response_status.update(label=f'({target_language_name}) - {get_translation("erreur_traduction")}', state="error", expanded=False)
|
| 401 |
-
|
| 402 |
-
if st.session_state.audio_list:
|
| 403 |
-
with st.status(f"{get_translation('concatenation_audio_en_cours')}", expanded=False) as audio_status:
|
| 404 |
-
audio_status.update(label=f"{get_translation('concatenation_audio_en_cours')}", state="running", expanded=False)
|
| 405 |
-
try:
|
| 406 |
-
st.session_state.final_audio = concatenate_audio_files(st.session_state.audio_list)
|
| 407 |
-
with st.container(border=True):
|
| 408 |
-
|
| 409 |
-
# Générer un nom de fichier unique
|
| 410 |
-
st.session_state.timestamp = time.strftime("%Y%m%d-%H%M%S")
|
| 411 |
-
st.session_state.langues = "_".join([lang["iso-639-1"] for lang in st.session_state.selected_languages])
|
| 412 |
-
st.session_state.nom_fichier = f"reponse_audio_{st.session_state.langues}_{st.session_state.timestamp}.mp3"
|
| 413 |
-
|
| 414 |
-
st.audio(st.session_state.final_audio, format="audio/mp3", autoplay=st.session_state.autoplay_tts)
|
| 415 |
-
|
| 416 |
-
st.download_button(
|
| 417 |
-
label=f"📥 {get_translation('telecharger_audio')}",
|
| 418 |
-
data=st.session_state.final_audio,
|
| 419 |
-
file_name=st.session_state.nom_fichier,
|
| 420 |
-
mime="audio/mp3",
|
| 421 |
-
use_container_width=True,
|
| 422 |
-
type="primary",
|
| 423 |
-
key=f"download_button_{st.session_state.langues}_{st.session_state.timestamp}",
|
| 424 |
-
)
|
| 425 |
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
|
| 430 |
-
audio_status.update(label=f"{get_translation('erreur_concatenation_audio')} : {str(e)}", state="error", expanded=True)
|
| 431 |
-
|
| 432 |
|
| 433 |
|
| 434 |
def clear_inputs_garbages(sessions_state_list: Optional[list] =
|
|
@@ -446,11 +471,10 @@ def clear_inputs_garbages(sessions_state_list: Optional[list] =
|
|
| 446 |
delete_session_state_var(it_var_name)
|
| 447 |
|
| 448 |
|
| 449 |
-
#clear_inputs_garbages()
|
| 450 |
|
|
|
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
-
main_page()
|
| 455 |
|
| 456 |
|
|
|
|
| 3 |
import io
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
+
import uuid
|
| 7 |
import re
|
| 8 |
import tempfile
|
| 9 |
import time
|
|
|
|
| 18 |
from io import BytesIO
|
| 19 |
from copy import deepcopy
|
| 20 |
|
| 21 |
+
|
| 22 |
# Third-party libraries
|
| 23 |
import requests
|
| 24 |
import streamlit as st
|
| 25 |
+
import streamlit.components.v1 as components
|
| 26 |
+
|
| 27 |
#from audiorecorder import audiorecorder
|
| 28 |
from openai import OpenAI
|
| 29 |
from pydub import AudioSegment
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
+
def save_attachment(attachment):
|
| 61 |
+
"""Sauvegarde la pièce jointe et retourne le chemin."""
|
| 62 |
+
# Créer un dossier pour les pièces jointes s'il n'existe pas
|
| 63 |
+
attachments_dir = 'attachments'
|
| 64 |
+
os.makedirs(attachments_dir, exist_ok=True)
|
| 65 |
+
|
| 66 |
+
# Générer un nom de fichier unique
|
| 67 |
+
file_extension = os.path.splitext(attachment.name)[1]
|
| 68 |
+
filename = f"{uuid.uuid4()}{file_extension}"
|
| 69 |
+
file_path = os.path.join(attachments_dir, filename)
|
| 70 |
+
|
| 71 |
+
# Sauvegarder le fichier
|
| 72 |
+
with open(file_path, 'wb') as f:
|
| 73 |
+
f.write(attachment.getbuffer())
|
| 74 |
+
|
| 75 |
+
return file_path
|
| 76 |
+
|
| 77 |
# Au début du fichier, après les imports
|
| 78 |
st.set_page_config(
|
| 79 |
page_title=f"DEMORRHA - (v{__version__})",
|
|
|
|
| 330 |
format_func=lambda lang: f"{LANGUAGES_EMOJI.get(lang, '')} {lang}"
|
| 331 |
)
|
| 332 |
|
| 333 |
+
if st.session_state.get('show_report_form', False):
|
| 334 |
+
# show_report_form()
|
| 335 |
+
pass
|
| 336 |
+
else:
|
| 337 |
+
with st.container(border=True):
|
| 338 |
+
# Interface utilisateur pour le chat textuel
|
| 339 |
+
st.session_state.user_input = st.chat_input(
|
| 340 |
+
get_translation("entrez_message")
|
| 341 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
+
st.experimental_audio_input("Record a voice message",on_change=recorder_released, key="rec_widget")
|
| 344 |
+
|
| 345 |
+
if st.session_state.user_input:
|
| 346 |
+
# Appeler la fonction de modération
|
| 347 |
+
moderation_result = api_moderation_openai_text(st.session_state.user_input)
|
| 348 |
+
if moderation_result.get("flagged"):
|
| 349 |
+
st.error("Votre message a été jugé inapproprié et ne peut pas être traité.")
|
| 350 |
+
return # Arrêter le traitement si le message est inapproprié
|
| 351 |
+
elif "error" in moderation_result:
|
| 352 |
+
st.error(moderation_result["error"])
|
| 353 |
+
return # Gérer les erreurs de modération
|
| 354 |
+
|
| 355 |
+
# Réinitialiser l'état précédent
|
| 356 |
+
st.session_state.full_response = ""
|
| 357 |
+
|
| 358 |
+
with st.chat_message("user", avatar="👤"):
|
| 359 |
+
st.markdown(st.session_state.user_input)
|
| 360 |
+
|
| 361 |
+
# Traitement du message texte de l'utilisateur
|
| 362 |
+
if st.session_state.language_detected is None:
|
| 363 |
+
st.session_state.language_detected = detect_language(
|
| 364 |
+
input_text = st.session_state.user_input,
|
| 365 |
+
temperature = 0.01,
|
| 366 |
+
context_window = 512,
|
| 367 |
+
model="gpt-4o"
|
| 368 |
+
)
|
| 369 |
|
| 370 |
+
st.session_state.audio_list = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
|
| 372 |
+
for cursor_selected_lang in st.session_state.selected_languages:
|
| 373 |
+
st.session_state.target_language = cursor_selected_lang["iso-639-1"]
|
| 374 |
+
target_language_name = cursor_selected_lang["language"]
|
| 375 |
|
| 376 |
+
# Réinitialiser les messages avant de traiter une nouvelle entrée
|
| 377 |
+
st.session_state.messages = []
|
| 378 |
+
st.session_state.full_response = ""
|
| 379 |
+
|
| 380 |
+
# Initialisation du mode de traitement pour la langue cible actuelle
|
| 381 |
+
st.session_state.system_prompt, st.session_state.operation_prompt = init_process_mode(from_lang=
|
| 382 |
+
(
|
| 383 |
+
st.session_state.language_detected if "language_detected" in st.session_state.language_detected else convert_language_name_to_iso6391(
|
| 384 |
+
st.session_state.interface_language
|
| 385 |
+
)
|
| 386 |
+
),
|
| 387 |
+
to_lang=st.session_state.target_language
|
| 388 |
+
)
|
| 389 |
+
# display error with st.error ; if (st.session_state.system_prompt, st.session_state.operation_prompt) is "", "" or None, None, and raise error
|
| 390 |
+
if (not st.session_state.system_prompt) or (not st.session_state.operation_prompt):
|
| 391 |
+
st.error("Erreur : Les prompts système ou d'opération sont vides.")
|
| 392 |
+
raise ValueError("Les prompts système ou d'opération ne peuvent pas être vides.")
|
| 393 |
+
|
| 394 |
+
with st.status(f'({target_language_name}) - {get_translation("traduction_en_cours")}', expanded=True) as response_status:
|
| 395 |
+
with st.chat_message("assistant", avatar="👻"):
|
| 396 |
+
message_placeholder = st.empty()
|
| 397 |
+
st.session_state.response_generator = process_message(
|
| 398 |
+
st.session_state.user_input,
|
| 399 |
+
st.session_state.operation_prompt,
|
| 400 |
+
st.session_state.system_prompt
|
| 401 |
+
)
|
| 402 |
|
| 403 |
+
response_status.update(label=f'({target_language_name}) - {get_translation("traduction_en_cours")}', state="running", expanded=True)
|
| 404 |
+
for response_chunk in st.session_state.response_generator:
|
| 405 |
+
message_placeholder.markdown(response_chunk)
|
| 406 |
+
|
| 407 |
+
st.session_state.end_response = st.session_state.response_generator.close() # Obtenir la réponse complète à la fin
|
| 408 |
+
if st.session_state.full_response != "":
|
| 409 |
+
message_placeholder.markdown(st.session_state.full_response)
|
| 410 |
+
|
| 411 |
+
if st.session_state.enable_tts_for_input_from_text_field:
|
| 412 |
+
response_status.update(label=f'({target_language_name}) - {get_translation("traduction_terminee")} ; {get_translation("synthese_vocale_en_cours")}', state="running", expanded=False)
|
| 413 |
+
st.session_state.tts_audio, st.session_state.tts_duration = process_tts_message(st.session_state.full_response)
|
| 414 |
+
del st.session_state.full_response
|
| 415 |
+
if st.session_state.tts_audio:
|
| 416 |
+
st.audio(base64.b64decode(st.session_state.tts_audio.encode()), format="audio/mp3", autoplay=False)
|
| 417 |
+
st.session_state.audio_list.append((base64.b64decode(st.session_state.tts_audio.encode()), st.session_state.tts_duration))
|
| 418 |
+
response_status.update(label=f'({target_language_name}) - {get_translation("traduction_terminee")} ; {get_translation("synthese_vocale_terminee")}', state="complete", expanded=False)
|
| 419 |
+
else:
|
| 420 |
+
response_status.update(label=f'({target_language_name}) - {get_translation("erreur_synthese_vocale")}', state="error", expanded=False)
|
| 421 |
|
| 422 |
+
else:
|
| 423 |
+
response_status.update(label=f'({target_language_name}) - {get_translation("traduction_terminee")}', state="complete", expanded=False)
|
| 424 |
else:
|
| 425 |
+
response_status.update(label=f'({target_language_name}) - {get_translation("erreur_traduction")}', state="error", expanded=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
|
| 427 |
+
if st.session_state.audio_list:
|
| 428 |
+
with st.status(f"{get_translation('concatenation_audio_en_cours')}", expanded=False) as audio_status:
|
| 429 |
+
audio_status.update(label=f"{get_translation('concatenation_audio_en_cours')}", state="running", expanded=False)
|
| 430 |
+
try:
|
| 431 |
+
st.session_state.final_audio = concatenate_audio_files(st.session_state.audio_list)
|
| 432 |
+
with st.container(border=True):
|
| 433 |
+
|
| 434 |
+
# Générer un nom de fichier unique
|
| 435 |
+
st.session_state.timestamp = time.strftime("%Y%m%d-%H%M%S")
|
| 436 |
+
st.session_state.langues = "_".join([lang["iso-639-1"] for lang in st.session_state.selected_languages])
|
| 437 |
+
st.session_state.nom_fichier = f"reponse_audio_{st.session_state.langues}_{st.session_state.timestamp}.mp3"
|
| 438 |
+
|
| 439 |
+
st.audio(st.session_state.final_audio, format="audio/mp3", autoplay=st.session_state.autoplay_tts)
|
| 440 |
+
|
| 441 |
+
st.download_button(
|
| 442 |
+
label=f"📥 {get_translation('telecharger_audio')}",
|
| 443 |
+
data=st.session_state.final_audio,
|
| 444 |
+
file_name=st.session_state.nom_fichier,
|
| 445 |
+
mime="audio/mp3",
|
| 446 |
+
use_container_width=True,
|
| 447 |
+
type="primary",
|
| 448 |
+
key=f"download_button_{st.session_state.langues}_{st.session_state.timestamp}",
|
| 449 |
+
)
|
| 450 |
+
|
| 451 |
+
audio_status.update(label=f"{get_translation('concatenation_audio_terminee')}", state="complete", expanded=True)
|
| 452 |
+
except Exception as e:
|
| 453 |
+
st.error(f"{get_translation('erreur_concatenation_audio')} : {str(e)}")
|
| 454 |
+
|
| 455 |
+
audio_status.update(label=f"{get_translation('erreur_concatenation_audio')} : {str(e)}", state="error", expanded=True)
|
| 456 |
|
|
|
|
|
|
|
| 457 |
|
| 458 |
|
| 459 |
def clear_inputs_garbages(sessions_state_list: Optional[list] =
|
|
|
|
| 471 |
delete_session_state_var(it_var_name)
|
| 472 |
|
| 473 |
|
|
|
|
| 474 |
|
| 475 |
+
main_page()
|
| 476 |
|
| 477 |
|
| 478 |
|
|
|
|
| 479 |
|
| 480 |
|