Community Cloud - Long Running functions & Buffer

Hello everyone,

I have a smoothly running private app on streamlit which I deployed yesterday evening. My issue is the following. I use the OpenAi API to do some pdf extraction, since the PDFs are a bit longer this takes it time. Everything works great with the secrets. The issue is in the runtime, and prints.

This is the code:

    model_button = st.button("PDF Extractor")

    if model_button:
        if st.session_state.price is None:
            st.write("🚫🚫🚫 Please set Price! 🚫🚫🚫 ")
            pass
        else:
            st.write("Model is used, please wait!") # --> This only shows for one instant and then 
                                                                                disappears 
            MODEL = "gpt-4-1106-preview"

            try:
                if uploaded_file:
                              ....
                print("here start model")
                sys.stdout.flush()
                st.session_state.mapped_dict  =\
                    GPT_pipe_parallel(path,
                                      st.secrets["open_ai"]["api_key"],
                                      selected_option,
                                      MODEL,
                                      st.session_state.price)
                print("model finished)
                sys.stdout.flush()

                st.write("βœ…βœ…βœ… Model finished! βœ…βœ…βœ…")

            except Exception as e:
                print(e)
                print("Error")
                st.write("An error occurred:", e)

Image of the log:

As you can see in the image the model stars as planned with the print model β€œstarts here”. Somewhere above in the code I print the price + sys.stoudt.flush(), but as you can see it prints it over and over in the time the GPT_pipe_parallel function runs. In my understanding the code gets rerun from the top, it passes the price printing statement and then the price is printed. Since my button is not in a session state the code does not run anymore through the if statement. Here 6 is the end of the GPT_pipe_parallel function right above the return line.
In the offline version the entire code waits until the function finished running and proceeds to the next line. How would I replicate this behavior or achieve it??

in general the deployed behavior varies quite extensively from the local one, is there a way to make it more local like

down here the required information:

  1. Are you running your app locally or is it deployed?
    Both (working locally) β†’ Big issues on the cloud
  2. If your app is deployed:
    a. Is it deployed on Community Cloud or another hosting platform?
    Community cloud
    b. Share the link to the public deployed app.
    No it is private
  3. Share the full text of the error message (not a screenshot).
    There are no real errors
  4. Share the Streamlit and Python versions.
    streamlit==1.30.0, Python 3.9.13

I hope this is understandable, I am rather new to the deploying of streamlit apps. Any help is much appreciated, since the topic is rather urgent!

I have added the button into a session state and tried to put the entire code into a spinner. Neither of this worked!

Any help is much appreciated