AiDeveloper1 commited on
Commit
44defdc
·
verified ·
1 Parent(s): efe8df6

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +118 -117
templates/index.html CHANGED
@@ -1,150 +1,151 @@
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
- <!-- Add more as needed -->
51
- </select>
52
-
53
- <div class="speaker" style="display: flex; justify-content: space-between; width: 100%; box-shadow: 0 0 13px #0000003d; border-radius: 5px; margin-top: 10px;">
54
- <p id="action" style="color: grey; font-weight: 800; padding: 0; padding-left: 2rem;"></p>
55
- <button id="speech" onclick="runSpeechRecog()" style="border: transparent; padding: 0 0.5rem;">
56
- Tap to Speak
57
- </button>
58
- </div>
59
- </div>
60
 
61
  <script>
62
- let synth = window.speechSynthesis;
63
-
64
- function runSpeechRecog() {
65
- const selectedLang = document.getElementById("languageSelector").value;
66
- const action = document.getElementById('action');
67
-
68
- let recognition = new webkitSpeechRecognition();
69
- recognition.lang = selectedLang;
70
- recognition.interimResults = false;
 
 
 
 
71
  recognition.continuous = false;
 
72
 
73
- recognition.onstart = () => {
74
- action.innerHTML = "Listening...";
 
 
 
 
 
 
75
  };
76
 
77
- recognition.onresult = (event) => {
78
- var transcript = event.results[0][0].transcript;
79
- action.innerHTML = "";
80
- sendMessage(transcript);
81
- };
82
 
83
- recognition.onerror = (event) => {
84
- action.innerHTML = "Error: " + event.error;
 
 
 
 
 
 
 
 
 
 
 
85
  };
86
 
87
  recognition.onend = () => {
88
- action.innerHTML = "";
89
  };
90
 
91
- recognition.start();
92
- }
93
-
94
- function sendMessage(message) {
95
- showUserMessage(message);
96
- sendToFlaskAPI(message);
 
97
  }
98
 
99
- function sendToFlaskAPI(message) {
100
- fetch('/api/process_text', {
101
- method: 'POST',
102
- headers: {
103
- 'Content-Type': 'application/json',
104
- },
105
- body: JSON.stringify({ text: message })
106
- })
107
- .then(response => response.json())
108
- .then(data => {
109
- console.log('Response from Flask API:', data);
110
- handleResponse(data);
111
- })
112
- .catch(error => {
113
- console.error('Error sending data to Flask API:', error);
114
- });
115
- }
116
 
117
- function handleResponse(data) {
118
- showBotMessage(data);
119
- speakResponse(data);
120
- }
121
 
122
- function showUserMessage(message) {
123
- var chatBox = document.getElementById('chat-box');
124
- var userMessageHTML = '<div class="user-message">' + message + '</div>';
125
- chatBox.innerHTML += userMessageHTML;
126
- }
127
-
128
- function showBotMessage(message) {
129
- var chatBox = document.getElementById('chat-box');
130
- var botMessageHTML = '<div class="bot-message">' + message + '</div>';
131
- chatBox.innerHTML += botMessageHTML;
132
- }
133
 
134
- function speakResponse(response) {
135
- var utterance = new SpeechSynthesisUtterance(response);
136
- speechSynthesis.speak(utterance);
137
- window.addEventListener('beforeunload', function () {
138
- if (synth.speaking) {
139
- synth.cancel();
 
 
 
 
 
 
 
 
140
  }
141
- });
142
- document.getElementById('speech').addEventListener('click', function () {
143
- if (synth.speaking) {
144
- synth.cancel();
145
- }
146
- });
147
- }
148
  </script>
149
  </body>
150
  </html>
 
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.0">
6
+ <title>Text Translator</title>
7
  <style>
