interstellar_ai/app/backend/voice_backend.ts

27 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2024-09-25 12:33:52 +02:00
import axios from "axios";
2024-09-30 12:45:19 +02:00
export const sendToVoiceRecognition = (audio_data: Blob): Promise<string> => {
2024-10-11 09:25:23 +02:00
// 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
2024-09-26 09:37:33 +02:00
2024-10-11 09:25:23 +02:00
// Set the API URL dynamically based on the environment
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/voice_recognition");
2024-10-07 11:16:51 +02:00
if (typeof window !== 'undefined') {
2024-10-11 09:25:23 +02:00
apiURL.hostname = window.location.hostname; // Use the current hostname in the browser
2024-10-07 11:16:51 +02:00
} else {
2024-10-11 09:25:23 +02:00
apiURL.hostname = "localhost"; // Fallback for server-side
2024-10-07 11:16:51 +02:00
}
2024-10-11 09:25:23 +02:00
// Send the audio data to the API using POST request
return axios.post(apiURL.href, formdata)
2024-09-30 12:45:19 +02:00
.then((response) => {
2024-10-11 09:25:23 +02:00
return response.data.response; // Return the response from the API
2024-09-30 12:45:19 +02:00
})
.catch(error => {
2024-10-11 09:25:23 +02:00
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
});
};