Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- SERVER_fastAPI.py +39 -12
SERVER_fastAPI.py
CHANGED
|
@@ -1,18 +1,39 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException, Response
|
| 2 |
-
from pydantic import BaseModel
|
|
|
|
| 3 |
from elevenlabs import generate_speech
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
class SpeechRequest(BaseModel):
|
| 8 |
-
model: str
|
| 9 |
-
input: str
|
| 10 |
voice: str
|
| 11 |
|
| 12 |
@app.post("/v1/audio/speech")
|
|
|
|
| 13 |
async def create_speech(request: SpeechRequest):
|
| 14 |
try:
|
| 15 |
-
# Map the OpenAI-style request to your ElevenLabs function
|
| 16 |
result = generate_speech(
|
| 17 |
model=request.model,
|
| 18 |
voice=request.voice,
|
|
@@ -20,23 +41,29 @@ async def create_speech(request: SpeechRequest):
|
|
| 20 |
)
|
| 21 |
|
| 22 |
if isinstance(result, list):
|
| 23 |
-
# If result is a list, it means there was an error
|
| 24 |
raise HTTPException(status_code=result[0], detail=result[1])
|
| 25 |
|
| 26 |
-
# If successful, return the audio content
|
| 27 |
return Response(content=result, media_type="audio/mpeg")
|
| 28 |
|
| 29 |
except Exception as e:
|
| 30 |
raise HTTPException(status_code=500, detail=str(e))
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
@app.get("/")
|
| 33 |
async def root():
|
| 34 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|
| 37 |
import uvicorn
|
| 38 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 39 |
-
|
| 40 |
-
# print("Sample URL: http://localhost:7860/v1/audio/speech?model=eleven_multilingual_v2&input=Hello+world&voice=XB0fDUnXU5powFXDhCwa")
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException, Response
|
| 2 |
+
from pydantic import BaseModel, Field
|
| 3 |
+
from typing import Optional
|
| 4 |
from elevenlabs import generate_speech
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
VOICE_MAPPING = {
|
| 9 |
+
"charlottee": "XB0fDUnXU5powFXDhCwa",
|
| 10 |
+
"daniel": "onwK4e9ZLuTAKqWW03F9",
|
| 11 |
+
"callum": "N2lVS1w4EtoT3dr4eOWO",
|
| 12 |
+
"charlie": "IKne3meq5aSn9XLyUdCD",
|
| 13 |
+
"clyde": "2EiwWnXFnvU5JabPnv8n",
|
| 14 |
+
"dave": "CYw3kZ02Hs0563khs1Fj",
|
| 15 |
+
"emily": "LcfcDJNUP1GQjkzn1xUU",
|
| 16 |
+
"ethan": "g5CIjZEefAph4nQFvHAz",
|
| 17 |
+
"fin": "D38z5RcWu1voky8WS1ja",
|
| 18 |
+
"freya": "jsCqWAovK2LkecY7zXl4",
|
| 19 |
+
"gigi": "jBpfuIE2acCO8z3wKNLl",
|
| 20 |
+
"giovanni": "zcAOhNBS3c14rBihAFp1",
|
| 21 |
+
"glinda": "z9fAnlkpzviPz146aGWa",
|
| 22 |
+
"grace": "oWAxZDx7w5VEj9dCyTzz",
|
| 23 |
+
"harry": "SOYHLrjzK2X1ezoPC6cr",
|
| 24 |
+
"james": "ZQe5CZNOzWyzPSCn5a3c",
|
| 25 |
+
"jeremy": "bVMeCyTHy58xNoL34h3p"
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
class SpeechRequest(BaseModel):
|
| 29 |
+
model: Optional[str] = Field(default="eleven_multilingual_v2")
|
| 30 |
+
input: str = Field(..., max_length=500)
|
| 31 |
voice: str
|
| 32 |
|
| 33 |
@app.post("/v1/audio/speech")
|
| 34 |
+
@app.get("/v1/audio/speech")
|
| 35 |
async def create_speech(request: SpeechRequest):
|
| 36 |
try:
|
|
|
|
| 37 |
result = generate_speech(
|
| 38 |
model=request.model,
|
| 39 |
voice=request.voice,
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
if isinstance(result, list):
|
|
|
|
| 44 |
raise HTTPException(status_code=result[0], detail=result[1])
|
| 45 |
|
|
|
|
| 46 |
return Response(content=result, media_type="audio/mpeg")
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
raise HTTPException(status_code=500, detail=str(e))
|
| 50 |
|
| 51 |
+
@app.post("/v1/voices")
|
| 52 |
+
@app.get("/v1/voices")
|
| 53 |
+
async def get_voices():
|
| 54 |
+
return {"voices": list(VOICE_MAPPING.keys())}
|
| 55 |
+
|
| 56 |
@app.get("/")
|
| 57 |
async def root():
|
| 58 |
+
return {
|
| 59 |
+
"message": "Welcome to the Elevenlabs Text-to-Speech API",
|
| 60 |
+
"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'}",
|
| 61 |
+
"openai_compatibility": "This API is designed to be compatible with OpenAI's text-to-speech endpoint structure.",
|
| 62 |
+
"sample_url": "https://devsdocode-elevenlabs.hf.space/v1/audio/speech?model=eleven_multilingual_v2&input=Hello+world&voice=charlottee",
|
| 63 |
+
"character_limit": "The API supports up to 500 characters at a time in the input text parameter.",
|
| 64 |
+
"available_voices": "To get a list of available voices, send a POST or GET request to /v1/voices"
|
| 65 |
+
}
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
import uvicorn
|
| 69 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|