CoolFace
Apppublic

daniilf/llm_tb_testing_ui

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py170 linesDownload Raw Back to root
1import streamlit as st2import os3import random4import importlib.util5import firebase_admin6from firebase_admin import credentials, firestore7import json8 9 10# Set page configuration as the first command11st.set_page_config(12    page_title="TB Chatbot Evaluation",13    page_icon="๐Ÿ‘‹",14)15 16 17PASSCODE = os.environ["MY_PASSCODE"]18creds_dict = {19    "type": os.environ.get("FIREBASE_TYPE", "service_account"),20    "project_id": os.environ.get("FIREBASE_PROJECT_ID"),21    "private_key_id": os.environ.get("FIREBASE_PRIVATE_KEY_ID"),22    "private_key": os.environ.get("FIREBASE_PRIVATE_KEY", "").replace("\\n", "\n"),23    "client_email": os.environ.get("FIREBASE_CLIENT_EMAIL"),24    "client_id": os.environ.get("FIREBASE_CLIENT_ID"),25    "auth_uri": os.environ.get("FIREBASE_AUTH_URI", "https://accounts.google.com/o/oauth2/auth"),26    "token_uri": os.environ.get("FIREBASE_TOKEN_URI", "https://oauth2.googleapis.com/token"),27    "auth_provider_x509_cert_url": os.environ.get("FIREBASE_AUTH_PROVIDER_X509_CERT_URL", 28                                                "https://www.googleapis.com/oauth2/v1/certs"),29    "client_x509_cert_url": os.environ.get("FIREBASE_CLIENT_X509_CERT_URL"),30    "universe_domain": "googleapis.com"31 32}33# Create a temporary JSON file34file_path  = "coco-evaluation-firebase-adminsdk-p3m64-99c4ea22c1.json"35with open(file_path, 'w') as json_file:36    json.dump(creds_dict, json_file, indent=2)37 38# Initialize Firebase39if not firebase_admin._apps:40    cred = credentials.Certificate("coco-evaluation-firebase-adminsdk-p3m64-99c4ea22c1.json")41    firebase_admin.initialize_app(cred)42db = firestore.client()43 44 45 46# Set passcode for authentication47PASSCODE = os.environ["MY_PASSCODE"]48 49# Initialize authentication state50if "authenticated" not in st.session_state:51    st.session_state["authenticated"] = False52 53 54# Initialize session state variables55def init_session_state():56    if "authenticated" not in st.session_state:57        st.session_state["authenticated"] = False58    if "evaluator_confirmed" not in st.session_state:59        st.session_state["evaluator_confirmed"] = None60    if "model_order" not in st.session_state:61        st.session_state["model_order"] = []62    if "current_index" not in st.session_state:63        st.session_state["current_index"] = 064    if "models_completed" not in st.session_state:65        st.session_state["models_completed"] = False66    if "evaluation_status" not in st.session_state or not st.session_state["evaluation_status"]:67        st.session_state["evaluation_status"] = {}68    if "all_evaluations" not in st.session_state:69        st.session_state["all_evaluations"] = {}70    if "evaluation_ids" not in st.session_state:71        st.session_state["evaluation_ids"] = {}72    if "start_time" not in st.session_state:73        st.session_state["start_time"] = None74    if "evaluation_durations" not in st.session_state:75        st.session_state["evaluation_durations"] = {}76    if "submitted_evaluations" not in st.session_state:77        st.session_state["submitted_evaluations"] = set()78 79 80init_session_state()81 82 83# Display Welcome Page84if not st.session_state["authenticated"]:85    st.markdown(f"<h1 style='text-align: center;'>Welcome to the TB Chatbot Evaluation</h1>", unsafe_allow_html=True)86    87    col1, col2, col3 = st.columns([2, 1, 1])88    with col1:89        st.write("Are you an evaluator?")90    with col2:91        if st.button("Yes"):92            st.session_state["evaluator_confirmed"] = True93    with col3:94        if st.button("No"):95            st.session_state["evaluator_confirmed"] = False96 97    if st.session_state["evaluator_confirmed"]:98        evaluator_id = st.text_input("Enter your Evaluator ID (can be anything)")99        passcode = st.text_input("Enter Passcode to Access Models (password is the same)", type="password")100        if st.button("Submit"):101            print("Hello")102            if passcode == PASSCODE and evaluator_id:103                print("Submitted")104                # Save Evaluator ID105                db.collection("evaluator_ids").document(evaluator_id).set({106                    "evaluator_id": evaluator_id,107                    "timestamp": firestore.SERVER_TIMESTAMP108                })109 110                # Update session state111                st.session_state["authenticated"] = True112                st.session_state["evaluator_id"] = evaluator_id113 114 115# Show the main content only if authenticated116if st.session_state["authenticated"]:117    # Sidebar with only randomized page navigation118    with st.sidebar:119        # Only create the page mapping if it doesn't already exist in the session state120        if "page_mapping" not in st.session_state:121            # Get list of page files and randomize their order122            PAGES_DIR = "pages"123            page_files = [f for f in os.listdir(PAGES_DIR) if f.endswith(".py")]124            random.shuffle(page_files)125            126            # Create generic names for display127            generic_names = [f"Model {chr(65 + i)}" for i in range(len(page_files))]128            129            # Create a dictionary to map display names to file paths130            st.session_state["page_mapping"] = {131                generic_name: os.path.join(PAGES_DIR, page_file) for generic_name, page_file in zip(generic_names, page_files)132            }133        134        # Retrieve the consistent mapping from session state135        pages = st.session_state["page_mapping"]136        137        # Sidebar navigation selectbox with generic labels138        selected_generic_name = st.selectbox("Navigation", list(pages.keys()), label_visibility="collapsed")139 140    # Load and run the selected page dynamically using the selected generic name141    selected_page_path = pages[selected_generic_name]142    spec = importlib.util.spec_from_file_location(selected_generic_name, selected_page_path)143    module = importlib.util.module_from_spec(spec)144    spec.loader.exec_module(module)145 146    # Display main welcome message147    st.markdown("""148        # Welcome to the TB Chatbot Simulation Portal  149 150        This portal allows you to interact with our TB-powered chatbot, built using OpenAI's GPT-4. Here, you can evaluate multiple chatbot configurations, each designed to foster trust, empathy, and medical accuracy in responses.  151 152        ## Purpose  153 154        Your task is to assess the chatbot models by interacting with them using patient scenario-based questions. Evaluate each model based on the following principles:  155 156        ### Trust  157        - Does the response convey confidence and reliability?  158        - Are the answers factually accurate and rooted in medical expertise?  159                160        ### Medical Accuracy  161        - Are responses aligned with current medical guidelines and best practices?  162        - Do they avoid misinformation or potentially harmful suggestions?  163 164        ### Empathy  165        - Does the chatbot demonstrate understanding and compassion?  166        - Are responses emotionally sensitive and supportive?  167 168        Your feedback will help us identify the best model to support our mission of enhancing patient communication and care through AI-driven solutions.  169        """)170