Update app.py
Browse files
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
|
| 126 |
try:
|
| 127 |
-
|
| 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 |
-
#
|
| 136 |
-
if
|
| 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 |
-
# 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 |
|