Skip to content

Commit

Permalink
Merge pull request #81 from hamdan-27/textbox-option
Browse files Browse the repository at this point in the history
added text-area option for textbox feedback
  • Loading branch information
jeffkayne authored Sep 14, 2023
2 parents 404b924 + a8002b1 commit b92d64a
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions trubrics/integrations/streamlit/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def st_feedback(
component: str,
feedback_type: str,
model: str,
textbox_type: str = "text-input",
prompt_id: Optional[str] = None,
tags: list = [],
metadata: dict = {},
Expand All @@ -58,6 +59,7 @@ def st_feedback(
- textbox: open textbox feedback
- thumbs: 👍 / 👎 UI buttons
- faces: 😞 / 🙁 / 😐 / 🙂 / 😀 UI buttons
textbox_type: if textbox selected as feedback_type, the type of textbox to use ["text-input", "text-area"]
model: the model used - can be a model version, a link to the saved model artifact in cloud storage, etc
prompt_id: id of the prompt object
tags: a list of tags for the feedback
Expand All @@ -74,7 +76,7 @@ def st_feedback(
if key is None:
key = feedback_type
if feedback_type == "textbox":
text = self.st_textbox_ui(key, label=open_feedback_label)
text = self.st_textbox_ui(type=textbox_type, key=key, label=open_feedback_label)
if text:
user_response = {"type": feedback_type, "score": None, "text": text}
if save_to_trubrics:
Expand Down Expand Up @@ -160,11 +162,14 @@ def _pydantic_to_dict(feedback: Feedback) -> dict:
return feedback.dict()

@staticmethod
def st_textbox_ui(key: Optional[str] = None, label: Optional[str] = None) -> Optional[str]:
def st_textbox_ui(
type: str = "text-input", key: Optional[str] = None, label: Optional[str] = None
) -> Optional[str]:
"""
Trubrics 'textbox' UI component.
Args:
type: type of textbox to use (should be one of ['text-area', 'text-input'])
key: a key for the streamlit components (necessary if calling this method multiple times with the same type)
label: the textbox's streamlit label
"""
Expand All @@ -181,10 +186,17 @@ def clear_session_state():
st.session_state[f"previous_{key}_state"] = st.session_state[f"{key}_title"]
st.session_state[f"{key}_title"] = ""

title = st.text_input(
label=label or "Provide some feedback",
key=f"{key}_title",
)
if type not in ("text-input", "text-area"):
raise ValueError("textbox_type must be one of ['text-area', 'text-input'].")
else:
if type == "text-input":
title = st.text_input(
label=label or "Provide some feedback",
key=f"{key}_title",
)
elif type == "text-area":
title = st.text_area(label=label or "Provide some feedback", key=f"{key}_title")

if title:
st.button("Save feedback", on_click=clear_session_state, key=f"{key}_save_button")
if st.session_state[f"{key}_save_button"]:
Expand Down

1 comment on commit b92d64a

@hamdan-27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much! please notify when available on pypi :)

Please sign in to comment.