chatbot_v2.1 / pages /admin_dashboard.py
NightPassenger's picture
Upload 8 files
60e4d0e verified
import streamlit as st
import pandas as pd
import json
import os
from qa_loader import load_qa_and_create_vectorstore
from langchain_chroma import Chroma
# πŸ”Ή Page Configuration
st.set_page_config(page_title="Admin Dashboard", layout="wide")
# πŸ“‚ Load available log files
log_dir = "logs"
log_files = [f for f in os.listdir(log_dir) if f.startswith("auto_test_results")]
# πŸ”Ή Read the latest log file
if log_files:
latest_log = sorted(log_files)[-1]
log_path = os.path.join(log_dir, latest_log)
with open(log_path, "r", encoding="utf-8") as f:
log_data = f.readlines()
else:
log_data = []
# πŸ“Š **Log Analysis**
st.title("πŸ“Š Admin Dashboard")
st.subheader("πŸ” AI Model Log Analysis")
if log_data:
correct_count = sum(1 for line in log_data if "βœ… Correct" in line)
incorrect_count = sum(1 for line in log_data if "❌ Incorrect" in line)
total_count = correct_count + incorrect_count
accuracy = (correct_count / total_count) * 100 if total_count > 0 else 0
st.metric("βœ… Correct Answers", correct_count)
st.metric("❌ Incorrect Answers", incorrect_count)
st.metric("🎯 Accuracy", f"{accuracy:.2f}%")
# **Most Frequently Asked Questions**
question_lines = [line for line in log_data if line.startswith("Q:")]
question_counts = pd.Series(question_lines).value_counts().head(10)
st.subheader("πŸ“Œ Most Frequently Asked Questions")
st.write(question_counts)
else:
st.warning("⚠️ No log records found. Please run the automated tests.")
# πŸ“š **Q&A Data Update**
st.subheader("πŸ“Œ Update Q&A Database")
# Load existing Q&A data
qa_file = "Q&A_cleaned.json"
if os.path.exists(qa_file):
with open(qa_file, "r", encoding="utf-8") as f:
qa_data = json.load(f)
else:
qa_data = []
# Add a new question
new_question = st.text_input("πŸ“ New Question:")
new_answer = st.text_area("πŸ“Œ Answer:")
if st.button("πŸ’Ύ Save"):
if new_question and new_answer:
qa_data.append({"QUESTION": new_question, "ANSWER": new_answer})
with open(qa_file, "w", encoding="utf-8") as f:
json.dump(qa_data, f, indent=4)
st.success("βœ… New question added!")
else:
st.warning("⚠️ Please enter both a question and an answer.")
# **Edit Incorrect Answers**
st.subheader("πŸ”„ Edit Incorrect Answers")
if qa_data:
question_list = [q["QUESTION"] for q in qa_data]
selected_question = st.selectbox("πŸ”Ž Select Question to Edit:", question_list)
selected_item = next((q for q in qa_data if q["QUESTION"] == selected_question), None)
updated_answer = st.text_area("✏️ Updated Answer:", selected_item["ANSWER"] if selected_item else "")
if st.button("πŸ“ Update"):
if selected_item:
selected_item["ANSWER"] = updated_answer
with open(qa_file, "w", encoding="utf-8") as f:
json.dump(qa_data, f, indent=4)
st.success("βœ… Answer updated!")
# **Update Vector Database**
st.subheader("πŸ”„ Update Vector Database")
if st.button("πŸ“₯ Re-train"):
retriever = load_qa_and_create_vectorstore()
st.success("βœ… Vector database successfully updated!")