Implementing Human in the loop to decide whether to continue

Here is my code, but basically I have an application that uses a wizard and creates a prompt.
Then it gets the information back from the LLM and I want to verify with the user that what was returned is what they want, or do they want to make changes in the wizard to change the prompt, and resubmit.
The application generates a recipe, then will create a story, images and create a document with all of this laid out. I want them to verify the recipe is what they want, for example, it may return an ingredient they didn’t realize they wan to exclude and get a new recipe.
So, below I generate a random number. When you type to continue the displayed random number shouldn’t change, but it does.
I put delays in to give the user time to type yes and to simulate calling the LLM.
Is there any way to allow human in the loop or is this something Streamlit can’t do and I should just look for a different approach.
It may be that I should just put the random value into a session state and then when it reloads look for the session state and if it is there then somehow decide that they wanted to continue, but I don’t think I can even get the value of cont_choice and put that into a session state either.

My initial plan was to use a button, as I assumed that when a button is clicked it would fire an event and do the action, but that doesn’t happen in streamlit so I tried a textfield as I wanted something that may block.

import streamlit as st
import time
import random

st.title = "human in loop example"
done = False
if done == False:
    r = random.random()
    st.write(f"random value is  {r}")
    time.sleep(10)

    cont_choice = st.text_input("type yes to continue")
    time.sleep(20)
    if cont_choice == 'yes':
        done = True
        st.write("continue with rest of app")
1 Like

Is this the kind of thing you are looking for ?

import streamlit as st
import time
import random


def main():
    st.title("Human in the Loop Example")

    # Initialize stage if this is the first run
    if st.session_state.get('stage') is None:
        st.session_state.stage = "generate"

    if st.session_state.stage == "generate":
        # Generate data
        with st.spinner("Generating data..."):
            time.sleep(10)  # Simulate a long-running task
        # Set the generated data in session state
        st.session_state.data = random.random()

        # Now that the data is generated, we move to the review stage
        st.session_state.stage = "review"
        st.rerun()

    elif st.session_state.stage == "review":
        # Review data
        st.write("Review the generated data:")
        st.write(st.session_state.data)

        # User can approve or reject the data
        if st.button("Approve"):
            st.success("Data approved!")
            st.session_state.stage = "complete"
            st.rerun()
        elif st.button("Reject"):
            st.error("Data rejected. Regenerating...")
            st.session_state.stage = "generate"
            st.rerun()

    elif st.session_state.stage == "complete":
        # Completion stage
        st.write("Process complete!")
        st.write("Final data:", st.session_state.data)
        st.write("Continue with the rest of the app")

if __name__ == '__main__':
    main()

This may be what I wanted. Never saw st.rerun actually.
Thanks