Create core/app.py
Browse files- core/app.py +77 -0
core/app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, Response, Request
|
2 |
+
from fastapi.responses import HTMLResponse
|
3 |
+
from fastapi.staticfiles import StaticFiles
|
4 |
+
from fastapi.templating import Jinja2Templates
|
5 |
+
from pydantic import BaseModel, Field
|
6 |
+
from typing import Optional
|
7 |
+
from vocify import generate_speech
|
8 |
+
import os
|
9 |
+
|
10 |
+
# Create necessary directories if they don't exist
|
11 |
+
os.makedirs("static", exist_ok=True)
|
12 |
+
os.makedirs("templates", exist_ok=True)
|
13 |
+
|
14 |
+
app = FastAPI(title="Pyxilabs._.Vocify", description="A Text-to-Speech API")
|
15 |
+
|
16 |
+
# Mount static files directory
|
17 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
18 |
+
|
19 |
+
# Initialize templates
|
20 |
+
templates = Jinja2Templates(directory="templates")
|
21 |
+
|
22 |
+
VOICE_MAPPING = {
|
23 |
+
"charlottee": "XB0fDUnXU5powFXDhCwa",
|
24 |
+
"daniel": "onwK4e9ZLuTAKqWW03F9",
|
25 |
+
"callum": "N2lVS1w4EtoT3dr4eOWO",
|
26 |
+
"charlie": "IKne3meq5aSn9XLyUdCD",
|
27 |
+
"clyde": "2EiwWnXFnvU5JabPnv8n",
|
28 |
+
"dave": "CYw3kZ02Hs0563khs1Fj",
|
29 |
+
"emily": "LcfcDJNUP1GQjkzn1xUU",
|
30 |
+
"ethan": "g5CIjZEefAph4nQFvHAz",
|
31 |
+
"fin": "D38z5RcWu1voky8WS1ja",
|
32 |
+
"freya": "jsCqWAovK2LkecY7zXl4",
|
33 |
+
"gigi": "jBpfuIE2acCO8z3wKNLl",
|
34 |
+
"giovanni": "zcAOhNBS3c14rBihAFp1",
|
35 |
+
"glinda": "z9fAnlkpzviPz146aGWa",
|
36 |
+
"grace": "oWAxZDx7w5VEj9dCyTzz",
|
37 |
+
"harry": "SOYHLrjzK2X1ezoPC6cr",
|
38 |
+
"james": "ZQe5CZNOzWyzPSCn5a3c",
|
39 |
+
"jeremy": "bVMeCyTHy58xNoL34h3p"
|
40 |
+
}
|
41 |
+
|
42 |
+
class SpeechRequest(BaseModel):
|
43 |
+
model: Optional[str] = Field(default="eleven_multilingual_v2")
|
44 |
+
input: str = Field(..., max_length=500)
|
45 |
+
voice: str
|
46 |
+
|
47 |
+
@app.post("/v1/audio/speech")
|
48 |
+
@app.get("/v1/audio/speech")
|
49 |
+
async def create_speech(request: SpeechRequest):
|
50 |
+
try:
|
51 |
+
result = generate_speech(
|
52 |
+
model=request.model,
|
53 |
+
voice=request.voice,
|
54 |
+
input_text=request.input
|
55 |
+
)
|
56 |
+
|
57 |
+
if isinstance(result, list):
|
58 |
+
raise HTTPException(status_code=result[0], detail=result[1])
|
59 |
+
|
60 |
+
return Response(content=result, media_type="audio/mpeg")
|
61 |
+
|
62 |
+
except Exception as e:
|
63 |
+
raise HTTPException(status_code=500, detail=str(e))
|
64 |
+
|
65 |
+
@app.post("/v1/voices")
|
66 |
+
@app.get("/v1/voices")
|
67 |
+
async def get_voices():
|
68 |
+
return {"voices": list(VOICE_MAPPING.keys())}
|
69 |
+
|
70 |
+
@app.get("/", response_class=HTMLResponse)
|
71 |
+
async def root(request: Request):
|
72 |
+
with open("templates/index.html", "r") as file:
|
73 |
+
return HTMLResponse(content=file.read())
|
74 |
+
|
75 |
+
if __name__ == "__main__":
|
76 |
+
import uvicorn
|
77 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|