Spaces:
Sleeping
Sleeping
File size: 4,806 Bytes
b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d 59eea30 b32ac0d |
1 2 3 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 65 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
body {
font-family: Arial, sans-serif;
background-color: #eef1f5;
margin: 0;
padding: 20px;
}
#chat-container {
max-width: 600px;
margin: auto;
padding: 20px;
border-radius: 10px;
background-color: #f9f9f9;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
#chat-history {
height: 400px;
overflow-y: auto;
margin-bottom: 10px;
border: 1px solid #ddd;
background: white;
border-radius: 10px;
padding: 10px;
}
.message {
margin: 5px 0;
padding: 10px;
border-radius: 10px;
position: relative;
max-width: 80%;
}
.user-message {
background-color: #d1ecf1;
border: 1px solid #bee5eb;
color: #0c5460;
margin-left: auto;
}
.bot-message {
background-color: #f8d7da;
border: 1px solid #f5c6cb;
color: #721c24;
margin-right: auto;
}
#loading {
display: none;
margin: 10px 0;
text-align: center;
}
#user-input {
border: 1px solid #ccc;
}
#user-input:focus {
border-color: #007bff;
outline: none;
}
.btn-send {
background-color: #007bff;
color: white;
}
.btn-send:hover {
background-color: #0056b3;
transform: scale(1.05);
}
.btn-send:active {
transform: scale(0.95);
}
</style>
<title>Chat Interface</title>
</head>
<body>
<div id="chat-container" class="rounded p-4 shadow">
<div id="chat-history" class="mb-3"></div>
<div id="loading" class="spinner-border text-primary" role="status">
<span class="sr-only">Loading...</span>
</div>
<div class="input-group">
<input type="text" id="user-input" class="form-control" placeholder="Type your message..." aria-label="Message input">
<div class="input-group-append">
<button id="send-button" class="btn btn-send" aria-label="Send message">Send</button>
</div>
</div>
</div>
<script>
document.getElementById("send-button").addEventListener("click", sendMessage);
document.getElementById("user-input").addEventListener("keypress", function(event) {
if (event.key === "Enter") {
event.preventDefault();
sendMessage();
}
});
async function sendMessage() {
const input = document.getElementById("user-input");
const message = input.value.trim();
if (message === "") {
return;
}
addMessage("User", message, "user-message");
input.value = "";
// Show loading spinner
document.getElementById("loading").style.display = "block";
try {
const response = await fetch("/chat/", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ message })
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
addMessage("Bot", data.response, "bot-message");
} catch (error) {
console.error('Error:', error);
addMessage("Bot", "Sorry, something went wrong.", "bot-message");
} finally {
// Hide loading spinner
document.getElementById("loading").style.display = "none";
}
}
function addMessage(sender, message, className) {
const chatHistory = document.getElementById("chat-history");
const messageElement = document.createElement("div");
messageElement.className = `message ${className}`;
messageElement.innerHTML = `<strong>${sender}:</strong> ${message}`;
chatHistory.appendChild(messageElement);
chatHistory.scrollTop = chatHistory.scrollHeight;
}
</script>
</body>
</html>
|