File size: 2,306 Bytes
c4dd4fa
 
 
 
 
 
 
123aebe
c4dd4fa
 
 
0a8e2ef
c4dd4fa
 
0a8e2ef
c4dd4fa
 
0a8e2ef
 
 
c4dd4fa
 
 
 
 
 
 
 
 
 
 
 
 
c32edfc
c4dd4fa
0a8e2ef
c4dd4fa
5527542
c4dd4fa
 
0a8e2ef
c4dd4fa
 
0a8e2ef
c4dd4fa
 
c758190
c4dd4fa
 
c758190
c4dd4fa
 
 
 
c758190
99e691c
c4dd4fa
 
 
 
 
 
 
 
 
 
c758190
c4dd4fa
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
const micContainer = document.querySelector('.mic-container');
const circle = document.querySelector('.circle');
const audioPlayback = document.getElementById('audioPlayback');
const transcribeButton = document.getElementById('transcribeButton');
const transcriptionResult = document.getElementById('transcriptionResult');
const loadingSpinner = document.getElementById('loadingSpinner');

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

micContainer.addEventListener('click', async () => {
    if (circle.classList.contains('active')) {
        stopRecording();
    } else {
        await startRecording();
    }
});

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 = 'block';
    };
    mediaRecorder.start();
    circle.classList.add('active');
    transcribeButton.style.display = 'none'; // Hide transcribe button initially
};

const stopRecording = () => {
    mediaRecorder.stop();
    circle.classList.remove('active');
};

transcribeButton.addEventListener('click', async () => {
    if (!audioBlob) return;

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

    loadingSpinner.style.display = 'block';
    transcriptionResult.textContent = '';

    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}`;
    } finally {
        loadingSpinner.style.display = 'none';
    }
});