-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotate.py
More file actions
156 lines (127 loc) · 4.48 KB
/
Copy pathannotate.py
File metadata and controls
156 lines (127 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import gradio as gr
import pandas as pd
import os
import glob
# Create rubric directory if it doesn't exist
os.makedirs("rubric", exist_ok=True)
def load_csv_files():
"""Get list of all CSV files in the runs directory"""
return sorted(glob.glob("runs/*.csv"))
def load_csv(file_path):
"""Load a CSV file and return as a pandas DataFrame"""
df = pd.read_csv(file_path)
# Add 'label' column if it doesn't exist
if 'label' not in df.columns:
df['label'] = ""
return df, file_path
def update_label(df, file_path, index, label):
"""Update the label for a specific row in the DataFrame"""
if df is not None and 0 <= index < len(df):
df.at[index, 'label'] = label
# Return the updated row for display
return df.iloc[index].to_dict(), df, index
return None, df, index
def save_csv(df, file_path):
"""Save the DataFrame to the rubric directory"""
if df is not None:
# Create the output filename based on the input filename
input_filename = os.path.basename(file_path)
output_path = os.path.join("rubric", "rubric.csv")
# Save the DataFrame to CSV
df.to_csv(output_path, index=False)
return f"Saved to {output_path}"
return "No data to save"
def view_row(df, index):
"""View a specific row in the DataFrame"""
if df is not None and 0 <= index < len(df):
return df.iloc[index].to_dict(), index
return None, index
def next_row(df, index):
"""Move to the next row"""
if df is not None and index < len(df) - 1:
index += 1
return df.iloc[index].to_dict(), index
return None, index
def prev_row(df, index):
"""Move to the previous row"""
if df is not None and index > 0:
index -= 1
return df.iloc[index].to_dict(), index
return None, index
# Define the Gradio interface
with gr.Blocks() as app:
gr.Markdown("# CSV Annotation Tool")
# State variables
df_state = gr.State(None)
file_path_state = gr.State("")
current_index = gr.State(0)
with gr.Row():
with gr.Column(scale=1):
# File selection
file_dropdown = gr.Dropdown(
label="Select CSV file from runs directory",
choices=load_csv_files(),
interactive=True
)
load_button = gr.Button("Load File")
# Navigation
with gr.Row():
prev_button = gr.Button("Previous Row")
next_button = gr.Button("Next Row")
# Labeling buttons
with gr.Row():
pass_button = gr.Button("PASS", variant="primary")
fail_button = gr.Button("FAIL", variant="secondary")
# Save button
save_button = gr.Button("Save to rubric/rubric.csv", variant="success")
save_result = gr.Textbox(label="Save Result")
with gr.Column(scale=2):
# Display current row
row_display = gr.JSON(label="Current Row")
# Display current index
index_display = gr.Number(label="Current Row Index", precision=0)
# Set up event handlers
load_button.click(
load_csv,
inputs=[file_dropdown],
outputs=[df_state, file_path_state]
).then(
view_row,
inputs=[df_state, current_index],
outputs=[row_display, index_display]
)
prev_button.click(
prev_row,
inputs=[df_state, current_index],
outputs=[row_display, current_index]
).then(
lambda x: x,
inputs=[current_index],
outputs=[index_display]
)
next_button.click(
next_row,
inputs=[df_state, current_index],
outputs=[row_display, current_index]
).then(
lambda x: x,
inputs=[current_index],
outputs=[index_display]
)
pass_button.click(
update_label,
inputs=[df_state, file_path_state, current_index, gr.Textbox(value="PASS", visible=False)],
outputs=[row_display, df_state, current_index]
)
fail_button.click(
update_label,
inputs=[df_state, file_path_state, current_index, gr.Textbox(value="FAIL", visible=False)],
outputs=[row_display, df_state, current_index]
)
save_button.click(
save_csv,
inputs=[df_state, file_path_state],
outputs=[save_result]
)
if __name__ == "__main__":
app.launch()