Tri4 commited on
Commit
0a8e2ef
·
verified ·
1 Parent(s): 40c866a

Create design.js

Browse files
Files changed (1) hide show
  1. static/js/design.js +73 -0
static/js/design.js ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ let isRecording = false;
2
+ let mediaRecorder;
3
+ let recordedChunks = [];
4
+
5
+ const micContainer = document.getElementsByClassName('mic-container')[0];
6
+ const circle = micContainer.getElementsByClassName('circle')[0];
7
+
8
+ micContainer.addEventListener('click', function () {
9
+ if (!isRecording) {
10
+ startRecording();
11
+ } else {
12
+ stopRecording();
13
+ }
14
+ });
15
+
16
+ function startRecording() {
17
+ navigator.mediaDevices.getUserMedia({ audio: true })
18
+ .then(function (stream) {
19
+ mediaRecorder = new MediaRecorder(stream);
20
+ mediaRecorder.start();
21
+
22
+ mediaRecorder.ondataavailable = function (e) {
23
+ recordedChunks.push(e.data);
24
+ };
25
+
26
+ // Update UI for recording
27
+ circle.classList.add('active');
28
+ document.getElementById('output').textContent = 'Recording...';
29
+ isRecording = true;
30
+ })
31
+ .catch(function (err) {
32
+ console.error('The following error occurred: ' + err);
33
+ });
34
+ }
35
+
36
+ function stopRecording() {
37
+ mediaRecorder.stop();
38
+
39
+ mediaRecorder.onstop = function () {
40
+ const blob = new Blob(recordedChunks, { type: 'audio/webm' });
41
+ recordedChunks = [];
42
+ const audioURL = window.URL.createObjectURL(blob);
43
+
44
+ // Update UI after recording
45
+ circle.classList.remove('active');
46
+ document.getElementById('output').textContent = 'Click to Transcribe';
47
+
48
+ // Handle the Transcribe button click
49
+ document.getElementById('output').addEventListener('click', function () {
50
+ transcribeAudio(blob);
51
+ });
52
+
53
+ isRecording = false;
54
+ };
55
+ }
56
+
57
+ function transcribeAudio(blob) {
58
+ const formData = new FormData();
59
+ formData.append('audio', blob, 'audio.webm');
60
+
61
+ //fetch('https://tri4-semalab.hf.space/transcribe', { // https://jikoni-semabox.hf.space/transcribe
62
+ fetch('https://jikoni-semabox.hf.space/transcribe', { // https://tri4-semalab.hf.space/transcribe
63
+ method: 'POST',
64
+ body: formData
65
+ })
66
+ .then(response => response.json())
67
+ .then(data => {
68
+ document.getElementById('output').textContent = data.transcription;
69
+ })
70
+ .catch(error => {
71
+ console.error('Error:', error);
72
+ });
73
+ }