Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import uuid
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
import torch
|
| 8 |
+
from TTS.api import TTS
|
| 9 |
+
|
| 10 |
+
# ---------------------------
|
| 11 |
+
# CONFIG
|
| 12 |
+
# ---------------------------
|
| 13 |
+
ALLOWED_API_KEYS = ["your_master_key_here"] # You can add/remove keys
|
| 14 |
+
MODEL_NAME = "coqui/XTTS-v2"
|
| 15 |
+
OUTPUT_DIR = "outputs"
|
| 16 |
+
|
| 17 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
# ---------------------------
|
| 20 |
+
# LOAD MODEL (CPU mode)
|
| 21 |
+
# ---------------------------
|
| 22 |
+
device = "cpu"
|
| 23 |
+
tts = TTS(model_name=MODEL_NAME).to(device)
|
| 24 |
+
|
| 25 |
+
# ---------------------------
|
| 26 |
+
# FASTAPI INIT
|
| 27 |
+
# ---------------------------
|
| 28 |
+
app = FastAPI()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# ---------------------------
|
| 32 |
+
# REQUEST BODY
|
| 33 |
+
# ---------------------------
|
| 34 |
+
class TTSRequest(BaseModel):
|
| 35 |
+
api_key: str
|
| 36 |
+
text: str
|
| 37 |
+
language: str = "en"
|
| 38 |
+
speaker_wav: str | None = None
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# ---------------------------
|
| 42 |
+
# ROOT
|
| 43 |
+
# ---------------------------
|
| 44 |
+
@app.get("/")
|
| 45 |
+
def root():
|
| 46 |
+
return {"message": "XTTS Custom API Running Successfully!"}
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# ---------------------------
|
| 50 |
+
# TTS ENDPOINT
|
| 51 |
+
# ---------------------------
|
| 52 |
+
@app.post("/generate")
|
| 53 |
+
def generate_audio(req: TTSRequest):
|
| 54 |
+
|
| 55 |
+
# API KEY VALIDATION
|
| 56 |
+
if req.api_key not in ALLOWED_API_KEYS:
|
| 57 |
+
raise HTTPException(status_code=401, detail="Invalid API Key")
|
| 58 |
+
|
| 59 |
+
# FILE NAME
|
| 60 |
+
file_id = str(uuid.uuid4())
|
| 61 |
+
out_file = f"{OUTPUT_DIR}/{file_id}.wav"
|
| 62 |
+
|
| 63 |
+
# RUN TTS
|
| 64 |
+
try:
|
| 65 |
+
tts.tts_to_file(
|
| 66 |
+
text=req.text,
|
| 67 |
+
file_path=out_file,
|
| 68 |
+
speaker_wav=req.speaker_wav,
|
| 69 |
+
language=req.language
|
| 70 |
+
)
|
| 71 |
+
except Exception as e:
|
| 72 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 73 |
+
|
| 74 |
+
# RETURN AUDIO FILE
|
| 75 |
+
return {"status": "success", "audio_url": f"/audio/{file_id}.wav"}
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# ---------------------------
|
| 79 |
+
# AUDIO FILE SERVE
|
| 80 |
+
# ---------------------------
|
| 81 |
+
@app.get("/audio/{file_name}")
|
| 82 |
+
def get_audio(file_name: str):
|
| 83 |
+
file_path = os.path.join(OUTPUT_DIR, file_name)
|
| 84 |
+
if not os.path.exists(file_path):
|
| 85 |
+
raise HTTPException(status_code=404, detail="File not found")
|
| 86 |
+
return FileResponse(path=file_path, media_type="audio/wav")
|