From 6504aeba0d153494144572d18084cae460617822 Mon Sep 17 00:00:00 2001 From: Andrei Stoica Date: Tue, 20 Feb 2024 11:31:36 -0500 Subject: [PATCH] sending conversation to backend --- speech-speech/frontend/src/App.tsx | 54 +++++++++++++++++++++++++-- speech-speech/frontend/vite.config.ts | 11 ++++-- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/speech-speech/frontend/src/App.tsx b/speech-speech/frontend/src/App.tsx index e1516c4..1ec4db4 100644 --- a/speech-speech/frontend/src/App.tsx +++ b/speech-speech/frontend/src/App.tsx @@ -1,5 +1,10 @@ import { useState } from "react"; -import { TbMicrophone2, TbPlayerPlay, TbPlayerStop } from "react-icons/tb"; +import { + TbBrandOpenai, + TbMicrophone2, + TbPlayerPlay, + TbPlayerStop, +} from "react-icons/tb"; import "./App.css"; function Header() { @@ -13,8 +18,12 @@ function Header() { } let audioBlobs = []; -let streamBeingCaptured = null; -let mediaRecorder = null; +let streamBeingCaptured: MediaStream | null = null; +let mediaRecorder: MediaRecorder | null = null; +let chat: Array = [{ + role: "system", + content: "You are a helpful assistant.", +}]; function get_mic() { if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { @@ -26,6 +35,7 @@ function get_mic() { } function startRecord() { + audioBlobs = [] get_mic().then((stream) => { streamBeingCaptured = stream; mediaRecorder = new MediaRecorder(stream); @@ -33,7 +43,7 @@ function startRecord() { mediaRecorder.addEventListener("dataavailable", (event) => { audioBlobs.push(event.data); }); - mediaRecorder.start() + mediaRecorder.start(); }); } @@ -74,6 +84,33 @@ function Controls() { setRecordState(false); } } + + function sendAudio() { + var formData = new FormData(); + formData.append("audio", new Blob(audioBlobs, { type: "audio/webm" })); + fetch("http://100.82.51.22:8001/get-text", { + "method": "POST", + "body": formData, + }).then((res) => res.json()) + .then((res) => { + console.log(res); + chat.push({ role: "user", content: res["user-transcript"] }); + console.log(chat); + send_msg(); + }); + } + + function send_msg() { + fetch("http://100.82.51.22:8001/conversation", { + "method": "POST", + "body": JSON.stringify(chat), + }).then((res) => res.json()) + .then((res) => { + chat.push(res) + console.log(res); + }); + } + return (
+ +
); } diff --git a/speech-speech/frontend/vite.config.ts b/speech-speech/frontend/vite.config.ts index 861b04b..0b87a57 100644 --- a/speech-speech/frontend/vite.config.ts +++ b/speech-speech/frontend/vite.config.ts @@ -1,7 +1,12 @@ -import { defineConfig } from 'vite' -import react from '@vitejs/plugin-react-swc' +import { defineConfig } from "vite"; +import react from "@vitejs/plugin-react-swc"; // https://vitejs.dev/config/ export default defineConfig({ plugins: [react()], -}) + server: { + headers: { + "Access-Control-Allow-Origin": '*', + }, + }, +});