File size: 6,087 Bytes
2875866
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import streamlit as st
import json
import os
from datetime import datetime
import pandas as pd

PROJECTS_DIR = "projects"
DATASET_DIR = "dataset"

# Ensure projects directory exists
if not os.path.exists(PROJECTS_DIR):
    os.makedirs(PROJECTS_DIR)

# Helper Functions
def list_projects():
    return [f.replace(".json", "") for f in os.listdir(PROJECTS_DIR) if f.endswith(".json")]

def load_project(project_name):
    file_path = os.path.join(PROJECTS_DIR, f"{project_name}.json")
    if os.path.exists(file_path):
        with open(file_path, "r") as f:
            return json.load(f)
    return None

def save_project(project_name, data):
    file_path = os.path.join(PROJECTS_DIR, f"{project_name}.json")
    with open(file_path, "w") as f:
        json.dump(data, f, indent=4)

def initialize_project(project_name):
    data = {
        "project_name": project_name,
        "assertions": {"deterministic": [], "misc": [], "factual": "", "sql-only": False},
        "log_history": [],
        "accuracy_history": []
    }
    save_project(project_name, data)
    return data

# Streamlit UI
st.set_page_config(page_title="ValidLM Project Manager", layout="wide")
st.sidebar.title("πŸ“ Project Manager")

# Sidebar - Project Management
project_action = st.sidebar.selectbox("Select Action", ["Create New Project", "Open Existing Project"])

if project_action == "Create New Project":
    new_project_name = st.sidebar.text_input("Enter Project Name")
    if st.sidebar.button("Create Project") and new_project_name:
        if new_project_name in list_projects():
            st.sidebar.error("Project with this name already exists.")
        else:
            project_data = initialize_project(new_project_name)
            st.session_state["current_project"] = project_data
            st.success(f"Project '{new_project_name}' created successfully!")

elif project_action == "Open Existing Project":
    existing_projects = list_projects()
    selected_project = st.sidebar.selectbox("Select Project", existing_projects)
    if st.sidebar.button("Open Project") and selected_project:
        project_data = load_project(selected_project)
        if project_data:
            st.session_state["current_project"] = project_data
        else:
            st.sidebar.error("Failed to load project.")

# Main Content
if "current_project" in st.session_state:
    project = st.session_state["current_project"]

    st.title(f"πŸ“Š Project: {project['project_name']}")

    # Assertions Section
    st.header("Add new assertions")
    assertion_type = st.selectbox("Assertion Type", ["deterministic", 'factual', "misc"])

    if assertion_type == "deterministic":
        check_type = st.selectbox("Select Deterministic Check Type", ["regex", "json_format", "contains", "not-contains"])
        check_value = st.text_area("Enter pattern")
        if st.button("Add Deterministic Assertion") and check_value:
            assertion_data = {
                "check_type": check_type,
                "value": check_value,
            }
            project["assertions"]["deterministic"].append(assertion_data)
            save_project(project["project_name"], project)
            st.success("Deterministic Assertion added.")

    elif assertion_type == "factual":
        fact = st.file_uploader("Provide knowledgebase for factual assertion", type=["pdf", "docx"])
        if st.button("Add") and fact:
            project_id = project["project_name"]
            file_extension = os.path.splitext(fact.name)[1]
            # current working dir
            saved_path = os.path.join(os.getcwd(), DATASET_DIR, f"{project_id}{file_extension}")
            with open(saved_path, "wb") as f:
                f.write(fact.getbuffer())
            project["assertions"]["knowledgebase"] = saved_path
            st.success("Factual Assertion added and file saved.")

    elif assertion_type == "misc":
        new_assertion = st.text_input("Add Miscellaneous Assertion")
        if st.button("Add Miscellaneous Assertion") and new_assertion:
            project["assertions"]["misc"].append(new_assertion)
            save_project(project["project_name"], project)
            st.success("Miscellaneous Assertion added.")

    st.subheader("Current Assertions")
    for a_type, assertions in project["assertions"].items():
        if (a_type == 'factual' or a_type == "sql-only"):
            st.write(f"**{a_type.capitalize()}: {assertions}**")
            continue
        st.write(f"**{a_type.capitalize()} Assertions:**" if len(assertions) > 0 else "") 
        for assertion in assertions:
            st.write(f"- {assertion}")

    # Log History
    st.header("πŸ“ Application Log History")
    if project["log_history"]:
        log_df = pd.DataFrame(project["log_history"], columns=["Timestamp", "Event"])
        st.dataframe(log_df)
    else:
        st.write("No logs available.")

    # Accuracy History
    st.header("πŸ“ˆ Accuracy History")
    if project["accuracy_history"]:
        acc_df = pd.DataFrame(project["accuracy_history"], columns=["Timestamp", "Accuracy"])
        st.line_chart(acc_df.set_index("Timestamp"))
    else:
        st.write("No accuracy data available.")

    # Simulate Log & Accuracy Updates
    if st.button("Simulate Log Entry"):
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        project["log_history"].append([timestamp, "Sample log event."])
        save_project(project["project_name"], project)
        st.experimental_rerun()

    if st.button("Simulate Accuracy Update"):
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        accuracy = round(50 + 50 * (os.urandom(1)[0] / 255), 2)
        project["accuracy_history"].append([timestamp, accuracy])
        save_project(project["project_name"], project)
        st.experimental_rerun()
else:
    st.title("πŸ” No Project Selected")
    st.write("Please create or open a project from the sidebar.")