-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.ts
138 lines (122 loc) · 4.26 KB
/
script.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const apiKey: string = '';
let micButtonClicked: boolean = false;
function micButton() {
if (!micButtonClicked) {
micButtonClicked = true;
Main();
}
}
async function getAudio() {
console.log("started");
const micButton = document.getElementById("startMic");
micButton.classList.toggle('recording');
return new Promise(async (resolve, reject): Promise<Blob | boolean> => {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
let mediaRecorder = new MediaRecorder(stream);
let chunks = [];
mediaRecorder.addEventListener('dataavailable', (e) => {
chunks.push(e.data);
});
mediaRecorder.addEventListener('stop', () => {
console.log("stopped audio");
micButton.classList.toggle('recording');
let audioBlob: Blob = new Blob(chunks);
resolve(audioBlob);
});
micButton.addEventListener('click', () => {
if (!(mediaRecorder.state === "inactive")) {
mediaRecorder.stop();
}
});
mediaRecorder.start();
let timeoutId = setTimeout(() => {
if (mediaRecorder.state === "recording") {
mediaRecorder.stop();
console.log("stopped audio");
micButton.classList.toggle('recording');
let audioBlob: Blob = new Blob(chunks);
resolve(audioBlob);
}
}, 15000); // Set time limit to 15 seconds
}
catch (error) {
console.error('Failed to access user media', error);
alert("Accept Mic Permission to use")
location.reload();
reject(error);
return false;
}
});
}
function transcribeAudio(audioBlob) {
return new Promise((resolve, reject) => {
let formData = new FormData();
formData.append('audio', audioBlob, 'recording.wav');
fetch('/api/transcribe', {
method: 'POST',
body: formData
})
.then(response => response.text())
.then((text) => resolve(text))
.catch(error => {
console.error('Error occurred while making API call:', error);
reject(error);
});
});
}
function fetchPrompt(fullPrompt: { role: string; apiKey: string; Prompt: unknown }): Promise<any> {
return fetch('/api/getrequest', {
method: 'POST',
body: JSON.stringify(fullPrompt),
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
if (!response.ok) {
throw new Error(`API call failed with status ${response.status}`);
}
console.log('API call succeeded');
return response.json();
})
.catch(error => {
console.error('Error occurred while making API call:', error);
throw error;
});
}
function speakText(text): void {
if (!('speechSynthesis' in window)) {
alert("Sorry, your browser doesn't support text to speech!");
return;
}
let textToSpeech = new SpeechSynthesisUtterance();
textToSpeech.text = text;
window.speechSynthesis.speak(textToSpeech);
}
async function Main(): Promise<void> {
let blobData = await getAudio();
console.log("got audio")
let audioText = await transcribeAudio(blobData)
console.log("got audio text: " + audioText)
let fullPrompt = {
Prompt: audioText,
apiKey: apiKey,
role: "Be a helpful assistant."
}
fetchPrompt(fullPrompt)
.then(replyData => {
if (!replyData) {
console.log("Error getting Prompt");
return;
}
console.log("got reply data")
let messageBody: HTMLElement = document.getElementById("messageBody");
messageBody.innerText = replyData.choices[0].message.content;
speakText(replyData.choices[0].message.content);
})
.catch(error => {
console.error("Error occurred while fetching Prompt:", error);
});
micButtonClicked = false;
}