JirasakJo commited on
Commit
5e756bc
·
verified ·
1 Parent(s): 3fc1194

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -41
app.py CHANGED
@@ -143,55 +143,82 @@ def save_qa_history(history_entry):
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
- # st.info("Saved locally, attempting GitHub upload...")
147
-
148
- # Push to GitHub
149
- github_token = os.getenv('GITHUB_TOKEN') or st.secrets['GITHUB_TOKEN']
150
  if not github_token:
151
- st.error("GitHub token not found!")
152
  return
153
 
154
- from github import Github
155
- g = Github(github_token)
156
-
157
- # Replace with your repository name
158
- repo = g.get_repo("jirasaksaimekJijo/swu-chat-bot-project")
159
-
160
  try:
161
- # Try to get the file first
162
- contents = repo.get_contents("qa_history.json")
163
- response = repo.update_file(
164
- path="qa_history.json",
165
- message="Update QA history",
166
- content=json.dumps(history_data, ensure_ascii=False, indent=2),
167
- sha=contents.sha
168
- )
169
- # st.success(f"Successfully updated file on GitHub: {response.commit.html_url}")
170
- except Exception as e:
171
- # st.info(f"File doesn't exist yet, creating new one... Error was: {str(e)}")
172
- # File doesn't exist, create it
173
- response = repo.create_file(
174
- path="qa_history.json",
175
- message="Create QA history",
176
- content=json.dumps(history_data, ensure_ascii=False, indent=2)
177
- )
178
- # st.success(f"Successfully created file on GitHub: {response['commit'].html_url}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
  except Exception as e:
181
- # st.error(f"Error saving QA history: {str(e)}")
182
  import traceback
183
- # st.error(f"Full error: {traceback.format_exc()}")
184
-
185
  def add_to_qa_history(query: str, answer: str):
186
- """Add new QA pair to history"""
187
- history_entry = {
188
- "timestamp": (datetime.now() + timedelta(hours=5)).isoformat(),
189
- "query": query,
190
- "answer": answer
191
- }
192
-
193
- save_qa_history(history_entry)
194
- return history_entry
 
 
 
 
 
 
 
 
 
 
 
 
195
 
196
  def add_to_history(role: str, message: str):
197
  """Add message to chat history and save if it's a complete QA pair"""
 
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 GitHub with error logging
147
+ github_token = os.getenv('GITHUB_TOKEN') or st.secrets.get('GITHUB_TOKEN')
 
 
148
  if not github_token:
149
+ st.error("GitHub token not found in environment or secrets!")
150
  return
151
 
 
 
 
 
 
 
152
  try:
153
+ from github import Github
154
+ g = Github(github_token)
155
+
156
+ # Add debug logging
157
+ st.info("Attempting to connect to GitHub repository...")
158
+
159
+ repo = g.get_repo("jirasaksaimekJijo/swu-chat-bot-project")
160
+
161
+ try:
162
+ # Try to get the file first
163
+ contents = repo.get_contents("qa_history.json")
164
+ st.info("Existing qa_history.json found, updating...")
165
+
166
+ # Ensure content is properly encoded
167
+ content = json.dumps(history_data, ensure_ascii=False, indent=2)
168
+
169
+ response = repo.update_file(
170
+ path="qa_history.json",
171
+ message="Update QA history",
172
+ content=content,
173
+ sha=contents.sha,
174
+ branch="main" # Explicitly specify branch
175
+ )
176
+ st.success("Successfully updated qa_history.json on GitHub")
177
+
178
+ except Exception as file_error:
179
+ st.info("File not found, creating new qa_history.json...")
180
+ # File doesn't exist, create it
181
+ content = json.dumps(history_data, ensure_ascii=False, indent=2)
182
+ response = repo.create_file(
183
+ path="qa_history.json",
184
+ message="Create QA history",
185
+ content=content,
186
+ branch="main" # Explicitly specify branch
187
+ )
188
+ st.success("Successfully created qa_history.json on GitHub")
189
+
190
+ except Exception as github_error:
191
+ st.error(f"GitHub API error: {str(github_error)}")
192
+ import traceback
193
+ st.error(f"Full GitHub error trace: {traceback.format_exc()}")
194
 
195
  except Exception as e:
196
+ st.error(f"General error in save_qa_history: {str(e)}")
197
  import traceback
198
+ st.error(f"Full error trace: {traceback.format_exc()}")
199
+
200
  def add_to_qa_history(query: str, answer: str):
201
+ """Add new QA pair to history with validation"""
202
+ try:
203
+ # Validate inputs
204
+ if not query or not answer:
205
+ st.warning("Empty query or answer detected, skipping history update")
206
+ return None
207
+
208
+ # Create history entry with proper timestamp
209
+ history_entry = {
210
+ "timestamp": (datetime.now() + timedelta(hours=5)).strftime("%Y-%m-%dT%H:%M:%S"),
211
+ "query": query,
212
+ "answer": answer
213
+ }
214
+
215
+ # Save entry
216
+ save_qa_history(history_entry)
217
+ return history_entry
218
+
219
+ except Exception as e:
220
+ st.error(f"Error in add_to_qa_history: {str(e)}")
221
+ return None
222
 
223
  def add_to_history(role: str, message: str):
224
  """Add message to chat history and save if it's a complete QA pair"""