dschandra commited on
Commit
0347f6c
·
verified ·
1 Parent(s): cce0f39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -16
app.py CHANGED
@@ -34,6 +34,24 @@ html_code = """
34
  h1 {
35
  color: #333;
36
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  .status {
38
  margin-top: 20px;
39
  font-size: 18px;
@@ -57,15 +75,17 @@ html_code = """
57
  </head>
58
  <body>
59
  <h1>AI Dining Assistant</h1>
60
- <div class="status" id="status">Welcome to AI Dining Assistant! How can I help you today?</div>
 
61
  <div class="response" id="response" style="display: none;">Response will appear here...</div>
62
  <script>
 
63
  const status = document.getElementById('status');
64
  const response = document.getElementById('response');
65
  let mediaRecorder;
66
  let audioChunks = [];
67
 
68
- function startListening() {
69
  navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
70
  mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm;codecs=opus' });
71
  mediaRecorder.start();
@@ -85,32 +105,22 @@ html_code = """
85
  const data = await result.json();
86
  response.textContent = data.response;
87
  response.style.display = 'block';
88
- status.textContent = 'Listening...';
89
  // Speak the response
90
  const utterance = new SpeechSynthesisUtterance(data.response);
91
  speechSynthesis.speak(utterance);
92
  if (data.response.includes("Goodbye")) {
93
- status.textContent = 'Conversation ended.';
94
- } else {
95
- setTimeout(startListening, 1000); // Restart listening after response
96
  }
97
  } catch (error) {
98
  response.textContent = 'Error occurred. Please try again.';
99
  response.style.display = 'block';
100
- status.textContent = 'Listening...';
101
- setTimeout(startListening, 1000); // Restart listening
102
  }
103
  };
104
  setTimeout(() => mediaRecorder.stop(), 5000); // Stop recording after 5 seconds
105
  }).catch(() => status.textContent = 'Microphone access denied.');
106
- }
107
-
108
- // Start listening automatically on page load
109
- window.onload = () => {
110
- const utterance = new SpeechSynthesisUtterance('Welcome to AI Dining Assistant! How can I help you today?');
111
- speechSynthesis.speak(utterance);
112
- setTimeout(startListening, 3000);
113
- };
114
  </script>
115
  </body>
116
  </html>
 
34
  h1 {
35
  color: #333;
36
  }
37
+ .mic-button {
38
+ width: 80px;
39
+ height: 80px;
40
+ border-radius: 50%;
41
+ background-color: #007bff;
42
+ color: white;
43
+ font-size: 24px;
44
+ border: none;
45
+ display: flex;
46
+ align-items: center;
47
+ justify-content: center;
48
+ cursor: pointer;
49
+ box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
50
+ transition: background-color 0.3s;
51
+ }
52
+ .mic-button:hover {
53
+ background-color: #0056b3;
54
+ }
55
  .status {
56
  margin-top: 20px;
57
  font-size: 18px;
 
75
  </head>
76
  <body>
77
  <h1>AI Dining Assistant</h1>
78
+ <button class="mic-button" id="mic-button">🎤</button>
79
+ <div class="status" id="status">Press the mic button to start the conversation...</div>
80
  <div class="response" id="response" style="display: none;">Response will appear here...</div>
81
  <script>
82
+ const micButton = document.getElementById('mic-button');
83
  const status = document.getElementById('status');
84
  const response = document.getElementById('response');
85
  let mediaRecorder;
86
  let audioChunks = [];
87
 
88
+ micButton.addEventListener('click', () => {
89
  navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
90
  mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm;codecs=opus' });
91
  mediaRecorder.start();
 
105
  const data = await result.json();
106
  response.textContent = data.response;
107
  response.style.display = 'block';
108
+ status.textContent = 'Press the mic button to continue...';
109
  // Speak the response
110
  const utterance = new SpeechSynthesisUtterance(data.response);
111
  speechSynthesis.speak(utterance);
112
  if (data.response.includes("Goodbye")) {
113
+ status.textContent = 'Conversation ended. Press the mic button to start again.';
 
 
114
  }
115
  } catch (error) {
116
  response.textContent = 'Error occurred. Please try again.';
117
  response.style.display = 'block';
118
+ status.textContent = 'Press the mic button to try again.';
 
119
  }
120
  };
121
  setTimeout(() => mediaRecorder.stop(), 5000); // Stop recording after 5 seconds
122
  }).catch(() => status.textContent = 'Microphone access denied.');
123
+ });
 
 
 
 
 
 
 
124
  </script>
125
  </body>
126
  </html>