Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,8 @@
|
|
1 |
import streamlit as st
|
2 |
-
from git import Repo
|
3 |
-
import platform
|
4 |
-
import git
|
5 |
import json
|
6 |
import os
|
7 |
-
import time
|
8 |
from datetime import datetime, timedelta
|
|
|
9 |
from huggingface_hub import HfApi
|
10 |
from pathlib import Path
|
11 |
from calendar_rag import (
|
@@ -14,21 +11,6 @@ from calendar_rag import (
|
|
14 |
PipelineConfig
|
15 |
)
|
16 |
|
17 |
-
def get_repo_path():
|
18 |
-
"""
|
19 |
-
Get repository path based on the operating system
|
20 |
-
"""
|
21 |
-
if platform.system() == 'Windows':
|
22 |
-
return r"D:\Last SWU\swu-chat-bot-project"
|
23 |
-
else:
|
24 |
-
# For Linux/Mac, use the current directory
|
25 |
-
return os.path.join(os.getcwd(), "swu-chat-bot-project")
|
26 |
-
|
27 |
-
# Define repository path
|
28 |
-
REPO_PATH = get_repo_path()
|
29 |
-
GITHUB_REPO_URL = "https://github.com/jirasaksaimekJijo/swu-chat-bot-project.git"
|
30 |
-
|
31 |
-
|
32 |
# Custom CSS for enhanced styling
|
33 |
def load_custom_css():
|
34 |
st.markdown("""
|
@@ -102,7 +84,7 @@ def load_custom_css():
|
|
102 |
|
103 |
.status-offline {
|
104 |
background-color: #FDE8E8;
|
105 |
-
color:
|
106 |
}
|
107 |
</style>
|
108 |
""", unsafe_allow_html=True)
|
@@ -140,181 +122,66 @@ def load_qa_history():
|
|
140 |
return []
|
141 |
|
142 |
def save_qa_history(history_entry):
|
143 |
-
"""
|
144 |
-
Save QA history to Git with improved error handling and logging.
|
145 |
-
"""
|
146 |
try:
|
147 |
-
|
148 |
-
repo_path = Path(REPO_PATH) # Use the global REPO_PATH instead of env variable
|
149 |
-
history_file = repo_path / "qa_history.json"
|
150 |
|
151 |
-
#
|
152 |
-
history_data = []
|
153 |
if history_file.exists():
|
154 |
-
|
155 |
-
|
156 |
history_data = json.load(f)
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
# Update history
|
161 |
-
if isinstance(history_entry, list):
|
162 |
-
history_data.extend(history_entry)
|
163 |
else:
|
164 |
-
history_data
|
|
|
|
|
|
|
165 |
|
166 |
# Save updated history
|
167 |
-
with open(
|
168 |
json.dump(history_data, f, ensure_ascii=False, indent=2)
|
169 |
|
170 |
-
#
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
repo.index.add([str(history_file)])
|
180 |
-
print(f"Updated existing file: {history_file}")
|
181 |
-
|
182 |
-
if repo.is_dirty(untracked_files=True):
|
183 |
-
commit = repo.index.commit(f"Update QA history at {datetime.now().isoformat()}")
|
184 |
-
origin = repo.remote('origin')
|
185 |
-
|
186 |
-
try:
|
187 |
-
origin.push()
|
188 |
-
print(f"Successfully pushed commit {commit.hexsha}")
|
189 |
-
return True
|
190 |
-
except git.GitCommandError as push_error:
|
191 |
-
print(f"Push error: {str(push_error)}")
|
192 |
-
if "Authentication failed" in str(push_error):
|
193 |
-
print("Authentication failed - please check your Git credentials")
|
194 |
-
return False
|
195 |
-
else:
|
196 |
-
print("No changes to commit")
|
197 |
-
return True
|
198 |
-
|
199 |
except Exception as e:
|
200 |
-
|
201 |
-
return False
|
202 |
|
203 |
def add_to_qa_history(query: str, answer: str):
|
204 |
-
"""Add new QA pair to history
|
205 |
history_entry = {
|
206 |
"timestamp": (datetime.now() + timedelta(hours=7)).isoformat(),
|
207 |
"query": query,
|
208 |
"answer": answer
|
209 |
}
|
210 |
|
211 |
-
|
212 |
-
if 'all_history' not in st.session_state:
|
213 |
-
st.session_state.all_history = []
|
214 |
-
st.session_state.all_history.append(history_entry)
|
215 |
-
|
216 |
-
# Immediately try to save if GitHub sync is enabled
|
217 |
-
if st.session_state.github_sync_enabled:
|
218 |
-
success = save_qa_history(history_entry)
|
219 |
-
if not success:
|
220 |
-
print("Failed to save QA history to GitHub")
|
221 |
-
|
222 |
return history_entry
|
223 |
|
224 |
-
def fix_repo_path():
|
225 |
-
"""
|
226 |
-
Fix repository path for Windows environments
|
227 |
-
"""
|
228 |
-
# Define the correct Windows path
|
229 |
-
windows_path = r"D:\Last SWU\swu-chat-bot-project"
|
230 |
-
|
231 |
-
# Set the environment variable with the correct path
|
232 |
-
if platform.system() == 'Windows':
|
233 |
-
os.environ["GITHUB_REPO_PATH"] = windows_path
|
234 |
-
else:
|
235 |
-
# Convert Windows path to the current OS format
|
236 |
-
os.environ["GITHUB_REPO_PATH"] = str(Path(windows_path))
|
237 |
-
|
238 |
-
return os.environ["GITHUB_REPO_PATH"]
|
239 |
-
|
240 |
-
# Add this at the start of your main() function
|
241 |
-
def initialize_paths():
|
242 |
-
"""
|
243 |
-
Initialize and verify repository paths
|
244 |
-
"""
|
245 |
-
repo_path = fix_repo_path()
|
246 |
-
print(f"Repository path set to: {repo_path}")
|
247 |
-
|
248 |
-
# Verify the path exists
|
249 |
-
path_exists = os.path.exists(repo_path)
|
250 |
-
git_dir_exists = os.path.exists(os.path.join(repo_path, '.git'))
|
251 |
-
|
252 |
-
print(f"Repository path exists: {path_exists}")
|
253 |
-
print(f"Git directory exists: {git_dir_exists}")
|
254 |
-
|
255 |
-
return path_exists and git_dir_exists
|
256 |
-
|
257 |
-
def check_git_config():
|
258 |
-
"""
|
259 |
-
Check and verify Git configuration
|
260 |
-
"""
|
261 |
-
try:
|
262 |
-
repo_path = Path(os.getenv('GITHUB_REPO_PATH', '')).resolve()
|
263 |
-
repo = Repo(str(repo_path))
|
264 |
-
|
265 |
-
# Check Git configuration
|
266 |
-
config = repo.config_reader()
|
267 |
-
|
268 |
-
# Get user information
|
269 |
-
try:
|
270 |
-
user_name = config.get_value("user", "name")
|
271 |
-
user_email = config.get_value("user", "email")
|
272 |
-
print(f"Git User: {user_name}")
|
273 |
-
print(f"Git Email: {user_email}")
|
274 |
-
except Exception:
|
275 |
-
print("❌ Git user configuration not found!")
|
276 |
-
return False
|
277 |
-
|
278 |
-
# Check credential helper
|
279 |
-
try:
|
280 |
-
credential_helper = config.get_value("credential", "helper")
|
281 |
-
print(f"Credential helper: {credential_helper}")
|
282 |
-
except Exception:
|
283 |
-
print("⚠️ No credential helper configured")
|
284 |
-
|
285 |
-
return True
|
286 |
-
|
287 |
-
except Exception as e:
|
288 |
-
print(f"❌ Git configuration error: {str(e)}")
|
289 |
-
return False
|
290 |
-
|
291 |
def clear_qa_history():
|
292 |
-
"""Clear QA history
|
293 |
try:
|
294 |
-
#
|
295 |
-
st.session_state.all_history = []
|
296 |
-
|
297 |
-
# Clear local file
|
298 |
with open("qa_history.json", "w", encoding="utf-8") as f:
|
299 |
json.dump([], f, ensure_ascii=False, indent=2)
|
300 |
|
301 |
-
# Push to GitHub
|
302 |
-
if st.session_state.github_sync_enabled:
|
303 |
-
save_qa_history([])
|
304 |
-
|
305 |
# Push to Hugging Face
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
)
|
315 |
-
except Exception as hf_error:
|
316 |
-
print(f"Hugging Face sync failed: {str(hf_error)}")
|
317 |
-
|
318 |
except Exception as e:
|
319 |
st.error(f"Error clearing QA history: {str(e)}")
|
320 |
|
@@ -324,12 +191,9 @@ def add_to_history(role: str, message: str):
|
|
324 |
|
325 |
# If this is an assistant response, save the QA pair
|
326 |
if role == "assistant" and len(st.session_state.chat_history) >= 2:
|
|
|
327 |
user_query = st.session_state.chat_history[-2][1]
|
328 |
-
|
329 |
-
|
330 |
-
# Save to storage if GitHub sync is enabled
|
331 |
-
if st.session_state.github_sync_enabled:
|
332 |
-
save_qa_history(history_entry)
|
333 |
|
334 |
def display_chat_history():
|
335 |
"""Display chat history with enhanced styling"""
|
@@ -349,208 +213,9 @@ def display_chat_history():
|
|
349 |
</div>
|
350 |
""", unsafe_allow_html=True)
|
351 |
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
"""
|
356 |
-
try:
|
357 |
-
repo_path = Path(REPO_PATH)
|
358 |
-
|
359 |
-
# Create directory if it doesn't exist
|
360 |
-
repo_path.mkdir(parents=True, exist_ok=True)
|
361 |
-
|
362 |
-
# Initialize repository if .git doesn't exist
|
363 |
-
if not (repo_path / '.git').exists():
|
364 |
-
try:
|
365 |
-
# Initialize new repository
|
366 |
-
repo = Repo.init(str(repo_path))
|
367 |
-
|
368 |
-
# Add remote
|
369 |
-
try:
|
370 |
-
origin = repo.remote('origin')
|
371 |
-
except ValueError:
|
372 |
-
origin = repo.create_remote('origin', GITHUB_REPO_URL)
|
373 |
-
|
374 |
-
# Set up Git configuration
|
375 |
-
with repo.config_writer() as config:
|
376 |
-
config.set_value("user", "name", "jirasaksaimekJijo")
|
377 |
-
config.set_value("user", "email", "[email protected]")
|
378 |
-
|
379 |
-
print(f"Repository initialized at {repo_path}")
|
380 |
-
return True
|
381 |
-
|
382 |
-
except Exception as init_error:
|
383 |
-
print(f"Failed to initialize repository: {str(init_error)}")
|
384 |
-
return False
|
385 |
-
else:
|
386 |
-
print(f"Repository already exists at {repo_path}")
|
387 |
-
return True
|
388 |
-
|
389 |
-
except Exception as e:
|
390 |
-
print(f"Failed to ensure repository exists: {str(e)}")
|
391 |
-
return False
|
392 |
-
|
393 |
-
def initialize_github_sync():
|
394 |
-
"""
|
395 |
-
Initialize GitHub repository connection with proper path verification
|
396 |
-
"""
|
397 |
-
try:
|
398 |
-
# Ensure repository exists
|
399 |
-
if not ensure_repo_exists():
|
400 |
-
return False
|
401 |
-
|
402 |
-
repo_path = Path(REPO_PATH)
|
403 |
-
print(f"Initializing repo at: {repo_path}")
|
404 |
-
|
405 |
-
try:
|
406 |
-
# Initialize repository
|
407 |
-
repo = Repo(str(repo_path))
|
408 |
-
|
409 |
-
# Check remote configuration
|
410 |
-
try:
|
411 |
-
origin = repo.remote('origin')
|
412 |
-
urls = list(origin.urls)
|
413 |
-
if not urls:
|
414 |
-
print("Remote 'origin' has no URL")
|
415 |
-
return False
|
416 |
-
|
417 |
-
print(f"Connected to remote: {urls[0]}")
|
418 |
-
|
419 |
-
# Test Git operations
|
420 |
-
try:
|
421 |
-
status = repo.git.status()
|
422 |
-
print("Git status verified:")
|
423 |
-
print(status)
|
424 |
-
return True
|
425 |
-
|
426 |
-
except git.GitCommandError as git_error:
|
427 |
-
print(f"Git command error: {str(git_error)}")
|
428 |
-
return False
|
429 |
-
|
430 |
-
except ValueError as remote_error:
|
431 |
-
print(f"Remote error: {str(remote_error)}")
|
432 |
-
return False
|
433 |
-
|
434 |
-
except Exception as repo_error:
|
435 |
-
print(f"Repository error: {str(repo_error)}")
|
436 |
-
return False
|
437 |
-
|
438 |
-
except Exception as e:
|
439 |
-
print(f"Initialization error: {str(e)}")
|
440 |
-
return False
|
441 |
-
|
442 |
-
# Initialize global state
|
443 |
-
if 'all_history' not in st.session_state:
|
444 |
-
st.session_state.all_history = []
|
445 |
-
|
446 |
-
# Add this function to test the connection
|
447 |
-
def test_github_connection():
|
448 |
-
"""
|
449 |
-
Test GitHub connection by attempting a simple operation
|
450 |
-
"""
|
451 |
-
try:
|
452 |
-
repo_path = Path(r"D:\Last SWU\swu-chat-bot-project")
|
453 |
-
repo = Repo(str(repo_path))
|
454 |
-
|
455 |
-
# Get current status
|
456 |
-
status = repo.git.status()
|
457 |
-
print("Repository Status:")
|
458 |
-
print(status)
|
459 |
-
|
460 |
-
# Get remote information
|
461 |
-
origin = repo.remote('origin')
|
462 |
-
print("\nRemote URLs:")
|
463 |
-
for url in origin.urls:
|
464 |
-
print(url)
|
465 |
-
|
466 |
-
# Get current branch
|
467 |
-
branch = repo.active_branch
|
468 |
-
print(f"\nActive branch: {branch.name}")
|
469 |
-
|
470 |
-
return True
|
471 |
-
|
472 |
-
except Exception as e:
|
473 |
-
print(f"Connection test failed: {str(e)}")
|
474 |
-
return False
|
475 |
-
|
476 |
-
def debug_github_connection():
|
477 |
-
"""
|
478 |
-
Debug GitHub connection issues and print detailed status
|
479 |
-
"""
|
480 |
-
try:
|
481 |
-
# 1. Check repository path
|
482 |
-
repo_path = Path(os.getenv('GITHUB_REPO_PATH', '')).resolve()
|
483 |
-
print(f"1. Checking repository path: {repo_path}")
|
484 |
-
if not repo_path.exists():
|
485 |
-
print("❌ Repository path does not exist!")
|
486 |
-
return False
|
487 |
-
print("✅ Repository path exists")
|
488 |
-
|
489 |
-
# 2. Check .git directory
|
490 |
-
git_dir = repo_path / '.git'
|
491 |
-
print(f"2. Checking .git directory: {git_dir}")
|
492 |
-
if not git_dir.exists():
|
493 |
-
print("❌ .git directory not found!")
|
494 |
-
return False
|
495 |
-
print("✅ .git directory found")
|
496 |
-
|
497 |
-
# 3. Initialize repository
|
498 |
-
try:
|
499 |
-
repo = Repo(str(repo_path))
|
500 |
-
print("3. Repository initialized successfully")
|
501 |
-
except Exception as repo_error:
|
502 |
-
print(f"❌ Failed to initialize repository: {str(repo_error)}")
|
503 |
-
return False
|
504 |
-
|
505 |
-
# 4. Check remote configuration
|
506 |
-
try:
|
507 |
-
origin = repo.remote('origin')
|
508 |
-
urls = list(origin.urls)
|
509 |
-
print(f"4. Remote URLs found: {urls}")
|
510 |
-
|
511 |
-
if not urls:
|
512 |
-
print("❌ No remote URLs configured!")
|
513 |
-
return False
|
514 |
-
print("✅ Remote URL configured correctly")
|
515 |
-
|
516 |
-
# 5. Check Git credentials
|
517 |
-
try:
|
518 |
-
repo.git.status()
|
519 |
-
print("5. Git status check passed")
|
520 |
-
except git.GitCommandError as status_error:
|
521 |
-
print(f"❌ Git status check failed: {str(status_error)}")
|
522 |
-
return False
|
523 |
-
|
524 |
-
# 6. Test basic Git operations
|
525 |
-
test_file = repo_path / 'test_sync.txt'
|
526 |
-
try:
|
527 |
-
# Create test file
|
528 |
-
test_file.write_text("test sync")
|
529 |
-
repo.index.add([str(test_file)])
|
530 |
-
repo.index.commit("Test sync commit")
|
531 |
-
print("6. Test commit created successfully")
|
532 |
-
|
533 |
-
# Try to push
|
534 |
-
origin.push()
|
535 |
-
print("✅ Push successful!")
|
536 |
-
|
537 |
-
# Clean up test file
|
538 |
-
test_file.unlink()
|
539 |
-
return True
|
540 |
-
|
541 |
-
except git.GitCommandError as push_error:
|
542 |
-
print(f"❌ Push failed: {str(push_error)}")
|
543 |
-
if "Authentication failed" in str(push_error):
|
544 |
-
print("💡 Authentication error - please check your Git credentials")
|
545 |
-
return False
|
546 |
-
|
547 |
-
except ValueError as remote_error:
|
548 |
-
print(f"❌ Remote configuration error: {str(remote_error)}")
|
549 |
-
return False
|
550 |
-
|
551 |
-
except Exception as e:
|
552 |
-
print(f"❌ General error: {str(e)}")
|
553 |
-
return False
|
554 |
|
555 |
def main():
|
556 |
# Page config
|
@@ -574,9 +239,6 @@ def main():
|
|
574 |
if 'chat_history' not in st.session_state:
|
575 |
st.session_state.chat_history = []
|
576 |
|
577 |
-
if 'github_sync_enabled' not in st.session_state:
|
578 |
-
st.session_state.github_sync_enabled = repo_exists and initialize_github_sync()
|
579 |
-
|
580 |
# Load QA history at startup
|
581 |
if 'qa_history_loaded' not in st.session_state:
|
582 |
st.session_state.qa_history_loaded = True
|
@@ -684,40 +346,6 @@ def main():
|
|
684 |
st.session_state.chat_history = []
|
685 |
clear_qa_history() # This function handles both local and remote clearing
|
686 |
st.rerun()
|
687 |
-
|
688 |
-
with info_col:
|
689 |
-
# GitHub Sync Status
|
690 |
-
with st.expander("🔧 GitHub Connection Status", expanded=True):
|
691 |
-
col1, col2 = st.columns([1, 2])
|
692 |
-
|
693 |
-
with col1:
|
694 |
-
if st.button("Test Connection"):
|
695 |
-
with st.spinner("Testing connection..."):
|
696 |
-
if test_github_connection():
|
697 |
-
st.success("✅ GitHub connection successful!")
|
698 |
-
st.session_state.github_sync_enabled = True
|
699 |
-
else:
|
700 |
-
st.error("❌ GitHub connection failed!")
|
701 |
-
st.session_state.github_sync_enabled = False
|
702 |
-
|
703 |
-
with col2:
|
704 |
-
# Display current status
|
705 |
-
status = "🟢 Connected" if st.session_state.github_sync_enabled else "🔴 Disconnected"
|
706 |
-
st.info(f"Current Status: {status}")
|
707 |
-
|
708 |
-
# Show repository details
|
709 |
-
if st.checkbox("Show Repository Details"):
|
710 |
-
try:
|
711 |
-
repo = Repo(str(Path(REPO_PATH)))
|
712 |
-
st.write("Repository Information:")
|
713 |
-
st.code(f"""
|
714 |
-
Path: {REPO_PATH}
|
715 |
-
Remote URL: {list(repo.remote('origin').urls)[0]}
|
716 |
-
Current Branch: {repo.active_branch.name}
|
717 |
-
Git Status: {repo.git.status()}
|
718 |
-
""")
|
719 |
-
except Exception as e:
|
720 |
-
st.error(f"Error accessing repository: {str(e)}")
|
721 |
|
722 |
# System information
|
723 |
st.markdown("""
|
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
import json
|
3 |
import os
|
|
|
4 |
from datetime import datetime, timedelta
|
5 |
+
import subprocess
|
6 |
from huggingface_hub import HfApi
|
7 |
from pathlib import Path
|
8 |
from calendar_rag import (
|
|
|
11 |
PipelineConfig
|
12 |
)
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
# Custom CSS for enhanced styling
|
15 |
def load_custom_css():
|
16 |
st.markdown("""
|
|
|
84 |
|
85 |
.status-offline {
|
86 |
background-color: #FDE8E8;
|
87 |
+
color: rgb(255, 255, 255);
|
88 |
}
|
89 |
</style>
|
90 |
""", unsafe_allow_html=True)
|
|
|
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 |
|
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 = {
|
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 |
|
|
|
191 |
|
192 |
# If this is an assistant response, save the QA pair
|
193 |
if role == "assistant" and len(st.session_state.chat_history) >= 2:
|
194 |
+
# Get the corresponding user query (previous message)
|
195 |
user_query = st.session_state.chat_history[-2][1]
|
196 |
+
add_to_qa_history(user_query, message)
|
|
|
|
|
|
|
|
|
197 |
|
198 |
def display_chat_history():
|
199 |
"""Display chat history with enhanced styling"""
|
|
|
213 |
</div>
|
214 |
""", unsafe_allow_html=True)
|
215 |
|
216 |
+
# # Initialize global state
|
217 |
+
# if 'all_history' not in st.session_state:
|
218 |
+
# st.session_state.all_history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
|
220 |
def main():
|
221 |
# Page config
|
|
|
239 |
if 'chat_history' not in st.session_state:
|
240 |
st.session_state.chat_history = []
|
241 |
|
|
|
|
|
|
|
242 |
# Load QA history at startup
|
243 |
if 'qa_history_loaded' not in st.session_state:
|
244 |
st.session_state.qa_history_loaded = True
|
|
|
346 |
st.session_state.chat_history = []
|
347 |
clear_qa_history() # This function handles both local and remote clearing
|
348 |
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
349 |
|
350 |
# System information
|
351 |
st.markdown("""
|