Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,29 +1,50 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
-
from fastapi.responses import StreamingResponse
|
| 3 |
-
from pydantic import BaseModel
|
| 4 |
-
from kokoro import KPipeline
|
| 5 |
-
import soundfile as sf
|
| 6 |
-
import io
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.responses import StreamingResponse
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from kokoro import KPipeline
|
| 5 |
+
import soundfile as sf
|
| 6 |
+
import io
|
| 7 |
+
import logging
|
| 8 |
+
import time
|
| 9 |
+
|
| 10 |
+
# --- Configurer les logs ---
|
| 11 |
+
logging.basicConfig(level=logging.INFO)
|
| 12 |
+
logger = logging.getLogger("kokoro-stream")
|
| 13 |
+
|
| 14 |
+
app = FastAPI()
|
| 15 |
+
|
| 16 |
+
# Initialiser le pipeline au démarrage
|
| 17 |
+
logger.info("🔍 Initialisation du pipeline Kokoro...")
|
| 18 |
+
pipeline = KPipeline(lang_code='a', device='cpu')
|
| 19 |
+
logger.info("✅ Pipeline Kokoro initialisé")
|
| 20 |
+
|
| 21 |
+
class TTSRequest(BaseModel):
|
| 22 |
+
text: str
|
| 23 |
+
voice: str = 'af_heart'
|
| 24 |
+
speed: float = 1.0
|
| 25 |
+
|
| 26 |
+
@app.post("/tts/stream")
|
| 27 |
+
async def stream_speech(request: TTSRequest):
|
| 28 |
+
logger.info(f"🚀 Streaming demandé pour le texte: '{request.text}'")
|
| 29 |
+
start_time = time.time()
|
| 30 |
+
|
| 31 |
+
def generate():
|
| 32 |
+
chunk_count = 0
|
| 33 |
+
for result in pipeline(request.text, voice=request.voice, speed=request.speed):
|
| 34 |
+
chunk_count += 1
|
| 35 |
+
if hasattr(result.audio, "numpy"):
|
| 36 |
+
audio_data = result.audio.numpy()
|
| 37 |
+
else:
|
| 38 |
+
audio_data = result.audio
|
| 39 |
+
|
| 40 |
+
logger.info(f"✅ Chunk {chunk_count} généré, taille={len(audio_data)} samples")
|
| 41 |
+
|
| 42 |
+
buffer = io.BytesIO()
|
| 43 |
+
sf.write(buffer, audio_data, 24000, format='WAV')
|
| 44 |
+
buffer.seek(0)
|
| 45 |
+
yield buffer.read()
|
| 46 |
+
|
| 47 |
+
elapsed = time.time() - start_time
|
| 48 |
+
logger.info(f"🏁 Streaming terminé, {chunk_count} chunks envoyés en {elapsed:.2f}s")
|
| 49 |
+
|
| 50 |
+
return StreamingResponse(generate(), media_type="audio/wav")
|