tonyassi commited on
Commit
9abf335
·
verified ·
1 Parent(s): 6cd28e1

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +40 -0
  2. templates/index.html +317 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, jsonify
2
+ import speech_recognition as sr
3
+
4
+ app = Flask(__name__)
5
+
6
+ @app.route('/')
7
+ def index():
8
+ return render_template('index.html')
9
+
10
+ @app.route('/send_message', methods=['POST'])
11
+ def send_message():
12
+ user_input = request.form['user_input']
13
+ response = echo_response(user_input)
14
+ return jsonify({'response': response, 'user_input': user_input})
15
+
16
+ def echo_response(user_input):
17
+ return user_input
18
+
19
+ @app.route('/process_audio', methods=['POST'])
20
+ def process_audio():
21
+ if 'audio' not in request.files:
22
+ return "No audio file provided", 400
23
+
24
+ audio_file = request.files['audio']
25
+ recognizer = sr.Recognizer()
26
+
27
+ with sr.AudioFile(audio_file) as source:
28
+ audio = recognizer.record(source)
29
+
30
+ try:
31
+ text = recognizer.recognize_google(audio)
32
+ print("Transcribed Text:", text)
33
+ return text
34
+ except sr.UnknownValueError:
35
+ return "Could not understand audio", 400
36
+ except sr.RequestError as e:
37
+ return f"Could not request results from Google Speech Recognition service; {e}", 500
38
+
39
+ if __name__ == '__main__':
40
+ app.run(debug=True)
templates/index.html ADDED
@@ -0,0 +1,317 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
6
+ <title>Flask Chat App</title>
7
+ <style>
8
+ body, html {
9
+ margin: 0;
10
+ padding: 0;
11
+ width: 100%;
12
+ height: 100%;
13
+ background-color: #fddde6;
14
+ display: flex;
15
+ flex-direction: column;
16
+ align-items: center;
17
+ justify-content: flex-start;
18
+ font-family: Arial, sans-serif;
19
+ font-weight: 300;
20
+ }
21
+ #video-container {
22
+ padding-top: 2%;
23
+ width: auto;
24
+ height: auto;
25
+ }
26
+ #video-container video {
27
+ width: auto;
28
+ height: 35vh;
29
+ }
30
+
31
+ #chat-container, #input-container {
32
+ width: 90%;
33
+ max-width: 1200px;
34
+ margin: 10px;
35
+ }
36
+ #chat-container {
37
+ flex: 1;
38
+ overflow-y: auto;
39
+ background-color: white;
40
+ border-radius: 10px;
41
+ padding: 2%;
42
+ box-shadow: 0 0 10px rgba(0,0,0,0.1);
43
+ display: flex;
44
+ flex-direction: column;
45
+ }
46
+ .chat-bubble {
47
+ margin: 5px;
48
+ padding: 10px;
49
+ border-radius: 10px;
50
+ max-width: 80%;
51
+ }
52
+ .user-bubble {
53
+ align-self: flex-end;
54
+ background-color: #e0e0e0;
55
+ }
56
+ .bot-bubble {
57
+ align-self: flex-start;
58
+ background-color: #ffb6c1;
59
+ color: white;
60
+ }
61
+ #input-container {
62
+ display: flex;
63
+ align-items: center;
64
+ padding-bottom: 2%;
65
+ }
66
+ #input-container input {
67
+ flex: 1;
68
+ padding: 10px;
69
+ border: 1px solid #ccc;
70
+ border-radius: 5px;
71
+ height: 2.5vh;
72
+ }
73
+
74
+ #input-container button {
75
+ padding: 0;
76
+ margin-left: 10px;
77
+ border: none;
78
+ background-color: transparent;
79
+ cursor: pointer;
80
+ height: calc(2.5vh + 15px);
81
+ display: flex;
82
+ align-items: center;
83
+ justify-content: center;
84
+ }
85
+
86
+ #input-container button img {
87
+ height: 100%;
88
+ width: auto;
89
+ }
90
+
91
+ /* New styles for the menu */
92
+ .menu {
93
+ position: absolute;
94
+ top: 10px;
95
+ left: 10px;
96
+ z-index: 1000;
97
+ }
98
+
99
+ .menu .hamburger {
100
+ display: block;
101
+ cursor: pointer;
102
+ width: 30px;
103
+ height: 30px;
104
+ }
105
+
106
+ .menu .hamburger div {
107
+ width: 100%;
108
+ height: 4px;
109
+ background-color: black;
110
+ margin: 5px 0;
111
+ transition: all 0.3s;
112
+ }
113
+
114
+ .menu-content {
115
+ display: none;
116
+ position: absolute;
117
+ top: 40px;
118
+ left: 0;
119
+ background-color: white;
120
+ box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
121
+ border-radius: 5px;
122
+ overflow: hidden;
123
+ width: 150px;
124
+ }
125
+
126
+ .menu-content a {
127
+ display: block;
128
+ padding: 5px 10px;
129
+ color: black;
130
+ text-decoration: none;
131
+ transition: background-color 0.3s;
132
+ }
133
+
134
+ .menu-content a:hover {
135
+ background-color: #f0f0f0;
136
+ }
137
+
138
+ #audio-recording-container {
139
+ display: none;
140
+ align-items: center;
141
+ justify-content: center;
142
+ width: 90%;
143
+ max-width: 1200px;
144
+ margin: 10px;
145
+ padding-top: 7%;
146
+ }
147
+
148
+ #audio-record-button {
149
+ width: 150px;
150
+ height: 150px;
151
+ background-color: #e1fceb;
152
+ color: black;
153
+ border-radius: 50%;
154
+ cursor: pointer;
155
+ display: flex;
156
+ align-items: center;
157
+ justify-content: center;
158
+ font-size: 16px;
159
+ font-weight: bold;
160
+ border: 5px solid white;
161
+ box-shadow: 0 0 20px 5px rgba(255, 255, 255, 0.8);
162
+
163
+ }
164
+ </style>
165
+ </head>
166
+ <body>
167
+ <div class="menu">
168
+ <div class="hamburger" onclick="toggleMenu()">
169
+ <div></div>
170
+ <div></div>
171
+ <div></div>
172
+ </div>
173
+ <div class="menu-content" id="menu-content">
174
+ <a href="#" onclick="showTextChat()">Text Chat</a>
175
+ <a href="#" onclick="showAudioChat()">Audio Chat</a>
176
+ <a href="#">Settings</a>
177
+ </div>
178
+ </div>
179
+
180
+ <div id="video-container">
181
+ <video src="https://cdn-lfs-us-1.huggingface.co/repos/61/c4/61c4367976a0aef9d7ba3bb5650c0efc98207f17f6d3a4a71d3d7f9b265578a3/159ba43daee671b34e01d65c7b753bac22c139f653eeeaf3c6da1220b7c9ecf0?response-content-disposition=inline%3B+filename*%3DUTF-8%27%27lucy-vid-1.mp4%3B+filename%3D%22lucy-vid-1.mp4%22%3B&response-content-type=video%2Fmp4&Expires=1723399367&Policy=eyJTdGF0ZW1lbnQiOlt7IkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOkVwb2NoVGltZSI6MTcyMzM5OTM2N319LCJSZXNvdXJjZSI6Imh0dHBzOi8vY2RuLWxmcy11cy0xLmh1Z2dpbmdmYWNlLmNvL3JlcG9zLzYxL2M0LzYxYzQzNjc5NzZhMGFlZjlkN2JhM2JiNTY1MGMwZWZjOTgyMDdmMTdmNmQzYTRhNzFkM2Q3ZjliMjY1NTc4YTMvMTU5YmE0M2RhZWU2NzFiMzRlMDFkNjVjN2I3NTNiYWMyMmMxMzlmNjUzZWVlYWYzYzZkYTEyMjBiN2M5ZWNmMD9yZXNwb25zZS1jb250ZW50LWRpc3Bvc2l0aW9uPSomcmVzcG9uc2UtY29udGVudC10eXBlPSoifV19&Signature=PqaCnSHH9xJ%7E-TgkrZ0mH96bbPf7IJBnJWZHYqNU4hFKRa0N9ibxdFm5HAWySMOCNrVGMVTzGjTWAX8gmcQNfHtDRL%7EfX%7El8zZpP2tErEm4Q8AmjhmYTWDATBG0pXCJQtaSjesQK3q9RQ2yMEiJDC9VWa2OhiBJW25LnL%7EAqPLiNpX8sRs07boI677XDmgRL7qOWEOtrBqvJn5ZCgaLybRhu8ySKvCSUwWtEv1gWQ-XT28NZ6h4nDy5yEqkMw9yVcJs4U8JUCHL2cxgHxGK22FK1o3EMiZ3LvMjzWZNVlLUbJJVFO4XDNqMsumV6oLEmWNZtILyEaD6FfGw6grLJNw__&Key-Pair-Id=K24J24Z295AEI9" playsinline autoplay loop muted></video>
182
+ </div>
183
+ <div id="chat-container"></div>
184
+ <div id="input-container">
185
+ <input type="text" id="user-input" placeholder="Type your message...">
186
+ <button onclick="sendMessage()">
187
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/648a824a8ca6cf9857d1349c/aJO71FyXi40-U_AOjwPmt.png" alt="Send">
188
+ </button>
189
+ </div>
190
+
191
+ <div id="audio-recording-container">
192
+ <button id="audio-record-button" onclick="startRecording()">Start</button>
193
+ </div>
194
+
195
+ <script>
196
+ function sendMessage() {
197
+ var userInput = document.getElementById('user-input').value;
198
+ if (userInput.trim() === "") return;
199
+
200
+ var chatContainer = document.getElementById('chat-container');
201
+
202
+ // Create user bubble
203
+ var userBubble = document.createElement('div');
204
+ userBubble.textContent = userInput;
205
+ userBubble.className = 'chat-bubble user-bubble';
206
+ chatContainer.appendChild(userBubble);
207
+
208
+ // Create bot bubble with "..." text
209
+ var botBubble = document.createElement('div');
210
+ botBubble.textContent = ". . .";
211
+ botBubble.className = 'chat-bubble bot-bubble';
212
+ chatContainer.appendChild(botBubble);
213
+
214
+ chatContainer.scrollTop = chatContainer.scrollHeight;
215
+ document.getElementById('user-input').value = '';
216
+
217
+ var xhr = new XMLHttpRequest();
218
+ xhr.open('POST', '/send_message', true);
219
+ xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
220
+ xhr.onload = function () {
221
+ if (xhr.status === 200) {
222
+ var response = JSON.parse(xhr.responseText);
223
+
224
+ // Update bot bubble with the actual response
225
+ botBubble.textContent = response.response;
226
+ chatContainer.scrollTop = chatContainer.scrollHeight;
227
+ }
228
+ };
229
+ xhr.send('user_input=' + encodeURIComponent(userInput));
230
+ }
231
+
232
+
233
+ document.getElementById('user-input').addEventListener('keydown', function(event) {
234
+ if (event.key === 'Enter') {
235
+ sendMessage();
236
+ }
237
+ });
238
+
239
+ function toggleMenu() {
240
+ var menuContent = document.getElementById('menu-content');
241
+ if (menuContent.style.display === 'block') {
242
+ menuContent.style.display = 'none';
243
+ } else {
244
+ menuContent.style.display = 'block';
245
+ }
246
+ }
247
+
248
+ function showTextChat() {
249
+ document.getElementById('chat-container').style.display = 'flex';
250
+ document.getElementById('input-container').style.display = 'flex';
251
+ document.getElementById('audio-recording-container').style.display = 'none';
252
+ document.getElementById('menu-content').style.display = 'none';
253
+ }
254
+
255
+ function showAudioChat() {
256
+ document.getElementById('chat-container').style.display = 'none';
257
+ document.getElementById('input-container').style.display = 'none';
258
+ document.getElementById('audio-recording-container').style.display = 'flex';
259
+ document.getElementById('menu-content').style.display = 'none';
260
+ }
261
+
262
+ let isRecording = false;
263
+ let mediaRecorder;
264
+ let audioChunks = [];
265
+
266
+ function startRecording() {
267
+ const recordButton = document.getElementById('audio-record-button');
268
+
269
+ if (isRecording) {
270
+ // Stop recording
271
+ mediaRecorder.stop();
272
+ recordButton.style.backgroundColor = "#e1fceb";
273
+ recordButton.textContent = "Start";
274
+ isRecording = false;
275
+ } else {
276
+ // Start recording
277
+ navigator.mediaDevices.getUserMedia({ audio: true })
278
+ .then(stream => {
279
+ mediaRecorder = new MediaRecorder(stream);
280
+ mediaRecorder.start();
281
+ mediaRecorder.ondataavailable = event => {
282
+ audioChunks.push(event.data);
283
+ };
284
+ mediaRecorder.onstop = () => {
285
+ const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
286
+ sendAudioToServer(audioBlob);
287
+ audioChunks = []; // Reset for next recording
288
+ };
289
+
290
+ recordButton.style.backgroundColor = "#ff6666";
291
+ recordButton.textContent = "STOP";
292
+ isRecording = true;
293
+ });
294
+ }
295
+ }
296
+
297
+ function sendAudioToServer(audioBlob) {
298
+ const formData = new FormData();
299
+ formData.append('audio', audioBlob);
300
+
301
+ fetch('/process_audio', {
302
+ method: 'POST',
303
+ body: formData
304
+ })
305
+ .then(response => response.text())
306
+ .then(text => {
307
+ console.log("Transcribed Text: ", text);
308
+ // You can display the transcribed text in the chat if needed
309
+ })
310
+ .catch(error => {
311
+ console.error("Error:", error);
312
+ });
313
+ }
314
+
315
+ </script>
316
+ </body>
317
+ </html>