Spaces:
Sleeping
Sleeping
Update templates/index.html
Browse files- templates/index.html +134 -106
templates/index.html
CHANGED
@@ -2,74 +2,69 @@
|
|
2 |
<html lang="en">
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
-
<
|
6 |
-
<title>Text Translator</title>
|
7 |
<style>
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
flex-direction: column;
|
12 |
-
align-items: center;
|
13 |
-
padding: 20px;
|
14 |
-
}
|
15 |
-
select, input, button {
|
16 |
-
margin: 10px;
|
17 |
-
padding: 8px;
|
18 |
-
font-size: 16px;
|
19 |
-
}
|
20 |
-
#output {
|
21 |
-
margin-top: 20px;
|
22 |
padding: 10px;
|
23 |
border: 1px solid #ccc;
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
}
|
28 |
-
|
29 |
-
background-color: #
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
cursor: pointer;
|
34 |
}
|
35 |
-
#
|
36 |
-
|
|
|
|
|
|
|
|
|
37 |
}
|
38 |
</style>
|
39 |
</head>
|
40 |
<body>
|
41 |
-
<
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
<
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
<script>
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
const outputDiv = document.getElementById('output');
|
62 |
-
|
63 |
-
// Check if browser supports Web Speech API
|
64 |
-
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
65 |
-
let recognition = null;
|
66 |
-
|
67 |
-
if (SpeechRecognition) {
|
68 |
-
recognition = new SpeechRecognition();
|
69 |
-
recognition.continuous = false;
|
70 |
-
recognition.interimResults = false;
|
71 |
|
72 |
-
// Map language
|
73 |
const speechLangMap = {
|
74 |
'English (US)': 'en-US',
|
75 |
'Hindi (India)': 'hi-IN',
|
@@ -79,73 +74,106 @@
|
|
79 |
'Arabic (Saudi Arabia)': 'ar-SA'
|
80 |
};
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
// Set initial language
|
88 |
-
recognition.lang = speechLangMap[languageSelect.value] || 'en-US';
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
speakBtn.textContent = 'Listening...';
|
94 |
-
});
|
95 |
|
96 |
recognition.onresult = (event) => {
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
};
|
101 |
|
102 |
-
recognition.
|
103 |
-
|
104 |
};
|
105 |
|
106 |
-
recognition.
|
107 |
-
|
108 |
-
speakBtn.textContent = 'Tap to Speak';
|
109 |
};
|
110 |
-
|
111 |
-
|
112 |
-
speakBtn.textContent = 'Speech Not Supported';
|
113 |
}
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
-
|
121 |
-
|
|
|
122 |
return;
|
123 |
}
|
|
|
|
|
|
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
};
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
const result = await response.json();
|
140 |
-
if (response.ok) {
|
141 |
-
outputDiv.textContent = `Response (${result.language}): ${result.response}`;
|
142 |
-
} else {
|
143 |
-
outputDiv.textContent = `Error: ${result.error}`;
|
144 |
}
|
145 |
-
}
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
149 |
</script>
|
150 |
</body>
|
151 |
</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="English (US)">English (US)</option>
|
45 |
+
<option value="Hindi (India)">Hindi (India)</option>
|
46 |
+
<option value="Spanish (Spain)">Spanish (Spain)</option>
|
47 |
+
<option value="French (France)">French (France)</option>
|
48 |
+
<option value="German (Germany)">German (Germany)</option>
|
49 |
+
<option value="Arabic (Saudi Arabia)">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: 0; 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 selectedLang = document.getElementById("languageSelector").value;
|
65 |
+
const action = document.getElementById('action');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
// Map language names to speech recognition language codes
|
68 |
const speechLangMap = {
|
69 |
'English (US)': 'en-US',
|
70 |
'Hindi (India)': 'hi-IN',
|
|
|
74 |
'Arabic (Saudi Arabia)': 'ar-SA'
|
75 |
};
|
76 |
|
77 |
+
let recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
78 |
+
recognition.lang = speechLangMap[selectedLang] || 'en-US';
|
79 |
+
recognition.interimResults = false;
|
80 |
+
recognition.continuous = false;
|
|
|
|
|
|
|
81 |
|
82 |
+
recognition.onstart = () => {
|
83 |
+
action.innerHTML = "Listening...";
|
84 |
+
};
|
|
|
|
|
85 |
|
86 |
recognition.onresult = (event) => {
|
87 |
+
var transcript = event.results[0][0].transcript;
|
88 |
+
action.innerHTML = "";
|
89 |
+
sendMessage(transcript, selectedLang);
|
90 |
};
|
91 |
|
92 |
+
recognition.onerror = (event) => {
|
93 |
+
action.innerHTML = "Error: " + event.error;
|
94 |
};
|
95 |
|
96 |
+
recognition.onend = () => {
|
97 |
+
action.innerHTML = "";
|
|
|
98 |
};
|
99 |
+
|
100 |
+
recognition.start();
|
|
|
101 |
}
|
102 |
|
103 |
+
function sendMessage(message, language) {
|
104 |
+
showUserMessage(message);
|
105 |
+
sendToFlaskAPI(message, language);
|
106 |
+
}
|
107 |
+
|
108 |
+
function sendToFlaskAPI(message, language) {
|
109 |
+
fetch('/api/process_text', {
|
110 |
+
method: 'POST',
|
111 |
+
headers: {
|
112 |
+
'Content-Type': 'application/json',
|
113 |
+
},
|
114 |
+
body: JSON.stringify({ text: message, language: language })
|
115 |
+
})
|
116 |
+
.then(response => response.json())
|
117 |
+
.then(data => {
|
118 |
+
console.log('Response from Flask API:', data);
|
119 |
+
handleResponse(data);
|
120 |
+
})
|
121 |
+
.catch(error => {
|
122 |
+
console.error('Error sending data to Flask API:', error);
|
123 |
+
showBotMessage('Error: Unable to process request');
|
124 |
+
});
|
125 |
+
}
|
126 |
|
127 |
+
function handleResponse(data) {
|
128 |
+
if (data.error) {
|
129 |
+
showBotMessage(data.error);
|
130 |
return;
|
131 |
}
|
132 |
+
showBotMessage(data.response);
|
133 |
+
speakResponse(data.response, data.language);
|
134 |
+
}
|
135 |
|
136 |
+
function showUserMessage(message) {
|
137 |
+
var chatBox = document.getElementById('chat-box');
|
138 |
+
var userMessageHTML = '<div class="user-message">' + message + '</div>';
|
139 |
+
chatBox.innerHTML += userMessageHTML;
|
140 |
+
chatBox.scrollTop = chatBox.scrollHeight; // Auto-scroll to bottom
|
141 |
+
}
|
142 |
+
|
143 |
+
function showBotMessage(message) {
|
144 |
+
var chatBox = document.getElementById('chat-box');
|
145 |
+
var botMessageHTML = '<div class="bot-message">' + message + '</div>';
|
146 |
+
chatBox.innerHTML += botMessageHTML;
|
147 |
+
chatBox.scrollTop = chatBox.scrollHeight; // Auto-scroll to bottom
|
148 |
+
}
|
149 |
+
|
150 |
+
function speakResponse(response, language) {
|
151 |
+
// Map language names to speech synthesis language codes
|
152 |
+
const speechLangMap = {
|
153 |
+
'English (US)': 'en-US',
|
154 |
+
'Hindi (India)': 'hi-IN',
|
155 |
+
'Spanish (Spain)': 'es-ES',
|
156 |
+
'French (France)': 'fr-FR',
|
157 |
+
'German (Germany)': 'de-DE',
|
158 |
+
'Arabic (Saudi Arabia)': 'ar-SA'
|
159 |
};
|
160 |
|
161 |
+
var utterance = new SpeechSynthesisUtterance(response);
|
162 |
+
utterance.lang = speechLangMap[language] || 'en-US';
|
163 |
+
synth.speak(utterance);
|
164 |
+
|
165 |
+
// Cancel speech on page unload or new speech input
|
166 |
+
window.addEventListener('beforeunload', () => {
|
167 |
+
if (synth.speaking) {
|
168 |
+
synth.cancel();
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
}
|
170 |
+
});
|
171 |
+
document.getElementById('speech').addEventListener('click', () => {
|
172 |
+
if (synth.speaking) {
|
173 |
+
synth.cancel();
|
174 |
+
}
|
175 |
+
});
|
176 |
+
}
|
177 |
</script>
|
178 |
</body>
|
179 |
</html>
|