File size: 3,601 Bytes
859f5d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Asian Food QnA Chatbot</title>

    <style>

        #user-input {

            width: 50%;

            max-width: 600px;

        }

    </style>

    <script>

        let recognition;



        function startSpeechRecognition() {

            if (!('webkitSpeechRecognition' in window)) {

                alert('Speech Recognition not supported in this browser.');

                return;

            }



            recognition = new webkitSpeechRecognition();

            recognition.continuous = false;

            recognition.interimResults = false;

            recognition.lang = 'en-US';



            recognition.onstart = function () {

                document.getElementById("speech-status").textContent = "Listening...";

            };



            recognition.onresult = function (event) {

                const speechResult = event.results[0][0].transcript;

                document.getElementById("user-input").value = speechResult;

                askBot(speechResult);

            };



            recognition.onerror = function (event) {

                alert("Speech Recognition Error: " + event.error);

            };



            recognition.onend = function () {

                document.getElementById("speech-status").textContent = "";

            };



            recognition.start();

        }



        async function askBot(question) {

            const responseBox = document.getElementById("response-box");

            responseBox.textContent = "Waiting for response...";



            try {

                const response = await fetch('/ask', {

                    method: 'POST',

                    headers: {

                        'Content-Type': 'application/json'

                    },

                    body: JSON.stringify({ question })

                });



                const data = await response.json();



                if (data.error) {

                    responseBox.textContent = "Error: " + data.error;

                } else {

                    responseBox.innerHTML = "You: " + question + "<br>" + "Bot: " + data.answer;                    

                    //responseBox.textContent = data.answer;

                    const audioUrl = data.audio + "?t=" + new Date().getTime(); // Add a timestamp to prevent caching

                    const audio = new Audio(audioUrl);

                    audio.play();

                    document.getElementById("user-input").value = "";

                }

            } catch (error) {

                responseBox.textContent = "Error: Unable to process the request.";

            }

        }



        function handleFormSubmit(event) {

            event.preventDefault();

            const question = document.getElementById("user-input").value;

            if (question.trim()) {

                askBot(question);

            }

        }

    </script>
</head>
<body>
    <h1>Asian Food QnA Chatbot - English</h1>
    <form onsubmit="handleFormSubmit(event)">
        <input type="text" id="user-input"  placeholder="Type your question here..." required>
        <button type="submit">Ask</button>
    </form>
    <button onclick="startSpeechRecognition()">🎤 Speak</button>
    <span id="speech-status" style="color: green; margin-left: 10px;"></span>
    <div id="response-box" style="margin-top: 20px; font-weight: bold;">Response:</div>
</body>
</html>