Spaces:
Running
Running
Utkarsh Verma
commited on
Commit
·
c13e300
1
Parent(s):
dd124d4
Changes in UI
Browse files- templates/index.html +162 -148
templates/index.html
CHANGED
@@ -1,157 +1,171 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
<html lang="en">
|
3 |
<head>
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
</head>
|
65 |
<body>
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
body: JSON.stringify({ message: userInput })
|
118 |
-
});
|
119 |
-
|
120 |
-
const data = await response.json();
|
121 |
-
chatBox.removeChild(typingIndicator);
|
122 |
-
updateChatBox("bot", data.reply || "Error in response");
|
123 |
-
} catch (err) {
|
124 |
-
chatBox.removeChild(typingIndicator);
|
125 |
-
updateChatBox("bot", "Something went wrong.");
|
126 |
-
}
|
127 |
-
}
|
128 |
-
|
129 |
-
// Send with Enter key
|
130 |
-
document.getElementById("user-input").addEventListener("keydown", function (event) {
|
131 |
-
if (event.key === "Enter") {
|
132 |
-
event.preventDefault();
|
133 |
-
sendMessage();
|
134 |
-
}
|
135 |
});
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
</body>
|
157 |
</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 |
+
html, body {
|
9 |
+
margin: 0;
|
10 |
+
padding: 0;
|
11 |
+
height: 100%;
|
12 |
+
width: 100%;
|
13 |
+
font-family: 'Segoe UI', sans-serif;
|
14 |
+
background-color: #121212;
|
15 |
+
color: #f1f1f1;
|
16 |
+
}
|
17 |
+
|
18 |
+
body {
|
19 |
+
display: flex;
|
20 |
+
flex-direction: column;
|
21 |
+
}
|
22 |
+
|
23 |
+
h1 {
|
24 |
+
text-align: center;
|
25 |
+
margin: 10px 0;
|
26 |
+
}
|
27 |
+
|
28 |
+
#chat-box {
|
29 |
+
flex: 1;
|
30 |
+
border-top: 1px solid #444;
|
31 |
+
border-bottom: 1px solid #444;
|
32 |
+
background-color: #1e1e1e;
|
33 |
+
overflow-y: auto;
|
34 |
+
padding: 20px;
|
35 |
+
border-radius: 0;
|
36 |
+
}
|
37 |
+
|
38 |
+
.chat-wrapper {
|
39 |
+
display: flex;
|
40 |
+
justify-content: center;
|
41 |
+
align-items: center;
|
42 |
+
padding: 10px;
|
43 |
+
background-color: #1a1a1a;
|
44 |
+
}
|
45 |
+
|
46 |
+
#user-input {
|
47 |
+
flex: 1;
|
48 |
+
padding: 12px;
|
49 |
+
font-size: 16px;
|
50 |
+
border-radius: 6px;
|
51 |
+
border: none;
|
52 |
+
margin-right: 10px;
|
53 |
+
background-color: #2a2a2a;
|
54 |
+
color: #fff;
|
55 |
+
}
|
56 |
+
|
57 |
+
#send-btn, #mic-btn {
|
58 |
+
padding: 10px 15px;
|
59 |
+
border: none;
|
60 |
+
background-color: #1db954;
|
61 |
+
color: white;
|
62 |
+
cursor: pointer;
|
63 |
+
border-radius: 6px;
|
64 |
+
margin-left: 5px;
|
65 |
+
}
|
66 |
+
|
67 |
+
#send-btn:hover, #mic-btn:hover {
|
68 |
+
background-color: #1ed760;
|
69 |
+
}
|
70 |
+
|
71 |
+
.typing {
|
72 |
+
font-style: italic;
|
73 |
+
opacity: 0.7;
|
74 |
+
}
|
75 |
+
|
76 |
+
p {
|
77 |
+
margin: 8px 0;
|
78 |
+
line-height: 1.5;
|
79 |
+
}
|
80 |
+
</style>
|
81 |
</head>
|
82 |
<body>
|
83 |
+
<h1>🧠 AI Chatbot</h1>
|
84 |
+
|
85 |
+
<div id="chat-box"></div>
|
86 |
+
|
87 |
+
<div class="chat-wrapper">
|
88 |
+
<input type="text" id="user-input" placeholder="Type a message..." />
|
89 |
+
<button id="send-btn" onclick="sendMessage()">Send</button>
|
90 |
+
<button id="mic-btn" onclick="startListening()">🎤</button>
|
91 |
+
</div>
|
92 |
+
|
93 |
+
<script>
|
94 |
+
// Load saved chat history
|
95 |
+
window.onload = function () {
|
96 |
+
const history = localStorage.getItem("chatHistory");
|
97 |
+
if (history) {
|
98 |
+
document.getElementById("chat-box").innerHTML = history;
|
99 |
+
}
|
100 |
+
};
|
101 |
+
|
102 |
+
function saveHistory() {
|
103 |
+
const chatBox = document.getElementById("chat-box");
|
104 |
+
localStorage.setItem("chatHistory", chatBox.innerHTML);
|
105 |
+
}
|
106 |
+
|
107 |
+
function updateChatBox(role, message) {
|
108 |
+
let chatBox = document.getElementById("chat-box");
|
109 |
+
const tag = role === "user" ? "You" : "Bot";
|
110 |
+
chatBox.innerHTML += `<p><b>${tag}:</b> ${message}</p>`;
|
111 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
112 |
+
saveHistory();
|
113 |
+
}
|
114 |
+
|
115 |
+
async function sendMessage() {
|
116 |
+
let userInput = document.getElementById("user-input").value.trim();
|
117 |
+
if (!userInput) return;
|
118 |
+
|
119 |
+
updateChatBox("user", userInput);
|
120 |
+
document.getElementById("user-input").value = "";
|
121 |
+
|
122 |
+
const chatBox = document.getElementById("chat-box");
|
123 |
+
const typingIndicator = document.createElement("p");
|
124 |
+
typingIndicator.className = "typing";
|
125 |
+
typingIndicator.innerText = "Bot is typing...";
|
126 |
+
chatBox.appendChild(typingIndicator);
|
127 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
128 |
+
|
129 |
+
try {
|
130 |
+
const response = await fetch("/chat", {
|
131 |
+
method: "POST",
|
132 |
+
headers: { "Content-Type": "application/json" },
|
133 |
+
body: JSON.stringify({ message: userInput })
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
});
|
135 |
|
136 |
+
const data = await response.json();
|
137 |
+
chatBox.removeChild(typingIndicator);
|
138 |
+
updateChatBox("bot", data.reply || "Error in response");
|
139 |
+
} catch (err) {
|
140 |
+
chatBox.removeChild(typingIndicator);
|
141 |
+
updateChatBox("bot", "Something went wrong.");
|
142 |
+
}
|
143 |
+
}
|
144 |
+
|
145 |
+
document.getElementById("user-input").addEventListener("keydown", function (event) {
|
146 |
+
if (event.key === "Enter") {
|
147 |
+
event.preventDefault();
|
148 |
+
sendMessage();
|
149 |
+
}
|
150 |
+
});
|
151 |
+
|
152 |
+
function startListening() {
|
153 |
+
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
154 |
+
recognition.lang = 'en-US';
|
155 |
+
recognition.interimResults = false;
|
156 |
+
|
157 |
+
recognition.onresult = function (event) {
|
158 |
+
const transcript = event.results[0][0].transcript;
|
159 |
+
document.getElementById("user-input").value = transcript;
|
160 |
+
sendMessage();
|
161 |
+
};
|
162 |
+
|
163 |
+
recognition.onerror = function () {
|
164 |
+
updateChatBox("bot", "Speech recognition failed.");
|
165 |
+
};
|
166 |
+
|
167 |
+
recognition.start();
|
168 |
+
}
|
169 |
+
</script>
|
170 |
</body>
|
171 |
</html>
|