Spaces:
Sleeping
Sleeping
Commit
·
39b72b0
1
Parent(s):
8c58248
feat: Add chatbot UI for chatbot application
Browse files- app.py +17 -4
- static/script.js +55 -0
- static/style.css +72 -0
- templates/index.html +19 -0
app.py
CHANGED
@@ -1,8 +1,21 @@
|
|
1 |
-
from fastapi import FastAPI
|
|
|
|
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
|
|
|
|
5 |
|
6 |
-
@app.get("/")
|
7 |
-
def
|
8 |
-
return {"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
2 |
+
from fastapi.responses import HTMLResponse
|
3 |
+
from fastapi.staticfiles import StaticFiles
|
4 |
+
from fastapi.templating import Jinja2Templates
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
9 |
+
templates = Jinja2Templates(directory="templates")
|
10 |
|
11 |
+
@app.get("/", response_class=HTMLResponse)
|
12 |
+
async def get_chat_interface(request: Request):
|
13 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
14 |
+
|
15 |
+
@app.post("/chat")
|
16 |
+
async def chat(request: Request):
|
17 |
+
data = await request.json()
|
18 |
+
user_message = data.get("message")
|
19 |
+
# Simple echo bot logic - replace with actual chatbot logic later
|
20 |
+
bot_response = f"You said: {user_message}"
|
21 |
+
return {"response": bot_response}
|
static/script.js
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
document.addEventListener('DOMContentLoaded', () => {
|
2 |
+
const messageInput = document.getElementById('message-input');
|
3 |
+
const sendButton = document.getElementById('send-button');
|
4 |
+
const chatMessages = document.getElementById('chat-messages');
|
5 |
+
|
6 |
+
sendButton.addEventListener('click', sendMessage);
|
7 |
+
|
8 |
+
messageInput.addEventListener('keypress', function(event) {
|
9 |
+
if (event.key === 'Enter') {
|
10 |
+
sendMessage();
|
11 |
+
}
|
12 |
+
});
|
13 |
+
|
14 |
+
async function sendMessage() {
|
15 |
+
const messageText = messageInput.value.trim();
|
16 |
+
if (!messageText) return;
|
17 |
+
|
18 |
+
// Display user message
|
19 |
+
displayMessage(messageText, 'user');
|
20 |
+
messageInput.value = '';
|
21 |
+
|
22 |
+
// Send message to backend and get response
|
23 |
+
try {
|
24 |
+
const response = await fetch('/chat', {
|
25 |
+
method: 'POST',
|
26 |
+
headers: {
|
27 |
+
'Content-Type': 'application/json'
|
28 |
+
},
|
29 |
+
body: JSON.stringify({ message: messageText })
|
30 |
+
});
|
31 |
+
|
32 |
+
if (!response.ok) {
|
33 |
+
throw new Error(`HTTP error! status: ${response.status}`);
|
34 |
+
}
|
35 |
+
|
36 |
+
const data = await response.json();
|
37 |
+
displayMessage(data.response, 'bot'); // Display bot response
|
38 |
+
|
39 |
+
} catch (error) {
|
40 |
+
console.error('Error sending message:', error);
|
41 |
+
displayMessage('Error sending message.', 'bot'); // Display error message
|
42 |
+
}
|
43 |
+
}
|
44 |
+
|
45 |
+
function displayMessage(message, sender) {
|
46 |
+
const messageDiv = document.createElement('div');
|
47 |
+
messageDiv.classList.add('message');
|
48 |
+
if (sender === 'bot') {
|
49 |
+
messageDiv.classList.add('bot');
|
50 |
+
}
|
51 |
+
messageDiv.textContent = message;
|
52 |
+
chatMessages.appendChild(messageDiv);
|
53 |
+
chatMessages.scrollTop = chatMessages.scrollHeight; // Scroll to bottom
|
54 |
+
}
|
55 |
+
});
|
static/style.css
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
body {
|
2 |
+
font-family: sans-serif;
|
3 |
+
background-color: #f4f7f6;
|
4 |
+
margin: 0;
|
5 |
+
display: flex;
|
6 |
+
justify-content: center;
|
7 |
+
align-items: center;
|
8 |
+
min-height: 100vh;
|
9 |
+
}
|
10 |
+
|
11 |
+
.chat-container {
|
12 |
+
width: 400px;
|
13 |
+
height: 600px;
|
14 |
+
background-color: #fff;
|
15 |
+
border-radius: 10px;
|
16 |
+
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
|
17 |
+
display: flex;
|
18 |
+
flex-direction: column;
|
19 |
+
overflow: hidden;
|
20 |
+
}
|
21 |
+
|
22 |
+
.chat-messages {
|
23 |
+
flex-grow: 1;
|
24 |
+
padding: 20px;
|
25 |
+
overflow-y: auto;
|
26 |
+
display: flex;
|
27 |
+
flex-direction: column;
|
28 |
+
}
|
29 |
+
|
30 |
+
.message {
|
31 |
+
background-color: #e0e0e0;
|
32 |
+
color: #333;
|
33 |
+
padding: 10px 15px;
|
34 |
+
border-radius: 20px;
|
35 |
+
margin-bottom: 10px;
|
36 |
+
align-self: flex-start; /* Default to left align for user messages */
|
37 |
+
max-width: 80%;
|
38 |
+
word-wrap: break-word;
|
39 |
+
}
|
40 |
+
|
41 |
+
.message.bot {
|
42 |
+
background-color: #007bff;
|
43 |
+
color: white;
|
44 |
+
align-self: flex-end; /* Right align for bot messages */
|
45 |
+
}
|
46 |
+
|
47 |
+
.input-area {
|
48 |
+
padding: 20px;
|
49 |
+
border-top: 1px solid #e0e0e0;
|
50 |
+
display: flex;
|
51 |
+
}
|
52 |
+
|
53 |
+
#message-input {
|
54 |
+
flex-grow: 1;
|
55 |
+
padding: 10px;
|
56 |
+
border-radius: 20px;
|
57 |
+
border: 1px solid #ccc;
|
58 |
+
margin-right: 10px;
|
59 |
+
}
|
60 |
+
|
61 |
+
#send-button {
|
62 |
+
background-color: #007bff;
|
63 |
+
color: white;
|
64 |
+
border: none;
|
65 |
+
padding: 10px 20px;
|
66 |
+
border-radius: 20px;
|
67 |
+
cursor: pointer;
|
68 |
+
}
|
69 |
+
|
70 |
+
#send-button:hover {
|
71 |
+
background-color: #0056b3;
|
72 |
+
}
|
templates/index.html
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html>
|
3 |
+
<head>
|
4 |
+
<title>Modern Chatbot</title>
|
5 |
+
<link href="/static/style.css" rel="stylesheet">
|
6 |
+
</head>
|
7 |
+
<body>
|
8 |
+
<div class="chat-container">
|
9 |
+
<div id="chat-messages" class="chat-messages">
|
10 |
+
<!-- Messages will be displayed here -->
|
11 |
+
</div>
|
12 |
+
<div class="input-area">
|
13 |
+
<input type="text" id="message-input" placeholder="Type your message...">
|
14 |
+
<button id="send-button">Send</button>
|
15 |
+
</div>
|
16 |
+
</div>
|
17 |
+
<script src="/static/script.js"></script>
|
18 |
+
</body>
|
19 |
+
</html>
|