Spaces:
Paused
Paused
Update data/generate_audio.py
Browse files- data/generate_audio.py +37 -25
data/generate_audio.py
CHANGED
|
@@ -1,50 +1,62 @@
|
|
| 1 |
-
# Fichier: /data/generate_audio.py (VERSION
|
| 2 |
import sys
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
import wave
|
| 6 |
-
import collections
|
| 7 |
import torch
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
# Cette fonction force PyTorch à ignorer la nouvelle restriction "weights_only"
|
| 11 |
-
# qui bloque les anciens modèles Coqui TTS.
|
| 12 |
original_load = torch.load
|
| 13 |
def patched_load(*args, **kwargs):
|
| 14 |
kwargs['weights_only'] = False
|
| 15 |
return original_load(*args, **kwargs)
|
| 16 |
torch.load = patched_load
|
| 17 |
-
# -----------------------------------------------
|
| 18 |
|
| 19 |
from TTS.api import TTS
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
text =
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
try:
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
tts = TTS(model_name="tts_models/fr/mai/tacotron2-DDC", progress_bar=False, gpu=False)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
tts.tts_to_file(text=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
with wave.open(output_file, 'rb') as wf:
|
| 38 |
-
|
| 39 |
-
rate = wf.getframerate()
|
| 40 |
-
duration = frames / float(rate)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
"audioPath": output_file,
|
| 44 |
-
"duration": duration
|
| 45 |
-
}
|
| 46 |
-
print(json.dumps(output_data))
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
-
print(
|
| 50 |
sys.exit(1)
|
|
|
|
| 1 |
+
# Fichier: /data/generate_audio.py (VERSION ANTI-BRUIT & FILTRAGE)
|
| 2 |
import sys
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
import wave
|
|
|
|
| 6 |
import torch
|
| 7 |
+
import re
|
| 8 |
|
| 9 |
+
# FIX SÉCURITÉ PYTORCH
|
|
|
|
|
|
|
| 10 |
original_load = torch.load
|
| 11 |
def patched_load(*args, **kwargs):
|
| 12 |
kwargs['weights_only'] = False
|
| 13 |
return original_load(*args, **kwargs)
|
| 14 |
torch.load = patched_load
|
|
|
|
| 15 |
|
| 16 |
from TTS.api import TTS
|
| 17 |
|
| 18 |
+
def clean_text_pro(text):
|
| 19 |
+
"""Nettoyage chirurgical pour éviter les bruits de calcul"""
|
| 20 |
+
# 1. Remplacer les caractères problématiques vus dans vos logs
|
| 21 |
+
# Le modèle n'aime pas les accents combinés, on simplifie
|
| 22 |
+
text = text.replace('̃', 'n') # Fix pour le "on" nasal
|
| 23 |
+
text = text.replace('œ', 'oe')
|
| 24 |
+
|
| 25 |
+
# 2. Supprimer les caractères spéciaux inutiles
|
| 26 |
+
text = re.sub(r'[#@*<>]', '', text)
|
| 27 |
+
|
| 28 |
+
# 3. Normaliser les espaces et la ponctuation
|
| 29 |
+
text = text.replace('...', '…') # Utilise le vrai caractère de pause
|
| 30 |
+
|
| 31 |
+
# 4. Ajouter un micro-espace au début et à la fin pour éviter les "clics" d'attaque
|
| 32 |
+
return " " + text.strip() + " "
|
| 33 |
|
| 34 |
try:
|
| 35 |
+
if len(sys.argv) < 3: sys.exit(1)
|
| 36 |
|
| 37 |
+
input_text = sys.argv[1]
|
| 38 |
+
output_file = sys.argv[2]
|
| 39 |
+
processed_text = clean_text_pro(input_text)
|
| 40 |
+
|
| 41 |
+
# Initialisation
|
| 42 |
tts = TTS(model_name="tts_models/fr/mai/tacotron2-DDC", progress_bar=False, gpu=False)
|
| 43 |
|
| 44 |
+
# Génération
|
| 45 |
+
tts.tts_to_file(text=processed_text, file_path=output_file)
|
| 46 |
+
|
| 47 |
+
# --- POST-PROCESS OPTIONNEL (Si ffmpeg est nécessaire pour lisser) ---
|
| 48 |
+
# On peut forcer un petit fondu (fade-in/fade-out) pour supprimer les clics
|
| 49 |
+
clean_output = output_file.replace('.wav', '_clean.wav')
|
| 50 |
+
os.system(f"ffmpeg -y -i {output_file} -af 'afade=t=in:ss=0:d=0.05,afade=t=out:st=13.5:d=0.1' {clean_output} > /dev/null 2>&1")
|
| 51 |
+
# On remet le fichier propre à la place de l'original
|
| 52 |
+
if os.path.exists(clean_output):
|
| 53 |
+
os.replace(clean_output, output_file)
|
| 54 |
|
| 55 |
with wave.open(output_file, 'rb') as wf:
|
| 56 |
+
duration = wf.getnframes() / float(wf.getframerate())
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
print(json.dumps({"status": "success", "audioPath": output_file, "duration": round(duration, 2)}))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
+
print(json.dumps({"status": "error", "message": str(e)}), file=sys.stderr)
|
| 62 |
sys.exit(1)
|