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

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +124 -36
index.html CHANGED
@@ -2,73 +2,161 @@
2
  <html lang="en">
3
  <head>
4
  <meta charset="UTF-8">
5
- <title>Speech to Text Demo</title>
6
  <style>
7
- .container {
8
  max-width: 400px;
9
- margin: 30px auto;
10
- padding: 20px;
11
  border: 1px solid #ccc;
12
- border-radius: 8px;
13
- box-shadow: 0 0 10px rgba(0,0,0,0.1);
14
- font-family: sans-serif;
15
  }
16
- #output {
17
- margin-top: 20px;
18
- font-size: 18px;
19
- color: #333;
20
- background: #f0f0f0;
21
- padding: 10px;
 
 
 
22
  border-radius: 5px;
23
- min-height: 40px;
 
 
 
 
 
 
 
 
24
  }
25
  </style>
26
  </head>
27
  <body>
28
- <div class="container">
29
- <h3>Speech to Text</h3>
30
 
31
- <!-- Language Selector -->
32
  <select id="languageSelector">
33
  <option value="en-US">English (US)</option>
34
  <option value="hi-IN">Hindi (India)</option>
35
- <option value="es-ES">Spanish</option>
36
- <option value="fr-FR">French</option>
 
 
37
  </select>
38
 
39
- <!-- Button to Start Speech -->
40
- <button onclick="startRecognition()">Tap to Speak</button>
41
-
42
- <!-- Display Result -->
43
- <div id="output"></div>
 
44
  </div>
45
 
46
  <script>
47
- function startRecognition() {
48
- const lang = document.getElementById('languageSelector').value;
49
- const output = document.getElementById('output');
50
 
51
- const recognition = new webkitSpeechRecognition(); // works in Chrome
52
- recognition.lang = lang;
53
- recognition.continuous = false;
 
 
 
54
  recognition.interimResults = false;
 
55
 
56
  recognition.onstart = () => {
57
- output.textContent = "Listening...";
 
 
 
 
 
 
58
  };
59
 
60
  recognition.onerror = (event) => {
61
- output.textContent = "Error: " + event.error;
62
  };
63
 
64
- recognition.onresult = (event) => {
65
- const transcript = event.results[0][0].transcript;
66
- output.textContent = transcript;
67
- console.log("Recognized Text:", transcript); // Print to console
68
  };
69
 
70
  recognition.start();
71
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  </script>
73
  </body>
74
  </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>