Skip to content

Commit eb1e99b

Browse files
committed
fix returned feedback from st_feedback()
1 parent 84bbfdc commit eb1e99b

File tree

3 files changed

+61
-32
lines changed

3 files changed

+61
-32
lines changed

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pytest-cov>=3.0.0
2424

2525
#extras (Note: in setup.cfg)
2626
streamlit>=1.21.0
27-
streamlit-feedback>=0.1.0
27+
streamlit-feedback==0.1.1
2828
numpy>=1.21.6
2929
pandas>=1.3.5
3030
scikit-learn>=1.0.0,<1.1.0

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ profile = black
3030

3131
# integrations
3232
[options.extras_require]
33-
streamlit = streamlit>=1.20.0; streamlit-feedback>=0.1.0
33+
streamlit = streamlit>=1.20.0; streamlit-feedback==0.1.1

trubrics/integrations/streamlit/collect.py

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ def st_feedback(
4040
prompt_id: Optional[str] = None,
4141
tags: list = [],
4242
metadata: dict = {},
43-
user_response: Optional[dict] = None,
4443
user_id: Optional[str] = None,
4544
key: Optional[str] = None,
4645
open_feedback_label: Optional[str] = None,
4746
save_to_trubrics: bool = True,
4847
align: str = "flex-end",
49-
single_submit: bool = True,
48+
disable_with_score: Optional[str] = None,
5049
success_fail_message: bool = True,
5150
) -> Optional[dict]:
5251
"""
@@ -64,51 +63,78 @@ def st_feedback(
6463
tags: a list of tags for the feedback
6564
metadata: any data to save with the feedback
6665
user_id: an optional reference to a user, for example a username if there is a login, a cookie ID, etc
67-
key: a key for the streamlit components (necessary if calling this method multiple times with the same type)
66+
key: a key for the streamlit components (necessary if calling this method multiple times)
6867
open_feedback_label: label of optional text_input for "faces" or "thumbs" feedback_type
6968
save_to_trubrics: whether to save the feedback to Trubrics, or just to return the feedback object
69+
disable_with_score: an optional score to disable the component. Must be a "thumbs" emoji or a "faces" emoji.
70+
Can be used to pass state from one component to another.
7071
align: where to align the feedback component ["flex-end", "center", "flex-start"]
71-
single_submit: disables re-submission. This prevents users re-submitting feedback for a given prediction
72-
e.g. for a chatbot.
73-
success_fail_message: whether to display a st.success / st.error message on feedback submission.
72+
success_fail_message: whether to display an st.toast message on feedback submission.
7473
"""
7574
if key is None:
7675
key = feedback_type
7776
if feedback_type == "textbox":
7877
text = self.st_textbox_ui(key, label=open_feedback_label)
7978
if text:
8079
user_response = {"type": feedback_type, "score": None, "text": text}
80+
if save_to_trubrics:
81+
feedback = self.log_feedback(
82+
component=component,
83+
user_response=user_response,
84+
model=model,
85+
prompt_id=prompt_id,
86+
metadata=metadata,
87+
tags=tags,
88+
user_id=user_id,
89+
)
90+
if feedback is None:
91+
error_msg = "Error in pushing feedback issue to Trubrics."
92+
if success_fail_message:
93+
st.error(error_msg)
94+
else:
95+
if success_fail_message:
96+
st.success("Feedback saved to Trubrics.")
97+
return feedback.model_dump()
98+
else:
99+
user_response = Response(**user_response)
100+
feedback = Feedback(
101+
component=component,
102+
model=model,
103+
user_response=user_response,
104+
prompt_id=prompt_id,
105+
user_id=user_id,
106+
tags=tags,
107+
metadata=metadata,
108+
)
109+
return feedback.model_dump()
81110
elif feedback_type in ("thumbs", "faces"):
111+
112+
def _log_feedback_trubrics(user_response, **kwargs):
113+
feedback = self.log_feedback(user_response=user_response, **kwargs)
114+
if success_fail_message:
115+
if feedback:
116+
st.toast("Feedback saved to [Trubrics](https://trubrics.streamlit.app/).", icon="✅")
117+
return feedback.model_dump()
118+
else:
119+
st.toast("Error in saving feedback to [Trubrics](https://trubrics.streamlit.app/).", icon="❌")
120+
82121
user_response = streamlit_feedback(
83122
feedback_type=feedback_type,
84123
optional_text_label=open_feedback_label,
85-
single_submit=single_submit,
124+
disable_with_score=disable_with_score,
125+
on_submit=_log_feedback_trubrics if save_to_trubrics else None,
126+
kwargs={
127+
"component": component,
128+
"model": model,
129+
"prompt_id": prompt_id,
130+
"metadata": metadata,
131+
"tags": tags,
132+
"user_id": user_id,
133+
},
86134
align=align,
87135
key=key,
88136
)
89-
else:
90-
raise ValueError("feedback_type must be one of ['textbox', 'faces', 'thumbs'].")
91-
92-
if user_response:
93-
if save_to_trubrics:
94-
feedback = self.log_feedback(
95-
component=component,
96-
user_response=user_response,
97-
model=model,
98-
prompt_id=prompt_id,
99-
metadata=metadata,
100-
tags=tags,
101-
user_id=user_id,
102-
)
103-
if feedback is None:
104-
error_msg = "Error in pushing feedback issue to Trubrics."
105-
if success_fail_message:
106-
st.error(error_msg)
107-
else:
108-
if success_fail_message:
109-
st.success("Feedback saved to Trubrics.")
110-
return feedback.model_dump()
111-
else:
137+
if save_to_trubrics is False and user_response:
112138
user_response = Response(**user_response)
113139
feedback = Feedback(
114140
component=component,
@@ -120,6 +146,9 @@ def st_feedback(
120146
metadata=metadata,
121147
)
122148
return feedback.model_dump()
149+
return user_response
150+
else:
151+
raise ValueError("feedback_type must be one of ['textbox', 'faces', 'thumbs'].")
123152
return None
124153

125154
@staticmethod

0 commit comments

Comments
 (0)