File size: 3,694 Bytes
a2c10b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// static/js/script.js
document.addEventListener('DOMContentLoaded', () => {
    const startButton = document.getElementById('startRecording');
    const stopButton = document.getElementById('stopRecording');
    const queryText = document.getElementById('queryText');
    const queryForm = document.getElementById('queryForm');
    const submitButton = document.getElementById('submitButton');

    // Start recording
    startButton.addEventListener('click', async () => {
        try {
            const response = await fetch('/start_recording', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' }
            });
            const data = await response.json();

            if (data.status === "Recording started") {
                console.log('Recording started on server...');
                startButton.classList.add('hidden');
                stopButton.classList.remove('hidden');
            } else {
                console.error('Recording already in progress');
                alert('Recording already in progress');
            }
        } catch (err) {
            console.error('Error starting recording:', err);
            alert('Error starting recording: ' + err.message);
        }
    });

    // Stop recording
    stopButton.addEventListener('click', async () => {
        try {
            const response = await fetch('/stop_recording', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' }
            });

            if (response.ok) {
                const html = await response.text();
                const parser = new DOMParser();
                const doc = parser.parseFromString(html, 'text/html');
                const transcribedText = doc.querySelector('input[name="query_text"]').value;

                if (transcribedText) {
                    queryText.value = transcribedText;
                    queryText.readOnly = false; // Allow editing if needed
                    submitButton.disabled = false;
                    console.log('Transcribed text received:', transcribedText);
                } else {
                    const error = doc.querySelector('.text-red-700 p')?.textContent || 'Unknown error';
                    console.error('Transcription error:', error);
                    alert(error);
                }

                startButton.classList.remove('hidden');
                stopButton.classList.add('hidden');
            } else {
                console.error('Error stopping recording:', response.statusText);
                alert('Error stopping recording');
            }
        } catch (err) {
            console.error('Error stopping recording:', err);
            alert('Error stopping recording: ' + err.message);
        }
    });

    // Handle form submission
    queryForm.addEventListener('submit', async (event) => {
        event.preventDefault();
        const formData = new FormData(queryForm);

        const response = await fetch('/query', {
            method: 'POST',
            body: formData
        });

        if (response.ok) {
            const html = await response.text();
            document.body.innerHTML = html; // Update the page with query results
            console.log('Query processed successfully');
        } else {
            console.error('Error processing query:', response.statusText);
            alert('Error processing query');
        }
    });

    // Enable submit button if there's pre-filled text
    if (queryText.value.trim() !== '') {
        submitButton.disabled = false;
        console.log('Form auto-filled with initial text:', queryText.value);
    }
});