basic_chatbot / templates /index.html
arcsu1's picture
frst
4d0e37d
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Chatbot - Chat with GPT-2</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
min-height: 100vh;
padding: 20px;
display: flex;
flex-direction: column;
}
.container {
max-width: 800px;
margin: 0 auto;
width: 100%;
display: flex;
flex-direction: column;
height: calc(100vh - 40px);
}
header {
text-align: center;
color: white;
padding: 20px 0;
}
h1 {
font-size: 2rem;
font-weight: 700;
text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
margin-bottom: 5px;
}
.subtitle {
opacity: 0.9;
font-size: 1rem;
}
.chat-container {
flex: 1;
background: white;
border-radius: 16px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
display: flex;
flex-direction: column;
overflow: hidden;
}
.messages {
flex: 1;
overflow-y: auto;
padding: 20px;
display: flex;
flex-direction: column;
gap: 15px;
}
.message {
max-width: 80%;
padding: 12px 16px;
border-radius: 12px;
word-wrap: break-word;
animation: slideIn 0.3s ease;
}
@keyframes slideIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.message.user {
align-self: flex-end;
background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
color: white;
border-bottom-right-radius: 4px;
}
.message.ai {
align-self: flex-start;
background: #f3f4f6;
color: #1f2937;
border-bottom-left-radius: 4px;
}
.message.ai::before {
content: 'πŸ€– ';
}
.message.user::before {
content: 'πŸ‘€ ';
}
.typing-indicator {
align-self: flex-start;
padding: 12px 16px;
background: #f3f4f6;
border-radius: 12px;
border-bottom-left-radius: 4px;
display: none;
}
.typing-indicator.show {
display: block;
}
.typing-indicator span {
height: 8px;
width: 8px;
background: #9ca3af;
border-radius: 50%;
display: inline-block;
margin: 0 2px;
animation: bounce 1.4s infinite ease-in-out;
}
.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }
@keyframes bounce {
0%, 80%, 100% { transform: scale(0); }
40% { transform: scale(1); }
}
.input-area {
padding: 20px;
border-top: 1px solid #e5e7eb;
background: #fafafa;
}
.input-container {
display: flex;
gap: 10px;
}
#messageInput {
flex: 1;
padding: 12px 16px;
border: 2px solid #e5e7eb;
border-radius: 24px;
font-size: 1rem;
outline: none;
transition: border-color 0.2s;
}
#messageInput:focus {
border-color: #3b82f6;
}
#sendBtn {
padding: 12px 24px;
background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
color: white;
border: none;
border-radius: 24px;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
}
#sendBtn:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(59, 130, 246, 0.3);
}
#sendBtn:disabled {
opacity: 0.5;
cursor: not-allowed;
transform: none;
}
.clear-btn {
text-align: center;
padding: 10px;
border-top: 1px solid #e5e7eb;
background: #fafafa;
}
.clear-btn button {
padding: 8px 16px;
background: transparent;
color: #6b7280;
border: 1px solid #d1d5db;
border-radius: 8px;
font-size: 0.875rem;
cursor: pointer;
transition: all 0.2s;
}
.clear-btn button:hover {
background: #f3f4f6;
color: #374151;
}
.empty-state {
text-align: center;
color: #9ca3af;
padding: 40px 20px;
}
.empty-state h3 {
font-size: 1.5rem;
margin-bottom: 10px;
}
@media (max-width: 640px) {
h1 { font-size: 1.5rem; }
.message { max-width: 90%; }
#sendBtn { padding: 12px 20px; }
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>πŸ’¬ AI Chatbot</h1>
<p class="subtitle">Chat with fine-tuned GPT-2</p>
</header>
<div class="chat-container">
<div class="messages" id="messages">
<div class="empty-state">
<h3>πŸ‘‹ Hello!</h3>
<p>Start a conversation by typing a message below</p>
</div>
</div>
<div class="clear-btn">
<button id="clearBtn">Clear Chat</button>
</div>
<div class="input-area">
<div class="input-container">
<input type="text" id="messageInput" placeholder="Type your message..." autocomplete="off">
<button id="sendBtn">Send</button>
</div>
</div>
</div>
</div>
<script>
const messagesDiv = document.getElementById('messages');
const messageInput = document.getElementById('messageInput');
const sendBtn = document.getElementById('sendBtn');
const clearBtn = document.getElementById('clearBtn');
let conversationHistory = { user: [], ai: [] };
function addMessage(text, isUser) {
const emptyState = messagesDiv.querySelector('.empty-state');
if (emptyState) emptyState.remove();
const messageDiv = document.createElement('div');
messageDiv.className = `message ${isUser ? 'user' : 'ai'}`;
messageDiv.textContent = text;
messagesDiv.appendChild(messageDiv);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
function showTyping() {
const typing = document.createElement('div');
typing.className = 'typing-indicator show';
typing.id = 'typing';
typing.innerHTML = '<span></span><span></span><span></span>';
messagesDiv.appendChild(typing);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
function hideTyping() {
const typing = document.getElementById('typing');
if (typing) typing.remove();
}
async function sendMessage() {
const message = messageInput.value.trim();
if (!message) return;
addMessage(message, true);
conversationHistory.user.push(message);
messageInput.value = '';
sendBtn.disabled = true;
messageInput.disabled = true;
showTyping();
try {
const response = await fetch('/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(conversationHistory)
});
if (!response.ok) throw new Error('Failed to get response');
const data = await response.json();
hideTyping();
const aiResponse = data.response || 'Sorry, I could not generate a response.';
addMessage(aiResponse, false);
conversationHistory.ai.push(aiResponse);
} catch (error) {
hideTyping();
addMessage('Sorry, something went wrong. Please try again.', false);
conversationHistory.user.pop(); // Remove last user message on error
} finally {
sendBtn.disabled = false;
messageInput.disabled = false;
messageInput.focus();
}
}
sendBtn.addEventListener('click', sendMessage);
messageInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') sendMessage();
});
clearBtn.addEventListener('click', () => {
messagesDiv.innerHTML = '<div class="empty-state"><h3>πŸ‘‹ Hello!</h3><p>Start a conversation by typing a message below</p></div>';
conversationHistory = { user: [], ai: [] };
messageInput.value = '';
messageInput.focus();
});
messageInput.focus();
</script>
</body>
</html>