Sharal commited on
Commit
a3741a5
·
verified ·
1 Parent(s): 30b2990

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -5
app.py CHANGED
@@ -103,8 +103,7 @@ def initialize_LLM(llm_option, vector_db):
103
  def format_chat_history(chat_history):
104
  formatted_chat_history = []
105
  for user_message, bot_message in chat_history:
106
- formatted_chat_history.append(f"User: {user_message}")
107
- formatted_chat_history.append(f"Assistant: {bot_message}")
108
  return formatted_chat_history
109
 
110
  def conversation(qa_chain, message, history):
@@ -127,6 +126,11 @@ def conversation(qa_chain, message, history):
127
  st.error(f"Error in conversation: {e}")
128
  return qa_chain, history, "", []
129
 
 
 
 
 
 
130
  def main():
131
  st.sidebar.title("PDF Chatbot")
132
 
@@ -146,6 +150,9 @@ def main():
146
  if 'qa_chain' not in st.session_state:
147
  st.session_state['qa_chain'] = None
148
 
 
 
 
149
  st.sidebar.markdown("### Select Large Language Model (LLM)")
150
  llm_option = st.sidebar.radio("Available LLMs", list_llm_simple)
151
 
@@ -158,20 +165,26 @@ def main():
158
  st.title("Chat with your Document")
159
 
160
  if st.session_state['qa_chain']:
161
- history = []
162
  message = st.text_input("Ask a question")
163
 
164
  if st.button("Submit"):
165
  with st.spinner("Generating response..."):
166
- qa_chain, history, response_answer, sources = conversation(st.session_state['qa_chain'], message, history)
167
  st.session_state['qa_chain'] = qa_chain
 
168
 
169
  st.markdown("### Chatbot Response")
170
- st.write(response_answer)
 
 
 
 
171
 
172
  with st.expander("Relevant context from the source document"):
173
  for source in sources:
174
  st.text_area(f"Source - Page {source['page']}", value=source["content"], height=100)
175
 
 
 
176
  if __name__ == "__main__":
177
  main()
 
103
  def format_chat_history(chat_history):
104
  formatted_chat_history = []
105
  for user_message, bot_message in chat_history:
106
+ formatted_chat_history.append(f"User: {user_message}\nAssistant: {bot_message}\n")
 
107
  return formatted_chat_history
108
 
109
  def conversation(qa_chain, message, history):
 
126
  st.error(f"Error in conversation: {e}")
127
  return qa_chain, history, "", []
128
 
129
+ def download_history(chat_history):
130
+ formatted_chat_history = format_chat_history(chat_history)
131
+ history_text = "\n".join(formatted_chat_history)
132
+ st.download_button(label="Download Chat History", data=history_text, file_name="chat_history.txt", mime="text/plain")
133
+
134
  def main():
135
  st.sidebar.title("PDF Chatbot")
136
 
 
150
  if 'qa_chain' not in st.session_state:
151
  st.session_state['qa_chain'] = None
152
 
153
+ if 'chat_history' not in st.session_state:
154
+ st.session_state['chat_history'] = []
155
+
156
  st.sidebar.markdown("### Select Large Language Model (LLM)")
157
  llm_option = st.sidebar.radio("Available LLMs", list_llm_simple)
158
 
 
165
  st.title("Chat with your Document")
166
 
167
  if st.session_state['qa_chain']:
 
168
  message = st.text_input("Ask a question")
169
 
170
  if st.button("Submit"):
171
  with st.spinner("Generating response..."):
172
+ qa_chain, chat_history, response_answer, sources = conversation(st.session_state['qa_chain'], message, st.session_state['chat_history'])
173
  st.session_state['qa_chain'] = qa_chain
174
+ st.session_state['chat_history'] = chat_history
175
 
176
  st.markdown("### Chatbot Response")
177
+
178
+ # Display the chat history in a chat-like interface
179
+ for i, (user_msg, bot_msg) in enumerate(st.session_state['chat_history']):
180
+ st.markdown(f"**User:** {user_msg}")
181
+ st.markdown(f"**Assistant:** {bot_msg}")
182
 
183
  with st.expander("Relevant context from the source document"):
184
  for source in sources:
185
  st.text_area(f"Source - Page {source['page']}", value=source["content"], height=100)
186
 
187
+ download_history(st.session_state['chat_history'])
188
+
189
  if __name__ == "__main__":
190
  main()