sending conversation to backend
This commit is contained in:
parent
70da8a4ddb
commit
6504aeba0d
|
|
@ -1,5 +1,10 @@
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { TbMicrophone2, TbPlayerPlay, TbPlayerStop } from "react-icons/tb";
|
import {
|
||||||
|
TbBrandOpenai,
|
||||||
|
TbMicrophone2,
|
||||||
|
TbPlayerPlay,
|
||||||
|
TbPlayerStop,
|
||||||
|
} from "react-icons/tb";
|
||||||
import "./App.css";
|
import "./App.css";
|
||||||
|
|
||||||
function Header() {
|
function Header() {
|
||||||
|
|
@ -13,8 +18,12 @@ function Header() {
|
||||||
}
|
}
|
||||||
|
|
||||||
let audioBlobs = [];
|
let audioBlobs = [];
|
||||||
let streamBeingCaptured = null;
|
let streamBeingCaptured: MediaStream | null = null;
|
||||||
let mediaRecorder = null;
|
let mediaRecorder: MediaRecorder | null = null;
|
||||||
|
let chat: Array<Object> = [{
|
||||||
|
role: "system",
|
||||||
|
content: "You are a helpful assistant.",
|
||||||
|
}];
|
||||||
|
|
||||||
function get_mic() {
|
function get_mic() {
|
||||||
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
|
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
|
||||||
|
|
@ -26,6 +35,7 @@ function get_mic() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function startRecord() {
|
function startRecord() {
|
||||||
|
audioBlobs = []
|
||||||
get_mic().then((stream) => {
|
get_mic().then((stream) => {
|
||||||
streamBeingCaptured = stream;
|
streamBeingCaptured = stream;
|
||||||
mediaRecorder = new MediaRecorder(stream);
|
mediaRecorder = new MediaRecorder(stream);
|
||||||
|
|
@ -33,7 +43,7 @@ function startRecord() {
|
||||||
mediaRecorder.addEventListener("dataavailable", (event) => {
|
mediaRecorder.addEventListener("dataavailable", (event) => {
|
||||||
audioBlobs.push(event.data);
|
audioBlobs.push(event.data);
|
||||||
});
|
});
|
||||||
mediaRecorder.start()
|
mediaRecorder.start();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -74,6 +84,33 @@ function Controls() {
|
||||||
setRecordState(false);
|
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 (
|
return (
|
||||||
<div className="controls self-center flex justify-evenly p-5 text-5xl border-2 border-b-0 w-1/2 max-w-screen-sm min-w-fit">
|
<div className="controls self-center flex justify-evenly p-5 text-5xl border-2 border-b-0 w-1/2 max-w-screen-sm min-w-fit">
|
||||||
<button
|
<button
|
||||||
|
|
@ -90,6 +127,15 @@ function Controls() {
|
||||||
>
|
>
|
||||||
<TbPlayerPlay /> PLAY
|
<TbPlayerPlay /> PLAY
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
sendAudio();
|
||||||
|
}}
|
||||||
|
className="inline-flex"
|
||||||
|
>
|
||||||
|
<TbBrandOpenai /> SEND
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,12 @@
|
||||||
import { defineConfig } from 'vite'
|
import { defineConfig } from "vite";
|
||||||
import react from '@vitejs/plugin-react-swc'
|
import react from "@vitejs/plugin-react-swc";
|
||||||
|
|
||||||
// https://vitejs.dev/config/
|
// https://vitejs.dev/config/
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [react()],
|
plugins: [react()],
|
||||||
})
|
server: {
|
||||||
|
headers: {
|
||||||
|
"Access-Control-Allow-Origin": '*',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue