-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
65 lines (51 loc) · 1.87 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import loguru
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from fastapi.exceptions import HTTPException
from fastapi.middleware.cors import CORSMiddleware
from elevenlabs import APIError
from uvicorn.server import Server
from uvicorn.config import Config
from src.voice_handler import main as voice_gen
from interface import Data
import src
__all__ = ["src"]
logger = loguru.logger
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/", tags=["voice_generation"])
def root(data: Data):
"""
Endpoint for generating voice based on the received text and voice ID.
Args:
data (Data): The data object containing the text and voice ID.
Returns:
JSONResponse: The response object with the generated voice message.
Raises:
HTTPException: If the API request fails.
APIError: If there is an error in the API request.
"""
received_data: Data = data
text = received_data.text
voice_id = received_data.voice_id
try:
response = voice_gen(
text=text, voice_id=voice_id, out_path="src/audio/in/input.txt"
)
return JSONResponse(status_code=200, content={"message": f"{response}"})
except HTTPException as erorr:
logger.exception(f"API request failed: \n{erorr}\n{text}\n{voice_id}")
return JSONResponse(status_code=500, content={"error": str(erorr)})
except APIError as erorr:
logger.exception(f"API request failed: \n{erorr}\n{text}\n{voice_id}")
return JSONResponse(status_code=401, content={"error": str(erorr)})
if __name__ == "__main__":
server = Server(Config(app=app, host="localhost", port=9003, reload=True))
server.run()