SonFox2920/FC_annotation_check
0
1import streamlit as st
2import pandas as pd
3
4st.set_page_config(layout="wide")
5
6# Load the dataset
7def load_data():
8 uploaded_file = st.sidebar.file_uploader("Upload CSV file", type=["csv"])
9 if uploaded_file is not None:
10 df = pd.read_csv(uploaded_file)
11 if 'Label_n' not in df.columns:
12 df['Label_n'] = None # Add a Label_n column if it doesn't exist
13 if 'Annotator' not in df.columns:
14 df['Annotator'] = None # Add an Annotator column if it doesn't exist
15 return df, uploaded_file.name
16
17# Save the annotations from session_state to DataFrame
18def save_annotations_to_df(df):
19 for idx, label in enumerate(st.session_state.selected_labels):
20 df.at[idx, 'Label_n'] = label
21 return df
22
23# Create the annotation app
24def annotation_app():
25 tab0, tab1, tab2 = st.tabs(["Mission", "Annotate", "Save"])
26
27 with tab0:
28 st.title("Nhiệm vụ")
29 st.write("""
30 Nhiệm vụ của bạn là gán nhãn cho các câu tuyên bố dựa trên ngữ cảnh được cung cấp trước đó. Có ba nhãn mà bạn cần phải chọn:
31 <span style='color:#7FFF00'>SUPPORTED</span> (Được hỗ trợ), <span style='color:#DC143C'>REFUTED</span> (Bị phủ nhận), hoặc <span style='color:#FFD700'>NEI</span> (Không đủ thông tin). Dưới đây là các bước để thực hiện nhiệm vụ này:
32
33 1. **Đọc ngữ cảnh (context)**: Hiểu rõ nội dung, thông tin của đoạn văn bản được cung cấp.
34
35 2. **Xem câu tuyên bố (claim)**: Dựa trên thông tin, nội dung của đoạn văn bản, bạn sẽ đánh giá câu tuyên bố.
36
37 3. **Phân loại câu tuyên bố**: Chọn nhãn phù hợp cho câu tuyên bố:
38 - <span style='color:#7FFF00'>SUPPORTED</span> (Được hỗ trợ): Khi câu tuyên bố là chính xác theo thông tin trong ngữ cảnh.
39 - <span style='color:#DC143C'>REFUTED</span> (Bị phủ nhận): Khi câu tuyên bố là sai theo thông tin trong ngữ cảnh.
40 - <span style='color:#FFD700'>NEI</span> (Không đủ thông tin): Khi thông tin của câu tuyên bố không thể xác nhận được đúng hay sai dựa trên ngữ cảnh.
41
42 4. **Lưu dữ liệu**: Sau khi đã chọn nhãn cho tất cả các câu tuyên bố trong tệp CSV, hãy lưu lại kết quả với thông tin người gán nhãn.
43
44 5. **Di chuyển giữa các câu tuyên bố**: Sử dụng các nút "Previous" và "Next" để di chuyển giữa các câu tuyên bố trong tệp.
45
46 Sau khi hoàn thành, bạn có thể lưu lại toàn bộ dữ liệu với tên của mình để xác nhận nhiệm vụ đã được hoàn thành.
47 """, unsafe_allow_html=True)
48
49 data = load_data()
50
51 if data is None:
52 st.sidebar.warning("Please upload a CSV file.")
53 st.stop()
54
55 df, original_filename = data
56
57 with tab1:
58 # Drop unnamed column if it's just an index
59 df = df.loc[:, ~df.columns.str.contains('^Unnamed')]
60
61 if 'current_index' not in st.session_state:
62 st.session_state.current_index = 0
63
64 if 'selected_labels' not in st.session_state:
65 # Initialize with current DataFrame values
66 st.session_state.selected_labels = list(df['Label_n'])
67
68 max_index = len(df) - 1
69 current_index = st.session_state.current_index
70 current_row = df.iloc[current_index]
71
72 context_col = 'Context'
73 claim_col = 'Claim'
74 label_col = 'Label_n'
75
76 c3 = st.container(border=True)
77 with c3:
78 left_column, right_column = st.columns([0.65, 0.35])
79
80 with left_column:
81 c3_1 = st.container(border=True, height=550)
82 with c3_1:
83 st.subheader("Context")
84 st.write(current_row[context_col])
85
86 with right_column:
87 c3_2 = st.container(border=True, height=450)
88 with c3_2:
89 st.subheader("Claim")
90 st.write(current_row[claim_col])
91
92 st.subheader("Label")
93 c3_3 = st.container(border=True, height=200)
94 with c3_3:
95 current_label = st.session_state.selected_labels[current_index]
96
97 selected_label = st.selectbox(
98 "Select the label for this claim",
99 options=('', 'SUPPORTED', 'REFUTED', 'NEI'), # Add an empty string as the first option
100 index=0 if pd.isna(current_label) else ['SUPPORTED', 'REFUTED', 'NEI'].index(current_label) + 1, # Adjust index to match options
101 format_func=lambda x: 'Select label for claim ...' if x == '' else x, # Format empty string as placeholder text
102 )
103
104 if selected_label:
105 st.session_state.selected_labels[current_index] = selected_label
106
107 # Add navigation buttons (Previous, Next)
108 previous, next_ = st.columns(2, gap='large')
109
110 with previous:
111 if st.button("Previous"):
112 if current_index > 0:
113 st.session_state.current_index = current_index - 1
114 else:
115 st.session_state.current_index = max_index
116 st.experimental_rerun()
117
118 with next_:
119 if st.button("Next"):
120 if selected_label:
121 st.session_state.selected_labels[current_index] = selected_label
122 if current_index < max_index:
123 st.session_state.current_index = current_index + 1
124 else:
125 st.session_state.current_index = 0
126 st.experimental_rerun()
127
128 with tab2:
129 annotator_name = st.text_input("Enter your name to save annotations")
130
131 # Update the 'Annotator' column for all rows with the provided name
132 if annotator_name:
133 df['Annotator'] = annotator_name
134
135 # Update the DataFrame with all the labels before saving
136 df = save_annotations_to_df(df)
137
138 all_annotated = df['Label_n'].notna().all()
139
140 if not all_annotated:
141 st.warning("Please ensure all claims are annotated before saving.")
142 else:
143 if not annotator_name:
144 st.warning("Please enter your name before saving.")
145 else:
146 st.write("Annotated DataFrame:")
147 st.dataframe(df)
148
149 csv = df.to_csv(index=False).encode('utf-8')
150
151 save_filename = f"annotated_{df['Title'][0]}_{annotator_name}.csv"
152
153 st.download_button(
154 label="Save and Download",
155 data=csv,
156 file_name=save_filename,
157 mime='text/csv',
158 )
159 st.success(f"Data saved successfully as {save_filename}!")
160# Call the main function to run the app
161if __name__ == '__main__':
162 annotation_app()
163 