Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,125 +3,163 @@ import os
|
|
| 3 |
import gradio as gr
|
| 4 |
from TTS.api import TTS
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
output_folder = "output_audio"
|
| 11 |
-
os.makedirs(output_folder, exist_ok=True)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from TTS.api import TTS
|
| 5 |
|
| 6 |
+
class TTSInterface:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
# Initialisation du modèle TTS
|
| 9 |
+
self.tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
|
| 10 |
+
self.output_folder = "output_audio"
|
| 11 |
+
os.makedirs(self.output_folder, exist_ok=True)
|
| 12 |
+
|
| 13 |
+
def create_interface(self):
|
| 14 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 15 |
+
# Variables d'état
|
| 16 |
+
state = gr.State({
|
| 17 |
+
"project_name": "",
|
| 18 |
+
"sections": []
|
| 19 |
+
})
|
| 20 |
+
|
| 21 |
+
# En-tête
|
| 22 |
+
gr.Markdown("""
|
| 23 |
+
# 🎙️ Synthèse Vocale Margaux
|
| 24 |
+
## 👋 Bienvenue sur Margaux - Votre outil de synthèse vocale avancée
|
| 25 |
+
|
| 26 |
+
Margaux vous permet de générer des voix off naturelles à partir de textes, structurées par sections pour une meilleure qualité audio.
|
| 27 |
+
|
| 28 |
+
**Étapes principales :**
|
| 29 |
+
1. 🛠️ **Créer un projet** : Définissez le nom du projet et choisissez la voix.
|
| 30 |
+
2. ✍️ **Ajouter des sections** : Divisez votre texte en parties claires.
|
| 31 |
+
3. 🎧 **Générer les audios** : Chaque section est transformée en fichier audio.
|
| 32 |
+
4. 📁 **Sauvegardez le projet** : Finalisez et récupérez les fichiers.
|
| 33 |
+
""")
|
| 34 |
+
|
| 35 |
+
# Tabs pour les étapes
|
| 36 |
+
with gr.Tabs() as tabs:
|
| 37 |
+
# Étape 1: Configuration du projet
|
| 38 |
+
with gr.Tab("🛠️ Création du Projet"):
|
| 39 |
+
with gr.Column():
|
| 40 |
+
gr.Markdown("### Configuration initiale")
|
| 41 |
+
project_name = gr.Textbox(
|
| 42 |
+
label="Nom du Projet",
|
| 43 |
+
placeholder="Exemple : Capsule_Video_PLU",
|
| 44 |
+
scale=1
|
| 45 |
+
)
|
| 46 |
+
speaker = gr.Dropdown(
|
| 47 |
+
choices=["Margaux"],
|
| 48 |
+
value="Margaux",
|
| 49 |
+
label="Voix 🎙️",
|
| 50 |
+
scale=1
|
| 51 |
+
)
|
| 52 |
+
terms = gr.Checkbox(
|
| 53 |
+
label="✅ J'accepte les conditions d'utilisation"
|
| 54 |
+
)
|
| 55 |
+
create_btn = gr.Button("Créer le Projet ➡️", variant="primary")
|
| 56 |
+
|
| 57 |
+
# Étape 2: Gestion des sections
|
| 58 |
+
with gr.Tab("✍️ Sections"):
|
| 59 |
+
with gr.Column():
|
| 60 |
+
gr.Markdown("### Gestion des sections")
|
| 61 |
+
|
| 62 |
+
# Container pour les sections dynamiques
|
| 63 |
+
sections_container = gr.Column()
|
| 64 |
+
|
| 65 |
+
with gr.Row():
|
| 66 |
+
add_section = gr.Button("+ Ajouter une Section ➕")
|
| 67 |
+
remove_section = gr.Button("- Supprimer la dernière Section ➖")
|
| 68 |
+
|
| 69 |
+
# Template pour une section
|
| 70 |
+
def create_section_template(index):
|
| 71 |
+
with gr.Group():
|
| 72 |
+
with gr.Box():
|
| 73 |
+
gr.Markdown(f"#### Section {index}")
|
| 74 |
+
name = gr.Textbox(
|
| 75 |
+
label="Nom de la section",
|
| 76 |
+
value=f"Section_{index}"
|
| 77 |
+
)
|
| 78 |
+
text = gr.TextArea(
|
| 79 |
+
label="Texte à synthétiser",
|
| 80 |
+
placeholder="Entrez votre texte ici..."
|
| 81 |
+
)
|
| 82 |
+
return [name, text]
|
| 83 |
+
|
| 84 |
+
# Étape 3: Génération des audios
|
| 85 |
+
with gr.Tab("🎧 Génération"):
|
| 86 |
+
with gr.Column():
|
| 87 |
+
gr.Markdown("### Génération des audios")
|
| 88 |
+
generate_all = gr.Button("Générer tous les audios ▶️", variant="primary")
|
| 89 |
+
audio_outputs = gr.Column()
|
| 90 |
+
save_project = gr.Button("Sauvegarder le Projet ✅")
|
| 91 |
+
|
| 92 |
+
# Fonctions de callback
|
| 93 |
+
def update_project_info(name, terms):
|
| 94 |
+
if not terms:
|
| 95 |
+
return gr.Error("Veuillez accepter les conditions d'utilisation")
|
| 96 |
+
if not name.strip():
|
| 97 |
+
return gr.Error("Le nom du projet est requis")
|
| 98 |
+
return gr.Info(f"Projet '{name}' créé avec succès!")
|
| 99 |
+
|
| 100 |
+
def add_new_section(state):
|
| 101 |
+
current_sections = state["sections"]
|
| 102 |
+
new_section = {
|
| 103 |
+
"name": f"Section_{len(current_sections) + 1}",
|
| 104 |
+
"text": ""
|
| 105 |
+
}
|
| 106 |
+
current_sections.append(new_section)
|
| 107 |
+
return {"sections": current_sections}
|
| 108 |
+
|
| 109 |
+
def remove_last_section(state):
|
| 110 |
+
current_sections = state["sections"]
|
| 111 |
+
if current_sections:
|
| 112 |
+
current_sections.pop()
|
| 113 |
+
return {"sections": current_sections}
|
| 114 |
+
|
| 115 |
+
def generate_audio(state, section_data):
|
| 116 |
+
try:
|
| 117 |
+
# Logique de génération audio ici
|
| 118 |
+
output_path = os.path.join(
|
| 119 |
+
self.output_folder,
|
| 120 |
+
state["project_name"],
|
| 121 |
+
f"{section_data['name']}.wav"
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# Simulation de génération (à remplacer par votre logique TTS)
|
| 125 |
+
self.tts.tts_to_file(
|
| 126 |
+
text=section_data["text"],
|
| 127 |
+
file_path=output_path,
|
| 128 |
+
language="fr"
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
return gr.Audio(output_path)
|
| 132 |
+
except Exception as e:
|
| 133 |
+
return gr.Error(str(e))
|
| 134 |
+
|
| 135 |
+
# Événements
|
| 136 |
+
create_btn.click(
|
| 137 |
+
update_project_info,
|
| 138 |
+
inputs=[project_name, terms],
|
| 139 |
+
outputs=[gr.Info]
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
add_section.click(
|
| 143 |
+
add_new_section,
|
| 144 |
+
inputs=[state],
|
| 145 |
+
outputs=[state]
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
remove_section.click(
|
| 149 |
+
remove_last_section,
|
| 150 |
+
inputs=[state],
|
| 151 |
+
outputs=[state]
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
generate_all.click(
|
| 155 |
+
lambda x: [generate_audio(x, section) for section in x["sections"]],
|
| 156 |
+
inputs=[state],
|
| 157 |
+
outputs=[audio_outputs]
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
return demo
|
| 161 |
+
|
| 162 |
+
if __name__ == "__main__":
|
| 163 |
+
interface = TTSInterface()
|
| 164 |
+
demo = interface.create_interface()
|
| 165 |
+
demo.launch(debug=True)
|