Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -6,17 +6,22 @@ import numpy as np
|
|
| 6 |
import io
|
| 7 |
import logging
|
| 8 |
import os
|
|
|
|
| 9 |
# --- Logging ---
|
| 10 |
logging.basicConfig(level=logging.INFO)
|
| 11 |
logger = logging.getLogger("tts_stream")
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Initialiser le pipeline au démarrage
|
| 16 |
pipeline = KPipeline(lang_code='a', device='cpu')
|
| 17 |
logger.info("✅ KPipeline loaded successfully.")
|
| 18 |
-
|
| 19 |
-
print(f"cle{valid_hf_keys}")
|
| 20 |
class TTSRequest(BaseModel):
|
| 21 |
text: str
|
| 22 |
voice: str = 'af_heart'
|
|
@@ -24,21 +29,27 @@ class TTSRequest(BaseModel):
|
|
| 24 |
|
| 25 |
@app.post("/tts/stream")
|
| 26 |
async def stream_speech(request: TTSRequest, hf_key: str = Header(None)):
|
| 27 |
-
logger.info(f"🚀
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
raise HTTPException(status_code=401, detail="Unauthorized: invalid or missing hf_key")
|
|
|
|
|
|
|
|
|
|
| 30 |
def generate():
|
| 31 |
chunk_index = 0
|
| 32 |
try:
|
| 33 |
for result in pipeline(request.text, voice=request.voice, speed=request.speed):
|
| 34 |
chunk_index += 1
|
| 35 |
-
# Convertir en PCM float32
|
| 36 |
audio_bytes = result.audio.numpy().astype(np.float32).tobytes()
|
| 37 |
logger.info(f"✅ Chunk {chunk_index} ready, size={len(audio_bytes)} bytes")
|
| 38 |
yield audio_bytes
|
| 39 |
-
logger.info(f"🏁 Streaming finished:
|
| 40 |
except Exception as e:
|
| 41 |
-
logger.error(f"❌
|
| 42 |
raise
|
| 43 |
|
| 44 |
return StreamingResponse(
|
|
@@ -49,4 +60,4 @@ async def stream_speech(request: TTSRequest, hf_key: str = Header(None)):
|
|
| 49 |
"X-Channels": "1",
|
| 50 |
"X-Bit-Depth": "32"
|
| 51 |
}
|
| 52 |
-
)
|
|
|
|
| 6 |
import io
|
| 7 |
import logging
|
| 8 |
import os
|
| 9 |
+
|
| 10 |
# --- Logging ---
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
logger = logging.getLogger("tts_stream")
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
+
|
| 16 |
+
# Charger les clés valides depuis l'environnement
|
| 17 |
+
VALID_HF_KEYS = os.environ.get('VALID_HF_KEYS', '').split(',')
|
| 18 |
+
logger.info(f"✅ Loaded {len(VALID_HF_KEYS)} valid API key(s)")
|
| 19 |
+
logger.info(f"🔑 First key preview: {VALID_HF_KEYS[0][:10] + '...' if VALID_HF_KEYS and VALID_HF_KEYS[0] else 'NONE'}")
|
| 20 |
+
|
| 21 |
# Initialiser le pipeline au démarrage
|
| 22 |
pipeline = KPipeline(lang_code='a', device='cpu')
|
| 23 |
logger.info("✅ KPipeline loaded successfully.")
|
| 24 |
+
|
|
|
|
| 25 |
class TTSRequest(BaseModel):
|
| 26 |
text: str
|
| 27 |
voice: str = 'af_heart'
|
|
|
|
| 29 |
|
| 30 |
@app.post("/tts/stream")
|
| 31 |
async def stream_speech(request: TTSRequest, hf_key: str = Header(None)):
|
| 32 |
+
logger.info(f"🚀 Request: text='{request.text[:30]}...', voice='{request.voice}'")
|
| 33 |
+
logger.info(f"🔑 Received key: {hf_key[:10] + '...' if hf_key else 'NONE'}")
|
| 34 |
+
|
| 35 |
+
# Vérifier le token
|
| 36 |
+
if not hf_key or hf_key not in VALID_HF_KEYS:
|
| 37 |
+
logger.error(f"❌ Unauthorized: key not in valid list")
|
| 38 |
raise HTTPException(status_code=401, detail="Unauthorized: invalid or missing hf_key")
|
| 39 |
+
|
| 40 |
+
logger.info(f"✅ Key validated, generating audio...")
|
| 41 |
+
|
| 42 |
def generate():
|
| 43 |
chunk_index = 0
|
| 44 |
try:
|
| 45 |
for result in pipeline(request.text, voice=request.voice, speed=request.speed):
|
| 46 |
chunk_index += 1
|
|
|
|
| 47 |
audio_bytes = result.audio.numpy().astype(np.float32).tobytes()
|
| 48 |
logger.info(f"✅ Chunk {chunk_index} ready, size={len(audio_bytes)} bytes")
|
| 49 |
yield audio_bytes
|
| 50 |
+
logger.info(f"🏁 Streaming finished: {chunk_index} chunks")
|
| 51 |
except Exception as e:
|
| 52 |
+
logger.error(f"❌ Error at chunk {chunk_index}: {e}")
|
| 53 |
raise
|
| 54 |
|
| 55 |
return StreamingResponse(
|
|
|
|
| 60 |
"X-Channels": "1",
|
| 61 |
"X-Bit-Depth": "32"
|
| 62 |
}
|
| 63 |
+
)
|