Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,55 +5,92 @@ import scipy.io.wavfile as wavfile
|
|
| 5 |
import numpy as np
|
| 6 |
import tempfile
|
| 7 |
|
| 8 |
-
print("Chargement du modèle...")
|
| 9 |
model = VitsModel.from_pretrained("morlayecis0003/tts-pulaar")
|
| 10 |
tokenizer = VitsTokenizer.from_pretrained("facebook/mms-tts-ful")
|
| 11 |
model.eval()
|
| 12 |
-
print("Modèle chargé!")
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
if not
|
| 16 |
-
return None, "Veuillez entrer un texte"
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
fn=synthesize_speech,
|
| 40 |
-
inputs=[
|
| 41 |
-
gr.Textbox(label="Texte en pulaar", lines=3, placeholder="Ex: A jaaraama, No ñawdiri?"),
|
| 42 |
-
gr.Slider(0.5, 1.5, value=1.0, label="Volume")
|
| 43 |
-
],
|
| 44 |
-
outputs=[
|
| 45 |
-
gr.Audio(label="Audio généré"),
|
| 46 |
-
gr.Textbox(label="Statut")
|
| 47 |
-
],
|
| 48 |
-
title="🗣️ Synthèse Vocale Pulaar",
|
| 49 |
-
description="Entrez du texte en pulaar pour générer l'audio",
|
| 50 |
-
examples=[
|
| 51 |
-
["A jaaraama", 1.0],
|
| 52 |
-
["No ñawdiri?", 1.0],
|
| 53 |
-
["Miɗo yiɗi pulaar", 1.0],
|
| 54 |
-
["Jamma waali", 1.0]
|
| 55 |
-
]
|
| 56 |
-
)
|
| 57 |
|
|
|
|
| 58 |
if __name__ == "__main__":
|
| 59 |
-
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import tempfile
|
| 7 |
|
| 8 |
+
print("Chargement du modèle TTS Pulaar...")
|
| 9 |
model = VitsModel.from_pretrained("morlayecis0003/tts-pulaar")
|
| 10 |
tokenizer = VitsTokenizer.from_pretrained("facebook/mms-tts-ful")
|
| 11 |
model.eval()
|
| 12 |
+
print("✅ Modèle chargé avec succès!")
|
| 13 |
|
| 14 |
+
def synthèse_vocale(texte, volume=1.0):
|
| 15 |
+
if not texte or not texte.strip():
|
| 16 |
+
return None, "❌ Veuillez entrer un texte"
|
| 17 |
|
| 18 |
+
try:
|
| 19 |
+
# Tokenisation
|
| 20 |
+
entrées = tokenizer(texte, return_tensors="pt")
|
| 21 |
+
|
| 22 |
+
# Génération
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
sorties = model(**entrées)
|
| 25 |
+
|
| 26 |
+
# Récupérer l'audio
|
| 27 |
+
audio = sorties.waveform[0].cpu().numpy()
|
| 28 |
+
|
| 29 |
+
# Normalisation du volume
|
| 30 |
+
max_valeur = np.max(np.abs(audio))
|
| 31 |
+
if max_valeur > 0:
|
| 32 |
+
audio = audio * (0.9 * volume / max_valeur)
|
| 33 |
+
audio = np.clip(audio, -1.0, 1.0)
|
| 34 |
+
|
| 35 |
+
# Conversion pour sauvegarde
|
| 36 |
+
audio_int16 = (audio * 32767).astype(np.int16)
|
| 37 |
+
|
| 38 |
+
# Sauvegarde temporaire
|
| 39 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
| 40 |
+
wavfile.write(f.name, 16000, audio_int16)
|
| 41 |
+
return f.name, f"✅ Succès! Durée: {len(audio)/16000:.1f} secondes"
|
| 42 |
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return None, f"❌ Erreur: {str(e)}"
|
| 45 |
+
|
| 46 |
+
# Interface Gradio
|
| 47 |
+
with gr.Blocks(title="TTS Pulaar", theme=gr.themes.Soft()) as interface:
|
| 48 |
+
gr.Markdown("""
|
| 49 |
+
# 🗣️ Synthèse Vocale en Pulaar
|
| 50 |
+
### Entrez un texte en pulaar et générez l'audio correspondant
|
| 51 |
+
""")
|
| 52 |
|
| 53 |
+
with gr.Row():
|
| 54 |
+
with gr.Column(scale=2):
|
| 55 |
+
texte_entrée = gr.Textbox(
|
| 56 |
+
label="📝 Texte en Pulaar",
|
| 57 |
+
placeholder="Exemple: A jaaraama, No ñawdiri?",
|
| 58 |
+
lines=4
|
| 59 |
+
)
|
| 60 |
+
volume_entrée = gr.Slider(
|
| 61 |
+
label="🔊 Volume",
|
| 62 |
+
minimum=0.5,
|
| 63 |
+
maximum=1.5,
|
| 64 |
+
value=1.0,
|
| 65 |
+
step=0.1
|
| 66 |
+
)
|
| 67 |
+
bouton = gr.Button("🎵 Générer l'audio", variant="primary")
|
| 68 |
+
|
| 69 |
+
with gr.Column(scale=1):
|
| 70 |
+
sortie_audio = gr.Audio(label="🎧 Audio généré")
|
| 71 |
+
sortie_statut = gr.Textbox(label="📊 Statut", lines=2)
|
| 72 |
|
| 73 |
+
# Exemples
|
| 74 |
+
gr.Markdown("### 📋 Exemples de textes")
|
| 75 |
+
exemples = gr.Examples(
|
| 76 |
+
examples=[
|
| 77 |
+
["A jaaraama", 1.0],
|
| 78 |
+
["No ñawdiri?", 1.0],
|
| 79 |
+
["Miɗo yiɗi pulaar", 1.0],
|
| 80 |
+
["Jamma waali", 1.0],
|
| 81 |
+
["Miɗo hoɗi Dakaar", 1.0]
|
| 82 |
+
],
|
| 83 |
+
inputs=[texte_entrée, volume_entrée],
|
| 84 |
+
label="Cliquez sur un exemple pour le tester"
|
| 85 |
+
)
|
| 86 |
|
| 87 |
+
# Lier la fonction
|
| 88 |
+
bouton.click(
|
| 89 |
+
fn=synthèse_vocale,
|
| 90 |
+
inputs=[texte_entrée, volume_entrée],
|
| 91 |
+
outputs=[sortie_audio, sortie_statut]
|
| 92 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# Lancer l'application
|
| 95 |
if __name__ == "__main__":
|
| 96 |
+
interface.launch()
|