File size: 1,975 Bytes
4e70d0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Dining Assistant</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
    <h1>🍽️ AI Dining Assistant</h1>
    <img src="{{ url_for('static', filename='mic.png') }}" id="mic-button" onclick="recordAudio()" alt="Mic Button">
    <p id="status">Press the mic button to start...</p>
    <audio id="audio-player" controls></audio>
    <h3>Your Response:</h3>
    <p id="transcription"></p>

    <script>
        async function playWelcomeAudio() {
            let response = await fetch("/get_prompt");
            let data = await response.json();
            document.getElementById("audio-player").src = data.audio_url;
            document.getElementById("audio-player").play();
        }

        async function recordAudio() {
            let stream = await navigator.mediaDevices.getUserMedia({ audio: true });
            let mediaRecorder = new MediaRecorder(stream);
            let audioChunks = [];

            mediaRecorder.ondataavailable = event => {
                audioChunks.push(event.data);
            };

            mediaRecorder.onstop = async () => {
                let audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
                let formData = new FormData();
                formData.append("audio", audioBlob, "input.wav");

                let response = await fetch("/process_audio", {
                    method: "POST",
                    body: formData
                });

                let result = await response.json();
                document.getElementById("transcription").innerText = result.text || result.error;
            };

            mediaRecorder.start();
            setTimeout(() => mediaRecorder.stop(), 4000);
        }

        window.onload = playWelcomeAudio;
    </script>
</body>
</html>