|
from fastapi import FastAPI, HTTPException, Response, Request |
|
from fastapi.responses import HTMLResponse |
|
from fastapi.staticfiles import StaticFiles |
|
from fastapi.templating import Jinja2Templates |
|
from pydantic import BaseModel, Field |
|
from typing import Optional |
|
from vocify import generate_speech |
|
import os |
|
from fastapi.middleware.cors import CORSMiddleware |
|
|
|
|
|
os.makedirs("static", exist_ok=True) |
|
os.makedirs("templates", exist_ok=True) |
|
|
|
|
|
app = FastAPI( |
|
title="Pyxilabs._.Vocify", |
|
description="A Text-to-Speech API", |
|
swagger_ui_parameters={"favicon": "/static/icon.png"} |
|
) |
|
|
|
|
|
app.mount("/static", StaticFiles(directory="static"), name="static") |
|
|
|
|
|
templates = Jinja2Templates(directory="templates") |
|
|
|
|
|
MODEL_INFO = { |
|
"Pyx r1-voice": { |
|
"name": "Pyx r1-voice", |
|
"created": "2024-12-12", |
|
"owner": "Pyxilabs AI Studio", |
|
"voices": { |
|
|
|
"seraphina": "XB0fDUnXU5powFXDhCwa", |
|
"isabella": "LcfcDJNUP1GQjkzn1xUU", |
|
"astrid": "jsCqWAovK2LkecY7zXl4", |
|
"lila": "jBpfuIE2acCO8z3wKNLl", |
|
"elara": "z9fAnlkpzviPz146aGWa", |
|
"evelyn": "oWAxZDx7w5VEj9dCyTzz", |
|
|
|
|
|
"sebastian": "onwK4e9ZLuTAKqWW03F9", |
|
"finnian": "N2lVS1w4EtoT3dr4eOWO", |
|
"theodore": "IKne3meq5aSn9XLyUdCD", |
|
"magnus": "2EiwWnXFnvU5JabPnv8n", |
|
"oliver": "CYw3kZ02Hs0563khs1Fj", |
|
"liam": "g5CIjZEefAph4nQFvHAz", |
|
"arthur": "SOYHLrjzK2X1ezoPC6cr", |
|
"elliot": "ZQe5CZNOzWyzPSCn5a3c", |
|
"nathaniel": "bVMeCyTHy58xNoL34h3p", |
|
|
|
|
|
"rowan": "D38z5RcWu1voky8WS1ja", |
|
"avery": "zcAOhNBS3c14rBihAFp1", |
|
} |
|
} |
|
} |
|
|
|
|
|
class SpeechRequest(BaseModel): |
|
model: Optional[str] = Field(default="Pyx r1-voice") |
|
input: str = Field(..., max_length=5000) |
|
voice: str |
|
|
|
|
|
@app.post("/v1/audio/speech") |
|
@app.get("/v1/audio/speech") |
|
async def create_speech(request: Request, speech_request: SpeechRequest): |
|
try: |
|
if speech_request.model != "Pyx r1-voice": |
|
raise HTTPException(status_code=400, detail="Invalid model. Only 'Pyx r1-voice' is supported.") |
|
|
|
if speech_request.voice not in MODEL_INFO["Pyx r1-voice"]["voices"]: |
|
raise HTTPException(status_code=400, detail="Invalid voice ID") |
|
|
|
voice_id = MODEL_INFO["Pyx r1-voice"]["voices"][speech_request.voice] |
|
|
|
result = generate_speech( |
|
model="eleven_multilingual_v2", |
|
voice=voice_id, |
|
input_text=speech_request.input |
|
) |
|
|
|
if isinstance(result, list): |
|
raise HTTPException(status_code=result[0], detail=result[1]) |
|
|
|
return Response(content=result, media_type="audio/mpeg") |
|
|
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=str(e)) |
|
|
|
|
|
@app.get("/v1/models") |
|
async def get_models(): |
|
models_response = [] |
|
for model_id, model_data in MODEL_INFO.items(): |
|
model_info = { |
|
"id": model_id, |
|
"name": model_data["name"], |
|
"created": model_data["created"], |
|
"owner": model_data["owner"], |
|
"vocals": [ |
|
{"id": voice_name} |
|
for voice_name in model_data["voices"] |
|
] |
|
} |
|
models_response.append(model_info) |
|
|
|
return {"models": models_response} |
|
|
|
|
|
@app.get("/", response_class=HTMLResponse) |
|
async def root(request: Request): |
|
with open("templates/index.html", "r") as file: |
|
return HTMLResponse(content=file.read()) |
|
|
|
|
|
@app.get("/favicon.ico", include_in_schema=False) |
|
async def favicon(): |
|
return {"url": "/static/icon.png"} |
|
|
|
|
|
if __name__ == "__main__": |
|
import uvicorn |
|
uvicorn.run(app, host="0.0.0.0", port=7860) |
|
|