Tri4 commited on
Commit
279d46c
·
verified ·
1 Parent(s): 67b0fe2

Create chatgpt.js

Browse files
Files changed (1) hide show
  1. static/js/chatgpt.js +64 -0
static/js/chatgpt.js ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const recordButton = document.getElementById('recordButton');
2
+ const audioPlayback = document.getElementById('audioPlayback');
3
+ const transcribeButton = document.getElementById('transcribeButton');
4
+ const transcriptionResult = document.getElementById('transcriptionResult');
5
+
6
+ let mediaRecorder;
7
+ let audioChunks = [];
8
+ let audioBlob;
9
+ let audioUrl;
10
+
11
+ const startRecording = async () => {
12
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
13
+ mediaRecorder = new MediaRecorder(stream);
14
+ mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
15
+ mediaRecorder.onstop = () => {
16
+ audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
17
+ audioUrl = URL.createObjectURL(audioBlob);
18
+ audioPlayback.src = audioUrl;
19
+ audioPlayback.style.display = 'block';
20
+ transcribeButton.style.display = 'inline-block';
21
+ };
22
+ mediaRecorder.start();
23
+ recordButton.classList.add('pulsing');
24
+ recordButton.textContent = 'Stop Recording';
25
+ };
26
+
27
+ const stopRecording = () => {
28
+ mediaRecorder.stop();
29
+ recordButton.classList.remove('pulsing');
30
+ recordButton.textContent = 'Start Recording';
31
+ };
32
+
33
+ const transcribeAudio = async () => {
34
+ if (!audioBlob) return;
35
+
36
+ const formData = new FormData();
37
+ formData.append('audio', audioBlob, 'recording.wav');
38
+
39
+ try {
40
+ const response = await fetch('https://jikoni-semabox.hf.space/transcribe', {
41
+ method: 'POST',
42
+ body: formData
43
+ });
44
+
45
+ if (response.ok) {
46
+ const result = await response.json();
47
+ transcriptionResult.textContent = result.transcription || 'No transcription available.';
48
+ } else {
49
+ transcriptionResult.textContent = `Error: ${response.status}`;
50
+ }
51
+ } catch (error) {
52
+ transcriptionResult.textContent = `Request failed: ${error.message}`;
53
+ }
54
+ };
55
+
56
+ recordButton.addEventListener('click', () => {
57
+ if (mediaRecorder && mediaRecorder.state === 'recording') {
58
+ stopRecording();
59
+ } else {
60
+ startRecording();
61
+ }
62
+ });
63
+
64
+ transcribeButton.addEventListener('click', transcribeAudio);