Spaces:
Running
Running
Update static/index.html
Browse files- static/index.html +69 -135
static/index.html
CHANGED
@@ -12,7 +12,6 @@
|
|
12 |
margin: 0;
|
13 |
padding: 0;
|
14 |
}
|
15 |
-
|
16 |
#chat-container {
|
17 |
max-width: 500px;
|
18 |
margin: auto;
|
@@ -22,7 +21,6 @@
|
|
22 |
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
|
23 |
position: relative;
|
24 |
}
|
25 |
-
|
26 |
#chat-history {
|
27 |
height: 400px;
|
28 |
overflow-y: auto;
|
@@ -32,7 +30,6 @@
|
|
32 |
box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.1);
|
33 |
margin-bottom: 15px;
|
34 |
}
|
35 |
-
|
36 |
.message {
|
37 |
padding: 12px 15px;
|
38 |
margin: 10px 0;
|
@@ -40,36 +37,23 @@
|
|
40 |
font-size: 0.9em;
|
41 |
line-height: 1.4em;
|
42 |
position: relative;
|
43 |
-
cursor: pointer;
|
44 |
-
transition: background-color 0.3s;
|
45 |
}
|
46 |
-
|
47 |
.user-message {
|
48 |
background: linear-gradient(135deg, #c9e6f5 0%, #91c9e6 100%);
|
49 |
color: #006c8e;
|
50 |
margin-left: auto;
|
51 |
border-radius: 20px 20px 0 20px;
|
52 |
}
|
53 |
-
|
54 |
.bot-message {
|
55 |
background: linear-gradient(135deg, #fce0e0 0%, #85cfb6 100%);
|
56 |
color: #8b3e40;
|
57 |
margin-right: auto;
|
58 |
border-radius: 20px 20px 20px 0;
|
59 |
}
|
60 |
-
|
61 |
.user-icon, .bot-icon {
|
62 |
margin-right: 10px;
|
63 |
font-size: 1.2em;
|
64 |
}
|
65 |
-
|
66 |
-
#loading {
|
67 |
-
display: none;
|
68 |
-
margin: 15px 0;
|
69 |
-
text-align: center;
|
70 |
-
font-size: 1.2em;
|
71 |
-
}
|
72 |
-
|
73 |
#user-input {
|
74 |
border: 1px solid #ddd;
|
75 |
border-radius: 8px;
|
@@ -78,13 +62,11 @@
|
|
78 |
font-size: 1em;
|
79 |
width: calc(100% - 48px);
|
80 |
}
|
81 |
-
|
82 |
#user-input:focus {
|
83 |
border-color: #007bff;
|
84 |
outline: none;
|
85 |
}
|
86 |
-
|
87 |
-
.btn-send {
|
88 |
background-color: #007bff;
|
89 |
color: white;
|
90 |
border-radius: 8px;
|
@@ -94,160 +76,112 @@
|
|
94 |
font-size: 1.1em;
|
95 |
transition: background-color 0.3s;
|
96 |
}
|
97 |
-
|
98 |
-
.btn-send:hover {
|
99 |
background-color: #0056b3;
|
100 |
}
|
101 |
-
|
102 |
-
.btn-close {
|
103 |
-
position: absolute;
|
104 |
-
top: 12px;
|
105 |
-
right: 12px;
|
106 |
-
background-color: #dc3545;
|
107 |
-
color: white;
|
108 |
-
border: none;
|
109 |
-
border-radius: 50%;
|
110 |
-
width: 30px;
|
111 |
-
height: 30px;
|
112 |
-
font-size: 1.2em;
|
113 |
-
display: flex;
|
114 |
-
align-items: center;
|
115 |
-
justify-content: center;
|
116 |
-
cursor: pointer;
|
117 |
-
transition: background-color 0.3s;
|
118 |
-
}
|
119 |
-
|
120 |
-
.btn-close:hover {
|
121 |
-
background-color: #c82333;
|
122 |
-
}
|
123 |
-
|
124 |
.typing-indicator {
|
125 |
font-style: italic;
|
126 |
color: #888;
|
127 |
margin-top: 10px;
|
128 |
}
|
129 |
-
|
130 |
-
.timestamp {
|
131 |
-
font-size: 0.75em;
|
132 |
-
color: #999;
|
133 |
-
display: none;
|
134 |
-
margin-top: 5px;
|
135 |
-
}
|
136 |
</style>
|
137 |
-
<title>
|
138 |
</head>
|
139 |
<body>
|
140 |
-
<div id="chat-container"
|
141 |
-
<
|
142 |
-
<
|
143 |
-
<
|
144 |
-
|
145 |
-
|
146 |
-
<div class="input-group mb-2">
|
147 |
-
<textarea id="user-input" class="form-control" rows="2" placeholder="Type your message..." aria-label="Message input"></textarea>
|
148 |
-
<div class="input-group-append">
|
149 |
-
<button id="send-button" class="btn-send" aria-label="Send message">
|
150 |
-
<i class="fas fa-paper-plane"></i>
|
151 |
-
</button>
|
152 |
-
</div>
|
153 |
</div>
|
154 |
-
<small class="form-text text-muted">Press Shift + Enter for a new line</small>
|
155 |
</div>
|
156 |
|
157 |
<script>
|
158 |
-
|
159 |
-
|
160 |
-
document.getElementById("user-input").addEventListener("keypress", function(event) {
|
161 |
-
if (event.key === "Enter" && !event.shiftKey) {
|
162 |
-
event.preventDefault();
|
163 |
-
sendMessage();
|
164 |
-
}
|
165 |
-
});
|
166 |
-
document.getElementById("close-button").addEventListener("click", closeChat);
|
167 |
-
|
168 |
-
window.addEventListener('DOMContentLoaded', () => {
|
169 |
-
sendInitialBotMessage();
|
170 |
-
});
|
171 |
|
172 |
-
|
173 |
-
|
174 |
-
const
|
175 |
-
|
176 |
-
|
|
|
|
|
|
|
177 |
}
|
178 |
|
|
|
179 |
async function sendMessage() {
|
180 |
const input = document.getElementById("user-input");
|
181 |
-
const userId = document.getElementById("user-id").value;
|
182 |
const message = input.value.trim();
|
183 |
-
if (message
|
|
|
184 |
addMessage("User", message, "user-message");
|
185 |
-
chatHistoryArray.push({
|
186 |
input.value = "";
|
187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
try {
|
189 |
const response = await fetch("/chat/", {
|
190 |
method: "POST",
|
191 |
headers: { "Content-Type": "application/json" },
|
192 |
body: JSON.stringify({ message })
|
193 |
});
|
194 |
-
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
|
195 |
const data = await response.json();
|
196 |
-
addMessage("Bot", data.response, "bot-message");
|
197 |
-
chatHistoryArray.push({ userId, sender: "Bot", message: data.response });
|
198 |
-
} catch (error) {
|
199 |
-
console.error('Error:', error);
|
200 |
-
addMessage("Bot", "Sorry, something went wrong.", "bot-message");
|
201 |
-
} finally {
|
202 |
-
hideTypingIndicator();
|
203 |
-
}
|
204 |
-
}
|
205 |
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
headers: { "Content-Type": "application/json" },
|
212 |
-
body: JSON.stringify({ history: chatHistoryArray, userId })
|
213 |
-
});
|
214 |
} catch (error) {
|
215 |
-
|
216 |
-
|
217 |
-
|
|
|
|
|
218 |
}
|
219 |
}
|
220 |
|
221 |
-
|
222 |
-
|
223 |
-
const
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
return `<a href="${url}" target="_blank">${linkText}</a>`;
|
229 |
-
});
|
230 |
-
messageElement.innerHTML = `<span class="${sender.toLowerCase()}-icon">
|
231 |
-
${sender === "User" ? '<i class="fas fa-user"></i>' : '<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTa1q4s76cKilDcBElngWUNlMagmp18HjhF5A&s" alt="Bot" style="width: 20px; height: 20px; border-radius: 50%;">'}
|
232 |
-
</span>${formattedMessage}<div class="timestamp">${new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}</div>`;
|
233 |
-
chatHistory.appendChild(messageElement);
|
234 |
-
chatHistory.scrollTop = chatHistory.scrollHeight;
|
235 |
}
|
236 |
|
237 |
-
|
238 |
-
|
239 |
-
const
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
|
|
|
|
246 |
|
247 |
-
|
248 |
-
|
249 |
-
|
|
|
|
|
|
|
|
|
250 |
}
|
|
|
|
|
|
|
|
|
251 |
</script>
|
252 |
</body>
|
253 |
</html>
|
|
|
12 |
margin: 0;
|
13 |
padding: 0;
|
14 |
}
|
|
|
15 |
#chat-container {
|
16 |
max-width: 500px;
|
17 |
margin: auto;
|
|
|
21 |
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
|
22 |
position: relative;
|
23 |
}
|
|
|
24 |
#chat-history {
|
25 |
height: 400px;
|
26 |
overflow-y: auto;
|
|
|
30 |
box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.1);
|
31 |
margin-bottom: 15px;
|
32 |
}
|
|
|
33 |
.message {
|
34 |
padding: 12px 15px;
|
35 |
margin: 10px 0;
|
|
|
37 |
font-size: 0.9em;
|
38 |
line-height: 1.4em;
|
39 |
position: relative;
|
|
|
|
|
40 |
}
|
|
|
41 |
.user-message {
|
42 |
background: linear-gradient(135deg, #c9e6f5 0%, #91c9e6 100%);
|
43 |
color: #006c8e;
|
44 |
margin-left: auto;
|
45 |
border-radius: 20px 20px 0 20px;
|
46 |
}
|
|
|
47 |
.bot-message {
|
48 |
background: linear-gradient(135deg, #fce0e0 0%, #85cfb6 100%);
|
49 |
color: #8b3e40;
|
50 |
margin-right: auto;
|
51 |
border-radius: 20px 20px 20px 0;
|
52 |
}
|
|
|
53 |
.user-icon, .bot-icon {
|
54 |
margin-right: 10px;
|
55 |
font-size: 1.2em;
|
56 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
#user-input {
|
58 |
border: 1px solid #ddd;
|
59 |
border-radius: 8px;
|
|
|
62 |
font-size: 1em;
|
63 |
width: calc(100% - 48px);
|
64 |
}
|
|
|
65 |
#user-input:focus {
|
66 |
border-color: #007bff;
|
67 |
outline: none;
|
68 |
}
|
69 |
+
.btn-send, .btn-mic {
|
|
|
70 |
background-color: #007bff;
|
71 |
color: white;
|
72 |
border-radius: 8px;
|
|
|
76 |
font-size: 1.1em;
|
77 |
transition: background-color 0.3s;
|
78 |
}
|
79 |
+
.btn-send:hover, .btn-mic:hover {
|
|
|
80 |
background-color: #0056b3;
|
81 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
.typing-indicator {
|
83 |
font-style: italic;
|
84 |
color: #888;
|
85 |
margin-top: 10px;
|
86 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
</style>
|
88 |
+
<title>Voice Chatbot</title>
|
89 |
</head>
|
90 |
<body>
|
91 |
+
<div id="chat-container">
|
92 |
+
<div id="chat-history"></div>
|
93 |
+
<div class="input-group">
|
94 |
+
<textarea id="user-input" class="form-control" rows="1" placeholder="Type or speak your message..." aria-label="Message input"></textarea>
|
95 |
+
<button id="mic-button" class="btn-mic"><i class="fas fa-microphone"></i></button>
|
96 |
+
<button id="send-button" class="btn-send"><i class="fas fa-paper-plane"></i></button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
</div>
|
|
|
98 |
</div>
|
99 |
|
100 |
<script>
|
101 |
+
const chatHistoryArray = [];
|
102 |
+
const synth = window.speechSynthesis;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
+
// Add message to chat history
|
105 |
+
function addMessage(sender, message, className) {
|
106 |
+
const chatHistory = document.getElementById("chat-history");
|
107 |
+
const messageElement = document.createElement("div");
|
108 |
+
messageElement.className = `message ${className}`;
|
109 |
+
messageElement.innerHTML = `<strong>${sender}:</strong> ${message}`;
|
110 |
+
chatHistory.appendChild(messageElement);
|
111 |
+
chatHistory.scrollTop = chatHistory.scrollHeight;
|
112 |
}
|
113 |
|
114 |
+
// Send message to server
|
115 |
async function sendMessage() {
|
116 |
const input = document.getElementById("user-input");
|
|
|
117 |
const message = input.value.trim();
|
118 |
+
if (!message) return;
|
119 |
+
|
120 |
addMessage("User", message, "user-message");
|
121 |
+
chatHistoryArray.push({ sender: "User", message });
|
122 |
input.value = "";
|
123 |
+
|
124 |
+
// Simulate bot typing
|
125 |
+
const botTyping = document.createElement("div");
|
126 |
+
botTyping.className = "typing-indicator";
|
127 |
+
botTyping.textContent = "Bot is typing...";
|
128 |
+
document.getElementById("chat-history").appendChild(botTyping);
|
129 |
+
|
130 |
try {
|
131 |
const response = await fetch("/chat/", {
|
132 |
method: "POST",
|
133 |
headers: { "Content-Type": "application/json" },
|
134 |
body: JSON.stringify({ message })
|
135 |
});
|
|
|
136 |
const data = await response.json();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
+
botTyping.remove();
|
139 |
+
const botMessage = data.response;
|
140 |
+
addMessage("Bot", botMessage, "bot-message");
|
141 |
+
chatHistoryArray.push({ sender: "Bot", message: botMessage });
|
142 |
+
speak(botMessage);
|
|
|
|
|
|
|
143 |
} catch (error) {
|
144 |
+
botTyping.remove();
|
145 |
+
console.error("Error:", error);
|
146 |
+
const errorMessage = "Sorry, something went wrong.";
|
147 |
+
addMessage("Bot", errorMessage, "bot-message");
|
148 |
+
speak(errorMessage);
|
149 |
}
|
150 |
}
|
151 |
|
152 |
+
// Text-to-Speech function
|
153 |
+
function speak(text) {
|
154 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
155 |
+
utterance.lang = "en-US";
|
156 |
+
utterance.pitch = 1;
|
157 |
+
utterance.rate = 1;
|
158 |
+
synth.speak(utterance);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
}
|
160 |
|
161 |
+
// Speech-to-Text function
|
162 |
+
function startListening() {
|
163 |
+
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
164 |
+
recognition.lang = "en-US";
|
165 |
+
recognition.interimResults = false;
|
166 |
+
|
167 |
+
recognition.onresult = (event) => {
|
168 |
+
const transcript = event.results[0][0].transcript;
|
169 |
+
document.getElementById("user-input").value = transcript;
|
170 |
+
sendMessage();
|
171 |
+
};
|
172 |
|
173 |
+
recognition.onerror = (event) => {
|
174 |
+
console.error("Speech recognition error:", event.error);
|
175 |
+
addMessage("Bot", "Sorry, I couldn't understand you.", "bot-message");
|
176 |
+
speak("Sorry, I couldn't understand you.");
|
177 |
+
};
|
178 |
+
|
179 |
+
recognition.start();
|
180 |
}
|
181 |
+
|
182 |
+
// Event Listeners
|
183 |
+
document.getElementById("send-button").addEventListener("click", sendMessage);
|
184 |
+
document.getElementById("mic-button").addEventListener("click", startListening);
|
185 |
</script>
|
186 |
</body>
|
187 |
</html>
|