Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,8 @@ import scipy
|
|
4 |
import torchaudio
|
5 |
from speechbrain.pretrained import SepformerSeparation as separator
|
6 |
from shialifube import transliterate
|
|
|
|
|
7 |
|
8 |
# Chargement des modèles Whisper pour la transcription
|
9 |
model_roman = pipeline("automatic-speech-recognition", model="nairaxo/whisper-shikomori-latin")
|
@@ -37,24 +39,33 @@ def transcribe(audio, model_choice):
|
|
37 |
# Fonction de génération et d'amélioration audio
|
38 |
def generate_and_enhance_audio(text, script_choice):
|
39 |
try:
|
|
|
|
|
|
|
|
|
40 |
# Translittérer le texte si l'utilisateur a choisi l'arabe
|
41 |
if script_choice == "Alphabet arabe":
|
42 |
text = transliterate(text) # Translittération de l'arabe en latin
|
43 |
|
44 |
# Synthétiser la parole (audio original)
|
45 |
speech = synthesiser(text)
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
|
54 |
-
|
|
|
55 |
|
56 |
except Exception as e:
|
57 |
-
return f"Erreur lors de la génération ou de l'amélioration de l'audio : {str(e)}"
|
58 |
|
59 |
# Interface pour la transcription via microphone
|
60 |
with gr.Blocks() as mf_transcribe:
|
@@ -88,8 +99,13 @@ with gr.Blocks() as tts_interface:
|
|
88 |
with gr.Row():
|
89 |
original_audio = gr.Audio(label="Audio original", type="filepath")
|
90 |
enhanced_audio = gr.Audio(label="Audio amélioré", type="filepath")
|
|
|
91 |
generate_button = gr.Button("Générer l'audio")
|
92 |
-
generate_button.click(
|
|
|
|
|
|
|
|
|
93 |
|
94 |
# Interface principale avec onglets
|
95 |
with gr.Blocks() as demo:
|
|
|
4 |
import torchaudio
|
5 |
from speechbrain.pretrained import SepformerSeparation as separator
|
6 |
from shialifube import transliterate
|
7 |
+
import tempfile
|
8 |
+
import os
|
9 |
|
10 |
# Chargement des modèles Whisper pour la transcription
|
11 |
model_roman = pipeline("automatic-speech-recognition", model="nairaxo/whisper-shikomori-latin")
|
|
|
39 |
# Fonction de génération et d'amélioration audio
|
40 |
def generate_and_enhance_audio(text, script_choice):
|
41 |
try:
|
42 |
+
# Vérifier si le texte est vide
|
43 |
+
if not text.strip():
|
44 |
+
return None, None, "Erreur : Le texte d'entrée est vide."
|
45 |
+
|
46 |
# Translittérer le texte si l'utilisateur a choisi l'arabe
|
47 |
if script_choice == "Alphabet arabe":
|
48 |
text = transliterate(text) # Translittération de l'arabe en latin
|
49 |
|
50 |
# Synthétiser la parole (audio original)
|
51 |
speech = synthesiser(text)
|
52 |
+
sampling_rate = speech["sampling_rate"]
|
53 |
+
|
54 |
+
# Créer des fichiers temporaires pour l'audio original et amélioré
|
55 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as original_file:
|
56 |
+
original_output = original_file.name
|
57 |
+
scipy.io.wavfile.write(original_output, rate=sampling_rate, data=speech["audio"][0])
|
58 |
|
59 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as enhanced_file:
|
60 |
+
enhanced_output = enhanced_file.name
|
61 |
+
est_sources = model_enh.separate_file(path=original_output)
|
62 |
+
torchaudio.save(enhanced_output, est_sources[:, :, 0].detach().cpu(), sampling_rate)
|
63 |
|
64 |
+
# Retourner les fichiers audio au format attendu par Gradio
|
65 |
+
return (original_output, sampling_rate), (enhanced_output, sampling_rate), None
|
66 |
|
67 |
except Exception as e:
|
68 |
+
return None, None, f"Erreur lors de la génération ou de l'amélioration de l'audio : {str(e)}"
|
69 |
|
70 |
# Interface pour la transcription via microphone
|
71 |
with gr.Blocks() as mf_transcribe:
|
|
|
99 |
with gr.Row():
|
100 |
original_audio = gr.Audio(label="Audio original", type="filepath")
|
101 |
enhanced_audio = gr.Audio(label="Audio amélioré", type="filepath")
|
102 |
+
error_output = gr.Textbox(label="Erreur", visible=False)
|
103 |
generate_button = gr.Button("Générer l'audio")
|
104 |
+
generate_button.click(
|
105 |
+
fn=generate_and_enhance_audio,
|
106 |
+
inputs=[text_input, script_choice],
|
107 |
+
outputs=[original_audio, enhanced_audio, error_output]
|
108 |
+
)
|
109 |
|
110 |
# Interface principale avec onglets
|
111 |
with gr.Blocks() as demo:
|