Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# 1. Chargement de
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 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 |
-
#
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
fn=
|
| 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
|
| 42 |
examples=[
|
| 43 |
-
["
|
| 44 |
-
["Siyasa koo
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 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 bɛ 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()
|