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")
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()
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.