JirasakJo commited on
Commit
7d14e68
·
verified ·
1 Parent(s): b227742

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -26
app.py CHANGED
@@ -122,13 +122,17 @@ def load_qa_history():
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:
@@ -140,21 +144,13 @@ def save_qa_history(history_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
-
146
- # Push to Hugging Face
147
- hf_token = os.getenv('HF_TOKEN') or st.secrets['HF_TOKEN']
148
- api = HfApi(token=hf_token)
149
- api.upload_file(
150
- path_or_fileobj="qa_history.json",
151
- path_in_repo="qa_history.json",
152
- repo_id="JirasakJo/Questions_Graduate_Studies_Calendar_2024",
153
- repo_type="space"
154
- )
155
  except Exception as e:
156
  st.error(f"Error saving QA history: {str(e)}")
157
 
 
158
  def add_to_qa_history(query: str, answer: str):
159
  """Add new QA pair to history"""
160
  history_entry = {
@@ -169,19 +165,12 @@ def add_to_qa_history(query: str, answer: str):
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
 
 
122
  return []
123
 
124
  def save_qa_history(history_entry):
125
+ """Save QA history entry to local JSON file"""
126
  try:
127
+ # Define the local path where you want to save the history
128
+ local_path = Path("./qa_history/qa_history.json")
129
+
130
+ # Create the directory if it doesn't exist
131
+ local_path.parent.mkdir(parents=True, exist_ok=True)
132
 
133
  # Initialize or load existing history
134
+ if local_path.exists():
135
+ with open(local_path, "r", encoding="utf-8") as f:
136
  try:
137
  history_data = json.load(f)
138
  except json.JSONDecodeError:
 
144
  history_data.append(history_entry)
145
 
146
  # Save updated history
147
+ with open(local_path, "w", encoding="utf-8") as f:
148
  json.dump(history_data, f, ensure_ascii=False, indent=2)
149
+
 
 
 
 
 
 
 
 
 
150
  except Exception as e:
151
  st.error(f"Error saving QA history: {str(e)}")
152
 
153
+
154
  def add_to_qa_history(query: str, answer: str):
155
  """Add new QA pair to history"""
156
  history_entry = {
 
165
  def clear_qa_history():
166
  """Clear QA history file"""
167
  try:
168
+ local_path = Path("./qa_history/qa_history.json")
169
+
170
  # Write empty list to history file
171
+ with open(local_path, "w", encoding="utf-8") as f:
172
  json.dump([], f, ensure_ascii=False, indent=2)
173
+
 
 
 
 
 
 
 
 
 
174
  except Exception as e:
175
  st.error(f"Error clearing QA history: {str(e)}")
176