Spaces:
Runtime error
Runtime error
Commit ·
c6097b0
1
Parent(s): 45e060f
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Global variable to store the DataFrame
|
| 6 |
+
df = None
|
| 7 |
+
# Global variable to keep track of the current row index
|
| 8 |
+
current_row = 0
|
| 9 |
+
|
| 10 |
+
def load_csv(file):
|
| 11 |
+
global df
|
| 12 |
+
global current_row
|
| 13 |
+
# import the csv and set the data types to be int, string, string, string, string, string, string
|
| 14 |
+
df = pd.read_csv(file.name, dtype={'id':int, 'hs': str, 'cs': str, 'topic': str, 'tone': str, 'isCSContextuallyRelevant': str, 'isToneMatch': str})
|
| 15 |
+
current_row = 0
|
| 16 |
+
row_dict = df.iloc[current_row].to_dict()
|
| 17 |
+
return row_dict['id'], row_dict['hs'], row_dict['cs'], row_dict['topic'], row_dict['tone'], row_dict['isCSContextuallyRelevant'], row_dict['isToneMatch']
|
| 18 |
+
|
| 19 |
+
def annotate_row(isCSContextuallyRelevant, isToneMatch):
|
| 20 |
+
global df
|
| 21 |
+
global current_row
|
| 22 |
+
|
| 23 |
+
df.at[current_row, 'isCSContextuallyRelevant'] = isCSContextuallyRelevant
|
| 24 |
+
df.at[current_row, 'isToneMatch'] = isToneMatch
|
| 25 |
+
|
| 26 |
+
if current_row < len(df) - 1:
|
| 27 |
+
current_row += 1
|
| 28 |
+
else:
|
| 29 |
+
current_row = 0
|
| 30 |
+
df.to_csv('annotated_data.csv', index=False)
|
| 31 |
+
|
| 32 |
+
row_dict = df.iloc[current_row].to_dict()
|
| 33 |
+
return row_dict['id'], row_dict['hs'], row_dict['cs'], row_dict['topic'], row_dict['tone'], row_dict['isCSContextuallyRelevant'], row_dict['isToneMatch'], 'annotated_data.csv'
|
| 34 |
+
|
| 35 |
+
def navigate(direction):
|
| 36 |
+
global current_row
|
| 37 |
+
if direction == "Previous":
|
| 38 |
+
current_row = max(0, current_row - 1)
|
| 39 |
+
elif direction == "Next":
|
| 40 |
+
current_row = min(len(df) - 1, current_row + 1)
|
| 41 |
+
elif direction == "First Unlabeled":
|
| 42 |
+
unlabeled_row = df[df['isCSContextuallyRelevant'].isna()].index.min()
|
| 43 |
+
if not np.isnan(unlabeled_row):
|
| 44 |
+
current_row = int(unlabeled_row)
|
| 45 |
+
|
| 46 |
+
row_dict = df.iloc[current_row].to_dict()
|
| 47 |
+
return row_dict['id'], row_dict['hs'], row_dict['cs'], row_dict['topic'], row_dict['tone'], row_dict['isCSContextuallyRelevant'], row_dict['isToneMatch']
|
| 48 |
+
|
| 49 |
+
with gr.Blocks(theme=gr.themes.Soft()) as annotator:
|
| 50 |
+
gr.Markdown("## Data Annotation")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
gr.Markdown("### Upload CSV")
|
| 54 |
+
file_upload = gr.File()
|
| 55 |
+
btn_load = gr.Button("Load CSV")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
gr.Markdown("### Current Row")
|
| 59 |
+
with gr.Row():
|
| 60 |
+
idx = gr.Number(label='Index')
|
| 61 |
+
hs = gr.Textbox(label='HS')
|
| 62 |
+
cs = gr.Textbox(label='CS')
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
topic = gr.Textbox(label='Topic')
|
| 66 |
+
tone = gr.Textbox(label='Tone')
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
isCSContextuallyRelevant = gr.Radio(["1", "0"], label="Contextually Relevant?")
|
| 70 |
+
isToneMatch = gr.Radio(["1", "0"], label="Tone Match?")
|
| 71 |
+
btn_annotate = gr.Button("Annotate")
|
| 72 |
+
|
| 73 |
+
with gr.Row():
|
| 74 |
+
btn_previous = gr.Button("Previous")
|
| 75 |
+
btn_next = gr.Button("Next")
|
| 76 |
+
btn_first_unlabeled = gr.Button("First Unlabeled")
|
| 77 |
+
|
| 78 |
+
with gr.Row():
|
| 79 |
+
gr.Markdown("### Annotated Data File Download")
|
| 80 |
+
file_download = gr.File()
|
| 81 |
+
|
| 82 |
+
btn_load.click(load_csv, inputs=[file_upload], outputs=[idx, hs, cs, topic, tone, isCSContextuallyRelevant, isToneMatch], scroll_to_output=True)
|
| 83 |
+
btn_annotate.click(annotate_row, inputs=[isCSContextuallyRelevant, isToneMatch], outputs=[idx, hs, cs, topic, tone, isCSContextuallyRelevant, isToneMatch, file_download])
|
| 84 |
+
btn_previous.click(navigate, inputs=gr.Textbox("Previous", visible=False), outputs=[idx, hs, cs, topic, tone, isCSContextuallyRelevant, isToneMatch])
|
| 85 |
+
btn_next.click(navigate, inputs=gr.Textbox("Next", visible=False), outputs=[idx, hs, cs, topic, tone, isCSContextuallyRelevant, isToneMatch])
|
| 86 |
+
btn_first_unlabeled.click(navigate, inputs=gr.Textbox("First Unlabeled", visible=False), outputs=[idx, hs, cs, topic, tone, isCSContextuallyRelevant, isToneMatch])
|
| 87 |
+
|
| 88 |
+
annotator.launch()
|