3x new API endpoints

/api/voices/
/api/previewvoice/
/api/ready/
This commit is contained in:
erew123 authored and GitHub committed 2023-12-29 09:56:51 +00:00
1 parent 95659d5b7a
commit 65e257077c
1 file changed
+57
+57
View File
@@ -619,6 +619,53 @@ async def get_audio(filename: str):
audio_path = this_dir / "outputs" / filename
return FileResponse(audio_path)
#########################
#### VOICES LIST API ####
#########################
# Define the new endpoint
@app.get("/api/voices")
async def get_voices():
wav_files = list_files(this_dir / "voices")
return {"voices": wav_files}
###########################
#### PREVIEW VOICE API ####
###########################
@app.post("/api/previewvoice/", response_class=JSONResponse)
async def preview_voice(request: Request, voice: str = Form(...)):
try:
# Hardcoded settings
language = "en"
output_file_name = "api_preview_voice"
# Clean the voice filename for inclusion in the text
clean_voice_filename = re.sub(r'\.wav$', '', voice.replace(' ', '_'))
clean_voice_filename = re.sub(r'[^a-zA-Z0-9]', ' ', clean_voice_filename)
# Generate the audio
text = f"Hello, this is a preview of voice {clean_voice_filename}."
# Generate the audio
output_file_path = this_dir / "outputs" / f"{output_file_name}.wav"
await generate_audio(text, voice, language, output_file_path)
# Generate the URL
output_file_url = f'http://{params["ip_address"]}:{params["port_number"]}/audio/{output_file_name}.wav'
# Return the response with both local file path and URL
return JSONResponse(
content={
"status": "generate-success",
"output_file_path": str(output_file_path),
"output_file_url": str(output_file_url),
},
status_code=200,
)
except Exception as e:
print(f"An error occurred: {e}")
return JSONResponse(content={"error": "An error occurred"}, status_code=500)
########################
#### GENERATION API ####
########################
@@ -1717,10 +1764,20 @@ template = Template(simple_webpage)
# Render the template with the dynamic values
rendered_html = template.render(params=params)
###############################
#### Internal script ready ####
###############################
@app.get("/ready")
async def ready():
return Response("Ready endpoint")
############################
#### External API ready ####
############################
@app.get("/api/ready")
async def ready():
return Response("Ready")
@app.get("/")
async def read_root():
return HTMLResponse(content=rendered_html, status_code=200)