mkhorasani / Streamlit-Authenticator

A secure authentication module to validate user credentials in a Streamlit application.
Apache License 2.0
1.38k stars 229 forks source link

When using st.chat_input to enter a message, the message sometimes disappears immediately after input. #97

Closed yokei-dev closed 1 month ago

yokei-dev commented 7 months ago

Summary

When using st.chat_input to enter a message, the message sometimes disappears immediately after input.

Video

https://github.com/mkhorasani/Streamlit-Authenticator/assets/64062002/b9f3c76c-e8aa-435f-80b9-c5364f088d93

Investigation

This issue did not occur before the introduction of streamlit_authenticator, but it started happening after its integration.

Hypothesis

I suspect that something is causing a rerun after message input, which might be leading to the disappearance of the entered message.

Sample code

import streamlit as st
import openai
import os
import yaml
from yaml.loader import SafeLoader
import streamlit_authenticator as stauth

openai.api_key = os.environ["OPENAI_API_KEY"]

USER_NAME = "user"
ASSISTANT_NAME = "assistant"

with open('./config.yaml') as file:
    config = yaml.load(file, Loader=SafeLoader)

usernames = ["****"]
passwords = ["****"]

user_dict = {username: {'name': username, 'password': passwords} for username, passwords in zip(usernames, passwords)}
credentials = {'usernames': user_dict}

authenticator = stauth.Authenticate(
    credentials,
    config['cookie']['name'],
    config['cookie']['key'],
    config['cookie']['expiry_days'],
    config['preauthorized']
)

name, authentication_status, username = authenticator.login('Login', 'main')

def show_chat_log():
    ****
def chat_log_to_text():
    ****
def response_chatgpt():
    ****

def logged_in_page():

    user_message = st.chat_input("input your message")

    if user_message:

        show_chat_log()

        with st.chat_message(USER_NAME):
            st.write(user_message)

        response = response_chatgpt(
            user_message,
        )
        with st.chat_message(ASSISTANT_NAME):
            ****

if st.session_state["authentication_status"]:
    st.write(f'Welcome *{st.session_state["name"]}*')
    authenticator.logout('Logout', 'main', key='unique_key')
    logged_in_page()
elif st.session_state["authentication_status"] is False:
    st.error('Username/password is incorrect')
elif st.session_state["authentication_status"] is None:
    st.warning('Please enter your username and password')

@mkhorasani can you please help, would be greatly appreciated

mkhorasani commented 1 month ago

I will be closing this issue, and request that you kindly follow developments in #99.