无法使用 `streamlit` 对包含多个标签的数据集进行标注。

huangapple go评论135阅读模式
英文:

Could not use `streamlit` to annotate a dataset of multiple labels

问题

I am trying to build an annotation interface using streamlit.

In my dataset, each data point may have multiple labels (i.e. labels in the code below). However, I could only select one label using st.multiselect() rather than the expected "multiple select". Specifically, every time I click the one of the choices, the page will be updated and the next data point pops up.

I am not sure what went wrong after getting stuck in this for hours. Could anyone provide any pointers for me?

英文:

I am trying to build an annotation interface using streamlit.

In my dataset, each data point may have multiple labels (i.e. labels in the code below). However, I could only select one label using st.multiselect() rather than the expected "multiple select". Specifically, every time I click the one of the choices, the page will be updated and the next data point pops up.

I am not sure what went wrong after getting stuck in this for hours. Could anyone provide any pointers for me?

  1. import pandas as pd
  2. import streamlit as st
  3. df = pd.read_pickle("unlabeled.pkl")
  4. records = df.to_dict("records")
  5. if "annotations" not in st.session_state:
  6. st.session_state.records = records
  7. st.session_state.current_record = records[0]
  8. annotated_data = list()
  9. if st.session_state.records:
  10. labels = st.session_state.current_record["labels"]
  11. example = st.session_state.current_record["example"]
  12. text = st.session_state.current_record["text"]
  13. demo = "\n".join(["- {}".format(ee) for ee in example])
  14. text = "- {}".format(text)
  15. st.write(f"# Example\n{demo}\n# Output\n{text}")
  16. labels = st.multiselect(
  17. label="Select Labels",
  18. options=labels
  19. )
  20. st.write('You Selected:', labels)
  21. if st.button("Save"):
  22. st.session_state.records.remove(st.session_state.current_record)
  23. st.session_state.current_record = st.session_state.records[0]
  24. annotated_data.append(
  25. {
  26. **st.session_state.current_record,
  27. "label": labels
  28. }
  29. )
  30. if len(annotated_data) % 50 == 0:
  31. save_data(annotated_data)
  32. save_data(annotated_data)

答案1

得分: 1

你可以使用 一个表单 来防止应用程序在与多选小部件交互后重新运行(相反,应用程序不会重新运行,直到你点击 "提交")。

英文:

You can use a form to prevent the app from rerunning after interacting with the multiselect widget (instead, the app won't rerun until you hit "Submit").

huangapple
  • 本文由 发表于 2023年2月6日 14:44:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定