File size: 2,391 Bytes
44ce708
2658964
 
 
 
44ce708
2658964
 
 
44ce708
2658964
44ce708
2658964
 
 
 
 
 
 
44ce708
2658964
 
 
 
 
 
 
 
44ce708
2658964
 
 
 
 
44ce708
2658964
 
 
 
44ce708
 
2658964
 
 
 
 
44ce708
2658964
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import streamlit_authenticator as stauth
from streamlit_authenticator import Authenticate
from herbal_expert import herbal_expert
import yaml

def authentication_page():
    with open('creds.yaml') as f:
        creds = yaml.load(f, Loader=yaml.loader.SafeLoader)

    print("creds: ", creds)

    authenticator = Authenticate(
        creds['credentials'],
        creds['cookie']['name'],
        creds['cookie']['key'],
        creds['cookie']['expiry_days'],
        creds['preauthorized']
    )

    name, authentication_status, username = authenticator.login('Login', 'main')
    print("name: ", name)
    print("authentication_status: ", authentication_status)
    print("username: ", username)
    if authentication_status:
        authenticator.logout('Logout', 'main')
    if st.session_state["authentication_status"]:
        st.session_state.is_authenticated = True

def chatbot_page():
    st.title("Herbal Expert Chatbot")
    # Store LLM generated responses
    if "messages" not in st.session_state.keys():
        st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]

    # Display chat messages
    for message in st.session_state.messages:
        with st.chat_message(message["role"]):
            st.write(message["content"])


    # Function for generating LLM response
    def generate_response(prompt_input):
        print(st.session_state.messages)
        response = herbal_expert.query_expert(prompt_input)
        return response['response']


    # User-provided prompt
    if prompt := st.chat_input():
        st.session_state.messages.append({"role": "user", "content": prompt})
        with st.chat_message("user"):
            st.write(prompt)

    # Generate a new response if last message is not from assistant
    if st.session_state.messages[-1]["role"] != "assistant":
        with st.chat_message("assistant"):
            with st.spinner("Thinking..."):
                response = generate_response(prompt)
                st.write(response)
        message = {"role": "assistant", "content": response}
        st.session_state.messages.append(message)

if __name__ == "__main__":
    st.session_state.is_authenticated = False
    authentication_page()

    # Check if the user is authenticated before displaying the chatbot page
    if st.session_state.is_authenticated:
        chatbot_page()