jatinmehra commited on
Commit
a0f5aa1
ยท
1 Parent(s): b62e18b

Add chat history download options (HTML. JSON)

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py CHANGED
@@ -1,8 +1,11 @@
1
  import os
2
  import tempfile
 
3
  import streamlit as st
4
  from streamlit_chat import message
5
  from preprocessing import Model
 
 
6
 
7
  # Home Page Setup
8
  st.set_page_config(
@@ -67,6 +70,10 @@ def process_user_input():
67
  st.session_state["messages"].append((agent_response, False))
68
  st.session_state["user_input"] = ""
69
 
 
 
 
 
70
  def process_file():
71
  """
72
  Processes the uploaded PDF file and appends its content to the context.
@@ -83,6 +90,35 @@ def process_file():
83
  st.error(f"Failed to process file {file.name}: {str(e)}")
84
  os.remove(file_path)
85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  def main_page():
87
  """
88
  Main function to set up the Streamlit UI and handle user interactions.
@@ -135,6 +171,11 @@ def main_page():
135
  # Display messages and input box
136
  display_messages()
137
  st.text_input("Type your query and hit Enter", key="user_input", on_change=process_user_input, placeholder="Ask something about your documents...")
 
 
 
 
 
138
  # Developer info and bug report
139
  st.subheader("๐Ÿž Bug Report")
140
  st.markdown("""
@@ -150,5 +191,6 @@ def main_page():
150
  **Email**: [email protected]\n
151
  **Mobile**: 9910364780\n
152
  """)
 
153
  if __name__ == "__main__":
154
  main_page()
 
1
  import os
2
  import tempfile
3
+ import json
4
  import streamlit as st
5
  from streamlit_chat import message
6
  from preprocessing import Model
7
+ from io import BytesIO
8
+ import pickle
9
 
10
  # Home Page Setup
11
  st.set_page_config(
 
70
  st.session_state["messages"].append((agent_response, False))
71
  st.session_state["user_input"] = ""
72
 
73
+ # Save chat history temporarily on local storage
74
+ with open("chat_history.pkl", "wb") as f:
75
+ pickle.dump(st.session_state["messages"], f)
76
+
77
  def process_file():
78
  """
79
  Processes the uploaded PDF file and appends its content to the context.
 
90
  st.error(f"Failed to process file {file.name}: {str(e)}")
91
  os.remove(file_path)
92
 
93
+ def download_chat_history():
94
+ """
95
+ Allows users to download chat history in HTML or JSON format.
96
+ """
97
+ # Convert messages to JSON format
98
+ chat_data = [{"role": "user" if is_user else "assistant", "content": msg} for msg, is_user in st.session_state["messages"]]
99
+
100
+ # Download as JSON
101
+ json_data = json.dumps(chat_data, indent=4)
102
+ st.download_button(
103
+ label="๐Ÿ’พ Download Chat History as JSON",
104
+ data=json_data,
105
+ file_name="chat_history.json",
106
+ mime="application/json"
107
+ )
108
+
109
+ # Download as HTML
110
+ html_data = "<html><body><h1>Chat History</h1><ul>"
111
+ for entry in chat_data:
112
+ role = "User" if entry["role"] == "user" else "Assistant"
113
+ html_data += f"<li><strong>{role}:</strong> {entry['content']}</li>"
114
+ html_data += "</ul></body></html>"
115
+ st.download_button(
116
+ label="๐Ÿ’พ Download Chat History as HTML",
117
+ data=html_data,
118
+ file_name="chat_history.html",
119
+ mime="text/html"
120
+ )
121
+
122
  def main_page():
123
  """
124
  Main function to set up the Streamlit UI and handle user interactions.
 
171
  # Display messages and input box
172
  display_messages()
173
  st.text_input("Type your query and hit Enter", key="user_input", on_change=process_user_input, placeholder="Ask something about your documents...")
174
+
175
+ # Download chat history section
176
+ st.subheader("๐Ÿ’พ Download Chat History")
177
+ download_chat_history()
178
+
179
  # Developer info and bug report
180
  st.subheader("๐Ÿž Bug Report")
181
  st.markdown("""
 
191
  **Email**: [email protected]\n
192
  **Mobile**: 9910364780\n
193
  """)
194
+
195
  if __name__ == "__main__":
196
  main_page()