tarawele1 commited on
Commit
e2dbfa0
·
verified ·
1 Parent(s): 8c52a5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -38
app.py CHANGED
@@ -1,49 +1,47 @@
1
  import gradio as gr
2
- from TTS.utils.synthesizer import Synthesizer
3
- import os
 
 
4
 
5
- # 1. Chargement de votre modèle Bambara
6
- # Assurez-vous que les noms correspondent bien aux fichiers que vous avez uploadés
7
- config_path = "config.json"
8
- model_path = "best_model.pth"
9
 
10
- print("Chargement du modèle TTS...")
11
-
12
- synth = Synthesizer(
13
- tts_checkpoint=model_path,
14
- tts_config_path=config_path
15
- )
16
-
17
- # 2. Fonction qui s'exécute quand l'utilisateur clique sur "Générer"
18
- def lire_bambara(texte):
19
- # CORRECTION : On remplace les caractères spéciaux bambara par des caractères latins standard
20
- # (le PDF original avait perdu ces caractères à l'affichage)
21
- texte_nettoye = texte.replace('ɛ', 'e').replace('ɔ', 'o').replace('Ɛ', 'E').replace('Ɔ', 'O')
22
-
23
- # Synthèse de la voix avec le texte nettoyé
24
- wav = synth.tts(text=texte_nettoye)
25
 
26
- # Sauvegarde temporaire du fichier audio
27
- output_path = "output.wav"
28
- synth.save_wav(wav, output_path)
 
 
 
 
29
 
30
- return output_path
 
 
31
 
32
- # 3. Création de l'interface graphique avec Gradio
33
- interface = gr.Interface(
34
- fn=lire_bambara,
35
- inputs=gr.Textbox(
36
- label="Texte en Bambara",
37
- placeholder="Aw ni ce, ne be bamanankan fo..."
38
- ),
39
  outputs=gr.Audio(label="Audio généré", type="filepath"),
40
  title="Synthèse Vocale en Bambara",
41
- description="Entrez du texte en bambara, et le modèle générera la voix correspondante.",
42
  examples=[
43
- ["Ikireni jamanakuntigi Volodymyr Zelenskyy."],
44
- ["Siyasa koo be ka wuli."]
45
- ]
 
 
46
  )
47
 
48
- # 4. Lancement de l'application
49
- interface.launch()
 
1
  import gradio as gr
2
+ from transformers import VitsModel, AutoTokenizer
3
+ import torch
4
+ import scipy.io.wavfile
5
+ import tempfile
6
 
7
+ # 1. Chargement du "cerveau" bambara de Meta (S'exécute une seule fois au démarrage)
8
+ model_name = "facebook/mms-tts-bam"
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = VitsModel.from_pretrained(model_name)
11
 
12
+ def text_to_speech(texte):
13
+ if not texte.strip():
14
+ return None
15
+
16
+ # 2. Traduction du texte en langage machine
17
+ inputs = tokenizer(texte, return_tensors="pt")
 
 
 
 
 
 
 
 
 
18
 
19
+ # 3. Génération de l'audio
20
+ with torch.no_grad():
21
+ output = model(**inputs).waveform
22
+
23
+ # 4. Création d'un fichier audio temporaire
24
+ frequence = model.config.sampling_rate
25
+ donnees_audio = output[0].numpy()
26
 
27
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio:
28
+ scipy.io.wavfile.write(temp_audio.name, rate=frequence, data=donnees_audio)
29
+ return temp_audio.name
30
 
31
+ # 5. Création de l'interface graphique (Gradio)
32
+ demo = gr.Interface(
33
+ fn=text_to_speech,
34
+ inputs=gr.Textbox(label="Texte en Bambara", placeholder="Tapez votre texte ici...", lines=3),
 
 
 
35
  outputs=gr.Audio(label="Audio généré", type="filepath"),
36
  title="Synthèse Vocale en Bambara",
37
+ description="Entrez du texte en bambara. Cette application utilise le modèle officiel d'intelligence artificielle MMS de Meta pour générer la voix correspondante.",
38
  examples=[
39
+ ["Ikirɛni jamanakuntigi Volodymyr Zelenskyy."],
40
+ ["Siyasa koo ka wuli."],
41
+ ["Aw ni cɛ, ne bɛ bamanankan fɔ."]
42
+ ],
43
+ theme=gr.themes.Soft()
44
  )
45
 
46
+ if __name__ == "__main__":
47
+ demo.launch()