added templates and index view
Browse files- Dockerfile +1 -1
- app.py +7 -3
- static/script.js +50 -0
- static/styles.css +129 -0
- templates/chat.html +31 -0
- templates/index.html +13 -0
Dockerfile
CHANGED
@@ -13,4 +13,4 @@ COPY --chown=user ./requirements.txt requirements.txt
|
|
13 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
14 |
|
15 |
COPY --chown=user . /app
|
16 |
-
CMD ["gunicorn", "-b", "0.0.0.0:7860" , "
|
|
|
13 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
14 |
|
15 |
COPY --chown=user . /app
|
16 |
+
CMD ["gunicorn", "-b", "0.0.0.0:7860" , "app:app"]
|
app.py
CHANGED
@@ -22,7 +22,7 @@ from langchain_community.retrievers import PineconeHybridSearchRetriever
|
|
22 |
|
23 |
from langchain_groq import ChatGroq
|
24 |
|
25 |
-
from flask import Flask, request
|
26 |
from flask_cors import CORS
|
27 |
from flask_limiter import Limiter
|
28 |
from flask_limiter.util import get_remote_address
|
@@ -143,7 +143,7 @@ def handle_message(data):
|
|
143 |
chain = conversational_rag_chain.pick("answer")
|
144 |
|
145 |
try:
|
146 |
-
for chunk in
|
147 |
{"input": question},
|
148 |
config={
|
149 |
"configurable": {"session_id": "abc123"}
|
@@ -151,7 +151,7 @@ def handle_message(data):
|
|
151 |
):
|
152 |
emit('response', chunk, room=request.sid)
|
153 |
except:
|
154 |
-
for chunk in
|
155 |
{"input": question},
|
156 |
config={
|
157 |
"configurable": {"session_id": "abc123"}
|
@@ -159,5 +159,9 @@ def handle_message(data):
|
|
159 |
):
|
160 |
emit('response', chunk, room=request.sid)
|
161 |
|
|
|
|
|
|
|
|
|
162 |
if __name__ == '__main__':
|
163 |
socketio.run(app, debug=True)
|
|
|
22 |
|
23 |
from langchain_groq import ChatGroq
|
24 |
|
25 |
+
from flask import Flask, request, render_template
|
26 |
from flask_cors import CORS
|
27 |
from flask_limiter import Limiter
|
28 |
from flask_limiter.util import get_remote_address
|
|
|
143 |
chain = conversational_rag_chain.pick("answer")
|
144 |
|
145 |
try:
|
146 |
+
for chunk in chain.stream(
|
147 |
{"input": question},
|
148 |
config={
|
149 |
"configurable": {"session_id": "abc123"}
|
|
|
151 |
):
|
152 |
emit('response', chunk, room=request.sid)
|
153 |
except:
|
154 |
+
for chunk in chain.stream(
|
155 |
{"input": question},
|
156 |
config={
|
157 |
"configurable": {"session_id": "abc123"}
|
|
|
159 |
):
|
160 |
emit('response', chunk, room=request.sid)
|
161 |
|
162 |
+
app.route("/")
|
163 |
+
def index_view():
|
164 |
+
render_template('chat.html')
|
165 |
+
|
166 |
if __name__ == '__main__':
|
167 |
socketio.run(app, debug=True)
|
static/script.js
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const socket = io.connect('http://' + document.baseURI + ':' + location.port);
|
2 |
+
|
3 |
+
const chatBox = document.getElementById('chat-box');
|
4 |
+
const chatInput = document.getElementById('chat-input');
|
5 |
+
const sendButton = document.getElementById('send-button');
|
6 |
+
var converter = new showdown.Converter();
|
7 |
+
var response="";
|
8 |
+
|
9 |
+
function appendMessage(message, sender) {
|
10 |
+
last_message_ele = chatBox.children[chatBox.children.length - 2];
|
11 |
+
response += message;
|
12 |
+
message = converter.makeHtml(response);
|
13 |
+
if(last_message_ele && last_message_ele.classList.contains('bot')){
|
14 |
+
last_message_ele.lastChild.innerHTML = message;
|
15 |
+
}else{
|
16 |
+
if(sender == 'bot'){
|
17 |
+
document.getElementById("loader").classList.add('hidden');
|
18 |
+
}
|
19 |
+
const messageElement = document.createElement('div');
|
20 |
+
messageElement.classList.add('chat-message', sender);
|
21 |
+
messageElement.innerHTML = `<span>${message}</span>`;
|
22 |
+
if(sender == 'bot'){
|
23 |
+
chatBox.append(messageElement);
|
24 |
+
}else{
|
25 |
+
chatBox.prepend(messageElement);
|
26 |
+
}
|
27 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
28 |
+
}
|
29 |
+
}
|
30 |
+
|
31 |
+
sendButton.addEventListener('click', () => {
|
32 |
+
const message = chatInput.value.trim();
|
33 |
+
if (message) {
|
34 |
+
appendMessage(message, 'user');
|
35 |
+
document.getElementById("loader").classList.remove('hidden');
|
36 |
+
socket.emit('message', { question: message, session_id: 'abc123' });
|
37 |
+
chatInput.value = '';
|
38 |
+
response = "";
|
39 |
+
}
|
40 |
+
});
|
41 |
+
|
42 |
+
chatInput.addEventListener('keypress', (e) => {
|
43 |
+
if (e.key === 'Enter') {
|
44 |
+
sendButton.click();
|
45 |
+
}
|
46 |
+
});
|
47 |
+
|
48 |
+
socket.on('response', (response) => {
|
49 |
+
appendMessage(response, 'bot');
|
50 |
+
});
|
static/styles.css
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
body {
|
2 |
+
font-family: Arial, sans-serif;
|
3 |
+
background-color: #f4f4f4;
|
4 |
+
margin: 0;
|
5 |
+
padding: 0;
|
6 |
+
display: flex;
|
7 |
+
justify-content: center;
|
8 |
+
align-items: center;
|
9 |
+
height: 100vh;
|
10 |
+
overflow: scroll;
|
11 |
+
}
|
12 |
+
|
13 |
+
.chat-container {
|
14 |
+
max-width: 800px;
|
15 |
+
max-height: 1200px;
|
16 |
+
width: 100%;
|
17 |
+
height: 70vh;
|
18 |
+
background-color: #fff;
|
19 |
+
border-radius: 8px;
|
20 |
+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
21 |
+
display: flex;
|
22 |
+
flex-direction: column;
|
23 |
+
overflow: hidden;
|
24 |
+
}
|
25 |
+
|
26 |
+
.chat-box {
|
27 |
+
flex: 1;
|
28 |
+
padding: 20px;
|
29 |
+
overflow-y: auto;
|
30 |
+
border-bottom: 1px solid #ddd;
|
31 |
+
}
|
32 |
+
|
33 |
+
.chat-message {
|
34 |
+
margin-bottom: 15px;
|
35 |
+
}
|
36 |
+
|
37 |
+
.chat-message.user {
|
38 |
+
text-align: right;
|
39 |
+
}
|
40 |
+
|
41 |
+
.chat-message.user span {
|
42 |
+
background-color: #007bff;
|
43 |
+
color: #fff;
|
44 |
+
padding: 10px;
|
45 |
+
border-radius: 8px;
|
46 |
+
display: inline-block;
|
47 |
+
max-width: 70%;
|
48 |
+
}
|
49 |
+
|
50 |
+
.chat-message.bot span {
|
51 |
+
background-color: #e0e0e0;
|
52 |
+
color: #333;
|
53 |
+
padding: 10px;
|
54 |
+
border-radius: 8px;
|
55 |
+
display: inline-block;
|
56 |
+
max-width: 70%;
|
57 |
+
}
|
58 |
+
|
59 |
+
.input-container {
|
60 |
+
display: flex;
|
61 |
+
padding: 15px;
|
62 |
+
background-color: #f4f4f4;
|
63 |
+
}
|
64 |
+
|
65 |
+
#chat-input {
|
66 |
+
flex: 1;
|
67 |
+
padding: 10px;
|
68 |
+
border: 1px solid #ddd;
|
69 |
+
border-radius: 4px;
|
70 |
+
font-size: 16px;
|
71 |
+
}
|
72 |
+
|
73 |
+
#send-button {
|
74 |
+
padding: 10px 15px;
|
75 |
+
background-color: #007bff;
|
76 |
+
color: #fff;
|
77 |
+
border: none;
|
78 |
+
border-radius: 4px;
|
79 |
+
cursor: pointer;
|
80 |
+
margin-left: 10px;
|
81 |
+
font-size: 16px;
|
82 |
+
}
|
83 |
+
|
84 |
+
#send-button:hover {
|
85 |
+
background-color: #0056b3;
|
86 |
+
}
|
87 |
+
|
88 |
+
/* Dot Loader styles */
|
89 |
+
.dot-loader {
|
90 |
+
background-color: #e0e0e0;
|
91 |
+
color: #333;
|
92 |
+
padding: 10px;
|
93 |
+
border-radius: 8px;
|
94 |
+
width: fit-content;
|
95 |
+
display: flex;
|
96 |
+
justify-content: center;
|
97 |
+
align-items: center;
|
98 |
+
margin-top: 20px;
|
99 |
+
}
|
100 |
+
|
101 |
+
.dot-loader div {
|
102 |
+
width: 5px;
|
103 |
+
height: 5px;
|
104 |
+
margin: 0 5px;
|
105 |
+
background-color: #8a8a8a;
|
106 |
+
border-radius: 50%;
|
107 |
+
animation: dot 1.2s infinite ease-in-out;
|
108 |
+
}
|
109 |
+
|
110 |
+
.dot-loader div:nth-child(1) {
|
111 |
+
animation-delay: -0.32s;
|
112 |
+
}
|
113 |
+
|
114 |
+
.dot-loader div:nth-child(2) {
|
115 |
+
animation-delay: -0.16s;
|
116 |
+
}
|
117 |
+
|
118 |
+
@keyframes dot {
|
119 |
+
0%, 80%, 100% {
|
120 |
+
transform: scale(0);
|
121 |
+
}
|
122 |
+
40% {
|
123 |
+
transform: scale(1);
|
124 |
+
}
|
125 |
+
}
|
126 |
+
|
127 |
+
.hidden {
|
128 |
+
display: none;
|
129 |
+
}
|
templates/chat.html
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>Chat with LLM</title>
|
7 |
+
<link rel="stylesheet" href="../static/styles.css">
|
8 |
+
</head>
|
9 |
+
<body>
|
10 |
+
<div class="chat-container">
|
11 |
+
<div class="chat-box" id="chat-box">
|
12 |
+
|
13 |
+
|
14 |
+
<!-- Dot Loader element -->
|
15 |
+
<div id="loader" class="dot-loader hidden">
|
16 |
+
<div></div>
|
17 |
+
<div></div>
|
18 |
+
<div></div>
|
19 |
+
</div>
|
20 |
+
</div>
|
21 |
+
<div class="input-container">
|
22 |
+
<input type="text" id="chat-input" placeholder="Type your message here...">
|
23 |
+
<button id="send-button">Send</button>
|
24 |
+
</div>
|
25 |
+
</div>
|
26 |
+
<script src="https://cdn.socket.io/4.5.0/socket.io.min.js"></script>
|
27 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/2.1.0/showdown.min.js" integrity="sha512-LhccdVNGe2QMEfI3x4DVV3ckMRe36TfydKss6mJpdHjNFiV07dFpS2xzeZedptKZrwxfICJpez09iNioiSZ3hA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
28 |
+
|
29 |
+
<script src="../static/script.js"></script>
|
30 |
+
</body>
|
31 |
+
</html>
|
templates/index.html
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!-- templates/home.html -->
|
2 |
+
<!DOCTYPE html>
|
3 |
+
<html lang="en">
|
4 |
+
<head>
|
5 |
+
<meta charset="UTF-8">
|
6 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
7 |
+
<title>Home</title>
|
8 |
+
</head>
|
9 |
+
<body>
|
10 |
+
<h1>Welcome to the Flask Chat App</h1>
|
11 |
+
<p><a href="/chat">Go to Chat</a></p>
|
12 |
+
</body>
|
13 |
+
</html>
|