Spaces:
Paused
Paused
rick
commited on
align to PEP8
Browse files
app.py
CHANGED
|
@@ -15,12 +15,12 @@ from audiorecorder import audiorecorder
|
|
| 15 |
from openai import OpenAI
|
| 16 |
from pydub import AudioSegment
|
| 17 |
|
| 18 |
-
def
|
| 19 |
try:
|
| 20 |
-
with open(
|
| 21 |
-
return json.load(
|
| 22 |
except FileNotFoundError:
|
| 23 |
-
print(_("erreur_fichier_non_trouve").format(
|
| 24 |
return {}
|
| 25 |
except json.JSONDecodeError:
|
| 26 |
print(_("erreur_lecture_fichier").format(
|
|
@@ -32,7 +32,7 @@ def load_lang_ui(filepath: Optional[str] = "ui_lang_support.json") -> dict:
|
|
| 32 |
return {}
|
| 33 |
|
| 34 |
# Dictionnaire pour stocker les traductions
|
| 35 |
-
traductions =
|
| 36 |
|
| 37 |
# Fonction pour obtenir les traductions en fonction de la langue sélectionnée
|
| 38 |
def _(key):
|
|
@@ -42,13 +42,13 @@ def _(key):
|
|
| 42 |
client = OpenAI(api_key=getenv("OPENAI_API_KEY"))
|
| 43 |
|
| 44 |
# Fonction pour lire et retourner le contenu de fichiers textes
|
| 45 |
-
def
|
| 46 |
try:
|
| 47 |
-
with open(
|
| 48 |
-
|
| 49 |
-
return
|
| 50 |
except FileNotFoundError:
|
| 51 |
-
return _("erreur_fichier_non_trouve").format(
|
| 52 |
except Exception as e:
|
| 53 |
return _("erreur_lecture_fichier").format(str(e))
|
| 54 |
|
|
@@ -130,7 +130,6 @@ def get_duration_pydub(audio_file):
|
|
| 130 |
audio = AudioSegment.from_file(audio_file)
|
| 131 |
return audio.duration_seconds
|
| 132 |
|
| 133 |
-
|
| 134 |
# Fonction pour convertir du texte en parole
|
| 135 |
def text_to_speech(text):
|
| 136 |
response = client.audio.speech.create(
|
|
@@ -215,21 +214,21 @@ def process_message(message, operation_prompt="", tts_enabled=False):
|
|
| 215 |
class GlobalSystemPrompts:
|
| 216 |
# Méthode pour récupérer le prompt système pour la fonctionnalité Linguascribe
|
| 217 |
def linguascribe():
|
| 218 |
-
|
| 219 |
-
return
|
| 220 |
|
| 221 |
|
| 222 |
# Variables globales pour les prompts
|
| 223 |
-
|
| 224 |
-
|
| 225 |
|
| 226 |
|
| 227 |
# Fonction pour configurer le mode de traduction
|
| 228 |
def set_mode_translation(from_lang, dest_lang):
|
| 229 |
-
global
|
| 230 |
-
global
|
| 231 |
-
|
| 232 |
-
|
| 233 |
|
| 234 |
|
| 235 |
# Liste des langues supportées par l'application
|
|
@@ -319,7 +318,7 @@ def main():
|
|
| 319 |
|
| 320 |
# Vérifier si un message système existe déjà dans st.session_state.messages
|
| 321 |
if not any(message["role"] == "system" for message in st.session_state.messages):
|
| 322 |
-
st.session_state.messages.insert(0, {"role": "system", "content":
|
| 323 |
|
| 324 |
# Interface utilisateur pour le chat textuel
|
| 325 |
if user_input := st.chat_input(_("entrez_message")):
|
|
@@ -339,7 +338,7 @@ def main():
|
|
| 339 |
# Traitement du message de l'utilisateur pour la langue cible actuelle
|
| 340 |
tts_audio, tts_duration = process_message(
|
| 341 |
user_input,
|
| 342 |
-
operation_prompt=f"{
|
| 343 |
tts_enabled=st.session_state.enable_tts_for_input_from_text_field
|
| 344 |
)
|
| 345 |
if tts_audio is not None:
|
|
@@ -384,7 +383,7 @@ def main():
|
|
| 384 |
# Traitement du message de l'utilisateur pour la langue cible actuelle
|
| 385 |
tts_audio, tts_duration = process_message(
|
| 386 |
transcription,
|
| 387 |
-
operation_prompt=f"{
|
| 388 |
tts_enabled=st.session_state.enable_tts_for_input_from_audio_record
|
| 389 |
)
|
| 390 |
if tts_audio is not None:
|
|
|
|
| 15 |
from openai import OpenAI
|
| 16 |
from pydub import AudioSegment
|
| 17 |
|
| 18 |
+
def charger_ui_langue(chemin_fichier: Optional[str] = "ui_lang_support.json") -> dict:
|
| 19 |
try:
|
| 20 |
+
with open(chemin_fichier, 'r', encoding='utf-8') as fichier:
|
| 21 |
+
return json.load(fichier)
|
| 22 |
except FileNotFoundError:
|
| 23 |
+
print(_("erreur_fichier_non_trouve").format(chemin_fichier))
|
| 24 |
return {}
|
| 25 |
except json.JSONDecodeError:
|
| 26 |
print(_("erreur_lecture_fichier").format(
|
|
|
|
| 32 |
return {}
|
| 33 |
|
| 34 |
# Dictionnaire pour stocker les traductions
|
| 35 |
+
traductions = charger_ui_langue()
|
| 36 |
|
| 37 |
# Fonction pour obtenir les traductions en fonction de la langue sélectionnée
|
| 38 |
def _(key):
|
|
|
|
| 42 |
client = OpenAI(api_key=getenv("OPENAI_API_KEY"))
|
| 43 |
|
| 44 |
# Fonction pour lire et retourner le contenu de fichiers textes
|
| 45 |
+
def read_file(file_name):
|
| 46 |
try:
|
| 47 |
+
with open(file_name, 'r', encoding='utf-8') as file:
|
| 48 |
+
content = file.read()
|
| 49 |
+
return content
|
| 50 |
except FileNotFoundError:
|
| 51 |
+
return _("erreur_fichier_non_trouve").format(file_name)
|
| 52 |
except Exception as e:
|
| 53 |
return _("erreur_lecture_fichier").format(str(e))
|
| 54 |
|
|
|
|
| 130 |
audio = AudioSegment.from_file(audio_file)
|
| 131 |
return audio.duration_seconds
|
| 132 |
|
|
|
|
| 133 |
# Fonction pour convertir du texte en parole
|
| 134 |
def text_to_speech(text):
|
| 135 |
response = client.audio.speech.create(
|
|
|
|
| 214 |
class GlobalSystemPrompts:
|
| 215 |
# Méthode pour récupérer le prompt système pour la fonctionnalité Linguascribe
|
| 216 |
def linguascribe():
|
| 217 |
+
system_prompt = f"{read_file('linguascribe.prompt')}"
|
| 218 |
+
return system_prompt
|
| 219 |
|
| 220 |
|
| 221 |
# Variables globales pour les prompts
|
| 222 |
+
system_prompt = ""
|
| 223 |
+
op_prompt = ""
|
| 224 |
|
| 225 |
|
| 226 |
# Fonction pour configurer le mode de traduction
|
| 227 |
def set_mode_translation(from_lang, dest_lang):
|
| 228 |
+
global system_prompt
|
| 229 |
+
global op_prompt
|
| 230 |
+
system_prompt = GlobalSystemPrompts.linguascribe()
|
| 231 |
+
op_prompt = f"Translate({from_lang} to {dest_lang})"
|
| 232 |
|
| 233 |
|
| 234 |
# Liste des langues supportées par l'application
|
|
|
|
| 318 |
|
| 319 |
# Vérifier si un message système existe déjà dans st.session_state.messages
|
| 320 |
if not any(message["role"] == "system" for message in st.session_state.messages):
|
| 321 |
+
st.session_state.messages.insert(0, {"role": "system", "content": system_prompt})
|
| 322 |
|
| 323 |
# Interface utilisateur pour le chat textuel
|
| 324 |
if user_input := st.chat_input(_("entrez_message")):
|
|
|
|
| 338 |
# Traitement du message de l'utilisateur pour la langue cible actuelle
|
| 339 |
tts_audio, tts_duration = process_message(
|
| 340 |
user_input,
|
| 341 |
+
operation_prompt=f"{op_prompt}",
|
| 342 |
tts_enabled=st.session_state.enable_tts_for_input_from_text_field
|
| 343 |
)
|
| 344 |
if tts_audio is not None:
|
|
|
|
| 383 |
# Traitement du message de l'utilisateur pour la langue cible actuelle
|
| 384 |
tts_audio, tts_duration = process_message(
|
| 385 |
transcription,
|
| 386 |
+
operation_prompt=f"{op_prompt}",
|
| 387 |
tts_enabled=st.session_state.enable_tts_for_input_from_audio_record
|
| 388 |
)
|
| 389 |
if tts_audio is not None:
|