AiDeveloper1 commited on
Commit
5842128
·
verified ·
1 Parent(s): 709b524

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +146 -154
index.html CHANGED
@@ -1,162 +1,154 @@
1
  <!DOCTYPE html>
2
  <html lang="en">
3
  <head>
4
- <meta charset="UTF-8">
5
- <title>Voice Command | Chatbot</title>
6
- <style>
7
- .chat-container {
8
- max-width: 400px;
9
- margin: 20px auto;
10
- padding: 10px;
11
- border: 1px solid #ccc;
12
- border-radius: 5px;
13
- box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
14
- font-family: Arial, sans-serif;
15
- }
16
- .user-message {
17
- background-color: #f0f0f0;
18
- border-radius: 5px;
19
- padding: 5px 10px;
20
- margin: 5px 0;
21
- text-align: right;
22
- }
23
- .bot-message {
24
- background-color: #d3e9ff;
25
- border-radius: 5px;
26
- padding: 5px 10px;
27
- margin: 5px 0;
28
- }
29
- #languageSelector {
30
- width: 100%;
31
- margin-top: 10px;
32
- padding: 5px;
33
- border-radius: 5px;
34
- border: 1px solid #ccc;
35
- }
36
- </style>
 
 
 
 
 
 
 
 
 
 
 
 
37
  </head>
38
  <body>
39
- <div class="chat-container">
40
- <div id="chat-box"></div>
41
-
42
- <!-- Language Selection -->
43
- <select id="languageSelector">
44
- <option value="en-US">English (US)</option>
45
- <option value="hi-IN">Hindi (India)</option>
46
- <option value="es-ES">Spanish (Spain)</option>
47
- <option value="fr-FR">French (France)</option>
48
- <option value="de-DE">German (Germany)</option>
49
- <option value="ar-SA">Arabic (Saudi Arabia)</option>
50
- </select>
51
-
52
- <div class="speaker" style="display: flex; justify-content: space-between; width: 100%; box-shadow: 0 0 13px #0000003d; border-radius: 5px; margin-top: 10px;">
53
- <p id="action" style="color: grey; font-weight: 800; padding-left: 2rem;"></p>
54
- <button id="speech" onclick="runSpeechRecog()" style="border: transparent; padding: 0 0.5rem;">
55
- Tap to Speak
56
- </button>
57
- </div>
58
  </div>
59
-
60
- <script>
61
- let synth = window.speechSynthesis;
62
-
63
- function runSpeechRecog() {
64
- const action = document.getElementById('action');
65
- const recognition = new webkitSpeechRecognition();
66
-
67
- // Always recognize speech in English
68
- recognition.lang = "en-US";
69
- recognition.interimResults = false;
70
- recognition.continuous = false;
71
-
72
- recognition.onstart = () => {
73
- action.innerHTML = "Listening...";
74
- };
75
-
76
- recognition.onresult = (event) => {
77
- var transcript = event.results[0][0].transcript;
78
- action.innerHTML = "";
79
- sendMessage(transcript);
80
- };
81
-
82
- recognition.onerror = (event) => {
83
- action.innerHTML = "Error: " + event.error;
84
- };
85
-
86
- recognition.onend = () => {
87
- action.innerHTML = "";
88
- };
89
-
90
- recognition.start();
91
- }
92
-
93
- function sendMessage(message) {
94
- showUserMessage(message);
95
- sendToFlaskAPI(message);
96
- }
97
-
98
- function sendToFlaskAPI(message) {
99
- fetch('/api/process_text', {
100
- method: 'POST',
101
- headers: {
102
- 'Content-Type': 'application/json',
103
- },
104
- body: JSON.stringify({ text: message })
105
- })
106
- .then(response => response.json())
107
- .then(data => {
108
- console.log('Response from Flask API:', data);
109
- handleResponse(data);
110
- })
111
- .catch(error => {
112
- console.error('Error sending data to Flask API:', error);
113
- });
114
- }
115
-
116
- function handleResponse(data) {
117
- showBotMessage(data);
118
- speakResponse(data);
119
- }
120
-
121
- function showUserMessage(message) {
122
- const chatBox = document.getElementById('chat-box');
123
- chatBox.innerHTML += <div class="user-message">${message}</div>;
124
- }
125
-
126
- function showBotMessage(message) {
127
- const chatBox = document.getElementById('chat-box');
128
- chatBox.innerHTML += <div class="bot-message">${message}</div>;
129
- }
130
-
131
- function speakResponse(responseText) {
132
- const selectedLang = document.getElementById("languageSelector").value;
133
- const utterance = new SpeechSynthesisUtterance(responseText);
134
-
135
- // Set voice language based on selected option
136
- utterance.lang = selectedLang;
137
-
138
- const voices = synth.getVoices();
139
- const matchingVoice = voices.find(voice => voice.lang === selectedLang);
140
-
141
- if (matchingVoice) {
142
- utterance.voice = matchingVoice;
143
- }
144
-
145
- synth.speak(utterance);
146
-
147
- window.addEventListener('beforeunload', function () {
148
- if (synth.speaking) synth.cancel();
149
- });
150
-
151
- document.getElementById('speech').addEventListener('click', function () {
152
- if (synth.speaking) synth.cancel();
153
- });
154
- }
155
-
156
- // Load voices early for compatibility
157
- window.speechSynthesis.onvoiceschanged = () => {
158
- speechSynthesis.getVoices();
159
- };
160
- </script>
161
  </body>
