Spaces:
Paused
Paused
rick
commited on
Ajout: Traduction multilingue simultanée
Browse files
app.py
CHANGED
|
@@ -130,10 +130,15 @@ def process_message(message, operation_prompt="", tts_enabled=False):
|
|
| 130 |
|
| 131 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
# Classe pour stocker les prompts système globaux
|
| 139 |
class GlobalSystemPrompts:
|
|
@@ -147,11 +152,12 @@ SYSTEM_PROMPT=""
|
|
| 147 |
OP_PROMPT=""
|
| 148 |
|
| 149 |
# Fonction pour configurer le mode de traduction
|
| 150 |
-
def set_mode_translation(from_lang,
|
| 151 |
global SYSTEM_PROMPT
|
| 152 |
global OP_PROMPT
|
| 153 |
-
SYSTEM_PROMPT=GlobalSystemPrompts.linguascribe()
|
| 154 |
-
|
|
|
|
| 155 |
|
| 156 |
# Liste des langues supportées par l'application
|
| 157 |
SUPPORTED_LANGUAGES=["Afrikaans", "Arabic", "Armenian", "Azerbaijani", "Belarusian", "Bosnian", "Bulgarian", "Catalan", "Chinese", "Croatian", "Czech", "Danish", "Dutch", "English", "Estonian", "Finnish", "French", "Galician", "German", "Greek", "Hebrew", "Hindi", "Hungarian", "Icelandic", "Indonesian", "Italian", "Japanese", "Kannada", "Kazakh", "Korean", "Latvian", "Lithuanian", "Macedonian", "Malay", "Marathi", "Maori", "Nepali", "Norwegian", "Persian", "Polish", "Portuguese", "Romanian", "Russian", "Serbian", "Slovak", "Slovenian", "Spanish", "Swahili", "Swedish", "Tagalog", "Tamil", "Thai", "Turkish", "Ukrainian", "Urdu", "Vietnamese", "Welsh"]
|
|
@@ -191,8 +197,8 @@ def main():
|
|
| 191 |
if "process_mode" not in st.session_state:
|
| 192 |
st.session_state["process_mode"] = "translation"
|
| 193 |
|
| 194 |
-
if "
|
| 195 |
-
st.session_state.
|
| 196 |
|
| 197 |
if "enable_tts_for_input_from_text_field" not in st.session_state:
|
| 198 |
st.session_state["enable_tts_for_input_from_text_field"] = True
|
|
@@ -203,8 +209,7 @@ def main():
|
|
| 203 |
def init_process_mode():
|
| 204 |
# Configuration du mode de traduction si nécessaire
|
| 205 |
if "translation" == st.session_state["process_mode"]:
|
| 206 |
-
set_mode_translation(from_lang=st.session_state.language_detected,
|
| 207 |
-
|
| 208 |
|
| 209 |
init_process_mode()
|
| 210 |
|
|
@@ -223,7 +228,6 @@ def main():
|
|
| 223 |
st.session_state.language_detected = language_detection(input_text=user_input, temperature=0.01)
|
| 224 |
init_process_mode()
|
| 225 |
process_message(user_input, operation_prompt=f"{OP_PROMPT}", tts_enabled=st.session_state.enable_tts_for_input_from_text_field)
|
| 226 |
-
# #################################################################
|
| 227 |
# Affichage de l'historique des messages (sauf le message système)
|
| 228 |
for message in st.session_state.messages[1:]:
|
| 229 |
with st.chat_message(message["role"]):
|
|
@@ -234,7 +238,6 @@ def main():
|
|
| 234 |
st.write("Ou enregistrez votre message audio :")
|
| 235 |
audio = audiorecorder("Cliquez pour enregistrer", "Cliquez pour arrêter l'enregistrement")
|
| 236 |
|
| 237 |
-
|
| 238 |
# Traitement de l'entrée audio de l'utilisateur
|
| 239 |
if len(audio) > 0:
|
| 240 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
|
|
@@ -258,24 +261,23 @@ def main():
|
|
| 258 |
"Abattre les barrières linguistiques entre les hommes."
|
| 259 |
]))
|
| 260 |
|
| 261 |
-
# Fonction de rappel pour le changement de langue
|
| 262 |
-
def on_language_change():
|
| 263 |
-
st.session_state.target_language, st.session_state.messages = convert_language_name_to_iso6391(st.session_state.language_selector) , []
|
| 264 |
-
|
| 265 |
with st.container(border=True):
|
| 266 |
-
# Conteneur pour la sélection
|
| 267 |
-
st.subheader("Sélection
|
| 268 |
-
|
| 269 |
-
# Sélection
|
| 270 |
-
st.
|
| 271 |
-
label="Choisissez
|
| 272 |
-
options=
|
| 273 |
-
|
| 274 |
key="language_selector",
|
| 275 |
-
on_change=on_language_change,
|
| 276 |
-
index=SUPPORTED_LANGUAGES.index("English")
|
| 277 |
)
|
| 278 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
with st.container(border=True):
|
| 280 |
st.subheader("Paramètres TTS")
|
| 281 |
st.checkbox(
|
|
@@ -288,7 +290,6 @@ def main():
|
|
| 288 |
key="enable_tts_for_input_from_audio_record",
|
| 289 |
value=st.session_state.get("enable_tts_for_input_from_audio_record", True)
|
| 290 |
)
|
| 291 |
-
|
| 292 |
|
| 293 |
# Point d'entrée de l'application
|
| 294 |
if __name__ == "__main__":
|
|
|
|
| 130 |
|
| 131 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 132 |
|
| 133 |
+
# Afficher les traductions pour chaque langue cible
|
| 134 |
+
translations = full_response.split("\n\n")
|
| 135 |
+
for translation in translations:
|
| 136 |
+
lang, text = translation.split(":", 1)
|
| 137 |
+
with st.expander(f"Traduction en {lang.strip()}"):
|
| 138 |
+
st.markdown(text.strip())
|
| 139 |
+
if tts_enabled:
|
| 140 |
+
tts_audio = text_to_speech(text.strip())
|
| 141 |
+
st.audio(tts_audio, format="audio/mp3")
|
| 142 |
|
| 143 |
# Classe pour stocker les prompts système globaux
|
| 144 |
class GlobalSystemPrompts:
|
|
|
|
| 152 |
OP_PROMPT=""
|
| 153 |
|
| 154 |
# Fonction pour configurer le mode de traduction
|
| 155 |
+
def set_mode_translation(from_lang, dest_langs):
|
| 156 |
global SYSTEM_PROMPT
|
| 157 |
global OP_PROMPT
|
| 158 |
+
SYSTEM_PROMPT = GlobalSystemPrompts.linguascribe()
|
| 159 |
+
dest_langs_str = ", ".join(dest_langs)
|
| 160 |
+
OP_PROMPT = f"Translate({from_lang} to {dest_langs_str})"
|
| 161 |
|
| 162 |
# Liste des langues supportées par l'application
|
| 163 |
SUPPORTED_LANGUAGES=["Afrikaans", "Arabic", "Armenian", "Azerbaijani", "Belarusian", "Bosnian", "Bulgarian", "Catalan", "Chinese", "Croatian", "Czech", "Danish", "Dutch", "English", "Estonian", "Finnish", "French", "Galician", "German", "Greek", "Hebrew", "Hindi", "Hungarian", "Icelandic", "Indonesian", "Italian", "Japanese", "Kannada", "Kazakh", "Korean", "Latvian", "Lithuanian", "Macedonian", "Malay", "Marathi", "Maori", "Nepali", "Norwegian", "Persian", "Polish", "Portuguese", "Romanian", "Russian", "Serbian", "Slovak", "Slovenian", "Spanish", "Swahili", "Swedish", "Tagalog", "Tamil", "Thai", "Turkish", "Ukrainian", "Urdu", "Vietnamese", "Welsh"]
|
|
|
|
| 197 |
if "process_mode" not in st.session_state:
|
| 198 |
st.session_state["process_mode"] = "translation"
|
| 199 |
|
| 200 |
+
if "target_languages" not in st.session_state:
|
| 201 |
+
st.session_state.target_languages = ["en"]
|
| 202 |
|
| 203 |
if "enable_tts_for_input_from_text_field" not in st.session_state:
|
| 204 |
st.session_state["enable_tts_for_input_from_text_field"] = True
|
|
|
|
| 209 |
def init_process_mode():
|
| 210 |
# Configuration du mode de traduction si nécessaire
|
| 211 |
if "translation" == st.session_state["process_mode"]:
|
| 212 |
+
set_mode_translation(from_lang=st.session_state.language_detected, dest_langs=st.session_state.target_languages)
|
|
|
|
| 213 |
|
| 214 |
init_process_mode()
|
| 215 |
|
|
|
|
| 228 |
st.session_state.language_detected = language_detection(input_text=user_input, temperature=0.01)
|
| 229 |
init_process_mode()
|
| 230 |
process_message(user_input, operation_prompt=f"{OP_PROMPT}", tts_enabled=st.session_state.enable_tts_for_input_from_text_field)
|
|
|
|
| 231 |
# Affichage de l'historique des messages (sauf le message système)
|
| 232 |
for message in st.session_state.messages[1:]:
|
| 233 |
with st.chat_message(message["role"]):
|
|
|
|
| 238 |
st.write("Ou enregistrez votre message audio :")
|
| 239 |
audio = audiorecorder("Cliquez pour enregistrer", "Cliquez pour arrêter l'enregistrement")
|
| 240 |
|
|
|
|
| 241 |
# Traitement de l'entrée audio de l'utilisateur
|
| 242 |
if len(audio) > 0:
|
| 243 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
|
|
|
|
| 261 |
"Abattre les barrières linguistiques entre les hommes."
|
| 262 |
]))
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
with st.container(border=True):
|
| 265 |
+
# Conteneur pour la sélection des langues
|
| 266 |
+
st.subheader("Sélection des langues")
|
| 267 |
+
|
| 268 |
+
# Sélection multiple des langues de destination
|
| 269 |
+
selected_languages = st.multiselect(
|
| 270 |
+
label="Choisissez les langues de destination",
|
| 271 |
+
options=SUPPORTED_LANGUAGES,
|
| 272 |
+
default=["English"],
|
| 273 |
key="language_selector",
|
|
|
|
|
|
|
| 274 |
)
|
| 275 |
|
| 276 |
+
# Mettre à jour les langues cibles et réinitialiser les messages
|
| 277 |
+
if selected_languages != st.session_state.get("target_languages", []):
|
| 278 |
+
st.session_state.target_languages = [convert_language_name_to_iso6391(lang) for lang in selected_languages]
|
| 279 |
+
st.session_state.messages = []
|
| 280 |
+
|
| 281 |
with st.container(border=True):
|
| 282 |
st.subheader("Paramètres TTS")
|
| 283 |
st.checkbox(
|
|
|
|
| 290 |
key="enable_tts_for_input_from_audio_record",
|
| 291 |
value=st.session_state.get("enable_tts_for_input_from_audio_record", True)
|
| 292 |
)
|
|
|
|
| 293 |
|
| 294 |
# Point d'entrée de l'application
|
| 295 |
if __name__ == "__main__":
|