Spaces:
Paused
Paused
rick
commited on
bug fixed now... not done...
Browse files- pages/main.py +59 -49
pages/main.py
CHANGED
|
@@ -341,28 +341,28 @@ def process_message(
|
|
| 341 |
{"role": "system", "content": system_prompt},
|
| 342 |
{"role": "user", "content": payload_content}
|
| 343 |
]
|
| 344 |
-
full_response = ""
|
| 345 |
try:
|
| 346 |
for response in client.chat.completions.create(
|
| 347 |
model="gpt-4o-mini",
|
| 348 |
messages=st.session_state.messages,
|
| 349 |
stream=True,
|
| 350 |
temperature=0.1):
|
| 351 |
-
full_response += (response.choices[0].delta.content or "")
|
| 352 |
-
yield full_response + "▌"
|
| 353 |
|
| 354 |
# Utiliser regex pour supprimer les trois premiers et derniers guillemets doubles
|
| 355 |
-
full_response = re.sub(r'^"{3}|"{3}$', '', full_response.strip())
|
| 356 |
st.session_state.messages = [] #.append({"role": "assistant", "content": full_response})
|
| 357 |
-
st.session_state.full_response = full_response
|
| 358 |
-
return full_response
|
| 359 |
except Exception as e:
|
| 360 |
st.error(f"Une erreur s'est produite lors de la génération de la réponse : {e}")
|
| 361 |
return ""
|
| 362 |
|
| 363 |
-
def process_tts_message(
|
| 364 |
try:
|
| 365 |
-
tts_audio, tts_duration = text_to_speech(
|
| 366 |
return tts_audio, tts_duration
|
| 367 |
except Exception as e:
|
| 368 |
st.error(f"Une erreur s'est produite lors de la conversion texte-parole : {e}")
|
|
@@ -517,13 +517,6 @@ def convert_language_name_to_iso6391(language_data: Union[str, Dict[str, str]])
|
|
| 517 |
print(f"Langue non trouvée : {language_name}")
|
| 518 |
return "en" # Par défaut 'en' si la langue n'est pas trouvée
|
| 519 |
|
| 520 |
-
def on_languages_change() -> None:
|
| 521 |
-
"""Fonction de rappel pour le changement de langue(s) de destination."""
|
| 522 |
-
selected_language_names: List[str] = st.session_state.language_selector
|
| 523 |
-
st.session_state.selected_languages = [
|
| 524 |
-
{"language": lang, "iso-639-1": convert_language_name_to_iso6391(lang)}
|
| 525 |
-
for lang in selected_language_names
|
| 526 |
-
]
|
| 527 |
|
| 528 |
|
| 529 |
def init_process_mode(
|
|
@@ -788,7 +781,6 @@ def main_page():
|
|
| 788 |
key=f"download_button_{langues}_{timestamp}",
|
| 789 |
)
|
| 790 |
|
| 791 |
-
|
| 792 |
# check if `final_audio` var exists , if exists delete final_audio
|
| 793 |
#if 'final_audio' in locals():
|
| 794 |
# del final_audio
|
|
@@ -798,37 +790,51 @@ def main_page():
|
|
| 798 |
# del system_prompt
|
| 799 |
#if 'operation_prompt' in locals():
|
| 800 |
# del operation_prompt
|
| 801 |
-
|
| 802 |
#if 'user_input' in locals():
|
| 803 |
# del user_input
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 832 |
|
| 833 |
# Configuration de la barre latérale
|
| 834 |
with st.sidebar:
|
|
@@ -867,22 +873,26 @@ def main_page():
|
|
| 867 |
get_translation("choix_voix_tts"),
|
| 868 |
options=["alloy", "echo", "fable", "onyx", "nova", "shimmer"],
|
| 869 |
index=3, # "onyx" est à l'index 3
|
| 870 |
-
key="tts_voice"
|
|
|
|
| 871 |
)
|
| 872 |
st.checkbox(
|
| 873 |
get_translation("activer_tts_texte"),
|
| 874 |
key="enable_tts_for_input_from_text_field",
|
| 875 |
-
value=True
|
|
|
|
| 876 |
)
|
| 877 |
st.checkbox(
|
| 878 |
get_translation("activer_tts_audio"),
|
| 879 |
key="enable_tts_for_input_from_audio_record",
|
| 880 |
-
value=True
|
|
|
|
| 881 |
)
|
| 882 |
st.checkbox(
|
| 883 |
get_translation("lecture_auto_tts"),
|
| 884 |
key="autoplay_tts",
|
| 885 |
-
value=True
|
|
|
|
| 886 |
)
|
| 887 |
|
| 888 |
|
|
|
|
| 341 |
{"role": "system", "content": system_prompt},
|
| 342 |
{"role": "user", "content": payload_content}
|
| 343 |
]
|
| 344 |
+
st.session_state["full_response"] = ""
|
| 345 |
try:
|
| 346 |
for response in client.chat.completions.create(
|
| 347 |
model="gpt-4o-mini",
|
| 348 |
messages=st.session_state.messages,
|
| 349 |
stream=True,
|
| 350 |
temperature=0.1):
|
| 351 |
+
st.session_state.full_response += (response.choices[0].delta.content or "")
|
| 352 |
+
yield st.session_state.full_response + "▌"
|
| 353 |
|
| 354 |
# Utiliser regex pour supprimer les trois premiers et derniers guillemets doubles
|
| 355 |
+
st.session_state.full_response = re.sub(r'^"{3}|"{3}$', '', st.session_state.full_response.strip())
|
| 356 |
st.session_state.messages = [] #.append({"role": "assistant", "content": full_response})
|
| 357 |
+
###st.session_state.full_response = full_response
|
| 358 |
+
return st.session_state.full_response
|
| 359 |
except Exception as e:
|
| 360 |
st.error(f"Une erreur s'est produite lors de la génération de la réponse : {e}")
|
| 361 |
return ""
|
| 362 |
|
| 363 |
+
def process_tts_message(text_response: str) -> Tuple[Optional[bytes], Optional[float]]:
|
| 364 |
try:
|
| 365 |
+
tts_audio, tts_duration = text_to_speech(text_response)
|
| 366 |
return tts_audio, tts_duration
|
| 367 |
except Exception as e:
|
| 368 |
st.error(f"Une erreur s'est produite lors de la conversion texte-parole : {e}")
|
|
|
|
| 517 |
print(f"Langue non trouvée : {language_name}")
|
| 518 |
return "en" # Par défaut 'en' si la langue n'est pas trouvée
|
| 519 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
|
| 521 |
|
| 522 |
def init_process_mode(
|
|
|
|
| 781 |
key=f"download_button_{langues}_{timestamp}",
|
| 782 |
)
|
| 783 |
|
|
|
|
| 784 |
# check if `final_audio` var exists , if exists delete final_audio
|
| 785 |
#if 'final_audio' in locals():
|
| 786 |
# del final_audio
|
|
|
|
| 790 |
# del system_prompt
|
| 791 |
#if 'operation_prompt' in locals():
|
| 792 |
# del operation_prompt
|
|
|
|
| 793 |
#if 'user_input' in locals():
|
| 794 |
# del user_input
|
| 795 |
+
|
| 796 |
+
def clear_inputs_garbages():
|
| 797 |
+
if 'transcription' in locals():
|
| 798 |
+
del transcription
|
| 799 |
+
if 'operation_prompt' in locals():
|
| 800 |
+
del operation_prompt
|
| 801 |
+
if 'system_prompt' in locals():
|
| 802 |
+
del system_prompt
|
| 803 |
+
if 'audio_list' in locals():
|
| 804 |
+
del audio_list
|
| 805 |
+
if "full_response" in st.session_state:
|
| 806 |
+
del st.session_state["full_response"]
|
| 807 |
+
if "tts_audio" in locals():
|
| 808 |
+
del tts_audio
|
| 809 |
+
if 'tts_duration' in locals():
|
| 810 |
+
del tts_duration
|
| 811 |
+
if 'timestamp' in locals():
|
| 812 |
+
del timestamp
|
| 813 |
+
if 'langues' in locals():
|
| 814 |
+
del langues
|
| 815 |
+
if 'nom_fichier' in locals():
|
| 816 |
+
del nom_fichier
|
| 817 |
+
if 'final_audio' in locals():
|
| 818 |
+
del final_audio
|
| 819 |
+
if 'response_generator' in locals():
|
| 820 |
+
del response_generator
|
| 821 |
+
if 'end_response' in locals():
|
| 822 |
+
del end_response
|
| 823 |
+
|
| 824 |
+
if "messages" in st.session_state:
|
| 825 |
+
del st.session_state["messages"]
|
| 826 |
+
|
| 827 |
+
clear_inputs_garbages()
|
| 828 |
+
|
| 829 |
+
def on_languages_change() -> None:
|
| 830 |
+
"""Fonction de rappel pour le changement de langue(s) de destination."""
|
| 831 |
+
selected_language_names: List[str] = st.session_state.language_selector
|
| 832 |
+
st.session_state.selected_languages = [
|
| 833 |
+
{"language": lang, "iso-639-1": convert_language_name_to_iso6391(lang)}
|
| 834 |
+
for lang in selected_language_names
|
| 835 |
+
]
|
| 836 |
+
clear_inputs_garbages()
|
| 837 |
+
|
| 838 |
|
| 839 |
# Configuration de la barre latérale
|
| 840 |
with st.sidebar:
|
|
|
|
| 873 |
get_translation("choix_voix_tts"),
|
| 874 |
options=["alloy", "echo", "fable", "onyx", "nova", "shimmer"],
|
| 875 |
index=3, # "onyx" est à l'index 3
|
| 876 |
+
key="tts_voice",
|
| 877 |
+
on_change=clear_inputs_garbages
|
| 878 |
)
|
| 879 |
st.checkbox(
|
| 880 |
get_translation("activer_tts_texte"),
|
| 881 |
key="enable_tts_for_input_from_text_field",
|
| 882 |
+
value=True,
|
| 883 |
+
on_change=clear_inputs_garbages
|
| 884 |
)
|
| 885 |
st.checkbox(
|
| 886 |
get_translation("activer_tts_audio"),
|
| 887 |
key="enable_tts_for_input_from_audio_record",
|
| 888 |
+
value=True,
|
| 889 |
+
on_change=clear_inputs_garbages
|
| 890 |
)
|
| 891 |
st.checkbox(
|
| 892 |
get_translation("lecture_auto_tts"),
|
| 893 |
key="autoplay_tts",
|
| 894 |
+
value=True,
|
| 895 |
+
on_change=clear_inputs_garbages
|
| 896 |
)
|
| 897 |
|
| 898 |
|