JirasakJo commited on
Commit
ee0fcb3
·
verified ·
1 Parent(s): 8c6c5d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -13
app.py CHANGED
@@ -121,10 +121,25 @@ def load_qa_history():
121
  st.error(f"Error loading QA history: {str(e)}")
122
  return []
123
 
124
- def save_qa_history(history_data):
125
- """Save QA history to local JSON file and push to Hugging Face"""
126
  try:
127
- # Save locally
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  with open("qa_history.json", "w", encoding="utf-8") as f:
129
  json.dump(history_data, f, ensure_ascii=False, indent=2)
130
 
@@ -143,21 +158,33 @@ def save_qa_history(history_data):
143
  def add_to_qa_history(query: str, answer: str):
144
  """Add new QA pair to history"""
145
  history_entry = {
146
- "timestamp": (datetime.now() + timedelta(hours=6)).isoformat(),
147
  "query": query,
148
  "answer": answer
149
  }
150
 
151
- # Load existing history
152
- history_data = load_qa_history()
153
-
154
- # Add new entry
155
- history_data.append(history_entry)
156
-
157
- # Save updated history
158
- save_qa_history(history_data)
159
- return history_data
160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  def add_to_history(role: str, message: str):
162
  """Add message to chat history and save if it's a complete QA pair"""
163
  st.session_state.chat_history.append((role, message))
@@ -168,6 +195,12 @@ def add_to_history(role: str, message: str):
168
  user_query = st.session_state.chat_history[-2][1]
169
  add_to_qa_history(user_query, message)
170
 
 
 
 
 
 
 
171
  def display_chat_history():
172
  """Display chat history with enhanced styling"""
173
  for i, (role, message) in enumerate(st.session_state.chat_history):
 
121
  st.error(f"Error loading QA history: {str(e)}")
122
  return []
123
 
124
+ def save_qa_history(history_entry):
125
+ """Save QA history entry to local JSON file and push to Hugging Face"""
126
  try:
127
+ history_file = Path("qa_history.json")
128
+
129
+ # Initialize or load existing history
130
+ if history_file.exists():
131
+ with open(history_file, "r", encoding="utf-8") as f:
132
+ try:
133
+ history_data = json.load(f)
134
+ except json.JSONDecodeError:
135
+ history_data = []
136
+ else:
137
+ history_data = []
138
+
139
+ # Append new entry
140
+ history_data.append(history_entry)
141
+
142
+ # Save updated history
143
  with open("qa_history.json", "w", encoding="utf-8") as f:
144
  json.dump(history_data, f, ensure_ascii=False, indent=2)
145
 
 
158
  def add_to_qa_history(query: str, answer: str):
159
  """Add new QA pair to history"""
160
  history_entry = {
161
+ "timestamp": (datetime.now() + timedelta(hours=7)).isoformat(),
162
  "query": query,
163
  "answer": answer
164
  }
165
 
166
+ save_qa_history(history_entry)
167
+ return history_entry
 
 
 
 
 
 
 
168
 
169
+ def clear_qa_history():
170
+ """Clear QA history file"""
171
+ try:
172
+ # Write empty list to history file
173
+ with open("qa_history.json", "w", encoding="utf-8") as f:
174
+ json.dump([], f, ensure_ascii=False, indent=2)
175
+
176
+ # Push to Hugging Face
177
+ hf_token = os.getenv('HF_TOKEN') or st.secrets['HF_TOKEN']
178
+ api = HfApi(token=hf_token)
179
+ api.upload_file(
180
+ path_or_fileobj="qa_history.json",
181
+ path_in_repo="qa_history.json",
182
+ repo_id="JirasakJo/Questions_Graduate_Studies_Calendar_2024",
183
+ repo_type="space"
184
+ )
185
+ except Exception as e:
186
+ st.error(f"Error clearing QA history: {str(e)}")
187
+
188
  def add_to_history(role: str, message: str):
189
  """Add message to chat history and save if it's a complete QA pair"""
190
  st.session_state.chat_history.append((role, message))
 
195
  user_query = st.session_state.chat_history[-2][1]
196
  add_to_qa_history(user_query, message)
197
 
198
+ # Modify the clear history button handler in main():
199
+ if clear_history:
200
+ st.session_state.chat_history = []
201
+ clear_qa_history() # Clear saved history
202
+ st.rerun()
203
+
204
  def display_chat_history():
205
  """Display chat history with enhanced styling"""
206
  for i, (role, message) in enumerate(st.session_state.chat_history):