From bf68a89c118c7eb6cf93d87b256e806911059731 Mon Sep 17 00:00:00 2001 From: erew123 <35898566+erew123@users.noreply.github.com> Date: Sun, 28 Jul 2024 08:52:00 +0100 Subject: [PATCH] /api/openai-voicemap API endpoint Can call /api/openai-voicemap with curl -X PUT "http://localhost:7851/api/openai-voicemap" -H "Content-Type: application/json" -d "{\"alloy\":\"female_01.wav\",\"echo\":\"female_01.wav\",\"fable\":\"female_01.wav\",\"nova\":\"female_01.wav\",\"onyx\":\"male_01.wav\",\"shimmer\":\"male_02.wav\"}" Caveats: - You have to update all the voices in one go, not just one voice. - Its up to you to ensure you have the correct voices for the TTS engine that's loaded in which you can find currently available on /api/voices - The in-memory mappings will update, but Gradio will have cached what was originally in the settings, so it wont display the changes in the Gradio interface without a restart of AllTalk/Gradio --- tts_server.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/tts_server.py b/tts_server.py index 499084b..763d82a 100644 --- a/tts_server.py +++ b/tts_server.py @@ -216,8 +216,6 @@ async def apifunction_reload(request: Request): success = await model_engine.handle_tts_method_change(requested_model) if success: model_engine.current_model_loaded = requested_model - print(f"[{branding}ENG]") - print(f"[{branding}ENG] {branding} Server Ready.") model_currently_changing == False return Response(content=json.dumps({"status": "model-success"}), media_type="application/json") else: @@ -851,6 +849,38 @@ async def transcode_for_openai(input_file, output_format): print(f"[{branding}Debug] Transcoded file: {output_file}") if debug_openai else None return output_file +###################################################################################### +# API Endpoint - OpenAI Speech API compatable endpoint change engine voices Function # +###################################################################################### +class VoiceMappings(BaseModel): + alloy: str + echo: str + fable: str + nova: str + onyx: str + shimmer: str + +@app.put("/api/openai-voicemap") +async def update_openai_voice_mappings(mappings: VoiceMappings): + # Update in-memory versions (wont display in Gradio without a restart as gradio caches) + model_engine.openai_alloy = mappings.alloy + model_engine.openai_echo = mappings.echo + model_engine.openai_fable = mappings.fable + model_engine.openai_nova = mappings.nova + model_engine.openai_onyx = mappings.onyx + model_engine.openai_shimmer = mappings.shimmer + # Update model_settings.json file for the currently loaded TTS engine + try: + settings_file = this_dir / "system" / "tts_engines" / model_engine.engine_loaded / "model_settings.json" + with open(settings_file, "r+") as f: + settings = json.load(f) + settings["openai_voices"] = mappings.dict() + f.seek(0) + json.dump(settings, f, indent=4) + f.truncate() + except Exception as e: + raise HTTPException(status_code=500, detail=f"Failed to update model settings file: {str(e)}") + return {"message": "OpenAI voice mappings updated successfully"} ####################### # Play at the console #