Skip to content

Commit

Permalink
Feature/api server (#18)
Browse files Browse the repository at this point in the history
* chore: loosen python version

* docs: update README

* feat: add vectortore example.

* feat: add API_SERVER_* env variables

* fix: useRef not working

* fix: use NEXT_PUBLIC
  • Loading branch information
siisee11 authored Jun 10, 2023
1 parent c31865d commit d494cf8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 27 deletions.
12 changes: 12 additions & 0 deletions apps/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,20 @@ poetry add <package-name>


### Env variable

mac
```
brew install direnv
```

ubuntu
```
$ apt install direnv
$ vim ~/.bashrc
add eval "$(direnv hook bash)"
```


Expand Down
Binary file added apps/api/vectorstore.example
Binary file not shown.
20 changes: 16 additions & 4 deletions apps/web/.env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
############## ENV EXAMPLE #############
################################################################################
### GENERAL SETTINGS
################################################################################

############## SUPABASE ################
NEXT_PUBLIC_SUPABASE_URL="https://efdnnygaricwqyzlnthc.supabase.co"
# API_SERVER
API_SERVER_URL="http://localhost:9000"
API_SERVER_WEB_SOCKET="ws://localhost:9000"

################################################################################
### SUPABASE
################################################################################

NEXT_PUBLIC_SUPABASE_URL=""
NEXT_PUBLIC_SUPABASE_ANON_KEY=""
SUPABASE_SERVICE_ROLE_KEY=""

############## OPENAI ##################
################################################################################
### OPENAI
################################################################################

OPENAI_API_KEY=""
14 changes: 7 additions & 7 deletions apps/web/src/components/CurveAnimation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ const CurveAnimation = forwardRef((props, ref) => {
let animatestep = useRef(0);
let toEndRef = useRef(false);
let isListenRef = useRef(false);
let isProcessRef = useRef(false);
let isRunningRef = useRef(false);

const canvasRef = useRef(null);

useImperativeHandle(ref, () => ({
listen: listen,
process: process,
run: run,
finish: finish,
end: end,
}));
Expand Down Expand Up @@ -135,7 +135,7 @@ const CurveAnimation = forwardRef((props, ref) => {
)
);
if (isListenRef.current) animatestep.current = 40;
if (isProcessRef.current) animatestep.current = 60;
if (isRunningRef.current) animatestep.current = 60;
acceleration.current = easing(animatestep.current, 0, 1, 240);

if (acceleration.current > 0.35) {
Expand Down Expand Up @@ -164,19 +164,19 @@ const CurveAnimation = forwardRef((props, ref) => {
const listen = () => {
toEndRef.current = true;
isListenRef.current = true;
isProcessRef.current = false;
isRunningRef.current = false;
};

const process = () => {
const run = () => {
toEndRef.current = true;
isListenRef.current = false;
isProcessRef.current = true;
isRunningRef.current = true;
};

const finish = () => {
toEndRef.current = true;
isListenRef.current = false;
isProcessRef.current = false;
isRunningRef.current = false;
};

const end = () => {
Expand Down
25 changes: 13 additions & 12 deletions apps/web/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ import axios from "axios";

export default function Home() {
const [ws, setWs] = useState<WebSocket>(null);
const [isProcessing, setIsProcessing] = useState(false);
const [isRunning, setIsRunning] = useState(false);
const audioRef = useRef(null);


const onTranscribe = async (blob: Blob) => {
if (childRef.current) {
childRef.current.process();
childRef.current.run();
}
setIsProcessing(true);
setIsRunning(true);
const formdata = new FormData();
const file = new File([blob], "speech.mp3", {
type: "audio/mpeg",
});
formdata.append("audioData", file, "speech.mp3");
formdata.append("model", "whisper-1");

const url = "http://localhost:9000/transcriptions";
const url = `${process.env.NEXT_PUBLIC_API_SERVER_URL}/transcriptions`;
console.log("url", url)
const response = await axios.post(url, formdata, {
headers: { "Content-Type": "multipart/form-data" },
});
Expand Down Expand Up @@ -62,7 +63,7 @@ export default function Home() {

const childRef = useRef({
listen: () => {},
process: () => {},
run: () => {},
finish: () => {},
end: () => {},
});
Expand All @@ -78,14 +79,14 @@ export default function Home() {
// start listen animation (breathing)
};

const process = async () => {
const run = async () => {
// start processing animation (fast spin)
stopRecording();
// -> onTranscribe
};

useEffect(() => {
const endpoint = "ws://localhost:9000/chat";
const endpoint = `${process.env.NEXT_PUBLIC_API_SERVER_WEB_SOCKET}/chat`;
const ws = new WebSocket(endpoint);

ws.onmessage = async function (event) {
Expand Down Expand Up @@ -115,7 +116,7 @@ export default function Home() {
if (enableTTS) {
const formdata = new FormData();
formdata.append("text", finalText);
const url = "http://localhost:9000/tts";
const url = `${process.env.NEXT_PUBLIC_API_SERVER_URL}/tts`;
const response = await axios.post(url, formdata, {
headers: { "Content-Type": "multipart/form-data" },
responseType: "blob",
Expand All @@ -131,9 +132,9 @@ export default function Home() {
console.log("end");
childRef.current.end();
}
setIsProcessing(false);
setIsRunning(false);
} else if (data.type === "error") {
setIsProcessing(false);
setIsRunning(false);
const p = messages.lastChild.lastChild as HTMLParagraphElement;
p.innerHTML += data.message;
}
Expand Down Expand Up @@ -174,8 +175,8 @@ export default function Home() {
className="flex min-h-screen flex-col items-center justify-center py-2 "
onMouseDown={() => listen()}
onTouchStart={() => listen()}
onMouseUp={() => process()}
onTouchEnd={() => process()}
onMouseUp={() => run()}
onTouchEnd={() => run()}
>
<Head>
<title>JANOT</title>
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/pages/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function TestPage() {
formdata.append("audioData", file, "speech.mp3");
formdata.append("model", "whisper-1");

const url = "http://localhost:9000/transcriptions";
const url = `${process.env.API_SERVER_URL}/transcriptions`;
const response = await axios.post(url, formdata, {
headers: { "Content-Type": "multipart/form-data" },
});
Expand All @@ -39,7 +39,7 @@ export default function TestPage() {
formdata.append("audioData", file, "speech.mp3");
formdata.append("model", "whisper-1");

const url = "http://localhost:9000/transcriptions";
const url = `${process.env.API_SERVER_URL}/transcriptions`;
const response = await axios.post(url, formdata, {
headers: { "Content-Type": "multipart/form-data" },
});
Expand Down Expand Up @@ -71,7 +71,7 @@ export default function TestPage() {


useEffect(() => {
const endpoint = "ws://localhost:9000/chat";
const endpoint = `${process.env.API_SERVER_WEB_SOCKET}/chat`;
const ws = new WebSocket(endpoint);

ws.onmessage = function (event) {
Expand Down
4 changes: 3 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"NEXT_PUBLIC_SUPABASE_URL",
"NEXT_PUBLIC_SUPABASE_ANON_KEY",
"SUPABASE_SERVICE_ROLE_KEY",
"OPENAI_API_KEY"
"OPENAI_API_KEY",
"API_SERVER_URL",
"API_SERVER_WEB_SOCKET"
]
},
"lint": {},
Expand Down

0 comments on commit d494cf8

Please sign in to comment.