Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,17 +1,17 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from kokoro import KPipeline
|
| 5 |
import numpy as np
|
| 6 |
import io
|
| 7 |
import logging
|
| 8 |
-
|
| 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.")
|
|
@@ -22,9 +22,10 @@ class TTSRequest(BaseModel):
|
|
| 22 |
speed: float = 1.0
|
| 23 |
|
| 24 |
@app.post("/tts/stream")
|
| 25 |
-
async def stream_speech(request: TTSRequest):
|
| 26 |
logger.info(f"🚀 Streaming request received: text='{request.text[:50]}...', voice='{request.voice}', speed={request.speed}")
|
| 27 |
-
|
|
|
|
| 28 |
def generate():
|
| 29 |
chunk_index = 0
|
| 30 |
try:
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Header
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from kokoro import KPipeline
|
| 5 |
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 |
+
valid_hf_keys = os.getenv("hf_key")
|
| 15 |
# Initialiser le pipeline au démarrage
|
| 16 |
pipeline = KPipeline(lang_code='a', device='cpu')
|
| 17 |
logger.info("✅ KPipeline loaded successfully.")
|
|
|
|
| 22 |
speed: float = 1.0
|
| 23 |
|
| 24 |
@app.post("/tts/stream")
|
| 25 |
+
async def stream_speech(request: TTSRequest, hf_key: str = Header(None)):
|
| 26 |
logger.info(f"🚀 Streaming request received: text='{request.text[:50]}...', voice='{request.voice}', speed={request.speed}")
|
| 27 |
+
if hf_key is None or hf_key not in VALID_HF_KEYS:
|
| 28 |
+
raise HTTPException(status_code=401, detail="Unauthorized: invalid or missing hf_key")
|
| 29 |
def generate():
|
| 30 |
chunk_index = 0
|
| 31 |
try:
|