From ce31113abddca08aaa71222de7a875483326e92d Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Mon, 5 Feb 2024 21:36:03 -0800 Subject: [PATCH 1/5] refac: web api voice --- .../chat/Messages/ResponseMessage.svelte | 20 ++++-- src/lib/components/chat/Settings/Voice.svelte | 70 +++++++++---------- src/lib/stores/index.ts | 1 - 3 files changed, 48 insertions(+), 43 deletions(-) diff --git a/src/lib/components/chat/Messages/ResponseMessage.svelte b/src/lib/components/chat/Messages/ResponseMessage.svelte index c28eaffa..101dae56 100644 --- a/src/lib/components/chat/Messages/ResponseMessage.svelte +++ b/src/lib/components/chat/Messages/ResponseMessage.svelte @@ -1,7 +1,7 @@ @@ -31,7 +51,10 @@ class="flex flex-col h-full justify-between space-y-3 text-sm" on:submit|preventDefault={() => { saveSettings({ - speaker: speaker !== '' ? speaker : undefined + speech: { + engine: engine !== '' ? engine : undefined, + speaker: speaker !== '' ? speaker : undefined + } }); dispatch('save'); }} @@ -42,10 +65,16 @@
+ {#each voices as voice} + + {/each} + +
+ + {/if} From 1b040143eb14d5344ca0d83333ca7b9ce728b71f Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Mon, 5 Feb 2024 22:57:24 -0800 Subject: [PATCH 3/5] feat: cache request body --- backend/apps/openai/main.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/backend/apps/openai/main.py b/backend/apps/openai/main.py index 0e6bfb26..ee83979a 100644 --- a/backend/apps/openai/main.py +++ b/backend/apps/openai/main.py @@ -81,10 +81,15 @@ async def speech(request: Request, user=Depends(get_current_user)): body = await request.body() - filename = hashlib.sha256(body).hexdigest() + ".mp3" + print(body) + print(type(body)) + + name = hashlib.sha256(body).hexdigest() + SPEECH_CACHE_DIR = Path(CACHE_DIR).joinpath("./audio/speech/") SPEECH_CACHE_DIR.mkdir(parents=True, exist_ok=True) - file_path = SPEECH_CACHE_DIR.joinpath(filename) + file_path = SPEECH_CACHE_DIR.joinpath(f"{name}.mp3") + file_body_path = SPEECH_CACHE_DIR.joinpath(f"{name}.json") print(file_path) @@ -114,6 +119,9 @@ async def speech(request: Request, user=Depends(get_current_user)): for chunk in r.iter_content(chunk_size=8192): f.write(chunk) + with open(file_body_path, "w") as f: + json.dump(json.loads(body.decode("utf-8")), f) + # Return the saved file return FileResponse(file_path) From 519e27978fac02fe97d27fee4db9e3f3860949da Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Mon, 5 Feb 2024 22:58:02 -0800 Subject: [PATCH 4/5] refac --- backend/apps/openai/main.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/backend/apps/openai/main.py b/backend/apps/openai/main.py index ee83979a..bed9181a 100644 --- a/backend/apps/openai/main.py +++ b/backend/apps/openai/main.py @@ -81,9 +81,6 @@ async def speech(request: Request, user=Depends(get_current_user)): body = await request.body() - print(body) - print(type(body)) - name = hashlib.sha256(body).hexdigest() SPEECH_CACHE_DIR = Path(CACHE_DIR).joinpath("./audio/speech/") @@ -91,8 +88,6 @@ async def speech(request: Request, user=Depends(get_current_user)): file_path = SPEECH_CACHE_DIR.joinpath(f"{name}.mp3") file_body_path = SPEECH_CACHE_DIR.joinpath(f"{name}.json") - print(file_path) - # Check if the file already exists in the cache if file_path.is_file(): return FileResponse(file_path) @@ -110,8 +105,6 @@ async def speech(request: Request, user=Depends(get_current_user)): stream=True, ) - print(r) - r.raise_for_status() # Save the streaming content to a file @@ -125,12 +118,6 @@ async def speech(request: Request, user=Depends(get_current_user)): # Return the saved file return FileResponse(file_path) - # return StreamingResponse( - # r.iter_content(chunk_size=8192), - # status_code=r.status_code, - # headers=dict(r.headers), - # ) - except Exception as e: print(e) error_detail = "Ollama WebUI: Server Connection Error" From 30502676066d1546e0236b314af45dc45598205e Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Mon, 5 Feb 2024 23:14:24 -0800 Subject: [PATCH 5/5] feat: audio loading spinner added --- .../chat/Messages/ResponseMessage.svelte | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/lib/components/chat/Messages/ResponseMessage.svelte b/src/lib/components/chat/Messages/ResponseMessage.svelte index ded01391..7cd9da38 100644 --- a/src/lib/components/chat/Messages/ResponseMessage.svelte +++ b/src/lib/components/chat/Messages/ResponseMessage.svelte @@ -30,13 +30,14 @@ export let copyToClipboard: Function; export let regenerateResponse: Function; - let audioMap = {}; - let edit = false; let editedContent = ''; let tooltipInstance = null; + + let audioMap = {}; let speaking = null; + let loadingSpeech = false; $: tokens = marked.lexer(message.content); @@ -126,6 +127,7 @@ speaking = true; if ($settings?.speech?.engine === 'openai') { + loadingSpeech = true; const res = await synthesizeOpenAISpeech( localStorage.token, $settings?.speech?.speaker, @@ -140,6 +142,8 @@ const blobUrl = URL.createObjectURL(blob); console.log(blobUrl); + loadingSpeech = false; + const audio = new Audio(blobUrl); audioMap[message.id] = audio; @@ -458,10 +462,42 @@ ? 'visible' : 'invisible group-hover:visible'} p-1 rounded dark:hover:bg-gray-800 transition" on:click={() => { - toggleSpeakMessage(message); + if (!loadingSpeech) { + toggleSpeakMessage(message); + } }} > - {#if speaking} + {#if loadingSpeech} + + {:else if speaking}