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!")