Spaces:
Paused
Paused
rick
commited on
align to PEP8
Browse files
app.py
CHANGED
|
@@ -7,14 +7,20 @@ import re
|
|
| 7 |
import tempfile
|
| 8 |
import time
|
| 9 |
from os import getenv
|
| 10 |
-
from typing import Any
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Third-party libraries
|
|
|
|
| 13 |
import streamlit as st
|
| 14 |
from audiorecorder import audiorecorder
|
| 15 |
from openai import OpenAI
|
| 16 |
from pydub import AudioSegment
|
| 17 |
-
import requests
|
| 18 |
|
| 19 |
def load_ui_language(file_path: Optional[str] = "ui_lang_support.json") -> Dict[str, Any]:
|
| 20 |
"""
|
|
@@ -376,16 +382,19 @@ class GlobalSystemPrompts:
|
|
| 376 |
return ""
|
| 377 |
|
| 378 |
# Function to configure the translation mode
|
| 379 |
-
def set_translation_mode(from_lang, dest_lang):
|
| 380 |
"""
|
| 381 |
-
|
| 382 |
|
| 383 |
Args:
|
| 384 |
-
from_lang (str):
|
| 385 |
-
dest_lang (str):
|
|
|
|
|
|
|
|
|
|
| 386 |
"""
|
| 387 |
system_prompt = GlobalSystemPrompts.linguascribe()
|
| 388 |
-
operation_prompt = "Translate({} to {})"
|
| 389 |
return system_prompt, operation_prompt
|
| 390 |
|
| 391 |
# List of languages supported by the application
|
|
@@ -401,18 +410,19 @@ SUPPORTED_LANGUAGES = [
|
|
| 401 |
"Turkish", "Ukrainian", "Urdu", "Vietnamese", "Welsh"
|
| 402 |
]
|
| 403 |
|
| 404 |
-
def convert_language_name_to_iso6391(language_data: Union[str,
|
| 405 |
"""
|
| 406 |
Convertit un nom de langue en son code ISO 639-1.
|
| 407 |
|
| 408 |
Args:
|
| 409 |
-
language_data (Union[str,
|
|
|
|
| 410 |
|
| 411 |
Returns:
|
| 412 |
str: Le code ISO 639-1 pour la langue donnée, ou 'en' si non trouvé.
|
| 413 |
"""
|
| 414 |
# Dictionnaire associant les noms de langues aux codes ISO 639-1
|
| 415 |
-
language_to_iso = {
|
| 416 |
"Afrikaans": "af", "Arabic": "ar", "Armenian": "hy", "Azerbaijani": "az",
|
| 417 |
"Belarusian": "be", "Bosnian": "bs", "Bulgarian": "bg", "Catalan": "ca",
|
| 418 |
"Chinese": "zh", "Croatian": "hr", "Czech": "cs", "Danish": "da",
|
|
@@ -432,7 +442,7 @@ def convert_language_name_to_iso6391(language_data: Union[str, dict]) -> str:
|
|
| 432 |
|
| 433 |
# Vérifier si language_data est un dictionnaire
|
| 434 |
if isinstance(language_data, dict):
|
| 435 |
-
language_name = language_data.get('language')
|
| 436 |
else:
|
| 437 |
language_name = language_data
|
| 438 |
|
|
@@ -444,6 +454,29 @@ def convert_language_name_to_iso6391(language_data: Union[str, dict]) -> str:
|
|
| 444 |
print(f"Langue non trouvée : {language_name}")
|
| 445 |
return "en" # Par défaut 'en' si la langue n'est pas trouvée
|
| 446 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
# Fonction principale de l'application
|
| 448 |
def main():
|
| 449 |
"""Fonction principale qui configure et exécute l'application Streamlit."""
|
|
@@ -473,16 +506,6 @@ def main():
|
|
| 473 |
if "interface_language" not in st.session_state:
|
| 474 |
st.session_state.interface_language = "French" # Langue par défaut
|
| 475 |
|
| 476 |
-
def init_process_mode():
|
| 477 |
-
"""Initialise le mode de traitement pour la traduction si nécessaire."""
|
| 478 |
-
if "translation" == st.session_state["process_mode"]:
|
| 479 |
-
system_prompt, operation_prompt = set_translation_mode(
|
| 480 |
-
from_lang=st.session_state.language_detected,
|
| 481 |
-
dest_lang=st.session_state.target_language
|
| 482 |
-
)
|
| 483 |
-
return system_prompt, operation_prompt
|
| 484 |
-
return "", ""
|
| 485 |
-
|
| 486 |
system_prompt, operation_prompt = init_process_mode()
|
| 487 |
|
| 488 |
# Initialisation de l'historique des messages avec le prompt système
|
|
@@ -586,14 +609,6 @@ def main():
|
|
| 586 |
st.markdown(f"## {get_translation('a_propos')}")
|
| 587 |
st.info(get_translation("info_app"))
|
| 588 |
|
| 589 |
-
def on_languages_change():
|
| 590 |
-
"""Fonction de rappel pour le changement de langue(s) de destination."""
|
| 591 |
-
selected_language_names = st.session_state.language_selector
|
| 592 |
-
st.session_state.selected_languages = [
|
| 593 |
-
{"language": lang, "iso-639-1": convert_language_name_to_iso6391(lang)}
|
| 594 |
-
for lang in selected_language_names
|
| 595 |
-
]
|
| 596 |
-
|
| 597 |
with st.container(border=True):
|
| 598 |
st.subheader(get_translation("langue_interface"))
|
| 599 |
# Sélection de la langue de l'interface
|
|
|
|
| 7 |
import tempfile
|
| 8 |
import time
|
| 9 |
from os import getenv
|
| 10 |
+
from typing import Any
|
| 11 |
+
from typing import Dict
|
| 12 |
+
from typing import IO
|
| 13 |
+
from typing import List
|
| 14 |
+
from typing import Optional
|
| 15 |
+
from typing import Tuple
|
| 16 |
+
from typing import Union
|
| 17 |
|
| 18 |
# Third-party libraries
|
| 19 |
+
import requests
|
| 20 |
import streamlit as st
|
| 21 |
from audiorecorder import audiorecorder
|
| 22 |
from openai import OpenAI
|
| 23 |
from pydub import AudioSegment
|
|
|
|
| 24 |
|
| 25 |
def load_ui_language(file_path: Optional[str] = "ui_lang_support.json") -> Dict[str, Any]:
|
| 26 |
"""
|
|
|
|
| 382 |
return ""
|
| 383 |
|
| 384 |
# Function to configure the translation mode
|
| 385 |
+
def set_translation_mode(from_lang: str, dest_lang: str) -> Tuple[str, str]:
|
| 386 |
"""
|
| 387 |
+
Configure les prompts globaux pour le mode de traduction.
|
| 388 |
|
| 389 |
Args:
|
| 390 |
+
from_lang (str): La langue source.
|
| 391 |
+
dest_lang (str): La langue de destination.
|
| 392 |
+
|
| 393 |
+
Returns:
|
| 394 |
+
Tuple[str, str]: Un tuple contenant le prompt système et le prompt d'opération.
|
| 395 |
"""
|
| 396 |
system_prompt = GlobalSystemPrompts.linguascribe()
|
| 397 |
+
operation_prompt = f"Translate({from_lang} to {dest_lang})"
|
| 398 |
return system_prompt, operation_prompt
|
| 399 |
|
| 400 |
# List of languages supported by the application
|
|
|
|
| 410 |
"Turkish", "Ukrainian", "Urdu", "Vietnamese", "Welsh"
|
| 411 |
]
|
| 412 |
|
| 413 |
+
def convert_language_name_to_iso6391(language_data: Union[str, Dict[str, str]]) -> str:
|
| 414 |
"""
|
| 415 |
Convertit un nom de langue en son code ISO 639-1.
|
| 416 |
|
| 417 |
Args:
|
| 418 |
+
language_data (Union[str, Dict[str, str]]): Le nom de la langue ou un dictionnaire
|
| 419 |
+
contenant le nom de la langue.
|
| 420 |
|
| 421 |
Returns:
|
| 422 |
str: Le code ISO 639-1 pour la langue donnée, ou 'en' si non trouvé.
|
| 423 |
"""
|
| 424 |
# Dictionnaire associant les noms de langues aux codes ISO 639-1
|
| 425 |
+
language_to_iso: Dict[str, str] = {
|
| 426 |
"Afrikaans": "af", "Arabic": "ar", "Armenian": "hy", "Azerbaijani": "az",
|
| 427 |
"Belarusian": "be", "Bosnian": "bs", "Bulgarian": "bg", "Catalan": "ca",
|
| 428 |
"Chinese": "zh", "Croatian": "hr", "Czech": "cs", "Danish": "da",
|
|
|
|
| 442 |
|
| 443 |
# Vérifier si language_data est un dictionnaire
|
| 444 |
if isinstance(language_data, dict):
|
| 445 |
+
language_name = language_data.get('language', '')
|
| 446 |
else:
|
| 447 |
language_name = language_data
|
| 448 |
|
|
|
|
| 454 |
print(f"Langue non trouvée : {language_name}")
|
| 455 |
return "en" # Par défaut 'en' si la langue n'est pas trouvée
|
| 456 |
|
| 457 |
+
def on_languages_change() -> None:
|
| 458 |
+
"""Fonction de rappel pour le changement de langue(s) de destination."""
|
| 459 |
+
selected_language_names: List[str] = st.session_state.language_selector
|
| 460 |
+
st.session_state.selected_languages = [
|
| 461 |
+
{"language": lang, "iso-639-1": convert_language_name_to_iso6391(lang)}
|
| 462 |
+
for lang in selected_language_names
|
| 463 |
+
]
|
| 464 |
+
|
| 465 |
+
def init_process_mode() -> Tuple[str, str]:
|
| 466 |
+
"""
|
| 467 |
+
Initialise le mode de traitement pour la traduction si nécessaire.
|
| 468 |
+
|
| 469 |
+
Returns:
|
| 470 |
+
Tuple[str, str]: Un tuple contenant le prompt système et le prompt d'opération.
|
| 471 |
+
"""
|
| 472 |
+
if st.session_state["process_mode"] == "translation":
|
| 473 |
+
system_prompt, operation_prompt = set_translation_mode(
|
| 474 |
+
from_lang=st.session_state.language_detected,
|
| 475 |
+
dest_lang=st.session_state.target_language
|
| 476 |
+
)
|
| 477 |
+
return system_prompt, operation_prompt
|
| 478 |
+
return "", ""
|
| 479 |
+
|
| 480 |
# Fonction principale de l'application
|
| 481 |
def main():
|
| 482 |
"""Fonction principale qui configure et exécute l'application Streamlit."""
|
|
|
|
| 506 |
if "interface_language" not in st.session_state:
|
| 507 |
st.session_state.interface_language = "French" # Langue par défaut
|
| 508 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
system_prompt, operation_prompt = init_process_mode()
|
| 510 |
|
| 511 |
# Initialisation de l'historique des messages avec le prompt système
|
|
|
|
| 609 |
st.markdown(f"## {get_translation('a_propos')}")
|
| 610 |
st.info(get_translation("info_app"))
|
| 611 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 612 |
with st.container(border=True):
|
| 613 |
st.subheader(get_translation("langue_interface"))
|
| 614 |
# Sélection de la langue de l'interface
|