rick commited on
Commit
90e5cc8
·
unverified ·
1 Parent(s): dab5221

bug fixed now... not done...

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. pages/main.py +115 -120
app.py CHANGED
@@ -20,7 +20,7 @@ import streamlit as st
20
 
21
 
22
 
23
- __version__ = "1.2.2"
24
 
25
  # Au début du fichier, après les imports
26
  st.set_page_config(
 
20
 
21
 
22
 
23
+ __version__ = "1.2.3"
24
 
25
  # Au début du fichier, après les imports
26
  st.set_page_config(
pages/main.py CHANGED
@@ -24,7 +24,7 @@ from openai import OpenAI
24
  from pydub import AudioSegment
25
 
26
 
27
- __version__ = "1.2.2"
28
 
29
  # Au début du fichier, après les imports
30
  st.set_page_config(
@@ -578,140 +578,122 @@ def main_page():
578
  if "messages" not in st.session_state:
579
  st.session_state.messages = []
580
 
581
- # Interface utilisateur pour le chat textuel
582
- if user_input := st.chat_input(get_translation("entrez_message")):
583
-
584
- # Réinitialiser l'état précédent
585
- st.session_state.full_response = ""
586
-
587
- with st.chat_message("user", avatar="👤"):
588
- st.markdown(user_input)
589
 
590
- # Traitement du message texte de l'utilisateur
591
- if st.session_state.language_detected is None:
592
- st.session_state.language_detected = detect_language(
593
- input_text=user_input, temperature=0.01
594
- )
595
-
596
- audio_list = []
597
 
598
- for cursor_selected_lang in st.session_state.selected_languages:
599
- st.session_state.target_language = cursor_selected_lang["iso-639-1"]
600
- target_language_name = cursor_selected_lang["language"]
601
 
602
- # Réinitialiser les messages avant de traiter une nouvelle entrée
603
- st.session_state.messages = []
604
- st.session_state.full_response = ""
605
-
606
- # Initialisation du mode de traitement pour la langue cible actuelle
607
- system_prompt, operation_prompt = init_process_mode(from_lang=
608
- (
609
- st.session_state.language_detected if "language_detected" in st.session_state.language_detected else convert_language_name_to_iso6391(
610
- st.session_state.interface_language
611
- )
612
- ),
613
- to_lang=st.session_state.target_language
614
- )
615
- # display error with st.error ; if (system_prompt, operation_prompt) is "", "" or None, None, and raise error
616
- if not system_prompt or not operation_prompt:
617
- st.error("Erreur : Les prompts système ou d'opération sont vides.")
618
- raise ValueError("Les prompts système ou d'opération ne peuvent pas être vides.")
619
-
620
 
621
- with st.status(f'({target_language_name}) - {get_translation("traduction_en_cours")}', expanded=True) as response_status:
622
- with st.chat_message("assistant", avatar="👻"):
623
- message_placeholder = st.empty()
624
- response_generator = process_message(
625
- user_input, operation_prompt,
626
- st.session_state.enable_tts_for_input_from_text_field,
627
- system_prompt=system_prompt
628
- )
629
-
630
-
631
- response_status.update(label=f'({target_language_name}) - {get_translation("traduction_en_cours")}', state="running", expanded=True)
632
- for response_chunk in response_generator:
633
- message_placeholder.markdown(response_chunk)
634
-
635
- end_response = response_generator.close() # Obtenir la réponse complète à la fin
636
- if st.session_state.full_response != "":
637
- message_placeholder.markdown(st.session_state.full_response)
638
-
639
- if st.session_state.enable_tts_for_input_from_text_field:
640
- response_status.update(label=f'({target_language_name}) - {get_translation("traduction_terminee")} ; {get_translation("synthese_vocale_en_cours")}', state="running", expanded=False)
641
- tts_audio, tts_duration = process_tts_message(st.session_state.full_response)
642
- del st.session_state.full_response
643
- if tts_audio:
644
- st.audio(tts_audio, format="audio/mp3", autoplay=False)
645
- audio_list.append((tts_audio, tts_duration))
646
- del tts_audio
647
- del tts_duration
648
- response_status.update(label=f'({target_language_name}) - {get_translation("traduction_terminee")} ; {get_translation("synthese_vocale_terminee")}', state="complete", expanded=False)
649
- else:
650
- response_status.update(label=f'({target_language_name}) - {get_translation("erreur_synthese_vocale")}', state="error", expanded=False)
651
-
652
-
653
- else:
654
- response_status.update(label=f'({target_language_name}) - {get_translation("traduction_terminee")}', state="complete", expanded=False)
655
- else:
656
- response_status.update(label=f'({target_language_name}) - {get_translation("erreur_traduction")}', state="error", expanded=False)
657
-
658
-
659
- if audio_list:
660
- with st.status(f"{get_translation('concatenation_audio_en_cours')}", expanded=False) as audio_status:
661
- audio_status.update(label=f"{get_translation('concatenation_audio_en_cours')}", state="running", expanded=False)
662
- try:
663
- final_audio = concatenate_audio_files(audio_list)
664
- with st.container(border=True):
665
-
666
- # Générer un nom de fichier unique
667
- timestamp = time.strftime("%Y%m%d-%H%M%S")
668
- langues = "_".join([lang["iso-639-1"] for lang in st.session_state.selected_languages])
669
- nom_fichier = f"reponse_audio_{langues}_{timestamp}.mp3"
670
-
671
 
672
- st.audio(final_audio, format="audio/mp3", autoplay=st.session_state.autoplay_tts)
 
 
673
 
674
- st.download_button(
675
- label=f"📥 {get_translation('telecharger_audio')}",
676
- data=final_audio,
677
- file_name=nom_fichier,
678
- mime="audio/mp3",
679
- use_container_width=True,
680
- type="primary",
681
- key=f"download_button_{langues}_{timestamp}",
 
682
  )
683
-
684
- # ##
685
- audio_status.update(label=f"{get_translation('concatenation_audio_terminee')}", state="complete", expanded=True)
686
- except Exception as e:
687
- st.error(f"{get_translation('erreur_concatenation_audio')} : {str(e)}")
688
-
689
- # ##
690
- audio_status.update(label=f"{get_translation('erreur_concatenation_audio')} : {str(e)}", state="error", expanded=True)
691
 
692
- # check if `final_audio` var exists , if exists delete final_audio
693
- if 'final_audio' in locals():
694
- del final_audio
695
-
696
- if 'audio_list' in locals():
697
- del audio_list
 
 
 
698
 
699
- if 'system_prompt' in locals():
700
- del system_prompt
701
 
702
- if 'operation_prompt' in locals():
703
- del operation_prompt
 
704
 
705
- if 'user_input' in locals():
706
- del user_input
 
707
 
 
 
 
 
 
 
 
 
 
 
 
 
708
 
709
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
710
 
711
- with st.container(border=True):
712
- # Interface utilisateur pour l'enregistrement audio
713
- st.write(f"🗣️ {get_translation('enregistrez_message')}")
714
- if audio := audiorecorder(
 
 
 
 
 
 
 
 
715
  start_prompt=get_translation("cliquez_enregistrer"),
716
  stop_prompt=get_translation("cliquez_arreter"),
717
  pause_prompt=get_translation("cliquez_pause"),
@@ -802,6 +784,19 @@ def main_page():
802
  key=f"download_button_{langues}_{timestamp}",
803
  )
804
 
 
 
 
 
 
 
 
 
 
 
 
 
 
805
  if 'transcription' in locals():
806
  del transcription
807
  if 'operation_prompt' in locals():
 
24
  from pydub import AudioSegment
25
 
26
 
27
+ __version__ = "1.2.3"
28
 
29
  # Au début du fichier, après les imports
30
  st.set_page_config(
 
578
  if "messages" not in st.session_state:
579
  st.session_state.messages = []
580
 
581
+ with st.container(border=True):
582
+ # Interface utilisateur pour le chat textuel
583
+ if user_input := st.chat_input(get_translation("entrez_message")):
 
 
 
 
 
584
 
585
+ # Réinitialiser l'état précédent
586
+ st.session_state.full_response = ""
 
 
 
 
 
587
 
588
+ with st.chat_message("user", avatar="👤"):
589
+ st.markdown(user_input)
 
590
 
591
+ # Traitement du message texte de l'utilisateur
592
+ if st.session_state.language_detected is None:
593
+ st.session_state.language_detected = detect_language(
594
+ input_text=user_input, temperature=0.01
595
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
596
 
597
+ audio_list = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
598
 
599
+ for cursor_selected_lang in st.session_state.selected_languages:
600
+ st.session_state.target_language = cursor_selected_lang["iso-639-1"]
601
+ target_language_name = cursor_selected_lang["language"]
602
 
603
+ # Réinitialiser les messages avant de traiter une nouvelle entrée
604
+ st.session_state.messages = []
605
+ st.session_state.full_response = ""
606
+
607
+ # Initialisation du mode de traitement pour la langue cible actuelle
608
+ system_prompt, operation_prompt = init_process_mode(from_lang=
609
+ (
610
+ st.session_state.language_detected if "language_detected" in st.session_state.language_detected else convert_language_name_to_iso6391(
611
+ st.session_state.interface_language
612
  )
613
+ ),
614
+ to_lang=st.session_state.target_language
615
+ )
616
+ # display error with st.error ; if (system_prompt, operation_prompt) is "", "" or None, None, and raise error
617
+ if not system_prompt or not operation_prompt:
618
+ st.error("Erreur : Les prompts système ou d'opération sont vides.")
619
+ raise ValueError("Les prompts système ou d'opération ne peuvent pas être vides.")
 
620
 
621
+
622
+ with st.status(f'({target_language_name}) - {get_translation("traduction_en_cours")}', expanded=True) as response_status:
623
+ with st.chat_message("assistant", avatar="👻"):
624
+ message_placeholder = st.empty()
625
+ response_generator = process_message(
626
+ user_input, operation_prompt,
627
+ st.session_state.enable_tts_for_input_from_text_field,
628
+ system_prompt=system_prompt
629
+ )
630
 
 
 
631
 
632
+ response_status.update(label=f'({target_language_name}) - {get_translation("traduction_en_cours")}', state="running", expanded=True)
633
+ for response_chunk in response_generator:
634
+ message_placeholder.markdown(response_chunk)
635
 
636
+ end_response = response_generator.close() # Obtenir la réponse complète à la fin
637
+ if st.session_state.full_response != "":
638
+ message_placeholder.markdown(st.session_state.full_response)
639
 
640
+ if st.session_state.enable_tts_for_input_from_text_field:
641
+ response_status.update(label=f'({target_language_name}) - {get_translation("traduction_terminee")} ; {get_translation("synthese_vocale_en_cours")}', state="running", expanded=False)
642
+ tts_audio, tts_duration = process_tts_message(st.session_state.full_response)
643
+ del st.session_state.full_response
644
+ if tts_audio:
645
+ st.audio(tts_audio, format="audio/mp3", autoplay=False)
646
+ audio_list.append((tts_audio, tts_duration))
647
+ del tts_audio
648
+ del tts_duration
649
+ response_status.update(label=f'({target_language_name}) - {get_translation("traduction_terminee")} ; {get_translation("synthese_vocale_terminee")}', state="complete", expanded=False)
650
+ else:
651
+ response_status.update(label=f'({target_language_name}) - {get_translation("erreur_synthese_vocale")}', state="error", expanded=False)
652
 
653
 
654
+ else:
655
+ response_status.update(label=f'({target_language_name}) - {get_translation("traduction_terminee")}', state="complete", expanded=False)
656
+ else:
657
+ response_status.update(label=f'({target_language_name}) - {get_translation("erreur_traduction")}', state="error", expanded=False)
658
+
659
+
660
+ if audio_list:
661
+ with st.status(f"{get_translation('concatenation_audio_en_cours')}", expanded=False) as audio_status:
662
+ audio_status.update(label=f"{get_translation('concatenation_audio_en_cours')}", state="running", expanded=False)
663
+ try:
664
+ final_audio = concatenate_audio_files(audio_list)
665
+ with st.container(border=True):
666
+
667
+ # Générer un nom de fichier unique
668
+ timestamp = time.strftime("%Y%m%d-%H%M%S")
669
+ langues = "_".join([lang["iso-639-1"] for lang in st.session_state.selected_languages])
670
+ nom_fichier = f"reponse_audio_{langues}_{timestamp}.mp3"
671
+
672
+
673
+ st.audio(final_audio, format="audio/mp3", autoplay=st.session_state.autoplay_tts)
674
+
675
+ st.download_button(
676
+ label=f"📥 {get_translation('telecharger_audio')}",
677
+ data=final_audio,
678
+ file_name=nom_fichier,
679
+ mime="audio/mp3",
680
+ use_container_width=True,
681
+ type="primary",
682
+ key=f"download_button_{langues}_{timestamp}",
683
+ )
684
 
685
+ # ##
686
+ audio_status.update(label=f"{get_translation('concatenation_audio_terminee')}", state="complete", expanded=True)
687
+ except Exception as e:
688
+ st.error(f"{get_translation('erreur_concatenation_audio')} : {str(e)}")
689
+
690
+ # ##
691
+ audio_status.update(label=f"{get_translation('erreur_concatenation_audio')} : {str(e)}", state="error", expanded=True)
692
+
693
+
694
+ # Interface utilisateur pour l'enregistrement audio
695
+ # st.write(f"🗣️ {get_translation('enregistrez_message')}")
696
+ elif audio := audiorecorder(
697
  start_prompt=get_translation("cliquez_enregistrer"),
698
  stop_prompt=get_translation("cliquez_arreter"),
699
  pause_prompt=get_translation("cliquez_pause"),
 
784
  key=f"download_button_{langues}_{timestamp}",
785
  )
786
 
787
+
788
+ # check if `final_audio` var exists , if exists delete final_audio
789
+ #if 'final_audio' in locals():
790
+ # del final_audio
791
+ #if 'audio_list' in locals():
792
+ # del audio_list
793
+ #if 'system_prompt' in locals():
794
+ # del system_prompt
795
+ #if 'operation_prompt' in locals():
796
+ # del operation_prompt
797
+
798
+ if 'user_input' in locals():
799
+ del user_input
800
  if 'transcription' in locals():
801
  del transcription
802
  if 'operation_prompt' in locals():