2024-02-20 17:29:37 -05:00
|
|
|
import { useEffect, useRef, useState } from "react";
|
2024-02-20 11:31:36 -05:00
|
|
|
import {
|
|
|
|
|
TbBrandOpenai,
|
|
|
|
|
TbMicrophone2,
|
|
|
|
|
TbPlayerPlay,
|
|
|
|
|
TbPlayerStop,
|
|
|
|
|
} from "react-icons/tb";
|
2024-02-19 19:03:08 -05:00
|
|
|
import "./App.css";
|
|
|
|
|
|
2024-02-20 17:29:37 -05:00
|
|
|
type ChatMsg = {
|
|
|
|
|
role: string;
|
|
|
|
|
content: string;
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-25 17:47:25 -05:00
|
|
|
let audioBlobs: Array<Blob> = [];
|
2024-02-20 11:31:36 -05:00
|
|
|
let streamBeingCaptured: MediaStream | null = null;
|
|
|
|
|
let mediaRecorder: MediaRecorder | null = null;
|
2024-02-25 13:40:08 -05:00
|
|
|
|
2024-02-19 19:03:08 -05:00
|
|
|
function get_mic() {
|
|
|
|
|
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
|
|
|
|
|
console.log("getUserMedia supported.");
|
|
|
|
|
return navigator.mediaDevices.getUserMedia({ audio: true });
|
|
|
|
|
}
|
2024-02-25 17:47:25 -05:00
|
|
|
throw "getUserMedia not supported on your browser!";
|
2024-02-19 19:03:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function startRecord() {
|
2024-02-20 17:29:37 -05:00
|
|
|
audioBlobs = [];
|
2024-02-19 19:03:08 -05:00
|
|
|
get_mic().then((stream) => {
|
2024-02-25 17:47:25 -05:00
|
|
|
console.log("got mic");
|
2024-02-19 19:03:08 -05:00
|
|
|
streamBeingCaptured = stream;
|
|
|
|
|
mediaRecorder = new MediaRecorder(stream);
|
|
|
|
|
console.log("Starting Recording");
|
|
|
|
|
mediaRecorder.addEventListener("dataavailable", (event) => {
|
|
|
|
|
audioBlobs.push(event.data);
|
|
|
|
|
});
|
2024-02-20 11:31:36 -05:00
|
|
|
mediaRecorder.start();
|
2024-02-19 19:03:08 -05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stopRecord() {
|
2024-02-25 17:47:25 -05:00
|
|
|
if (!mediaRecorder) {
|
|
|
|
|
throw "MediaRecorder not set";
|
|
|
|
|
}
|
|
|
|
|
if (!streamBeingCaptured) {
|
|
|
|
|
throw "Stream not set";
|
|
|
|
|
}
|
2024-02-19 19:03:08 -05:00
|
|
|
mediaRecorder.stop();
|
|
|
|
|
streamBeingCaptured.getTracks()
|
|
|
|
|
.forEach((track) => track.stop());
|
|
|
|
|
mediaRecorder = null;
|
|
|
|
|
streamBeingCaptured = null;
|
|
|
|
|
console.log("Starting Recording");
|
|
|
|
|
console.log(audioBlobs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function playRecord() {
|
|
|
|
|
const audioBlob = new Blob(audioBlobs, { type: "audio/webm" });
|
|
|
|
|
const audioUrl = URL.createObjectURL(audioBlob);
|
2024-02-25 17:47:25 -05:00
|
|
|
const audio = new Audio(audioUrl);
|
2024-02-25 13:40:08 -05:00
|
|
|
audio.play();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function playMsg(msg: ChatMsg) {
|
2024-02-25 17:47:25 -05:00
|
|
|
const audio = new Audio(
|
|
|
|
|
"http://100.82.51.22:8001/speak?" +
|
|
|
|
|
new URLSearchParams({ text: msg.content }),
|
|
|
|
|
);
|
|
|
|
|
console.log("loading audio and playing?");
|
2024-02-19 19:03:08 -05:00
|
|
|
audio.play();
|
|
|
|
|
}
|
2024-02-25 13:40:08 -05:00
|
|
|
function Header() {
|
|
|
|
|
return (
|
|
|
|
|
<header className="header p-3">
|
|
|
|
|
<div className="title text-5xl font-extrabold">
|
|
|
|
|
Speach to Speech AI example
|
|
|
|
|
</div>
|
|
|
|
|
</header>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 17:47:25 -05:00
|
|
|
function Feed(props: { chat: Array<ChatMsg>; setChatStateFn: any }) {
|
|
|
|
|
const bottomRef = useRef<any>(null);
|
2024-02-20 17:29:37 -05:00
|
|
|
|
|
|
|
|
const scrollToBottom = () => {
|
2024-02-25 17:47:25 -05:00
|
|
|
if (bottomRef.current) {
|
|
|
|
|
bottomRef.current.scrollIntoView({ behavior: "smooth" });
|
|
|
|
|
}
|
2024-02-20 17:29:37 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
scrollToBottom();
|
|
|
|
|
console.log("scroll?");
|
|
|
|
|
});
|
|
|
|
|
|
2024-02-19 19:03:08 -05:00
|
|
|
return (
|
2024-02-20 17:29:37 -05:00
|
|
|
<div className="feed grow self-center w-5/6 max-w-screen-lg px-6 py-3 overflow-scroll">
|
|
|
|
|
<div className="content-center space-y-2 divide-y-4">
|
|
|
|
|
{props.chat.filter((m: ChatMsg) => m.role != "system").map((
|
|
|
|
|
m: ChatMsg,
|
2024-02-25 13:40:08 -05:00
|
|
|
i: number,
|
|
|
|
|
) => <Msg key={i} msg={m} />)}
|
2024-02-20 17:29:37 -05:00
|
|
|
</div>
|
|
|
|
|
<div ref={bottomRef} />
|
2024-02-19 19:03:08 -05:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-20 17:29:37 -05:00
|
|
|
function Msg(props: { msg: ChatMsg }) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="Messege text-lg">
|
|
|
|
|
<span className="font-bold">
|
|
|
|
|
{props.msg.role.toUpperCase()}:
|
|
|
|
|
</span>
|
|
|
|
|
<br />
|
|
|
|
|
<span className="ml-8">
|
|
|
|
|
{props.msg.content}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 17:47:25 -05:00
|
|
|
function Controls(props: { setChatStateFn: any, chat: Array<ChatMsg> }) {
|
2024-02-19 19:03:08 -05:00
|
|
|
const [recordState, setRecordState] = useState(false);
|
|
|
|
|
|
|
|
|
|
function toggleRecord() {
|
|
|
|
|
if (recordState == false) {
|
|
|
|
|
startRecord();
|
|
|
|
|
setRecordState(true);
|
|
|
|
|
} else {
|
|
|
|
|
stopRecord();
|
|
|
|
|
setRecordState(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-20 11:31:36 -05:00
|
|
|
|
|
|
|
|
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) => {
|
2024-02-25 17:47:25 -05:00
|
|
|
props.setChatStateFn((curState: Array<ChatMsg>) => [
|
2024-02-20 17:29:37 -05:00
|
|
|
...curState,
|
|
|
|
|
{ "role": "user", "content": res["user-transcript"] },
|
|
|
|
|
]);
|
|
|
|
|
fetch("http://100.82.51.22:8001/conversation", {
|
|
|
|
|
"method": "POST",
|
|
|
|
|
"body": JSON.stringify([...props.chat, {
|
|
|
|
|
"role": "user",
|
|
|
|
|
"content": res["user-transcript"],
|
|
|
|
|
}]),
|
|
|
|
|
}).then((res) => res.json())
|
|
|
|
|
.then((res) => {
|
2024-02-25 17:47:25 -05:00
|
|
|
props.setChatStateFn((
|
|
|
|
|
curState: Array<ChatMsg>,
|
|
|
|
|
) => [...curState, res]);
|
|
|
|
|
console.log("attempting to play result");
|
|
|
|
|
playMsg(res);
|
2024-02-20 17:29:37 -05:00
|
|
|
});
|
2024-02-20 11:31:36 -05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 19:03:08 -05:00
|
|
|
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">
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => toggleRecord()}
|
|
|
|
|
className={"inline-flex " + (recordState ? "text-red-500" : "")}
|
|
|
|
|
>
|
|
|
|
|
{recordState ? <TbPlayerStop /> : <TbMicrophone2 />}
|
|
|
|
|
{recordState ? "STOP" : "REC"}
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => playRecord()}
|
|
|
|
|
className="inline-flex text-green-500"
|
|
|
|
|
>
|
|
|
|
|
<TbPlayerPlay /> PLAY
|
|
|
|
|
</button>
|
2024-02-20 11:31:36 -05:00
|
|
|
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => {
|
|
|
|
|
sendAudio();
|
|
|
|
|
}}
|
|
|
|
|
className="inline-flex"
|
|
|
|
|
>
|
|
|
|
|
<TbBrandOpenai /> SEND
|
|
|
|
|
</button>
|
2024-02-19 19:03:08 -05:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function App() {
|
2024-02-20 17:29:37 -05:00
|
|
|
const [chatState, setChatState] = useState([{
|
|
|
|
|
role: "system",
|
|
|
|
|
content: "You are a helpful assistant.",
|
|
|
|
|
}]);
|
|
|
|
|
|
2024-02-19 19:03:08 -05:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<div className="h-screen center flex flex-col">
|
|
|
|
|
<div className="w-full max-w-screen-xl self-center">
|
|
|
|
|
<Header />
|
|
|
|
|
<hr className="mx-3 border-t-4" />
|
|
|
|
|
</div>
|
2024-02-20 17:29:37 -05:00
|
|
|
<Feed chat={chatState} setChatStateFn={setChatState} />
|
|
|
|
|
<Controls setChatStateFn={setChatState} chat={chatState} />
|
2024-02-19 19:03:08 -05:00
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default App;
|