Spaces:
Sleeping
Sleeping
Utkarsh Verma
commited on
Commit
Β·
4531016
1
Parent(s):
c13e300
Changes in UI
Browse files- templates/index.html +88 -103
templates/index.html
CHANGED
|
@@ -5,167 +5,152 @@
|
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
| 6 |
<title>AI Chatbot</title>
|
| 7 |
<style>
|
| 8 |
-
|
| 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: #
|
| 16 |
-
}
|
| 17 |
-
|
| 18 |
-
body {
|
| 19 |
-
display: flex;
|
| 20 |
-
flex-direction: column;
|
| 21 |
}
|
| 22 |
-
|
| 23 |
h1 {
|
| 24 |
text-align: center;
|
| 25 |
-
|
| 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 |
-
|
| 35 |
-
|
|
|
|
| 36 |
}
|
| 37 |
-
|
| 38 |
.chat-wrapper {
|
| 39 |
display: flex;
|
| 40 |
justify-content: center;
|
| 41 |
align-items: center;
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
-
|
| 46 |
-
#user-input {
|
| 47 |
flex: 1;
|
| 48 |
-
padding:
|
| 49 |
-
font-size: 16px;
|
| 50 |
-
border-radius: 6px;
|
| 51 |
border: none;
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
color: #fff;
|
| 55 |
}
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
padding: 10px 15px;
|
| 59 |
border: none;
|
| 60 |
-
|
| 61 |
-
color:
|
|
|
|
|
|
|
| 62 |
cursor: pointer;
|
| 63 |
-
|
| 64 |
-
margin-left: 5px;
|
| 65 |
}
|
| 66 |
-
|
| 67 |
-
|
| 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
|
| 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 |
-
|
| 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
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
chatBox.scrollTop = chatBox.scrollHeight;
|
| 112 |
-
saveHistory();
|
| 113 |
}
|
| 114 |
|
| 115 |
async function sendMessage() {
|
| 116 |
-
let userInput = document.getElementById("user-input").value
|
| 117 |
-
if (!userInput) return;
|
| 118 |
|
| 119 |
-
|
| 120 |
document.getElementById("user-input").value = "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
chatBox.appendChild(typingIndicator);
|
| 127 |
chatBox.scrollTop = chatBox.scrollHeight;
|
|
|
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 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 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
}
|
| 150 |
-
});
|
| 151 |
|
| 152 |
function startListening() {
|
| 153 |
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
| 154 |
-
recognition.lang =
|
| 155 |
-
recognition.
|
| 156 |
|
| 157 |
recognition.onresult = function (event) {
|
| 158 |
-
|
| 159 |
-
document.getElementById("user-input").value = transcript;
|
| 160 |
-
sendMessage();
|
| 161 |
};
|
|
|
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
| 166 |
|
| 167 |
-
|
|
|
|
| 168 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
</script>
|
| 170 |
</body>
|
| 171 |
</html>
|
|
|
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
| 6 |
<title>AI Chatbot</title>
|
| 7 |
<style>
|
| 8 |
+
body {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
font-family: 'Segoe UI', sans-serif;
|
| 10 |
+
margin: 0;
|
| 11 |
background-color: #121212;
|
| 12 |
+
color: #fff;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
}
|
|
|
|
| 14 |
h1 {
|
| 15 |
text-align: center;
|
| 16 |
+
padding: 1rem;
|
| 17 |
}
|
|
|
|
| 18 |
#chat-box {
|
|
|
|
|
|
|
|
|
|
| 19 |
background-color: #1e1e1e;
|
| 20 |
+
border: 1px solid #333;
|
| 21 |
+
border-radius: 10px;
|
| 22 |
+
padding: 1rem;
|
| 23 |
+
height: 80vh;
|
| 24 |
overflow-y: auto;
|
| 25 |
+
margin: 0 auto;
|
| 26 |
+
width: 90%;
|
| 27 |
+
max-width: 900px;
|
| 28 |
}
|
|
|
|
| 29 |
.chat-wrapper {
|
| 30 |
display: flex;
|
| 31 |
justify-content: center;
|
| 32 |
align-items: center;
|
| 33 |
+
gap: 10px;
|
| 34 |
+
margin: 1rem auto;
|
| 35 |
+
width: 90%;
|
| 36 |
+
max-width: 900px;
|
| 37 |
}
|
| 38 |
+
input[type="text"] {
|
|
|
|
| 39 |
flex: 1;
|
| 40 |
+
padding: 0.8rem;
|
|
|
|
|
|
|
| 41 |
border: none;
|
| 42 |
+
border-radius: 10px;
|
| 43 |
+
font-size: 1rem;
|
| 44 |
+
background-color: #2c2c2c;
|
| 45 |
color: #fff;
|
| 46 |
}
|
| 47 |
+
button {
|
| 48 |
+
padding: 0.8rem 1rem;
|
|
|
|
| 49 |
border: none;
|
| 50 |
+
border-radius: 10px;
|
| 51 |
+
background-color: #00bcd4;
|
| 52 |
+
color: #fff;
|
| 53 |
+
font-weight: bold;
|
| 54 |
cursor: pointer;
|
| 55 |
+
transition: background-color 0.3s ease;
|
|
|
|
| 56 |
}
|
| 57 |
+
button:hover {
|
| 58 |
+
background-color: #0097a7;
|
|
|
|
| 59 |
}
|
|
|
|
| 60 |
.typing {
|
| 61 |
font-style: italic;
|
| 62 |
opacity: 0.7;
|
| 63 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
</style>
|
| 65 |
</head>
|
| 66 |
<body>
|
| 67 |
+
<h1>π€ AI Chatbot</h1>
|
|
|
|
| 68 |
<div id="chat-box"></div>
|
| 69 |
|
| 70 |
<div class="chat-wrapper">
|
| 71 |
+
<input type="text" id="user-input" placeholder="Type a message..." onkeypress="handleKeyPress(event)" />
|
| 72 |
<button id="send-btn" onclick="sendMessage()">Send</button>
|
| 73 |
<button id="mic-btn" onclick="startListening()">π€</button>
|
| 74 |
+
<button id="clear-btn" onclick="clearChat()">ποΈ Clear</button>
|
| 75 |
</div>
|
| 76 |
|
| 77 |
<script>
|
| 78 |
+
const chatBox = document.getElementById("chat-box");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
function handleKeyPress(event) {
|
| 81 |
+
if (event.key === "Enter") {
|
| 82 |
+
sendMessage();
|
| 83 |
+
}
|
|
|
|
|
|
|
| 84 |
}
|
| 85 |
|
| 86 |
async function sendMessage() {
|
| 87 |
+
let userInput = document.getElementById("user-input").value;
|
| 88 |
+
if (!userInput.trim()) return;
|
| 89 |
|
| 90 |
+
appendMessage("You", userInput);
|
| 91 |
document.getElementById("user-input").value = "";
|
| 92 |
+
appendTyping();
|
| 93 |
+
|
| 94 |
+
let response = await fetch("/chat", {
|
| 95 |
+
method: "POST",
|
| 96 |
+
headers: { "Content-Type": "application/json" },
|
| 97 |
+
body: JSON.stringify({ message: userInput }),
|
| 98 |
+
});
|
| 99 |
+
|
| 100 |
+
let data = await response.json();
|
| 101 |
+
removeTyping();
|
| 102 |
+
|
| 103 |
+
let reply = data.reply || "Error in response";
|
| 104 |
+
appendMessage("Bot", reply);
|
| 105 |
+
saveChat();
|
| 106 |
+
}
|
| 107 |
|
| 108 |
+
function appendMessage(sender, text) {
|
| 109 |
+
let msg = document.createElement("p");
|
| 110 |
+
msg.innerHTML = `<b>${sender}:</b> ${text}`;
|
| 111 |
+
chatBox.appendChild(msg);
|
|
|
|
| 112 |
chatBox.scrollTop = chatBox.scrollHeight;
|
| 113 |
+
}
|
| 114 |
|
| 115 |
+
function appendTyping() {
|
| 116 |
+
let typing = document.createElement("p");
|
| 117 |
+
typing.className = "typing";
|
| 118 |
+
typing.id = "typing";
|
| 119 |
+
typing.innerText = "Bot is typing...";
|
| 120 |
+
chatBox.appendChild(typing);
|
| 121 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
}
|
| 123 |
|
| 124 |
+
function removeTyping() {
|
| 125 |
+
let typing = document.getElementById("typing");
|
| 126 |
+
if (typing) typing.remove();
|
| 127 |
+
}
|
|
|
|
|
|
|
| 128 |
|
| 129 |
function startListening() {
|
| 130 |
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
| 131 |
+
recognition.lang = "en-US";
|
| 132 |
+
recognition.start();
|
| 133 |
|
| 134 |
recognition.onresult = function (event) {
|
| 135 |
+
document.getElementById("user-input").value = event.results[0][0].transcript;
|
|
|
|
|
|
|
| 136 |
};
|
| 137 |
+
}
|
| 138 |
|
| 139 |
+
function clearChat() {
|
| 140 |
+
chatBox.innerHTML = "";
|
| 141 |
+
localStorage.removeItem("chatHistory");
|
| 142 |
+
}
|
| 143 |
|
| 144 |
+
function saveChat() {
|
| 145 |
+
localStorage.setItem("chatHistory", chatBox.innerHTML);
|
| 146 |
}
|
| 147 |
+
|
| 148 |
+
function loadChat() {
|
| 149 |
+
let saved = localStorage.getItem("chatHistory");
|
| 150 |
+
if (saved) chatBox.innerHTML = saved;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
window.onload = loadChat;
|
| 154 |
</script>
|
| 155 |
</body>
|
| 156 |
</html>
|