forked from thegeekygamechanger/Ollama-based-chat-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
284 lines (246 loc) Β· 13 KB
/
ui.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import streamlit as st
import pandas as pd
from datetime import datetime
import os
from authenticator import Authenticator
from file_operations import save_text_file
from retriever_setup import initialize_retriever, format_documents
from langchain.chat_models import ChatOllama
from langchain.prompts import ChatPromptTemplate
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from config import CHAT_SESSION_FOLDER, FEEDBACK_FOLDER, VECTORSTORE_PATH, MODEL_PATH, ENCODER_MODEL_PATH, LOGO_PATH
# Define ChatManager class
class ChatManager:
@staticmethod
def load_user_history(username):
filepath = os.path.join(CHAT_SESSION_FOLDER, f"{username}_history.csv")
if os.path.exists(filepath):
return pd.read_csv(filepath)
else:
return pd.DataFrame(columns=["timestamp", "question", "answer"])
@staticmethod
def save_user_history(username, new_data):
filepath = os.path.join(CHAT_SESSION_FOLDER, f"{username}_history.csv")
existing_data = pd.DataFrame()
if os.path.exists(filepath):
existing_data = pd.read_csv(filepath)
combined_data = pd.concat([existing_data, new_data], ignore_index=True)
combined_data.to_csv(filepath, index=False)
@staticmethod
def clear_user_main_chat():
st.session_state.main_chat = pd.DataFrame(columns=["timestamp", "question", "answer"])
@staticmethod
def clear_user_history(username):
filepath = os.path.join(CHAT_SESSION_FOLDER, f"{username}_history.csv")
if os.path.exists(filepath):
os.remove(filepath)
st.session_state.user_history = pd.DataFrame(columns=["timestamp", "question", "answer"])
st.session_state.main_chat = pd.DataFrame(columns=["timestamp", "question", "answer"])
@staticmethod
def clear_all_chat(username):
filepath = os.path.join(CHAT_SESSION_FOLDER, f"{username}_history.csv")
if os.path.exists(filepath):
os.remove(filepath)
# Clear in session state
st.session_state.user_history = pd.DataFrame(columns=["timestamp", "question", "answer"])
st.session_state.main_chat = pd.DataFrame(columns=["timestamp", "question", "answer"])
# Function to setup RAG chain
def setup_rag_chain(_retriever):
prompt_template = """
<s>[INST] You are a C-Bot Assitant developed by Harsh Kumar at C-DAC Pune.
{context}
You are a respectful and honest C-BOT assistant. Answer the user's questions using only the context provided to you. Answer coding-related questions with code and explanation. You should not start giving code to yourself, if someone ask to write any code, avoid giving code. Do not start the response with salutations, answer directly. Do not start generating random code from context provided to you. If any user ask you "who" starting question, answer them Harsh Kumar Developed you.
{question} [/INST] </s>
"""
prompt = ChatPromptTemplate.from_template(prompt_template)
llm = ChatOllama(model="llama3", verbose=True, callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]), temperature=0)
rag_chain_from_docs = (
RunnablePassthrough.assign(context=(lambda x: format_documents(x["context"])))
| prompt
| llm
| StrOutputParser()
)
return RunnableParallel({"context": _retriever, "question": RunnablePassthrough()}).assign(answer=rag_chain_from_docs)
# Function to backup chat history
def backup_chat_history(username):
backup_folder = os.path.join(FEEDBACK_FOLDER, "backup")
os.makedirs(backup_folder, exist_ok=True)
backup_filename = os.path.join(backup_folder, f"{username}_chat_backup_{datetime.now().strftime('%Y%m%d%H%M%S')}.csv")
st.session_state.user_history.to_csv(backup_filename, index=False)
# Function for main content
# Function for main content
def main_content(username):
st.sidebar.image(LOGO_PATH, width=100) # Replace with your image URL and width
st.sidebar.markdown(f'<h1 style="color: Navy;">Welcome, {username}</h1>', unsafe_allow_html=True)
page = st.sidebar.radio("", ["Home", "Feedback", "About", "Logout"])
# Initialize user history and main chat data if not already initialized
if "user_history" not in st.session_state or st.session_state.get("current_username") != username:
st.session_state.user_history = ChatManager.load_user_history(username)
st.session_state.current_username = username
if "main_chat" not in st.session_state:
st.session_state.main_chat = pd.DataFrame(columns=["timestamp", "question", "answer"])
# Handle different pages in the sidebar
if page == "Home":
# Initialize retriever and RAG chain if not already initialized
if "retriever" not in st.session_state:
st.session_state.retriever = initialize_retriever(VECTORSTORE_PATH, MODEL_PATH, ENCODER_MODEL_PATH)
_retriever = st.session_state.retriever
if "rag_chain_with_source" not in st.session_state:
st.session_state.rag_chain_with_source = setup_rag_chain(_retriever)
rag_chain_with_source = st.session_state.rag_chain_with_source
# User input section
with st.form(key='user_input_form', clear_on_submit=True):
user_question = st.text_input("Ask your question:", key='user_question_input', placeholder="Write Prompt")
submit_button_clicked = st.form_submit_button("Submit")
if submit_button_clicked:
output = {}
if user_question:
output_placeholder = st.empty()
for chunk in rag_chain_with_source.stream(user_question):
for key, value in chunk.items():
if key not in output:
output[key] = value
else:
output[key] += value
if output.get('answer'):
with output_placeholder.container():
st.markdown(
f'<div style="border: 2px solid blue; padding: 10px; border-radius: 5px; background-color: #e0f7fa;">'
f'<strong>π Question:</strong> {user_question}<br>'
f'<strong>πΎ Answer:</strong> {output["answer"]}'
f'</div>', unsafe_allow_html=True
)
st.session_state.last_response = output['answer']
if output:
new_data = pd.DataFrame([{"timestamp": datetime.now(), "question": user_question, "answer": output["answer"]}])
st.session_state.user_history = pd.concat([st.session_state.user_history, new_data], ignore_index=True)
ChatManager.save_user_history(username, new_data)
# Update the main chat in the session state
st.session_state.main_chat = pd.concat([st.session_state.main_chat, new_data], ignore_index=True)
# Main chat area
chat_container = st.container()
for _, row in st.session_state.main_chat.iterrows():
with chat_container:
st.write(f"**[{row['timestamp']}] π Question:** {row['question']}")
st.markdown(f"**πΎ Answer:** {row['answer']}")
st.divider()
# Sidebar: View history
with st.sidebar.expander("Chat History", expanded=False):
sidebar_search_query = st.text_input(
"Search history",
value=st.session_state.get("sidebar_search_history_query", ""),
placeholder="Start digging into it..."
)
filtered_history = st.session_state.user_history[
st.session_state.user_history.apply(lambda row: sidebar_search_query.lower() in row.to_string().lower(), axis=1)
] if sidebar_search_query else st.session_state.user_history
if st.button("Clear All Chat", key="clear_all_chat_button_sidebar"):
ChatManager.clear_all_chat(username)
# Clear the chats immediately without reloading the page
st.session_state.main_chat = pd.DataFrame(columns=["timestamp", "question", "answer"])
st.session_state.user_history = pd.DataFrame(columns=["timestamp", "question", "answer"])
st.experimental_rerun()
for _, row in filtered_history.iterrows():
st.write(f"**[{row['timestamp']}]**")
st.write(f"**Question:** {row['question']}")
st.write(f"**πΎ Answer:** {row['answer']}")
elif page == "Feedback":
feedback_page(username)
elif page == "About":
st.image(LOGO_PATH, width=100, caption='')
st.write("ApnaGPT is a powerful tool for answering questions based on the context provided, it is developed by Harsh Kumar .")
elif page == "Logout":
del st.session_state.username
st.session_state.main_chat = pd.DataFrame(columns=["timestamp", "question", "answer"])
st.session_state.user_question = ""
st.session_state.last_response = ""
# Clear main chat on logout
st.experimental_rerun()
# Function for feedback page
def feedback_page(username):
st.markdown(
'<div style="animation: pulse 2s infinite; text-align: center;">'
'<h3 style="color: #3498DB;">We value your feedback!</h3>'
'</div>',
unsafe_allow_html=True
)
feedback = st.text_area("Please provide your feedback here:", height=200, placeholder="Write here...")
emoji = st.radio(
"How do you feel about our service?",
("π", "π", "π"),
index=1,
horizontal=True,
help="π - Happy, π - Neutral, π - Unhappy"
)
submit_feedback = st.button("Submit Feedback")
if submit_feedback and feedback.strip():
save_text_file(os.path.join(FEEDBACK_FOLDER, f"{username}_{datetime.now().strftime('%Y%m%d%H%M%S')}.txt"), feedback)
st.success("Thank you for your feedback!")
# Function for landing page
def landing_page():
st.markdown("""
<style>
.main-header { /* Main header styling */
font-size: 2.5rem;
text-align: center;
color: #2E86C1;
margin-top: 20px;
animation: fadeIn 2s ease-in-out;
}
.sub-header { /* Sub-header styling */
font-size: 1.25rem;
text-align: center;
color: #34495E;
margin: 10px 0;
opacity: 0;
animation: fadeIn 2s ease-in-out forwards;
animation-delay: 1s;
}
.button-center { /* Button container styling */
display: flex;
justify-content: center;
margin-top: 20px;
}
.button { /* Button styling */
font-size: 1rem;
padding: 10px 30px;
color: white;
background: linear-gradient(135deg, #2E86C1, #1B4F72);
border: none;
border-radius: 25px;
cursor: pointer;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: background-color 0.3s, transform 0.3s, box-shadow 0.3s;
text-align: center;
animation: pulse 2s infinite;
}
.button:hover { /* Button hover effect */
background: linear-gradient(135deg, #1B4F72, #2E86C1);
transform: scale(1.05);
box-shadow: 0 6px 8px rgba(0, 0, 0, 0.2);
}
@keyframes pulse { /* Animation for button pulsing effect */
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}
@keyframes fadeIn { /* Animation for text fade-in */
from { opacity: 0; }
to { opacity: 1; }
}
</style>
<div class="main-header">Welcome to CDAC Mitra</div>
<div class="button-center">
<button class="button" onclick="window.location.href='/main'"><p>π Create User login<br><br>
π Please frame specific questions similar to the provided examples<br><br>
π If the response doesn't match your query, consider rephrasing the questions</button><br><br>
β¨It is Gen AI Powered Query Enhancement Systemβ¨<br><br>
β οΈ Disclaimer: This bot does not have memory elements, so it will not be able to connect with previous questions !<br><br>π All the Best, waiting to hear from YOU ALL π«΅</p>
</div>
""", unsafe_allow_html=True)
# Run the app
if __name__ == "__main__":
landing_page()