I am trying to use streamlit feedback and save the user feedback to a log file but its not working properly in a st.form and i am always getting None in my feedback, any suggestions??
With st.form("main"):
generate_answer()
feedback = streamlit_feedback(
feedback_type="thumbs",
optional_text_label="[Optional] Please provide an explanation",
align="flex-start")
logging.info(f"feedback log -{feedback}")
import streamlit as st
from streamlit_feedback import streamlit_feedback
with st.form("main", clear_on_submit=True):
st.write('answer ...')
feedback = streamlit_feedback(
feedback_type="thumbs",
optional_text_label="[Optional] Please provide an explanation",
align="flex-start"
)
st.form_submit_button('save')
st.write(f"feedback log -{feedback}")
Thank you for the reply but my requirement entails for every generated result it should have a feedback saved but i guess a form cannot have two submit button?.
query = st.text_area("enter your query")
with st.form("main", clear_on_submit=True):
st.form_submit_button('generate answer')
generate_answer(query) ## which generates an answer as per query input
feedback = streamlit_feedback(
feedback_type="thumbs",
optional_text_label="[Optional] Please provide an explanation",
align="flex-start"
)
..... 2nd submit/save button will throw error!
st.write(f"feedback log -{feedback}")
Add form for query, user presses the query button and see the response. Show the feedback and record feedback’s reply.
import streamlit as st
from streamlit import session_state as ss
from streamlit_feedback import streamlit_feedback
if 'qr' not in ss:
ss.qr = None
def ai_response(query):
ss.qr = "Hey, what's up?"
with st.form("query_form_k", clear_on_submit=False):
query = st.text_area("Enter your query")
st.form_submit_button('Get response', on_click=ai_response, args=(query,))
with st.container(border=True):
st.write('Ai Response:')
if ss.qr is not None:
st.write(ss.qr)
with st.container(border=True):
st.write('Your feedback:')
feedback = streamlit_feedback(
feedback_type="thumbs",
optional_text_label="[Optional] Please provide an explanation",
align="flex-start"
)
if ss.qr is not None and feedback:
st.write(feedback)
One doubt: is your feedback also not getting cleared after submitting a new query?
Once a feedback is submitted, I don’t get an option to clear and submit new feedback for new ai reponse or a new query.
import streamlit as st
from streamlit import session_state as ss
from streamlit_feedback import streamlit_feedback
import uuid
# The key of feedback is ss.fbk. Its value is ss.[ss.fbk]
# We do this to get a new feedback prompt.
# We use uuid to generate unique key converted to str.
if 'fbk' not in ss:
ss.fbk = str(uuid.uuid4())
# Query response
if 'qr' not in ss:
ss.qr = ''
def reset_cb():
"""Reset callback for feedback, etc.
This can be used to save interactions.
"""
ss.fbk = str(uuid.uuid4()) # use new key to display new feedback prompt
ss.qt = '' # reset query text
ss.qr = '' # reset query response
def ai_response():
"""Gets query text and use it to build a response.
It assigns its response to the query response.
"""
res = "Hey, what's up?"
ss.qr = f"Your query is {ss.qt}. My response is: {res}"
# Build a form to receive user query input.
with st.form("query_form_k", clear_on_submit=False):
st.text_area("Enter your query", key='qt')
st.form_submit_button('Get response', on_click=ai_response)
# Build a container to display ai response.
with st.container(border=True):
st.write('Ai Response:')
if ss.qr is not None:
st.write(ss.qr)
# Manage the feedback.
with st.container(border=True):
st.write('Your feedback:')
streamlit_feedback(
feedback_type="thumbs",
optional_text_label="[Optional] Please provide an explanation",
align="flex-start",
key=ss.fbk
)
fb = ss[ss.fbk]
if ss.qr != '' and fb:
st.write(fb)
if st.button('Restart', on_click=reset_cb):
st.rerun()
Thanks. I slightly modified this to reduce the number of buttons. On submitting ‘Get_response’ i run reset_cb. This works fine except 1 last bug.
on submitting the feedback, since the app reruns. The ai_response get’s cleared,
Any solution to keep the content as is in the form on submitting the feedback? Since the ai_response function is expensive, i don’t want to rerun it on submitting feedback.
import streamlit as st
from streamlit import session_state as ss
from streamlit_feedback import streamlit_feedback
import uuid
if 'fbk' not in ss:
ss.fbk = str(uuid.uuid4())
def reset_cb():
"""Reset callback for feedback, etc.
This can be used to save interactions.
"""
ss.fbk = str(uuid.uuid4()) # use new key to display new feedback prompt
def ai_response():
"""Gets query text and use it to build a response.
It assigns its response to the query response.
"""
res = "Hey, what's up?"
ss.qr = f"My response is: {res}"
st.write(ss.qr)
# Build a form to receive user query input.
with st.form("query_form_k", clear_on_submit=False):
st.text_area("Enter your query", key='qt')
check = st.form_submit_button('Get response', on_click=reset_cb)
if check:
st.write('Ai Response:')
ai_response()
# Manage the feedback.
with st.container():
st.write('Your feedback:')
streamlit_feedback(
feedback_type="thumbs",
optional_text_label="[Optional] Please provide an explanation",
align="flex-start",
key=ss.fbk
)
# if st.button('Restart', on_click=reset_cb):
# st.rerun()
Comment setting the query and response boxes to empty.
def reset_cb():
"""Reset callback for feedback, etc.
This can be used to save interactions.
"""
ss.fbk = str(uuid.uuid4()) # use new key to display new feedback prompt
# ss.qt = '' # reset query text
# ss.qr = '' # reset query response
Call the reset_cb inside the ai_response
def ai_response():
"""Gets query text and use it to build a response.
It assigns its response to the query response.
"""
res = "Hey, what's up?"
ss.qr = f"Your query is {ss.qt}. My response is: {res}"
reset_cb() # Creates a new feedback prompt
Refactor the code if necessary including updating the docstrings.
The Get response button has now two functions. To get ai response and to generate a new feedback prompt.
Here’s the revised that should help ensure your feedback gets captured correctly:
import streamlit as st
import logging
logging.basicConfig(level=logging.INFO)
with st.form("main"):
generate_answer_placeholder = st.empty() # Placeholder for where you want to generate the answer
feedback = streamlit_feedback(
feedback_type="thumbs",
optional_text_label="[Optional] Please provide an explanation",
align="flex-start"
)
submitted = st.form_submit_button("Submit Feedback")
if submitted:
# Log feedback only if the form has been submitted
logging.info(f"Feedback log - {feedback}")
st.write(f"Feedback received: {feedback}") # Display feedback for confirmation
# Replace this with the logic to save feedback to a log file
# For example: save_feedback_to_log(feedback)
Hope this helps!
Kind Regards,
Sahir Maharaj
Data Scientist | AI Engineer
Thanks @sahirmaharaj but this will not work. Generate answer needs a button, submit feedback has a separate button. 2 buttons will not work within the same form.
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.