Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
from fastapi import FastAPI, Response
|
| 2 |
from kokoro_onnx import Kokoro
|
| 3 |
-
from huggingface_hub import hf_hub_download
|
| 4 |
import soundfile as sf
|
| 5 |
import io
|
| 6 |
import os
|
|
@@ -8,42 +7,27 @@ import os
|
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
# --- Configuration ---
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
hf_hub_download(
|
| 26 |
-
repo_id=REPO_ID,
|
| 27 |
-
filename="voices.json",
|
| 28 |
-
local_dir="."
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
# Initialisation de Kokoro
|
| 32 |
-
kokoro = Kokoro("kokoro-v0_19.onnx", "voices.json")
|
| 33 |
-
print("✅ Kokoro est prêt !")
|
| 34 |
|
| 35 |
@app.post("/generate")
|
| 36 |
def generate_audio(text: str, voice: str = "af_bella", speed: float = 1.0, lang: str = "en-us"):
|
| 37 |
-
"""
|
| 38 |
-
Génère de l'audio et renvoie le fichier WAV directement.
|
| 39 |
-
"""
|
| 40 |
-
# Langues supportées par Kokoro: 'a' (US), 'b' (UK), 'f' (FR), 'j' (JP), etc.
|
| 41 |
-
# Petit mapping pour faciliter la vie
|
| 42 |
lang_map = {"fr": "f", "en": "a", "us": "a", "uk": "b"}
|
| 43 |
-
lang_code = lang_map.get(lang.lower(), lang)
|
| 44 |
|
| 45 |
try:
|
| 46 |
-
# Génération
|
| 47 |
audio, sample_rate = kokoro.create(
|
| 48 |
text,
|
| 49 |
voice=voice,
|
|
@@ -51,12 +35,10 @@ def generate_audio(text: str, voice: str = "af_bella", speed: float = 1.0, lang:
|
|
| 51 |
lang=lang_code
|
| 52 |
)
|
| 53 |
|
| 54 |
-
# Conversion en fichier binaire (WAV) en mémoire
|
| 55 |
buffer = io.BytesIO()
|
| 56 |
sf.write(buffer, audio, sample_rate, format='WAV')
|
| 57 |
buffer.seek(0)
|
| 58 |
|
| 59 |
-
# Renvoie le fichier audio
|
| 60 |
return Response(content=buffer.read(), media_type="audio/wav")
|
| 61 |
|
| 62 |
except Exception as e:
|
|
|
|
| 1 |
from fastapi import FastAPI, Response
|
| 2 |
from kokoro_onnx import Kokoro
|
|
|
|
| 3 |
import soundfile as sf
|
| 4 |
import io
|
| 5 |
import os
|
|
|
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
# --- Configuration ---
|
| 10 |
+
print("⏳ Initialisation de Kokoro...")
|
| 11 |
+
|
| 12 |
+
# Vérification simple (On ne télécharge RIEN, on utilise tes fichiers locaux)
|
| 13 |
+
if not os.path.exists("kokoro-v0_19.onnx") or not os.path.exists("voices.json"):
|
| 14 |
+
print("❌ ERREUR : Fichiers manquants ! Upload-les manuellement dans l'onglet Files.")
|
| 15 |
+
else:
|
| 16 |
+
print("✅ Fichiers modèles trouvés localement.")
|
| 17 |
+
|
| 18 |
+
# Initialisation
|
| 19 |
+
try:
|
| 20 |
+
kokoro = Kokoro("kokoro-v0_19.onnx", "voices.json")
|
| 21 |
+
print("✅ Kokoro est chargé !")
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"❌ Erreur au chargement du modèle : {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
@app.post("/generate")
|
| 26 |
def generate_audio(text: str, voice: str = "af_bella", speed: float = 1.0, lang: str = "en-us"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
lang_map = {"fr": "f", "en": "a", "us": "a", "uk": "b"}
|
| 28 |
+
lang_code = lang_map.get(lang.lower(), lang)
|
| 29 |
|
| 30 |
try:
|
|
|
|
| 31 |
audio, sample_rate = kokoro.create(
|
| 32 |
text,
|
| 33 |
voice=voice,
|
|
|
|
| 35 |
lang=lang_code
|
| 36 |
)
|
| 37 |
|
|
|
|
| 38 |
buffer = io.BytesIO()
|
| 39 |
sf.write(buffer, audio, sample_rate, format='WAV')
|
| 40 |
buffer.seek(0)
|
| 41 |
|
|
|
|
| 42 |
return Response(content=buffer.read(), media_type="audio/wav")
|
| 43 |
|
| 44 |
except Exception as e:
|