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

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

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?

import pandas as pd
import streamlit as st

df = pd.read_pickle("unlabeled.pkl")
records = df.to_dict("records")

if "annotations" not in st.session_state:
    st.session_state.records = records
    st.session_state.current_record = records[0]

annotated_data = list()

if st.session_state.records:
    labels = st.session_state.current_record["labels"]
    example = st.session_state.current_record["example"]
    text = st.session_state.current_record["text"]

    demo = "\n".join(["- {}".format(ee) for ee in example])
    text = "- {}".format(text)

    st.write(f"# Example\n{demo}\n# Output\n{text}")

    labels = st.multiselect(
        label="Select Labels",
        options=labels
    )

    st.write('You Selected:', labels)

    if st.button("Save"):
        st.session_state.records.remove(st.session_state.current_record)
        st.session_state.current_record = st.session_state.records[0]

    annotated_data.append(
        {
            **st.session_state.current_record,
            "label": labels
        }
    )

    if len(annotated_data) % 50 == 0:
        save_data(annotated_data)

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:

确定