miscjose commited on
Commit
5efc817
1 Parent(s): b7ace61

Initial Commit

Browse files
Files changed (2) hide show
  1. app.py +139 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+
3
+ import gradio as gr
4
+
5
+
6
+ pipelines_text = {
7
+ 'Spam': {'BERT': pipeline("text-classification", model="mariagrandury/distilbert-base-uncased-finetuned-sms-spam-detection"),
8
+ 'RoBERTa': pipeline("text-classification", model="mariagrandury/roberta-base-finetuned-sms-spam-detection")
9
+ },
10
+ 'Sentiment': {
11
+ 'BERT': pipeline("text-classification", model="lxyuan/distilbert-base-multilingual-cased-sentiments-student"),
12
+ 'RoBERTa': pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
13
+ },
14
+ 'Emotion': {'BERT': pipeline("text-classification", model="bhadresh-savani/bert-base-go-emotion"),
15
+ 'RoBERTa': pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
16
+ }
17
+ }
18
+
19
+ def parseImage(file, radio):
20
+ return file.name
21
+
22
+ max_textboxes = 100
23
+ def change_textboxes(n):
24
+ return [gr.Textbox.update(visible=True, interactive=True)]*n + [gr.Textbox.update(visible=False, interactive=True)]*(max_textboxes-int(n))
25
+
26
+ def parseText(text_upload_file, delimeter_dropdown):
27
+ delimeter_mapping = {'New Line': '\n','Tab': '\t','Comma': ','}
28
+ delimeter = delimeter_mapping[delimeter_dropdown]
29
+ text_boxes = ['' for i in range(max_textboxes)]
30
+ with open(text_upload_file.name, 'r') as f:
31
+ text_upload = f.read()
32
+ for idx, text in enumerate(text_upload.split(delimeter)):
33
+ text_boxes[idx] = text
34
+ return text_boxes
35
+
36
+ def annotateText(text_boxes_slider, annotation_radio, model_dropdown, *text_boxes_texbox):
37
+
38
+ text_boxes_texbox = [text for text in text_boxes_texbox]
39
+ res_label = ['' for i in range(max_textboxes)]
40
+ res_score = ['' for i in range(max_textboxes)]
41
+
42
+ # predictions
43
+ pipe = pipelines_text[annotation_radio][model_dropdown]
44
+ predictions = pipe([text_boxes_texbox[i] for i in range(text_boxes_slider)])
45
+ for idx, pred in enumerate(predictions):
46
+ # special case for spam (might change later)
47
+ if annotation_radio == 'Spam':
48
+ res_label[idx] = 'Not Spam' if pred['label'] == 'LABEL_0' else 'Spam'
49
+ else:
50
+ res_label[idx] = pred['label']
51
+ res_score[idx] = '{:.2f}'.format(pred['score'])
52
+
53
+ with open('annotations.csv', 'w') as f:
54
+ f.write('text,annotation,confidence\n')
55
+ for idx in range(max_textboxes):
56
+ if text_boxes_texbox[idx]:
57
+ f.write('{},{},{}\n'.format(text_boxes_texbox[idx], res_label[idx], res_score[idx]))
58
+
59
+ return ['./annotations.csv'] + text_boxes_texbox + res_label + res_score
60
+
61
+ with gr.Blocks() as demo:
62
+ gr.Markdown("# Data Annotation Tool")
63
+ gr.Markdown('Upload a file or enter text in the Data Viewer section. Sample files are at the end of the page.')
64
+ with gr.Tab("Text"):
65
+ with gr.Row():
66
+ with gr.Column():
67
+ gr.Markdown("## Data Upload")
68
+ text_upload_file = gr.File(file_types=['text'])
69
+ delimeter_dropdown = gr.Dropdown(choices=['New Line','Tab','Comma'], label='Delimeter')
70
+ text_upload_button = gr.Button('Parse File')
71
+
72
+ with gr.Row():
73
+ with gr.Column():
74
+ gr.Markdown("## Data Viewer")
75
+ # slider component
76
+ text_boxes_slider = gr.Slider(1, max_textboxes, value=3, step=1)
77
+ # text box components (3 visible and max_textboxes-3 not visible)
78
+ text_boxes_texbox = [gr.Textbox(show_label=False,interactive=True) for i in range(3)] + [gr.Textbox(show_label=False, visible=False) for i in range(max_textboxes-3)]
79
+ annotation_radio = gr.Radio(choices=['Spam', 'Sentiment', 'Emotion'], label='Annotation', value='RoBERTa')
80
+ model_dropdown = gr.Dropdown(choices=['BERT', 'RoBERTa'], label='Model')
81
+ text_submit_button = gr.Button('Annotate Data')
82
+ with gr.Row():
83
+ gr.Markdown("## Data Output")
84
+ with gr.Row():
85
+ with gr.Column(scale=6):
86
+ gr.Markdown("Text")
87
+ text_output_boxes = [gr.Textbox(show_label=False,interactive=False) for i in range(3)] + [gr.Textbox(show_label=False, visible=False, interactive=False) for i in range(max_textboxes-3)]
88
+ with gr.Column(scale=1):
89
+ gr.Markdown("Annotations")
90
+ text_output_annotations_boxes = [gr.Textbox(show_label=False,interactive=False) for i in range(3)] + [gr.Textbox(show_label=False, visible=False, interactive=False) for i in range(max_textboxes-3)]
91
+ with gr.Column(scale=1):
92
+ gr.Markdown("Confidence")
93
+ text_output_confidence_boxes = [gr.Textbox(show_label=False,interactive=False) for i in range(3)] + [gr.Textbox(show_label=False, visible=False, interactive=False) for i in range(max_textboxes-3)]
94
+
95
+ text_ouput_file = gr.File(label='File Output', file_types=['csv'])
96
+
97
+ gr.Markdown("## Test Examples")
98
+ with gr.Row():
99
+ with gr.Column():
100
+ gr.Examples(
101
+ examples=[['./examples/text/spam.txt', 'New Line'],['./examples/text/sentiment.txt', 'New Line'],['./examples/text/emotion.txt', 'New Line']],
102
+ fn=parseText,
103
+ inputs=[text_upload_file, delimeter_dropdown],
104
+ outputs=text_boxes_texbox,
105
+ cache_examples=True
106
+ )
107
+
108
+ # event listeners
109
+ text_upload_button.click(fn=parseText, inputs=[text_upload_file, delimeter_dropdown], outputs=text_boxes_texbox)
110
+
111
+ text_boxes_slider.change(fn=change_textboxes, inputs=text_boxes_slider, outputs=text_boxes_texbox)
112
+ text_boxes_slider.change(fn=change_textboxes, inputs=text_boxes_slider, outputs=text_output_boxes)
113
+ text_boxes_slider.change(fn=change_textboxes, inputs=text_boxes_slider, outputs=text_output_annotations_boxes)
114
+ text_boxes_slider.change(fn=change_textboxes, inputs=text_boxes_slider, outputs=text_output_confidence_boxes)
115
+
116
+ text_submit_button.click(fn=annotateText, inputs=[text_boxes_slider, annotation_radio, model_dropdown] + text_boxes_texbox, outputs=[text_ouput_file]+text_output_boxes + text_output_annotations_boxes+text_output_confidence_boxes)
117
+
118
+
119
+ with gr.Tab("Image"):
120
+ with gr.Row():
121
+ gr.Markdown("## Coming Soon!")
122
+ # with gr.Row():
123
+ # file_image = gr.File(file_count=['directory'],file_types=['image'], label='File Upload')
124
+ # image = gr.Image()
125
+ # with gr.Row():
126
+ # radio_image = gr.Radio(choices=['Object Detection'], label='Annotation')
127
+ # models_image = gr.Dropdown(choices=['DETR'], label='Model')
128
+ # with gr.Row():
129
+ # button_image = gr.Button('Submit')
130
+ # with gr.Row():
131
+ # output_image = gr.File(label='File Output', file_types=['image'])
132
+
133
+
134
+ # image tab event listeners
135
+ # button_image.click(fn=doImage, inputs=[file_image, radio_image], outputs=output_image)
136
+
137
+
138
+ if __name__ == "__main__":
139
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transformers
2
+ torch