File size: 3,281 Bytes
60e4d0e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
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!")
|