8
+ body {
 
 
 
 
 
 
9
  font-family: Arial, sans-serif;
10
+ display: flex;
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
+ min-height: 50px;
25
+ width: 80%;
26
+ max-width: 600px;
27
+ }
28
+ #speak-btn {
29
+ background-color: #4CAF50;
30
+ color: white;
31
+ border: none;
32
+ padding: 10px 20px;
33
+ cursor: pointer;
34
+ }
35
+ #speak-btn:hover {
36
+ background-color: #45a049;
37
  }
38
  </style>
39
  </head>
40
  <body>
41
+ <h1>Text Translator</h1>
42
+ <select id="language" name="language">
43
+ <option value="English (US)">English (US)</option>
44
+ <option value="Hindi (India)">Hindi (India)</option>
45
+ <option value="Spanish (Spain)">Spanish (Spain)</option>
46
+ <option value="French (France)">French (France)</option>
47
+ <option value="German (Germany)">German (Germany)</option>
48
+ <option value="Arabic (Saudi Arabia)">Arabic (Saudi Arabia)</option>
49
+ </select>
50
+ <input type="text" id="input-text" placeholder="Enter text or use speech input" style="width: 300px;">
51
+ <button id="speak-btn">Tap to Speak</button>
52
+ <button id="submit-btn">Submit</button>
53
+ <div id="output">Translated response will appear here</div>
 
 
 
 
 
 
 
 
54
 
55
  <script>
56
+ // Speech recognition setup
57
+ const speakBtn = document.getElementById('speak-btn');
58
+ const inputText = document.getElementById('input-text');
59
+ const languageSelect = document.getElementById('language');
60
+ const submitBtn = document.getElementById('submit-btn');
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 dropdown values to Web Speech API language codes
73
+ const speechLangMap = {
74
+ 'English (US)': 'en-US',
75
+ 'Hindi (India)': 'hi-IN',
76
+ 'Spanish (Spain)': 'es-ES',
77
+ 'French (France)': 'fr-FR',
78
+ 'German (Germany)': 'de-DE',
79
+ 'Arabic (Saudi Arabia)': 'ar-SA'
80
  };
81
 
82
+ // Update recognition language when dropdown changes
83
+ languageSelect.addEventListener('change', () => {
84
+ recognition.lang = speechLangMap[languageSelect.value] || 'en-US';
85
+ });
 
86
 
87
+ // Set initial language
88
+ recognition.lang = speechLangMap[languageSelect.value] || 'en-US';
89
+
90
+ // Handle speech recognition
91
+ speakBtn.addEventListener('click', () => {
92
+ recognition.start();
93
+ speakBtn.textContent = 'Listening...';
94
+ });
95
+
96
+ recognition.onresult = (event) => {
97
+ const transcript = event.results[0][0].transcript;
98
+ inputText.value = transcript;
99
+ speakBtn.textContent = 'Tap to Speak';
100
  };
101
 
102
  recognition.onend = () => {
103
+ speakBtn.textContent = 'Tap to Speak';
104
  };
105
 
106
+ recognition.onerror = (event) => {
107
+ outputDiv.textContent = `Speech recognition error: ${event.error}`;
108
+ speakBtn.textContent = 'Tap to Speak';
109
+ };
110
+ } else {
111
+ speakBtn.disabled = true;
112
+ speakBtn.textContent = 'Speech Not Supported';
113
  }
114
 
115
+ // Handle form submission
116
+ submitBtn.addEventListener('click', async () => {
117
+ const text = inputText.value.trim();
118
+ const language = languageSelect.value;
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
+ if (!text) {
121
+ outputDiv.textContent = 'Please enter or speak some text';
122
+ return;
123
+ }
124
 
125
+ const payload = {
126
+ text: text,
127
+ language: language
128
+ };
 
 
 
 
 
 
 
129
 
130
+ try {
131
+ const response = await fetch('/api/process_text', {
132
+ method: 'POST',
133
+ headers: {
134
+ 'Content-Type': 'application/json'
135
+ },
136
+ body: JSON.stringify(payload)
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
+ } catch (error) {
146
+ outputDiv.textContent = `Request failed: ${error.message}`;
147
+ }
148
+ });
 
 
 
149
  </script>
150
  </body>
151
  </html>