Spaces:
Paused
Paused
Commit
·
8773eb3
1
Parent(s):
8b9fad7
up
Browse files- pages/configuration_ui_lang.py +2 -2
- pages/main.py +144 -110
pages/configuration_ui_lang.py
CHANGED
|
@@ -74,8 +74,8 @@ translations = load_ui_language()
|
|
| 74 |
|
| 75 |
def language_selection_page():
|
| 76 |
st.set_page_config(initial_sidebar_state="collapsed")
|
| 77 |
-
st.set_page_config(page_icon="👹")
|
| 78 |
-
st.set_page_config(layout="wide")
|
| 79 |
# Fonction de rappel pour mettre à jour la langue temporaire lorsque la sélection change
|
| 80 |
def update_temp_language():
|
| 81 |
st.session_state['temp_interface_language'] = st.session_state['language_selector']
|
|
|
|
| 74 |
|
| 75 |
def language_selection_page():
|
| 76 |
st.set_page_config(initial_sidebar_state="collapsed")
|
| 77 |
+
#st.set_page_config(page_icon="👹")
|
| 78 |
+
#st.set_page_config(layout="wide")
|
| 79 |
# Fonction de rappel pour mettre à jour la langue temporaire lorsque la sélection change
|
| 80 |
def update_temp_language():
|
| 81 |
st.session_state['temp_interface_language'] = st.session_state['language_selector']
|
pages/main.py
CHANGED
|
@@ -155,6 +155,9 @@ def tts_settings(name__tts_voice,
|
|
| 155 |
|
| 156 |
@st.fragment
|
| 157 |
def recorder_released():
|
|
|
|
|
|
|
|
|
|
| 158 |
if "rec_widget" in st.session_state:
|
| 159 |
if st.session_state.rec_widget:
|
| 160 |
audio_recorded = True
|
|
@@ -285,10 +288,17 @@ def recorder_released():
|
|
| 285 |
|
| 286 |
except Exception as e:
|
| 287 |
st.error(f"[AUDIO] - {get_translation('erreur_importation_audio')}: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
def main_page():
|
| 290 |
"""Page principale de l'application."""
|
| 291 |
|
|
|
|
|
|
|
|
|
|
| 292 |
if "ui_chat_input_disabled" not in st.session_state:
|
| 293 |
st.session_state.ui_chat_input_disabled = False
|
| 294 |
|
|
@@ -402,93 +412,44 @@ def main_page():
|
|
| 402 |
pass
|
| 403 |
else:
|
| 404 |
with st.container(border=True):
|
| 405 |
-
# Interface utilisateur pour le chat textuel
|
| 406 |
-
st.session_state.user_input = st.chat_input(
|
| 407 |
-
get_translation("entrez_message"),
|
| 408 |
-
disabled=st.session_state.ui_chat_input_disabled
|
| 409 |
-
)
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
# Interface utilisateur pour l'upload de fichiers
|
| 413 |
-
st.session_state.uploaded_files = st.file_uploader(
|
| 414 |
-
"Choose files to upload",
|
| 415 |
-
accept_multiple_files=True,
|
| 416 |
-
type=CHAT_FILES_UPLOAD_ALLOWED_TYPES,
|
| 417 |
-
key="chat_files_upload",
|
| 418 |
-
disabled=st.session_state.ui_filesuploader_disabled
|
| 419 |
-
)
|
| 420 |
-
|
| 421 |
-
if st.session_state.uploaded_files is not None:
|
| 422 |
-
new_uploaded_files_hashes = []
|
| 423 |
-
for file in st.session_state.uploaded_files:
|
| 424 |
-
uploaded_file_hash = hash_file(file)
|
| 425 |
-
new_uploaded_files_hashes.append(uploaded_file_hash)
|
| 426 |
-
if uploaded_file_hash not in st.session_state.changed_uploaded_files.keys():
|
| 427 |
-
uploaded_file_name = file.name
|
| 428 |
-
uploaded_file_type = os.path.splitext(file.name)[1].lstrip('.')
|
| 429 |
-
st.session_state.changed_uploaded_files[uploaded_file_hash] = {
|
| 430 |
-
"name": uploaded_file_name,
|
| 431 |
-
"type": uploaded_file_type,
|
| 432 |
-
"bytes_data": file.read()
|
| 433 |
-
}
|
| 434 |
-
if uploaded_file_type in ["wav", "mp3"]:
|
| 435 |
-
audio = AudioSegment.from_wav(io.BytesIO(st.session_state.changed_uploaded_files[uploaded_file_hash]["bytes_data"]))
|
| 436 |
-
with tempfile.NamedTemporaryFile(suffix=f".{uploaded_file_type}", delete=False) as tmp_file:
|
| 437 |
-
audio.export(tmp_file, format=uploaded_file_type)
|
| 438 |
-
tmp_file.close()
|
| 439 |
-
st.session_state.changed_uploaded_files[uploaded_file_hash]["audio_transcription"] = transcribe_audio(
|
| 440 |
-
filepath=tmp_file.name
|
| 441 |
-
)
|
| 442 |
-
audio = AudioSegment.empty()
|
| 443 |
-
|
| 444 |
-
changed_uploaded_files = {}
|
| 445 |
-
for file_hash in st.session_state.changed_uploaded_files.keys():
|
| 446 |
-
if file_hash in new_uploaded_files_hashes:
|
| 447 |
-
changed_uploaded_files[file_hash] = st.session_state.changed_uploaded_files[file_hash]
|
| 448 |
-
st.session_state.changed_uploaded_files = changed_uploaded_files
|
| 449 |
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
args=(key, value, value["name"])
|
| 463 |
-
)
|
| 464 |
-
elif value["type"] in ["wav", "mp3"]:
|
| 465 |
-
st.write(f"**Filename:** {value['name']}")
|
| 466 |
-
audio = AudioSegment.from_file(io.BytesIO(value["bytes_data"]))
|
| 467 |
-
st.write(f"Frame rate: {audio.frame_rate}, Frame width: {audio.frame_width}, Duration: {audio.duration_seconds} seconds")
|
| 468 |
-
st.audio(value["bytes_data"],
|
| 469 |
-
format=f"audio/{value['type']}",
|
| 470 |
-
autoplay=False
|
| 471 |
-
)
|
| 472 |
-
if "audio_transcription" in value.keys():
|
| 473 |
-
audio_file_transcription = value["audio_transcription"]
|
| 474 |
-
with st.expander("View audio transcription"):
|
| 475 |
-
st.text_area( f"Edit text - {value['name']}",
|
| 476 |
-
value=audio_file_transcription,
|
| 477 |
-
key=value["name"],
|
| 478 |
-
on_change=callback_change_edited_text,
|
| 479 |
-
args=(key, value, value["name"])
|
| 480 |
-
)
|
| 481 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 482 |
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
|
|
|
| 490 |
|
| 491 |
if st.session_state.user_input:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
# Appeler la fonction de modération
|
| 493 |
moderation_result = api_moderation_openai_text(st.session_state.user_input)
|
| 494 |
if moderation_result.get("flagged"):
|
|
@@ -570,37 +531,110 @@ def main_page():
|
|
| 570 |
else:
|
| 571 |
response_status.update(label=f'({target_language_name}) - {get_translation("erreur_traduction")}', state="error", expanded=False)
|
| 572 |
|
| 573 |
-
if st.session_state.audio_list:
|
| 574 |
-
with st.status(f"{get_translation('concatenation_audio_en_cours')}", expanded=False) as audio_status:
|
| 575 |
-
audio_status.update(label=f"{get_translation('concatenation_audio_en_cours')}", state="running", expanded=False)
|
| 576 |
-
try:
|
| 577 |
-
st.session_state.final_audio = concatenate_audio_files(st.session_state.audio_list)
|
| 578 |
-
with st.container(border=True):
|
| 579 |
-
|
| 580 |
-
# Générer un nom de fichier unique
|
| 581 |
-
st.session_state.timestamp = time.strftime("%Y%m%d-%H%M%S")
|
| 582 |
-
st.session_state.langues = "_".join([lang["iso-639-1"] for lang in st.session_state.selected_languages])
|
| 583 |
-
st.session_state.nom_fichier = f"reponse_audio_{st.session_state.langues}_{st.session_state.timestamp}.mp3"
|
| 584 |
-
|
| 585 |
-
st.audio(st.session_state.final_audio, format="audio/mp3", autoplay=st.session_state.autoplay_tts)
|
| 586 |
-
|
| 587 |
-
st.download_button(
|
| 588 |
-
label=f"📥 {get_translation('telecharger_audio')}",
|
| 589 |
-
data=st.session_state.final_audio,
|
| 590 |
-
file_name=st.session_state.nom_fichier,
|
| 591 |
-
mime="audio/mp3",
|
| 592 |
-
use_container_width=True,
|
| 593 |
-
type="primary",
|
| 594 |
-
key=f"download_button_{st.session_state.langues}_{st.session_state.timestamp}",
|
| 595 |
-
)
|
| 596 |
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 602 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 603 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 604 |
|
| 605 |
|
| 606 |
main_page()
|
|
|
|
| 155 |
|
| 156 |
@st.fragment
|
| 157 |
def recorder_released():
|
| 158 |
+
if "audio_list" not in st.session_state:
|
| 159 |
+
st.session_state.audio_list = []
|
| 160 |
+
|
| 161 |
if "rec_widget" in st.session_state:
|
| 162 |
if st.session_state.rec_widget:
|
| 163 |
audio_recorded = True
|
|
|
|
| 288 |
|
| 289 |
except Exception as e:
|
| 290 |
st.error(f"[AUDIO] - {get_translation('erreur_importation_audio')}: {str(e)}")
|
| 291 |
+
#finally:
|
| 292 |
+
# st.session_state.ui_chat_input_disabled = False
|
| 293 |
+
# st.session_state.ui_audio_input_disabled = False
|
| 294 |
+
# st.session_state.ui_filesuploader_disabled = False
|
| 295 |
|
| 296 |
def main_page():
|
| 297 |
"""Page principale de l'application."""
|
| 298 |
|
| 299 |
+
if "audio_list" not in st.session_state:
|
| 300 |
+
st.session_state.audio_list = []
|
| 301 |
+
|
| 302 |
if "ui_chat_input_disabled" not in st.session_state:
|
| 303 |
st.session_state.ui_chat_input_disabled = False
|
| 304 |
|
|
|
|
| 412 |
pass
|
| 413 |
else:
|
| 414 |
with st.container(border=True):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
|
| 416 |
+
chat_input_tabs1, chat_input_tabs2, chat_input_tabs3 = st.tabs(["text_input", "audio_input", "file_upload_input"])
|
| 417 |
+
|
| 418 |
+
st.session_state.ui_chat_input_disabled = False
|
| 419 |
+
st.session_state.ui_audio_input_disabled = False
|
| 420 |
+
st.session_state.ui_filesuploader_disabled = False
|
| 421 |
+
|
| 422 |
+
with chat_input_tabs1:
|
| 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 |
+
with chat_input_tabs3:
|
| 430 |
+
# Interface utilisateur pour l'upload de fichiers
|
| 431 |
+
st.session_state.uploaded_files = st.file_uploader(
|
| 432 |
+
"Choose files to upload",
|
| 433 |
+
accept_multiple_files=True,
|
| 434 |
+
type=CHAT_FILES_UPLOAD_ALLOWED_TYPES,
|
| 435 |
+
key="chat_files_upload",
|
| 436 |
+
disabled=st.session_state.ui_filesuploader_disabled
|
| 437 |
+
)
|
| 438 |
|
| 439 |
+
with chat_input_tabs2:
|
| 440 |
+
# Interface utilisateur pour l'enregistrement audio
|
| 441 |
+
st.audio_input(
|
| 442 |
+
"Record a voice message",
|
| 443 |
+
on_change=recorder_released,
|
| 444 |
+
key="rec_widget",
|
| 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"):
|
|
|
|
| 531 |
else:
|
| 532 |
response_status.update(label=f'({target_language_name}) - {get_translation("erreur_traduction")}', state="error", expanded=False)
|
| 533 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
|
| 535 |
+
|
| 536 |
+
if st.session_state.audio_list:
|
| 537 |
+
#st.session_state.ui_chat_input_disabled = True
|
| 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()
|