From 8a94beae31b70cf4d23684e56397698c10002bda Mon Sep 17 00:00:00 2001 From: sageTheDM Date: Fri, 11 Oct 2024 09:25:23 +0200 Subject: [PATCH] voice_backend comments --- app/backend/voice_backend.ts | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/app/backend/voice_backend.ts b/app/backend/voice_backend.ts index d950380..c4a9a09 100644 --- a/app/backend/voice_backend.ts +++ b/app/backend/voice_backend.ts @@ -1,24 +1,26 @@ import axios from "axios"; export const sendToVoiceRecognition = (audio_data: Blob): Promise => { + // Create a new FormData instance to send the audio file + const formdata = new FormData(); + formdata.append("audio", audio_data); // Append the audio data to the FormData - const formdata = new FormData() - formdata.append("audio", audio_data) - - const apiURL = new URL("http://localhost:5000/interstellar_ai/api/voice_recognition") + // Set the API URL dynamically based on the environment + const apiURL = new URL("http://localhost:5000/interstellar_ai/api/voice_recognition"); if (typeof window !== 'undefined') { - apiURL.hostname = window.location.hostname; + apiURL.hostname = window.location.hostname; // Use the current hostname in the browser } else { - apiURL.hostname = "localhost" + apiURL.hostname = "localhost"; // Fallback for server-side } + // Send the audio data to the API using POST request return axios.post(apiURL.href, formdata) .then((response) => { - return response.data.response + return response.data.response; // Return the response from the API }) .catch(error => { - console.log("Error calling API:", error) - postMessage({ status: 500 }) - return "Error" - }) -} \ No newline at end of file + console.log("Error calling API:", error); // Log any error that occurs + postMessage({ status: 500 }); // Indicate an error status to the worker + return "Error"; // Return a fallback error message + }); +};