Spaces:
Sleeping
Sleeping
Utkarsh Verma
commited on
Commit
·
8bc5476
1
Parent(s):
732ddfe
Changes in UI
Browse files- templates/index.html +123 -134
templates/index.html
CHANGED
@@ -1,150 +1,139 @@
|
|
1 |
-
|
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>AI Chatbot</title>
|
7 |
-
<
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
23 |
}
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
</head>
|
26 |
<body>
|
27 |
<h1>AI Chatbot</h1>
|
28 |
-
<div id="chat-box"
|
29 |
-
<
|
30 |
-
|
31 |
-
</
|
32 |
-
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
display: flex;
|
45 |
-
flex-direction: column;
|
46 |
-
align-items: center;
|
47 |
-
justify-content: start;
|
48 |
-
padding: 30px;
|
49 |
-
min-height: 100vh;
|
50 |
-
}
|
51 |
-
|
52 |
-
h1 {
|
53 |
-
color: #333;
|
54 |
-
margin-bottom: 20px;
|
55 |
-
}
|
56 |
-
|
57 |
-
#chat-box {
|
58 |
-
width: 90%;
|
59 |
-
max-width: 600px;
|
60 |
-
height: 400px;
|
61 |
-
border: 1px solid #ddd;
|
62 |
-
background: #fff;
|
63 |
-
border-radius: 10px;
|
64 |
-
padding: 15px;
|
65 |
-
overflow-y: auto;
|
66 |
-
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
67 |
-
margin-bottom: 20px;
|
68 |
-
}
|
69 |
-
|
70 |
-
#chat-box p {
|
71 |
-
margin: 10px 0;
|
72 |
-
line-height: 1.4;
|
73 |
-
}
|
74 |
-
|
75 |
-
#chat-box b {
|
76 |
-
color: #0057b7;
|
77 |
-
}
|
78 |
-
|
79 |
-
.chat-input {
|
80 |
-
display: flex;
|
81 |
-
width: 90%;
|
82 |
-
max-width: 600px;
|
83 |
-
}
|
84 |
-
|
85 |
-
#user-input {
|
86 |
-
flex: 1;
|
87 |
-
padding: 12px;
|
88 |
-
border: 1px solid #ccc;
|
89 |
-
border-radius: 10px 0 0 10px;
|
90 |
-
font-size: 16px;
|
91 |
-
}
|
92 |
-
|
93 |
-
button {
|
94 |
-
padding: 12px 20px;
|
95 |
-
background: #0057b7;
|
96 |
-
color: #fff;
|
97 |
-
border: none;
|
98 |
-
border-radius: 0 10px 10px 0;
|
99 |
-
cursor: pointer;
|
100 |
-
font-size: 16px;
|
101 |
-
transition: background 0.3s ease;
|
102 |
-
}
|
103 |
-
|
104 |
-
button:hover {
|
105 |
-
background: #003f91;
|
106 |
-
}
|
107 |
-
</style>
|
108 |
-
<script>
|
109 |
-
async function sendMessage() {
|
110 |
-
let userInput = document.getElementById("user-input").value;
|
111 |
-
let chatBox = document.getElementById("chat-box");
|
112 |
-
|
113 |
-
if (!userInput.trim()) return;
|
114 |
-
|
115 |
-
chatBox.innerHTML += `<p><b>You:</b> ${userInput}</p>`;
|
116 |
-
chatBox.scrollTop = chatBox.scrollHeight;
|
117 |
-
|
118 |
-
let response = await fetch("/chat", {
|
119 |
-
method: "POST",
|
120 |
-
headers: { "Content-Type": "application/json" },
|
121 |
-
body: JSON.stringify({ message: userInput }),
|
122 |
-
});
|
123 |
-
|
124 |
-
let data = await response.json();
|
125 |
-
chatBox.innerHTML += `<p><b>Bot:</b> ${data.reply || "Error in response"}</p>`;
|
126 |
-
chatBox.scrollTop = chatBox.scrollHeight;
|
127 |
-
|
128 |
-
document.getElementById("user-input").value = "";
|
129 |
-
}
|
130 |
-
|
131 |
-
// Allow pressing Enter to send message
|
132 |
-
document.addEventListener("DOMContentLoaded", () => {
|
133 |
-
document.getElementById("user-input").addEventListener("keydown", (e) => {
|
134 |
-
if (e.key === "Enter") {
|
135 |
-
e.preventDefault();
|
136 |
-
sendMessage();
|
137 |
}
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>AI Chatbot</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
background-color: #121212;
|
10 |
+
color: #f1f1f1;
|
11 |
+
font-family: Arial, sans-serif;
|
12 |
+
display: flex;
|
13 |
+
flex-direction: column;
|
14 |
+
align-items: center;
|
15 |
+
padding: 20px;
|
16 |
+
}
|
17 |
|
18 |
+
h1 {
|
19 |
+
color: #00ffff;
|
20 |
+
}
|
21 |
|
22 |
+
#chat-box {
|
23 |
+
width: 100%;
|
24 |
+
max-width: 600px;
|
25 |
+
border: 1px solid #444;
|
26 |
+
padding: 10px;
|
27 |
+
height: 400px;
|
28 |
+
overflow-y: auto;
|
29 |
+
background-color: #1e1e1e;
|
30 |
+
border-radius: 8px;
|
31 |
+
margin-bottom: 10px;
|
32 |
+
}
|
33 |
|
34 |
+
#user-input {
|
35 |
+
width: 75%;
|
36 |
+
padding: 10px;
|
37 |
+
border-radius: 6px;
|
38 |
+
border: 1px solid #555;
|
39 |
+
background-color: #2a2a2a;
|
40 |
+
color: white;
|
41 |
}
|
42 |
+
|
43 |
+
button {
|
44 |
+
padding: 10px 15px;
|
45 |
+
background-color: #00ffff;
|
46 |
+
color: black;
|
47 |
+
border: none;
|
48 |
+
border-radius: 6px;
|
49 |
+
cursor: pointer;
|
50 |
+
margin-left: 5px;
|
51 |
+
}
|
52 |
+
|
53 |
+
.bot-typing {
|
54 |
+
color: #aaa;
|
55 |
+
font-style: italic;
|
56 |
+
}
|
57 |
+
|
58 |
+
.mic-button {
|
59 |
+
background-color: #444;
|
60 |
+
color: white;
|
61 |
+
border-radius: 50%;
|
62 |
+
padding: 8px 10px;
|
63 |
+
margin-left: 5px;
|
64 |
+
border: 1px solid #666;
|
65 |
+
}
|
66 |
+
</style>
|
67 |
</head>
|
68 |
<body>
|
69 |
<h1>AI Chatbot</h1>
|
70 |
+
<div id="chat-box"></div>
|
71 |
+
<div>
|
72 |
+
<input type="text" id="user-input" placeholder="Type a message..." />
|
73 |
+
<button onclick="sendMessage()">Send</button>
|
74 |
+
<button class="mic-button" onclick="startListening()">🎤</button>
|
75 |
+
</div>
|
76 |
|
77 |
+
<script>
|
78 |
+
let chatHistory = [];
|
79 |
+
|
80 |
+
function updateChatBox() {
|
81 |
+
const chatBox = document.getElementById("chat-box");
|
82 |
+
chatBox.innerHTML = "";
|
83 |
+
chatHistory.forEach(({ sender, message }) => {
|
84 |
+
chatBox.innerHTML += `<p><b>${sender}:</b> ${message}</p>`;
|
85 |
+
});
|
86 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
}
|
88 |
+
|
89 |
+
async function sendMessage() {
|
90 |
+
const inputEl = document.getElementById("user-input");
|
91 |
+
const message = inputEl.value.trim();
|
92 |
+
if (!message) return;
|
93 |
+
|
94 |
+
chatHistory.push({ sender: "You", message });
|
95 |
+
updateChatBox();
|
96 |
+
inputEl.value = "";
|
97 |
+
|
98 |
+
// Show typing...
|
99 |
+
chatHistory.push({ sender: "Bot", message: "<span class='bot-typing'>Typing...</span>" });
|
100 |
+
updateChatBox();
|
101 |
+
|
102 |
+
try {
|
103 |
+
const response = await fetch("/chat", {
|
104 |
+
method: "POST",
|
105 |
+
headers: { "Content-Type": "application/json" },
|
106 |
+
body: JSON.stringify({ message }),
|
107 |
+
});
|
108 |
+
|
109 |
+
const data = await response.json();
|
110 |
+
chatHistory.pop(); // Remove "Typing..." message
|
111 |
+
chatHistory.push({ sender: "Bot", message: data.reply || "Error in response" });
|
112 |
+
updateChatBox();
|
113 |
+
} catch (e) {
|
114 |
+
chatHistory.pop();
|
115 |
+
chatHistory.push({ sender: "Bot", message: "Failed to reach server." });
|
116 |
+
updateChatBox();
|
117 |
+
}
|
118 |
+
}
|
119 |
+
|
120 |
+
// 🎤 Speech-to-text
|
121 |
+
function startListening() {
|
122 |
+
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
123 |
+
recognition.lang = "en-US";
|
124 |
+
recognition.interimResults = false;
|
125 |
+
recognition.maxAlternatives = 1;
|
126 |
+
|
127 |
+
recognition.onresult = (event) => {
|
128 |
+
document.getElementById("user-input").value = event.results[0][0].transcript;
|
129 |
+
};
|
130 |
+
|
131 |
+
recognition.onerror = (event) => {
|
132 |
+
alert("Speech recognition error: " + event.error);
|
133 |
+
};
|
134 |
+
|
135 |
+
recognition.start();
|
136 |
+
}
|
137 |
+
</script>
|
138 |
</body>
|
139 |
</html>
|