File size: 2,125 Bytes
279d46c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const recordButton = document.getElementById('recordButton');
const audioPlayback = document.getElementById('audioPlayback');
const transcribeButton = document.getElementById('transcribeButton');
const transcriptionResult = document.getElementById('transcriptionResult');

let mediaRecorder;
let audioChunks = [];
let audioBlob;
let audioUrl;

const startRecording = async () => {
    const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
    mediaRecorder = new MediaRecorder(stream);
    mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
    mediaRecorder.onstop = () => {
        audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
        audioUrl = URL.createObjectURL(audioBlob);
        audioPlayback.src = audioUrl;
        audioPlayback.style.display = 'block';
        transcribeButton.style.display = 'inline-block';
    };
    mediaRecorder.start();
    recordButton.classList.add('pulsing');
    recordButton.textContent = 'Stop Recording';
};

const stopRecording = () => {
    mediaRecorder.stop();
    recordButton.classList.remove('pulsing');
    recordButton.textContent = 'Start Recording';
};

const transcribeAudio = async () => {
    if (!audioBlob) return;

    const formData = new FormData();
    formData.append('audio', audioBlob, 'recording.wav');

    try {
        const response = await fetch('https://jikoni-semabox.hf.space/transcribe', {
            method: 'POST',
            body: formData
        });

        if (response.ok) {
            const result = await response.json();
            transcriptionResult.textContent = result.transcription || 'No transcription available.';
        } else {
            transcriptionResult.textContent = `Error: ${response.status}`;
        }
    } catch (error) {
        transcriptionResult.textContent = `Request failed: ${error.message}`;
    }
};

recordButton.addEventListener('click', () => {
    if (mediaRecorder && mediaRecorder.state === 'recording') {
        stopRecording();
    } else {
        startRecording();
    }
});

transcribeButton.addEventListener('click', transcribeAudio);