File size: 3,851 Bytes
d934e05
 
47d1763
 
1e508c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47d1763
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1979a9d
47d1763
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8dc34d0
d934e05
 
 
 
 
 
 
 
1e508c0
 
 
2c5b1e7
1e508c0
d934e05
 
 
f8409b4
47d1763
f8409b4
 
33de646
 
 
 
47d1763
33de646
 
 
d934e05
 
 
 
 
 
1e508c0
d934e05
 
 
 
 
 
47d1763
d934e05
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
import streamlit as st

from src.common import *


def login(pw: str) -> bool:
    """
    Convenience wrapper for login functionality.  Has side effect of storing the logged in
    user name in the session state
    :param pw: Password to attempt login
    :return: True/False if logged in
    """
    pw_users = st.secrets['app_password'].split(';')
    for pu in pw_users:
        split_point=pu.find('(')
        pu_password = pu[0:split_point]
        if pu_password == pw:
            pu_user = pu[split_point + 1: -1]
            st.session_state['username'] = pu_user
            return True
    return False


def show_system_status_bar() -> None:
    endpoints = st.secrets['endpoints'].split(',')
    num_endpoints = len(endpoints)
    running = []
    stopped = []
    for e in endpoints:
        status = hf_endpoint_status('alfraser', e)
        if status == HF_RUNNING:
            running.append(e)
        else:
            stopped.append(f'{e} ({status})')

    if len(running) == num_endpoints:
        message = "**:large_green_circle: System Status - All LLM Endpoints Running**"
    elif len(stopped) == num_endpoints:
        message = "**:red_circle: System Status - No LLM Endpoints Running**"
    else:
        message = "**:large_orange_circle: System Status - Some LLM Endpoints Running**"

    with st.expander(message):
        if len(running) == num_endpoints:
            st.success("All LLM endpoints running")
            st.write("**Note** - endpoints shutdown after 15 minutes of inactivity")
        else:
            st.info(f"{len(running)} of {num_endpoints} LLM endpoints running")
            all = []
            for e in running:
                all.append(f'* :large_green_circle: {e}')
            for e in stopped:
                all.append(f'* :red_circle: {e}')
            st.write('\n'.join(all))
            if st.button("Start all endpoints"):
                for e in stopped:
                    resume_hf_endpoint('alfraser', e)
                st.write(
                    '**Note** - endpoint start may take multiple minutes, so refresh and check status periodically')


def st_setup(page_title: str, layout: str = 'wide', skip_login: bool = False) -> None:
    """
    Sets up standard outline (wide layout), checks for logged in status and then
    displays the login box if not logged in.  Should be used as a conditional to display
    the other content on the page.
    :param page_title: The streamlit page title for this page
    :return: bool - logged in status
    """
    st.set_page_config(page_title=page_title, layout=layout)

    username='username'
    if username in st.session_state:
        st.markdown(f'<div style="float: right; padding-top: 10px;">Logged in as <b>{st.session_state[username]}</b></span>', unsafe_allow_html=True)

    with st.sidebar:
        st.image('img/uob-logo.png', width=200)

    if skip_login:
        show_system_status_bar()
        return True

    # Option to add a running_local key to secrets.toml for local login to speed up development
    # This relies on the protection of streamlit secrets in production, which password login already relies on
    running_local = 'running_local'
    if running_local in st.secrets:
        show_system_status_bar()
        return True

    logged_in = 'logged_in'
    if logged_in not in st.session_state or st.session_state[logged_in]==False:
        _, c2, _ = st.columns(3)
        with c2:
            st.write('### Log in')
            pw = st.text_input(type='password', label='Password')
            if st.button('Log in'):
                if login(pw):
                    st.session_state[logged_in]=True
                    st.rerun()
                else:
                    st.info("Invalid password")
        return False
    else:
        show_system_status_bar()
        return True