Download app.py from 5trix/xtts: direct link, hf CLI and curl.
- Browser
- Download file 1.46 kB
-
https://huggingface.co/spaces/5trix/xtts/resolve/main/app.py
- Command line
-
hf download hf://spaces/5trix/xtts/app.py
-
curl -L -o app.py https://huggingface.co/spaces/5trix/xtts/resolve/main/app.py
1.46 kB
| 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" | |
| def root(): | |
| return {"status": "running"} | |
| def debug_key(): | |
| return { | |
| "key_length": len(API_KEY), | |
| "key_preview": API_KEY[:4] + "...", | |
| "is_default": API_KEY == "changeme" | |
| } | |
| 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") | |
| 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 | |
| } |