162
  </html>
 
1
  <!DOCTYPE html>
2
  <html lang="en">
3
  <head>
4
+ <meta charset="UTF-8" />
5
+ <title>Voice Command | Chatbot</title>
6
+ <style>
7
+ .chat-container {
8
+ max-width: 400px;
9
+ margin: 20px auto;
10
+ padding: 10px;
11
+ border: 1px solid #ccc;
12
+ border-radius: 5px;
13
+ box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
14
+ font-family: Arial, sans-serif;
15
+ }
16
+ .user-message, .bot-message {
17
+ border-radius: 5px;
18
+ padding: 5px 10px;
19
+ margin: 5px 0;
20
+ }
21
+ .user-message {
22
+ background-color: #f0f0f0;
23
+ text-align: right;
24
+ }
25
+ .bot-message {
26
+ background-color: #d3e9ff;
27
+ }
28
+ #languageSelector {
29
+ width: 100%;
30
+ margin-top: 10px;
31
+ padding: 5px;
32
+ border-radius: 5px;
33
+ border: 1px solid #ccc;
34
+ }
35
+ .speaker {
36
+ display: flex;
37
+ justify-content: space-between;
38
+ width: 100%;
39
+ box-shadow: 0 0 13px #0000003d;
40
+ border-radius: 5px;
41
+ margin-top: 10px;
42
+ }
43
+ #speech {
44
+ border: transparent;
45
+ padding: 0 0.5rem;
46
+ cursor: pointer;
47
+ }
48
+ </style>
49
  </head>
50
  <body>
51
+ <div class="chat-container">
52
+ <div id="chat-box"></div>
53
+
54
+ <select id="languageSelector">
55
+ <option value="en-US">English (US)</option>
56
+ <option value="hi-IN">Hindi (India)</option>
57
+ <option value="es-ES">Spanish (Spain)</option>
58
+ <option value="fr-FR">French (France)</option>
59
+ <option value="de-DE">German (Germany)</option>
60
+ <option value="ar-SA">Arabic (Saudi Arabia)</option>
61
+ </select>
62
+
63
+ <div class="speaker">
64
+ <p id="action" style="color: grey; font-weight: 800; padding-left: 2rem;"></p>
65
+ <button id="speech">Tap to Speak</button>
 
 
 
 
66
  </div>
67
+ </div>
68
+
69
+ <script>
70
+ const synth = window.speechSynthesis;
71
+ let recognition;
72
+
73
+ // Event listener for the button
74
+ document.getElementById("speech").addEventListener("click", () => {
75
+ runSpeechRecog();
76
+ });
77
+
78
+ function runSpeechRecog() {
79
+ const action = document.getElementById("action");
80
+
81
+ // Prevent multiple instances
82
+ if (recognition) recognition.abort();
83
+
84
+ // Ensure Chrome compatibility
85
+ const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
86
+ if (!SpeechRecognition) {
87
+ alert("Speech Recognition not supported in this browser. Use Google Chrome.");
88
+ return;
89
+ }
90
+
91
+ recognition = new SpeechRecognition();
92
+ recognition.lang = "en-US"; // Always English for input
93
+ recognition.interimResults = false;
94
+ recognition.continuous = false;
95
+
96
+ recognition.onstart = () => {
97
+ action.innerHTML = "Listening...";
98
+ };
99
+
100
+ recognition.onresult = (event) => {
101
+ const transcript = event.results[0][0].transcript;
102
+ action.innerHTML = "";
103
+ sendMessage(transcript);
104
+ };
105
+
106
+ recognition.onerror = (event) => {
107
+ action.innerHTML = "Error: " + event.error;
108
+ };
109
+
110
+ recognition.onend = () => {
111
+ action.innerHTML = "";
112
+ };
113
+
114
+ recognition.start();
115
+ }
116
+
117
+ function sendMessage(message) {
118
+ showUserMessage(message);
119
+ // Simulating response (replace with your backend call)
120
+ setTimeout(() => handleResponse("You said: " + message), 500);
121
+ }
122
+
123
+ function showUserMessage(message) {
124
+ const chatBox = document.getElementById("chat-box");
125
+ chatBox.innerHTML += <div class="user-message">${message}</div>;
126
+ }
127
+
128
+ function handleResponse(message) {
129
+ showBotMessage(message);
130
+ speakResponse(message);
131
+ }
132
+
133
+ function showBotMessage(message) {
134
+ const chatBox = document.getElementById("chat-box");
135
+ chatBox.innerHTML += <div class="bot-message">${message}</div>;
136
+ }
137
+
138
+ function speakResponse(responseText) {
139
+ const selectedLang = document.getElementById("languageSelector").value;
140
+ const utterance = new SpeechSynthesisUtterance(responseText);
141
+ utterance.lang = selectedLang;
142
+
143
+ const voices = synth.getVoices();
144
+ const match = voices.find(v => v.lang === selectedLang);
145
+ if (match) utterance.voice = match;
146
+
147
+ synth.speak(utterance);
148
+ }
149
+
150
+ // Ensure voices are loaded
151
+ window.speechSynthesis.onvoiceschanged = () => synth.getVoices();
152
+ </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  </body>
154
  </html>