File size: 1,463 Bytes
e7f9c93
 
 
 
774fc0a
e7f9c93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9382d8
 
 
 
 
 
 
 
 
e7f9c93
 
 
 
 
 
 
 
 
 
 
0aad7d0
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from fastapi import FastAPI, HTTPException, Header
from fastapi.responses import FileResponse
from pydantic import BaseModel
import torch, uuid, os
from TTS.api import TTS

app = FastAPI(title="XTTS v2 API")

print("Loading model...")
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2",
          gpu=torch.cuda.is_available())
print("Model ready!")

API_KEY = os.environ.get("API_KEY", "changeme")

class TTSRequest(BaseModel):
    text: str
    language: str = "en"
    speaker: str = "Damien Black"

@app.get("/")
def root():
    return {"status": "running"}

@app.get("/debug-key")
def debug_key():
    return {
        "key_length": len(API_KEY),
        "key_preview": API_KEY[:4] + "...",
        "is_default": API_KEY == "changeme"
    }

@app.post("/tts")
def generate(req: TTSRequest, x_api_key: str = Header(None)):
    if x_api_key != API_KEY:
        raise HTTPException(status_code=401, detail="Invalid API key")

    output = f"/tmp/{uuid.uuid4()}.wav"
    tts.tts_to_file(
        text=req.text,
        speaker=req.speaker,
        language=req.language,
        file_path=output
    )
    return FileResponse(output, media_type="audio/wav", filename="output.wav")

@app.post("/debug-auth")
def debug_auth(x_api_key: str = Header(None)):
    return {
        "received": x_api_key,
        "expected_preview": API_KEY[:4] + "...",
        "length_received": len(x_api_key) if x_api_key else 0,
        "matches": x_api_key == API_KEY
    }