Different Button behaviour on community cloud and locally

Hey everybody,
I have a streamlit app deployed on the cloud using the OpenAi API. My problem is that when I press the button it sometimes triggers the API call once sometimes twice. My question now is what brings the app to reload? I observe this behaviour with printing statements in the log.

This is my code:
temp_button_AI = st.button(“KI Model anwenden”)

    if temp_button_AI:
        st.session_state.AI_button = True

    if st.session_state.AI_button:
         run_combined_gpt(uploaded_file, selected_option, MODEL='gpt-4-1106-preview', add_on=300)

def run_combined_gpt(uploaded_file, selected_option, MODEL, add_on=300):
    if not validate_input():
        return

    try:
        with st.spinner('Model running, please wait ... !'):

            path = process_file(uploaded_file)
            description_text, mapped_dict = parallel_gpt_tasks(path, selected_option, MODEL, add_on=add_on)
            print("finished")
            sys.stdout.flush()
            st.session_state.description_text = description_text
            st.session_state.mapped_dict = mapped_dict

            st.success('✅✅✅ Model used! ✅✅✅')
            st.session_state.AI_button = False
    except Exception as e:
        st.write("An error occurred:", e)

Whithout the session state I have the issue that the app sometimes doesn’t go in the if statement.

Locally the button like this works, it just goes as planned line to line without being reloaded

The application is rather pricey with GPT. So if the button triggers it multiple times it gets super expensive.

Any help is welcomed!

  1. Share the link to the public deployed app.
    → Private
  2. Share the link to your app’s public GitHub repository (including a requirements file).
  3. Share the full text of the error message (not a screenshot).
    → No Error
  4. Share the Streamlit and Python versions.