omarbajouk commited on
Commit
d1d656e
·
verified ·
1 Parent(s): 58ee418

Delete src/tts_utils.py

Browse files
Files changed (1) hide show
  1. src/tts_utils.py +0 -96
src/tts_utils.py DELETED
@@ -1,96 +0,0 @@
1
- import os, uuid, asyncio
2
- from typing import Dict
3
- from pydub import AudioSegment
4
- import edge_tts
5
-
6
- from .config import TMP_DIR
7
-
8
- EDGE_VOICES: Dict[str, str] = {}
9
-
10
- async def fetch_edge_voices_async():
11
- """Charge dynamiquement toutes les voix FR/NL depuis Edge-TTS."""
12
- global EDGE_VOICES
13
- try:
14
- voices = await edge_tts.list_voices()
15
- filtered = [v for v in voices if v.get("Locale", "").startswith(("fr", "nl"))]
16
- filtered.sort(key=lambda v: (v.get("Locale"), v.get("Gender"), v.get("ShortName")))
17
- EDGE_VOICES = {
18
- f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v["ShortName"]
19
- for v in filtered
20
- }
21
- print(f"[Edge-TTS] {len(EDGE_VOICES)} voix FR/NL chargées.")
22
- except Exception as e:
23
- print(f"[Edge-TTS] Erreur chargement voix : {e}")
24
- EDGE_VOICES.update({
25
- "fr-FR-DeniseNeural - fr-FR (Female)": "fr-FR-DeniseNeural",
26
- "nl-NL-MaaikeNeural - nl-NL (Female)": "nl-NL-MaaikeNeural",
27
- })
28
-
29
- def init_edge_voices():
30
- """Démarre le chargement asynchrone sans bloquer Gradio."""
31
- try:
32
- loop = asyncio.get_event_loop()
33
- if loop.is_running():
34
- import nest_asyncio
35
- nest_asyncio.apply()
36
- loop.create_task(fetch_edge_voices_async())
37
- else:
38
- loop.run_until_complete(fetch_edge_voices_async())
39
- except RuntimeError:
40
- asyncio.run(fetch_edge_voices_async())
41
-
42
- def get_edge_voices(lang="fr"):
43
- """Retourne les voix déjà chargées (selon la langue)."""
44
- global EDGE_VOICES
45
- if not EDGE_VOICES:
46
- init_edge_voices()
47
- if lang == "fr":
48
- return [v for k, v in EDGE_VOICES.items() if k.startswith("fr-")]
49
- elif lang == "nl":
50
- return [v for k, v in EDGE_VOICES.items() if k.startswith("nl-")]
51
- return list(EDGE_VOICES.values())
52
-
53
- async def _edge_tts_async(text, voice, outfile):
54
- communicate = edge_tts.Communicate(text, voice)
55
- await communicate.save(outfile)
56
- return outfile
57
-
58
- def tts_edge(text: str, voice: str = "fr-FR-DeniseNeural") -> str:
59
- """Génère un fichier WAV avec Edge-TTS (et fallback gTTS)."""
60
- out_mp3 = os.path.join(TMP_DIR, f"edge_{uuid.uuid4().hex}.mp3")
61
- try:
62
- try:
63
- loop = asyncio.get_event_loop()
64
- if loop.is_running():
65
- import nest_asyncio
66
- nest_asyncio.apply()
67
- except RuntimeError:
68
- pass
69
- asyncio.run(_edge_tts_async(text, voice, out_mp3))
70
-
71
- out_wav = os.path.join(TMP_DIR, f"edge_{uuid.uuid4().hex}.wav")
72
- AudioSegment.from_file(out_mp3).export(out_wav, format="wav")
73
- os.remove(out_mp3)
74
- return out_wav
75
- except Exception as e:
76
- print(f"[Edge-TTS] Erreur : {e} → fallback gTTS")
77
- return tts_gtts(text, lang="fr" if voice.startswith("fr") else "nl")
78
-
79
- def tts_gtts(text: str, lang: str = "fr") -> str:
80
- from gtts import gTTS
81
- out = os.path.join(TMP_DIR, f"gtts_{uuid.uuid4().hex}.mp3")
82
- gTTS(text=text, lang=lang).save(out)
83
- out_wav = os.path.join(TMP_DIR, f"gtts_{uuid.uuid4().hex}.wav")
84
- AudioSegment.from_file(out).export(out_wav, format="wav")
85
- os.remove(out)
86
- return out_wav
87
-
88
-
89
- def normalize_audio_to_wav(in_path: str) -> str:
90
- """Convertit n'importe quel format en WAV 44.1kHz stéréo."""
91
- from pydub import AudioSegment
92
- wav_path = os.path.join(TMP_DIR, f"norm_{uuid.uuid4().hex}.wav")
93
- snd = AudioSegment.from_file(in_path)
94
- snd = snd.set_frame_rate(44100).set_channels(2).set_sample_width(2)
95
- snd.export(wav_path, format="wav")
96
- return wav_path