Elevenlabs / SERVER_fastAPI.py
DevsDoCode's picture
Upload 4 files
03bbabb verified
from fastapi import FastAPI, HTTPException, Response
from pydantic import BaseModel, Field
from typing import Optional
from elevenlabs import generate_speech
app = FastAPI()
VOICE_MAPPING = {
"charlottee": "XB0fDUnXU5powFXDhCwa",
"daniel": "onwK4e9ZLuTAKqWW03F9",
"callum": "N2lVS1w4EtoT3dr4eOWO",
"charlie": "IKne3meq5aSn9XLyUdCD",
"clyde": "2EiwWnXFnvU5JabPnv8n",
"dave": "CYw3kZ02Hs0563khs1Fj",
"emily": "LcfcDJNUP1GQjkzn1xUU",
"ethan": "g5CIjZEefAph4nQFvHAz",
"fin": "D38z5RcWu1voky8WS1ja",
"freya": "jsCqWAovK2LkecY7zXl4",
"gigi": "jBpfuIE2acCO8z3wKNLl",
"giovanni": "zcAOhNBS3c14rBihAFp1",
"glinda": "z9fAnlkpzviPz146aGWa",
"grace": "oWAxZDx7w5VEj9dCyTzz",
"harry": "SOYHLrjzK2X1ezoPC6cr",
"james": "ZQe5CZNOzWyzPSCn5a3c",
"jeremy": "bVMeCyTHy58xNoL34h3p"
}
class SpeechRequest(BaseModel):
model: Optional[str] = Field(default="eleven_multilingual_v2")
input: str = Field(..., max_length=500)
voice: str
@app.post("/v1/audio/speech")
@app.get("/v1/audio/speech")
async def create_speech(request: SpeechRequest):
try:
result = generate_speech(
model=request.model,
voice=request.voice,
input_text=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.post("/v1/voices")
@app.get("/v1/voices")
async def get_voices():
return {"voices": list(VOICE_MAPPING.keys())}
@app.get("/")
async def root():
return {
"message": "Welcome to the Elevenlabs Text-to-Speech API",
"usage": "Send a POST or GET request to /v1/audio/speech with the following JSON body: {'model': 'eleven_multilingual_v2', 'input': 'Your text here', 'voice': 'voice_name'}",
"openai_compatibility": "This API is designed to be compatible with OpenAI's text-to-speech endpoint structure.",
"sample_url": "https://devsdocode-elevenlabs.hf.space/v1/audio/speech?model=eleven_multilingual_v2&input=Hello+world&voice=charlottee",
"character_limit": "The API supports up to 500 characters at a time in the input text parameter.",
"available_voices": "To get a list of available voices, send a POST or GET request to /v1/voices"
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)