JirasakJo commited on
Commit
c01cb10
·
verified ·
1 Parent(s): 87f1ba3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -48
app.py CHANGED
@@ -121,57 +121,38 @@ def load_qa_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 with 20-minute delay"""
126
  try:
127
- # Check if last save time exists in session state
128
- if 'last_save_time' not in st.session_state:
129
- st.session_state.last_save_time = datetime.min
130
- st.session_state.pending_entries = []
131
-
132
- current_time = datetime.now()
133
- time_since_last_save = current_time - st.session_state.last_save_time
134
 
135
- # Add entry to pending list
136
- if 'pending_entries' not in st.session_state:
137
- st.session_state.pending_entries = []
138
- st.session_state.pending_entries.append(history_entry)
139
-
140
- # Only save if 20 minutes have passed since last save
141
- if time_since_last_save >= timedelta(minutes=1):
142
- history_file = Path("qa_history.json")
143
-
144
- # Load existing history
145
- if history_file.exists():
146
- with open(history_file, "r", encoding="utf-8") as f:
147
- try:
148
- history_data = json.load(f)
149
- except json.JSONDecodeError:
150
- history_data = []
151
- else:
152
- history_data = []
153
-
154
- # Add all pending entries
155
- history_data.extend(st.session_state.pending_entries)
156
-
157
- # Save updated history
158
- with open("qa_history.json", "w", encoding="utf-8") as f:
159
- json.dump(history_data, f, ensure_ascii=False, indent=2)
160
-
161
- # Push to Hugging Face
162
- hf_token = os.getenv('HF_TOKEN') or st.secrets['HF_TOKEN']
163
- api = HfApi(token=hf_token)
164
- api.upload_file(
165
- path_or_fileobj="qa_history.json",
166
- path_in_repo="qa_history.json",
167
- repo_id="JirasakJo/Questions_Graduate_Studies_Calendar_2024",
168
- repo_type="space"
169
- )
170
-
171
- # Update last save time and clear pending entries
172
- st.session_state.last_save_time = current_time
173
- st.session_state.pending_entries = []
174
-
175
  except Exception as e:
176
  st.error(f"Error saving QA history: {str(e)}")
177
 
 
121
  st.error(f"Error loading QA history: {str(e)}")
122
  return []
123
 
124
+
125
  def save_qa_history(history_entry):
126
+ """Save QA history entry to local JSON file and push to Hugging Face"""
127
  try:
128
+ history_file = Path("qa_history.json")
 
 
 
 
 
 
129
 
130
+ # Initialize or load existing history
131
+ if history_file.exists():
132
+ with open(history_file, "r", encoding="utf-8") as f:
133
+ try:
134
+ history_data = json.load(f)
135
+ except json.JSONDecodeError:
136
+ history_data = []
137
+ else:
138
+ history_data = []
139
+
140
+ # Append new entry
141
+ history_data.append(history_entry)
142
+
143
+ # Save updated history
144
+ with open("qa_history.json", "w", encoding="utf-8") as f:
145
+ json.dump(history_data, f, ensure_ascii=False, indent=2)
146
+
147
+ # Push to Hugging Face
148
+ hf_token = os.getenv('HF_TOKEN') or st.secrets['HF_TOKEN']
149
+ api = HfApi(token=hf_token)
150
+ api.upload_file(
151
+ path_or_fileobj="qa_history.json",
152
+ path_in_repo="qa_history.json",
153
+ repo_id="JirasakJo/Questions_Graduate_Studies_Calendar_2024",
154
+ repo_type="space"
155
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  except Exception as e:
157
  st.error(f"Error saving QA history: {str(e)}")
158