File size: 8,247 Bytes
20681aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html>
<html>
<head>
    <title>Agri Chatbot</title>
    <link rel="icon" type="image/x-icon" href="static/images/favicon.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="This is an Agriculture chatbot that helps farmers to make write desitions." />
    <link rel="icon" type="image/png" href="static/images/favicon.ico"/>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <!-- Option 1: Include in HTML -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
    <style>
        .loading-message {
            text-align: center;
            font-style: italic;
            color: #999;
        }

        #chat-container {
            height: 60vh;
            overflow-y: auto;
        }

        #user-input {
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Krissi Help Chatbot</h1>
        <p>Disclaimer:please have patience this might take some time</p>
        <div class="row chat-container" id="chat-container"></div>
        <div class="row">
            <div class="col-xs-12">
                <input type="text" class="form-control" id="user-input" placeholder="Type your message...">
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <button class="btn btn-primary btn-block" id="send-btn">Send</button>
            </div>
        </div>
        
        <div class="row">
            <div class="col-xs-12">
                <button class="btn btn-primary btn-block" id="record-btn">Record</button>
            </div>
        </div>

        <div class="row">
            <div class="col-xs-12">
                <audio id="audio" controls></audio>
            </div>
        </div>
        <footer class="footer">
            <div class="footer-links">
                <h2> Made by Mohammed Ashraf
                <a href="https://www.linkedin.com/in/mohammed97ashraf" target="_blank"><i class="bi bi-linkedin"></i></a>
                <a href="https://github.com/mohammed97ashraf" target="_blank"><i class="bi bi-github"></i></a></h2>
            </div>
        </footer>
    </div>

    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#send-btn').on('click', function() {
                sendMessage();
            });

            $('#user-input').on('keypress', function(e) {
                if (e.which === 13) {
                    sendMessage();
                }
            });

            $('#record-btn').on('click', function() {
                toggleRecording();
            });

            var recording = false;
            var recorder;

            function toggleRecording() {
                if (recording) {
                    stopRecording();
                    $('#record-btn').text('Record');
                } else {
                    startRecording();
                    $('#record-btn').text('Stop Recording');
                }
                recording = !recording;
            }

            
            function startRecording() {
                if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
                    navigator.mediaDevices.getUserMedia({ audio: true })
                        .then(function(stream) {
                            recorder = new MediaRecorder(stream);
                            var chunks = [];
            
                            recorder.addEventListener('dataavailable', function(e) {
                                chunks.push(e.data);
                            });
            
                            recorder.addEventListener('stop', function() {
                                var audioBlob = new Blob(chunks, { type: 'audio/webm' });
                                var formData = new FormData();
                                formData.append('audio', audioBlob, 'audio.webm');
                                sendAudio(formData);
                            });
            
                            recorder.start();
                        })
                        .catch(function(err) {
                            console.error('Error accessing microphone:', err);
                        });
                } else {
                    console.error('getUserMedia is not supported in this browser.');
                }
            }


            function stopRecording() {
                if (recorder) {
                    recorder.stop();
                }
            }

            function sendAudio(formData) {
                $('#chat-container').append('<div class="col-xs-12"><p class="loading-message">Uploading audio...</p></div>');
                scrollToBottom();

                $.ajax({
                    url: '/chat',
                    type: 'POST',
                    data: formData,
                    contentType: false,
                    processData: false,
                    success: function(response) {
                        $('#chat-container .loading-message:last').remove();

                        var transcription = response.text;
                        $('#user-input').val(transcription);
                        sendMessage();
                    },
                    error: function(xhr, status, error) {
                        console.error('Error uploading audio:', error);
                    }
                });
            }

            function sendMessage() {
                var userInput = $('#user-input').val();
                if (userInput.trim() !== '') {
                    $('#user-input').val('');
                    $('#chat-container').append('<div class="col-xs-12 text-right"><p class="alert alert-info">' + userInput + '</p></div>');
                    scrollToBottom();

                    $('#chat-container').append('<div class="col-xs-12"><p class="loading-message">Bot is typing...</p></div>');
                    scrollToBottom();

                    $.post('/chat', {text: userInput}, function(response) {
                        $('#chat-container .loading-message:last').remove();

                        $('#chat-container').append('<div class="col-xs-12 text-left"><p class="alert alert-success">' + response.text + '</p></div>');
                        scrollToBottom();

                        setVoice(response.voice);
                        playNotificationSound();
                        showNotification(response.text);
                    });
                }
            }

            function setVoice(voiceFile) {
                var audio = document.getElementById('audio');
                audio.pause();
                audio.src = voiceFile;
                audio.load();
                audio.play();
            }

            function playNotificationSound() {
                var audio = new Audio('{{ url_for('static', filename='audio/sound.mp3') }}');
                audio.play();
            }

            function showNotification(message) {
                if (Notification.permission === "granted") {
                    var notification = new Notification("New Message", {
                        body: message,
                        icon: "{{ url_for('static', filename='img/notification-icon.png') }}"
                    });
                } else if (Notification.permission !== "denied") {
                    Notification.requestPermission().then(function(permission) {
                        if (permission === "granted") {
                            var notification = new Notification("New Message", {
                                body: message,
                                icon: "{{ url_for('static', filename='img/notification-icon.png') }}"
                            });
                        }
                    });
                }
            }

            function scrollToBottom() {
                $('#chat-container').scrollTop($('#chat-container')[0].scrollHeight);
            }
        });
    </script>
    
</body>
</html>