Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- App.py +21 -0
- static/style.css +145 -0
- templates/index.html +162 -0
- templates/layout.html +23 -0
App.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
|
3 |
+
app = Flask(__name__)
|
4 |
+
|
5 |
+
# Store chat history
|
6 |
+
chat_history = []
|
7 |
+
|
8 |
+
@app.route('/')
|
9 |
+
def index():
|
10 |
+
return render_template('index.html', chat_history=chat_history)
|
11 |
+
|
12 |
+
@app.route('/send_message', methods=['POST'])
|
13 |
+
def send_message():
|
14 |
+
user_message = request.form['message']
|
15 |
+
# Here you can process the user message and generate a response
|
16 |
+
bot_response = f"Echo: {user_message}"
|
17 |
+
chat_history.append({'user': user_message, 'bot': bot_response})
|
18 |
+
return jsonify(chat_history=chat_history)
|
19 |
+
|
20 |
+
if __name__ == '__main__':
|
21 |
+
app.run(debug=True)
|
static/style.css
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* Reset some default browser styles */
|
2 |
+
* {
|
3 |
+
margin: 0;
|
4 |
+
padding: 0;
|
5 |
+
box-sizing: border-box;
|
6 |
+
}
|
7 |
+
|
8 |
+
body {
|
9 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
10 |
+
background-color: #f4f4f9;
|
11 |
+
display: flex;
|
12 |
+
justify-content: center;
|
13 |
+
align-items: center;
|
14 |
+
height: 100vh;
|
15 |
+
margin: 0;
|
16 |
+
overflow: hidden;
|
17 |
+
}
|
18 |
+
|
19 |
+
/* Container for the entire chat application */
|
20 |
+
.container {
|
21 |
+
display: flex;
|
22 |
+
width: 100%;
|
23 |
+
height: 100%;
|
24 |
+
background-color: #fff;
|
25 |
+
border-radius: 0;
|
26 |
+
box-shadow: none;
|
27 |
+
}
|
28 |
+
|
29 |
+
/* Sidebar for chat history */
|
30 |
+
.sidebar {
|
31 |
+
width: 30%;
|
32 |
+
padding: 20px;
|
33 |
+
border-right: 0px solid;
|
34 |
+
overflow-y: auto;
|
35 |
+
background-color:inherit;
|
36 |
+
height: 100%;
|
37 |
+
}
|
38 |
+
|
39 |
+
.sidebar h2 {
|
40 |
+
margin-bottom: 15px;
|
41 |
+
color: #444;
|
42 |
+
font-size: 20px;
|
43 |
+
}
|
44 |
+
|
45 |
+
.sidebar ul {
|
46 |
+
list-style: none;
|
47 |
+
}
|
48 |
+
|
49 |
+
.sidebar ul li {
|
50 |
+
margin-bottom: 10px;
|
51 |
+
padding: 10px;
|
52 |
+
background-color: #e9e9f0;
|
53 |
+
border-radius: 5px;
|
54 |
+
color: black;
|
55 |
+
}
|
56 |
+
|
57 |
+
/* Chatbox for displaying and sending messages */
|
58 |
+
.chatbox {
|
59 |
+
width: 70%;
|
60 |
+
padding: 20px;
|
61 |
+
display: flex;
|
62 |
+
flex-direction: column;
|
63 |
+
height: 100%;
|
64 |
+
}
|
65 |
+
|
66 |
+
.chatbox h2 {
|
67 |
+
margin-bottom: 15px;
|
68 |
+
color: #444;
|
69 |
+
font-size: 20px;
|
70 |
+
}
|
71 |
+
|
72 |
+
#messages {
|
73 |
+
flex-grow: 1;
|
74 |
+
overflow-y: auto;
|
75 |
+
margin-bottom: 20px;
|
76 |
+
}
|
77 |
+
|
78 |
+
#messages p {
|
79 |
+
margin-bottom: 10px;
|
80 |
+
padding: 10px;
|
81 |
+
border-radius: 5px;
|
82 |
+
color: black;
|
83 |
+
}
|
84 |
+
|
85 |
+
#messages p strong {
|
86 |
+
display: block;
|
87 |
+
margin-bottom: 5px;
|
88 |
+
|
89 |
+
}
|
90 |
+
|
91 |
+
#messages p.user-message {
|
92 |
+
background-color: #d4f4dd;
|
93 |
+
}
|
94 |
+
|
95 |
+
#messages p.bot-response {
|
96 |
+
background-color: #e0e0e0;
|
97 |
+
}
|
98 |
+
|
99 |
+
form {
|
100 |
+
display: flex;
|
101 |
+
align-items: center;
|
102 |
+
}
|
103 |
+
|
104 |
+
input[type="text"] {
|
105 |
+
flex-grow: 1;
|
106 |
+
padding: 10px;
|
107 |
+
border: 1px solid #ddd;
|
108 |
+
border-radius: 4px;
|
109 |
+
margin-right: 10px;
|
110 |
+
font-size: 16px;
|
111 |
+
}
|
112 |
+
|
113 |
+
button {
|
114 |
+
padding: 10px 20px;
|
115 |
+
border: none;
|
116 |
+
background-color: #007bff;
|
117 |
+
color: #fff;
|
118 |
+
border-radius: 4px;
|
119 |
+
cursor: pointer;
|
120 |
+
font-size: 16px;
|
121 |
+
transition: background-color 0.3s ease;
|
122 |
+
}
|
123 |
+
|
124 |
+
button:hover {
|
125 |
+
background-color: #0056b3;
|
126 |
+
}
|
127 |
+
|
128 |
+
/* Add a scrollbar style for better look */
|
129 |
+
.sidebar, #messages {
|
130 |
+
scrollbar-width: none;
|
131 |
+
scrollbar-color:inherit;
|
132 |
+
}
|
133 |
+
|
134 |
+
.sidebar::-webkit-scrollbar, #messages::-webkit-scrollbar {
|
135 |
+
width: 8px;
|
136 |
+
}
|
137 |
+
|
138 |
+
.sidebar::-webkit-scrollbar-track, #messages::-webkit-scrollbar-track {
|
139 |
+
background: #f7f7fa;
|
140 |
+
}
|
141 |
+
|
142 |
+
.sidebar::-webkit-scrollbar-thumb, #messages::-webkit-scrollbar-thumb {
|
143 |
+
background-color: #bbb;
|
144 |
+
border-radius: 10px;
|
145 |
+
}
|
templates/index.html
ADDED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{% extends 'layout.html' %} {% block content %}
|
2 |
+
<div class="container" id="app-ui">
|
3 |
+
<div class="sidebar">
|
4 |
+
<h2 style="color: inherit">Chat History</h2>
|
5 |
+
<ul id="chat-history">
|
6 |
+
{% for chat in chat_history %}
|
7 |
+
<li>User: {{ chat.user }}<br />Bot: {{ chat.bot }}</li>
|
8 |
+
{% endfor %}
|
9 |
+
</ul>
|
10 |
+
</div>
|
11 |
+
<div class="chatbox">
|
12 |
+
<h2 style="color: inherit">Chat</h2>
|
13 |
+
<div id="messages">
|
14 |
+
{% for chat in chat_history %}
|
15 |
+
<p class="user-message"><strong>User:</strong> {{ chat.user }}</p>
|
16 |
+
<p class="bot-response"><strong>Bot:</strong> {{ chat.bot }}</p>
|
17 |
+
{% endfor %}
|
18 |
+
</div>
|
19 |
+
<form id="chat-form">
|
20 |
+
<input
|
21 |
+
type="text"
|
22 |
+
id="message"
|
23 |
+
name="message"
|
24 |
+
placeholder="Type your message..."
|
25 |
+
/>
|
26 |
+
<button type="submit">Send</button>
|
27 |
+
</form>
|
28 |
+
</div>
|
29 |
+
|
30 |
+
<!--Theme button-->
|
31 |
+
<div
|
32 |
+
class="dropdown position-fixed top-0 end-0 mb-3 me-3 bd-mode-toggle"
|
33 |
+
style="margin-top: 10px; margin-right: 10px"
|
34 |
+
>
|
35 |
+
<button
|
36 |
+
class="btn py-2 d-flex align-items-center"
|
37 |
+
id="bd-theme"
|
38 |
+
type="button"
|
39 |
+
aria-expanded="false"
|
40 |
+
data-bs-toggle="dropdown"
|
41 |
+
aria-label="Toggle theme (auto)"
|
42 |
+
style="color: inherit"
|
43 |
+
>
|
44 |
+
<!--dropdown-toggle-->
|
45 |
+
Theme
|
46 |
+
<span class="visually-hidden" id="bd-theme-text">Toggle theme</span>
|
47 |
+
</button>
|
48 |
+
<ul
|
49 |
+
class="dropdown-menu dropdown-menu-end shadow"
|
50 |
+
aria-labelledby="bd-theme-text"
|
51 |
+
>
|
52 |
+
<li>
|
53 |
+
<button
|
54 |
+
type="button"
|
55 |
+
class="dropdown-item d-flex align-items-center"
|
56 |
+
data-bs-theme-value="light"
|
57 |
+
aria-pressed="false"
|
58 |
+
id="lt"
|
59 |
+
>
|
60 |
+
Light
|
61 |
+
</button>
|
62 |
+
</li>
|
63 |
+
<li>
|
64 |
+
<button
|
65 |
+
type="button"
|
66 |
+
class="dropdown-item d-flex align-items-center"
|
67 |
+
data-bs-theme-value="dark"
|
68 |
+
aria-pressed="false"
|
69 |
+
id="dt"
|
70 |
+
>
|
71 |
+
Dark
|
72 |
+
</button>
|
73 |
+
</li>
|
74 |
+
</ul>
|
75 |
+
</div>
|
76 |
+
</div>
|
77 |
+
|
78 |
+
<script>
|
79 |
+
document
|
80 |
+
.getElementById("chat-form")
|
81 |
+
.addEventListener("submit", function (event) {
|
82 |
+
event.preventDefault();
|
83 |
+
const message = document.getElementById("message").value;
|
84 |
+
if (message.trim() == "") {
|
85 |
+
alert(
|
86 |
+
"Prompt cannot be empty. Please enter a prompt to generate a response."
|
87 |
+
);
|
88 |
+
} else {
|
89 |
+
fetch("/send_message", {
|
90 |
+
method: "POST",
|
91 |
+
headers: {
|
92 |
+
"Content-Type": "application/x-www-form-urlencoded",
|
93 |
+
},
|
94 |
+
body: `message=${message}`,
|
95 |
+
})
|
96 |
+
.then((response) => response.json())
|
97 |
+
.then((data) => {
|
98 |
+
const chatHistory = document.getElementById("chat-history");
|
99 |
+
chatHistory.innerHTML = "";
|
100 |
+
data.chat_history.forEach((chat) => {
|
101 |
+
const listItem = document.createElement("li");
|
102 |
+
listItem.innerHTML = `User: ${chat.user}<br>Bot: ${chat.bot}`;
|
103 |
+
chatHistory.appendChild(listItem);
|
104 |
+
});
|
105 |
+
|
106 |
+
const messagesDiv = document.getElementById("messages");
|
107 |
+
messagesDiv.innerHTML = "";
|
108 |
+
data.chat_history.forEach((chat) => {
|
109 |
+
const userMessage = document.createElement("p");
|
110 |
+
userMessage.classList.add("user-message");
|
111 |
+
userMessage.innerHTML = `<strong>User:</strong> ${chat.user}`;
|
112 |
+
const botResponse = document.createElement("p");
|
113 |
+
botResponse.classList.add("bot-response");
|
114 |
+
botResponse.innerHTML = `<strong>Bot:</strong> ${chat.bot}`;
|
115 |
+
messagesDiv.appendChild(userMessage);
|
116 |
+
messagesDiv.appendChild(botResponse);
|
117 |
+
});
|
118 |
+
|
119 |
+
document.getElementById("message").value = "";
|
120 |
+
});
|
121 |
+
}
|
122 |
+
});
|
123 |
+
|
124 |
+
function SetDarkTheme() {
|
125 |
+
var body = document.body;
|
126 |
+
body.classList.remove("text-bg-light");
|
127 |
+
body.classList.add("text-bg-dark");
|
128 |
+
var ui = document.getElementById("app-ui");
|
129 |
+
ui.classList.remove("text-bg-light");
|
130 |
+
ui.classList.add("text-bg-dark");
|
131 |
+
// Store the theme preference in Local Storage
|
132 |
+
localStorage.setItem("theme", "dark");
|
133 |
+
}
|
134 |
+
|
135 |
+
function SetLightTheme() {
|
136 |
+
var body = document.body;
|
137 |
+
body.classList.add("text-bg-light");
|
138 |
+
body.classList.remove("text-bg-dark");
|
139 |
+
var ui = document.getElementById("app-ui");
|
140 |
+
ui.classList.add("text-bg-light");
|
141 |
+
ui.classList.remove("text-bg-dark");
|
142 |
+
// Store the theme preference in Local Storage
|
143 |
+
localStorage.setItem("theme", "light");
|
144 |
+
}
|
145 |
+
|
146 |
+
function applyThemePreference() {
|
147 |
+
var themePreference = localStorage.getItem("theme");
|
148 |
+
if (themePreference === "light") {
|
149 |
+
SetLightTheme();
|
150 |
+
} else {
|
151 |
+
SetDarkTheme();
|
152 |
+
}
|
153 |
+
}
|
154 |
+
|
155 |
+
// Add event listeners to the buttons
|
156 |
+
document.getElementById("lt").addEventListener("click", SetLightTheme);
|
157 |
+
document.getElementById("dt").addEventListener("click", SetDarkTheme);
|
158 |
+
|
159 |
+
// Call applyThemePreference on page load
|
160 |
+
applyThemePreference();
|
161 |
+
</script>
|
162 |
+
{% endblock %}
|
templates/layout.html
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>ChatGPT Clone</title>
|
7 |
+
<link rel="stylesheet" href="../static/style.css" />
|
8 |
+
<link
|
9 |
+
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
|
10 |
+
rel="stylesheet"
|
11 |
+
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
|
12 |
+
crossorigin="anonymous"
|
13 |
+
/>
|
14 |
+
</head>
|
15 |
+
<body class="text-bg-light">
|
16 |
+
{% block content %}{% endblock %}
|
17 |
+
<script
|
18 |
+
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
|
19 |
+
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
|
20 |
+
crossorigin="anonymous"
|
21 |
+
></script>
|
22 |
+
</body>
|
23 |
+
</html>
|