Update app.py
Browse files
app.py
CHANGED
@@ -14,13 +14,20 @@ from calendar_rag import (
|
|
14 |
PipelineConfig
|
15 |
)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# Define repository path
|
18 |
-
|
19 |
GITHUB_REPO_URL = "https://github.com/jirasaksaimekJijo/swu-chat-bot-project.git"
|
20 |
|
21 |
-
# Initialize global state
|
22 |
-
if 'all_history' not in st.session_state:
|
23 |
-
st.session_state.all_history = []
|
24 |
|
25 |
# Custom CSS for enhanced styling
|
26 |
def load_custom_css():
|
@@ -330,29 +337,63 @@ def display_chat_history():
|
|
330 |
</div>
|
331 |
""", unsafe_allow_html=True)
|
332 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
333 |
def initialize_github_sync():
|
334 |
"""
|
335 |
Initialize GitHub repository connection with proper path verification
|
336 |
"""
|
337 |
try:
|
338 |
-
#
|
339 |
-
|
340 |
-
print(f"Initializing repo at: {repo_path}")
|
341 |
-
|
342 |
-
# Verify repository exists
|
343 |
-
if not repo_path.exists():
|
344 |
-
print(f"Repository directory not found at: {repo_path}")
|
345 |
return False
|
346 |
|
|
|
|
|
|
|
347 |
try:
|
348 |
# Initialize repository
|
349 |
repo = Repo(str(repo_path))
|
350 |
|
351 |
-
# Verify .git directory
|
352 |
-
if not (repo_path / '.git').exists():
|
353 |
-
print(".git directory not found")
|
354 |
-
return False
|
355 |
-
|
356 |
# Check remote configuration
|
357 |
try:
|
358 |
origin = repo.remote('origin')
|
@@ -386,6 +427,10 @@ def initialize_github_sync():
|
|
386 |
print(f"Initialization error: {str(e)}")
|
387 |
return False
|
388 |
|
|
|
|
|
|
|
|
|
389 |
# Add this function to test the connection
|
390 |
def test_github_connection():
|
391 |
"""
|
@@ -504,8 +549,8 @@ def main():
|
|
504 |
initial_sidebar_state="collapsed"
|
505 |
)
|
506 |
|
507 |
-
#
|
508 |
-
|
509 |
|
510 |
# Load custom CSS
|
511 |
load_custom_css()
|
@@ -518,7 +563,7 @@ def main():
|
|
518 |
st.session_state.chat_history = []
|
519 |
|
520 |
if 'github_sync_enabled' not in st.session_state:
|
521 |
-
st.session_state.github_sync_enabled =
|
522 |
|
523 |
# Load QA history at startup
|
524 |
if 'qa_history_loaded' not in st.session_state:
|
@@ -549,7 +594,7 @@ def main():
|
|
549 |
# Query input section
|
550 |
st.markdown("""
|
551 |
<label for="query_input" style="font-size: 1.2rem; font-weight: 600; margin-bottom: 1rem; display: block;">
|
552 |
-
<span style="color: #
|
553 |
โปรดระบุคำถามเกี่ยวกับปฏิทินการศึกษา:
|
554 |
</span>
|
555 |
</label>
|
@@ -625,42 +670,43 @@ def main():
|
|
625 |
# Clear history
|
626 |
if clear_history:
|
627 |
st.session_state.chat_history = []
|
628 |
-
|
629 |
st.rerun()
|
630 |
-
|
631 |
-
# Add this to your main() function in the info_col section
|
632 |
-
with st.expander("🔧 GitHub Connection Status", expanded=True):
|
633 |
-
col1, col2 = st.columns([1, 2])
|
634 |
-
|
635 |
-
with col1:
|
636 |
-
if st.button("Test Connection"):
|
637 |
-
with st.spinner("Testing connection..."):
|
638 |
-
if test_github_connection():
|
639 |
-
st.success("✅ GitHub connection successful!")
|
640 |
-
st.session_state.github_sync_enabled = True
|
641 |
-
else:
|
642 |
-
st.error("❌ GitHub connection failed!")
|
643 |
-
st.session_state.github_sync_enabled = False
|
644 |
-
|
645 |
-
with col2:
|
646 |
-
# Display current status
|
647 |
-
status = "🟢 Connected" if st.session_state.github_sync_enabled else "🔴 Disconnected"
|
648 |
-
st.info(f"Current Status: {status}")
|
649 |
-
|
650 |
-
# Show repository details
|
651 |
-
if st.checkbox("Show Repository Details"):
|
652 |
-
repo_path = Path(r"D:\Last SWU\swu-chat-bot-project")
|
653 |
-
repo = Repo(str(repo_path))
|
654 |
-
|
655 |
-
st.write("Repository Information:")
|
656 |
-
st.code(f"""
|
657 |
-
Path: {repo_path}
|
658 |
-
Remote URL: {list(repo.remote('origin').urls)[0]}
|
659 |
-
Current Branch: {repo.active_branch.name}
|
660 |
-
Git Status: {repo.git.status()}
|
661 |
-
""")
|
662 |
|
663 |
with info_col:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
664 |
# System information
|
665 |
st.markdown("""
|
666 |
<div style="background-color: #F9FAFB; padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem;">
|
|
|
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():
|
|
|
337 |
</div>
|
338 |
""", unsafe_allow_html=True)
|
339 |
|
340 |
+
def ensure_repo_exists():
|
341 |
+
"""
|
342 |
+
Ensure the repository exists and is properly initialized
|
343 |
+
"""
|
344 |
+
try:
|
345 |
+
repo_path = Path(REPO_PATH)
|
346 |
+
|
347 |
+
# Create directory if it doesn't exist
|
348 |
+
repo_path.mkdir(parents=True, exist_ok=True)
|
349 |
+
|
350 |
+
# Initialize repository if .git doesn't exist
|
351 |
+
if not (repo_path / '.git').exists():
|
352 |
+
try:
|
353 |
+
# Initialize new repository
|
354 |
+
repo = Repo.init(str(repo_path))
|
355 |
+
|
356 |
+
# Add remote
|
357 |
+
try:
|
358 |
+
origin = repo.remote('origin')
|
359 |
+
except ValueError:
|
360 |
+
origin = repo.create_remote('origin', GITHUB_REPO_URL)
|
361 |
+
|
362 |
+
# Set up Git configuration
|
363 |
+
with repo.config_writer() as config:
|
364 |
+
config.set_value("user", "name", "jirasaksaimekJijo")
|
365 |
+
config.set_value("user", "email", "[email protected]")
|
366 |
+
|
367 |
+
print(f"Repository initialized at {repo_path}")
|
368 |
+
return True
|
369 |
+
|
370 |
+
except Exception as init_error:
|
371 |
+
print(f"Failed to initialize repository: {str(init_error)}")
|
372 |
+
return False
|
373 |
+
else:
|
374 |
+
print(f"Repository already exists at {repo_path}")
|
375 |
+
return True
|
376 |
+
|
377 |
+
except Exception as e:
|
378 |
+
print(f"Failed to ensure repository exists: {str(e)}")
|
379 |
+
return False
|
380 |
+
|
381 |
def initialize_github_sync():
|
382 |
"""
|
383 |
Initialize GitHub repository connection with proper path verification
|
384 |
"""
|
385 |
try:
|
386 |
+
# Ensure repository exists
|
387 |
+
if not ensure_repo_exists():
|
|
|
|
|
|
|
|
|
|
|
388 |
return False
|
389 |
|
390 |
+
repo_path = Path(REPO_PATH)
|
391 |
+
print(f"Initializing repo at: {repo_path}")
|
392 |
+
|
393 |
try:
|
394 |
# Initialize repository
|
395 |
repo = Repo(str(repo_path))
|
396 |
|
|
|
|
|
|
|
|
|
|
|
397 |
# Check remote configuration
|
398 |
try:
|
399 |
origin = repo.remote('origin')
|
|
|
427 |
print(f"Initialization error: {str(e)}")
|
428 |
return False
|
429 |
|
430 |
+
# Initialize global state
|
431 |
+
if 'all_history' not in st.session_state:
|
432 |
+
st.session_state.all_history = []
|
433 |
+
|
434 |
# Add this function to test the connection
|
435 |
def test_github_connection():
|
436 |
"""
|
|
|
549 |
initial_sidebar_state="collapsed"
|
550 |
)
|
551 |
|
552 |
+
# Ensure repository exists and initialize paths
|
553 |
+
repo_exists = ensure_repo_exists()
|
554 |
|
555 |
# Load custom CSS
|
556 |
load_custom_css()
|
|
|
563 |
st.session_state.chat_history = []
|
564 |
|
565 |
if 'github_sync_enabled' not in st.session_state:
|
566 |
+
st.session_state.github_sync_enabled = repo_exists and initialize_github_sync()
|
567 |
|
568 |
# Load QA history at startup
|
569 |
if 'qa_history_loaded' not in st.session_state:
|
|
|
594 |
# Query input section
|
595 |
st.markdown("""
|
596 |
<label for="query_input" style="font-size: 1.2rem; font-weight: 600; margin-bottom: 1rem; display: block;">
|
597 |
+
<span style="color: #000000; border-left: 4px solid #1E3A8A; padding-left: 0.8rem;">
|
598 |
โปรดระบุคำถามเกี่ยวกับปฏิทินการศึกษา:
|
599 |
</span>
|
600 |
</label>
|
|
|
670 |
# Clear history
|
671 |
if clear_history:
|
672 |
st.session_state.chat_history = []
|
673 |
+
clear_qa_history() # This function handles both local and remote clearing
|
674 |
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
675 |
|
676 |
with info_col:
|
677 |
+
# GitHub Sync Status
|
678 |
+
with st.expander("🔧 GitHub Connection Status", expanded=True):
|
679 |
+
col1, col2 = st.columns([1, 2])
|
680 |
+
|
681 |
+
with col1:
|
682 |
+
if st.button("Test Connection"):
|
683 |
+
with st.spinner("Testing connection..."):
|
684 |
+
if test_github_connection():
|
685 |
+
st.success("✅ GitHub connection successful!")
|
686 |
+
st.session_state.github_sync_enabled = True
|
687 |
+
else:
|
688 |
+
st.error("❌ GitHub connection failed!")
|
689 |
+
st.session_state.github_sync_enabled = False
|
690 |
+
|
691 |
+
with col2:
|
692 |
+
# Display current status
|
693 |
+
status = "🟢 Connected" if st.session_state.github_sync_enabled else "🔴 Disconnected"
|
694 |
+
st.info(f"Current Status: {status}")
|
695 |
+
|
696 |
+
# Show repository details
|
697 |
+
if st.checkbox("Show Repository Details"):
|
698 |
+
try:
|
699 |
+
repo = Repo(str(Path(REPO_PATH)))
|
700 |
+
st.write("Repository Information:")
|
701 |
+
st.code(f"""
|
702 |
+
Path: {REPO_PATH}
|
703 |
+
Remote URL: {list(repo.remote('origin').urls)[0]}
|
704 |
+
Current Branch: {repo.active_branch.name}
|
705 |
+
Git Status: {repo.git.status()}
|
706 |
+
""")
|
707 |
+
except Exception as e:
|
708 |
+
st.error(f"Error accessing repository: {str(e)}")
|
709 |
+
|
710 |
# System information
|
711 |
st.markdown("""
|
712 |
<div style="background-color: #F9FAFB; padding: 1.5rem; border-radius: 12px; margin-bottom: 2rem;">
|