anna-tch commited on
Commit
b7fdcd4
·
1 Parent(s): 9802c23

Add application file

Browse files
Files changed (1) hide show
  1. app.py +210 -73
app.py CHANGED
@@ -1,88 +1,143 @@
1
  import gradio as gr
 
 
2
 
3
- # Function to handle evaluation
4
- def evaluate_texts(*args):
5
- num_columns = (len(args) - 1) // 2 # Exclude preference from the count
6
- evaluations = []
7
 
8
- for i in range(num_columns):
9
- grammar = args[i * 2]
10
- coherence = args[i * 2 + 1]
11
 
12
- # Ensure required fields are filled
13
- if grammar is None or coherence is None:
14
- return "Error: Please fill out all fields before submitting."
15
-
16
- evaluations.append(f"Adapter {i+1}: Grammar {grammar}, Coherence {coherence}")
17
 
18
- # Preferred adapter
19
- preference = args[-1]
20
- evaluations.append(f"Preferred Text: {preference}")
21
 
22
- return "\n".join(evaluations)
 
 
23
 
24
- # Define the number of adapters (change dynamically)
25
- num_columns = 3 # Change to 2, 3, or 4 as needed
 
 
 
 
26
 
27
- with gr.Blocks() as demo:
28
- gr.Markdown("## Compare and Rate Generated Texts")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- # Description of scoring system
31
- gr.Markdown("""
32
- **Scoring System**
33
- - **Grammar Score**: -2 (Poor), -1 (Fair), 0 (Neutral), 1 (Good), 2 (Excellent)
34
- - **Coherence Score**: -2 (Poor), -1 (Fair), 0 (Neutral), 1 (Good), 2 (Excellent)
35
- """)
36
 
37
- adapters = [] # Store inputs dynamically
 
 
 
 
38
 
39
- for i in range(num_columns):
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  with gr.Row():
41
- # Left: Generated text (in a larger column)
42
  with gr.Column(scale=3):
43
- gr.Markdown(f"### Adapter {i+1}")
44
- adapter_text = gr.Textbox(label=f"Generated Text by Adapter {i+1}", lines=5, max_lines=6)
 
45
 
46
- # Right: Scoring section with **extra padding for spacing**
47
- with gr.Column(scale=1, min_width=150):
48
- gr.Markdown("### Scoring") # Adds a title for clarity
49
  grammar = gr.Radio(
50
- choices=["-2", "-1", "0", "1", "2"],
51
- label="Grammar",
52
  interactive=True
53
  )
54
  coherence = gr.Radio(
55
- choices=["-2", "-1", "0", "1", "2"],
56
- label="Coherence",
57
  interactive=True
58
  )
 
 
59
 
60
- # Store inputs for later
61
- adapters.append(adapter_text)
62
- adapters.append(grammar)
63
- adapters.append(coherence)
64
-
65
- # User preference dropdown
66
- preference = gr.Dropdown(
67
- choices=[f"Adapter {i+1}" for i in range(num_columns)],
68
- label="Choose Your Preferred Text",
69
- value=f"Adapter 1"
70
- )
71
 
72
- # Submit button + result box
73
  with gr.Row():
74
- submit_btn = gr.Button("Submit Ratings")
75
- result_output = gr.Textbox(label="Evaluation Results", lines=6)
 
76
 
77
- # Connect function to UI
78
- submit_btn.click(evaluate_texts, inputs=adapters + [preference], outputs=result_output)
79
 
80
- # Launch the Gradio app
 
 
 
 
 
 
 
 
81
  demo.launch()
82
 
 
83
  # import gradio as gr
84
 
85
- # # Define the function to display and rate multiple generations
 
 
 
 
 
 
 
 
 
 
 
 
86
  # def evaluate_texts(*args):
87
  # num_columns = (len(args) - 1) // 2 # Exclude preference from the count
88
  # evaluations = []
@@ -91,25 +146,25 @@ demo.launch()
91
  # grammar = args[i * 2]
92
  # coherence = args[i * 2 + 1]
93
 
94
- # # Validate required fields
95
  # if grammar is None or coherence is None:
96
  # return "Error: Please fill out all fields before submitting."
97
 
98
  # evaluations.append(f"Adapter {i+1}: Grammar {grammar}, Coherence {coherence}")
99
 
100
- # # Get preferred adapter
101
  # preference = args[-1]
102
  # evaluations.append(f"Preferred Text: {preference}")
103
 
104
  # return "\n".join(evaluations)
105
 
106
- # # Define the number of generated texts (adapters)
107
- # num_columns = 3 # Change this to 2, 3, or 4 dynamically
108
 
109
  # with gr.Blocks() as demo:
110
  # gr.Markdown("## Compare and Rate Generated Texts")
111
 
112
- # # Description of scores before the interface
113
  # gr.Markdown("""
114
  # **Scoring System**
115
  # - **Grammar Score**: -2 (Poor), -1 (Fair), 0 (Neutral), 1 (Good), 2 (Excellent)
@@ -117,45 +172,127 @@ demo.launch()
117
  # """)
118
 
119
  # adapters = [] # Store inputs dynamically
 
120
  # for i in range(num_columns):
121
  # with gr.Row():
122
- # # Left: Generated text
123
- # with gr.Column(scale=3): # Adjust width ratio (3:1)
124
  # gr.Markdown(f"### Adapter {i+1}")
125
  # adapter_text = gr.Textbox(label=f"Generated Text by Adapter {i+1}", lines=5, max_lines=6)
126
-
127
- # # Right: Scoring section
128
- # with gr.Column(scale=1):
 
129
  # grammar = gr.Radio(
130
  # choices=["-2", "-1", "0", "1", "2"],
131
- # label="Grammar Score",
132
  # interactive=True
133
  # )
134
  # coherence = gr.Radio(
135
  # choices=["-2", "-1", "0", "1", "2"],
136
- # label="Coherence Score",
137
  # interactive=True
138
  # )
139
 
140
- # # Store inputs for submission
141
  # adapters.append(adapter_text)
142
  # adapters.append(grammar)
143
  # adapters.append(coherence)
144
 
145
- # # Preference dropdown
146
  # preference = gr.Dropdown(
147
  # choices=[f"Adapter {i+1}" for i in range(num_columns)],
148
  # label="Choose Your Preferred Text",
149
  # value=f"Adapter 1"
150
  # )
151
 
152
- # # Submit button and output
153
  # with gr.Row():
154
  # submit_btn = gr.Button("Submit Ratings")
155
  # result_output = gr.Textbox(label="Evaluation Results", lines=6)
156
 
157
- # # Link function to interface
158
  # submit_btn.click(evaluate_texts, inputs=adapters + [preference], outputs=result_output)
159
 
160
- # # Launch the app
161
  # demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import datasets
3
+ from huggingface_hub import HfApi
4
 
5
+ # Load dataset from Hugging Face
6
+ ds_name = "anna-tch/generation-results"
7
+ dataset = datasets.load_dataset(ds_name)["train"]
 
8
 
9
+ # Identify generation columns (all except 'comment_id' and 'manual_annotation')
10
+ generation_columns = [col for col in dataset.column_names if col not in ["comment_id", "manual_annotation"]]
 
11
 
12
+ # Track annotation progress
13
+ current_index = 0
14
+ annotations = {} # Store annotations locally before pushing
 
 
15
 
 
 
 
16
 
17
+ def get_next_sample():
18
+ """Fetch the next unannotated sample"""
19
+ global current_index
20
 
21
+ # Find the next unannotated comment
22
+ while current_index < len(dataset):
23
+ sample = dataset[current_index]
24
+ if sample["manual_annotation"] is None:
25
+ return sample
26
+ current_index += 1
27
 
28
+ return None # No more samples to annotate
29
+
30
+
31
+ def annotate_text(grammar_scores, coherence_scores, preferred_text):
32
+ """Save annotation and move to next sample"""
33
+ global current_index
34
+
35
+ sample = dataset[current_index]
36
+
37
+ # Store annotation locally
38
+ annotations[sample["comment_id"]] = {
39
+ "grammar": dict(zip(generation_columns, grammar_scores)),
40
+ "coherence": dict(zip(generation_columns, coherence_scores)),
41
+ "preferred_text": preferred_text,
42
+ }
43
+
44
+ # update the dataset
45
+ dataset[current_index]["manual_annotation"] = annotations[sample["comment_id"]]
46
+
47
+ # Push updated dataset to Hugging Face
48
+ dataset.push_to_hub(ds_name)
49
+
50
+ # Move to next sample
51
+ current_index += 1
52
+ return update_ui()
53
+
54
+
55
+ def update_ui():
56
+ """Update UI with the next sample or display completion message"""
57
+ sample = get_next_sample()
58
+ if sample is None:
59
+ return gr.update(visible=False), gr.update(value="All comments annotated!"), gr.update(visible=False)
60
+
61
+ textboxes = [gr.update(value=sample[col]) for col in generation_columns]
62
+ return textboxes + [gr.update(value=None)] * len(generation_columns) + [gr.update(value=None)]
63
 
 
 
 
 
 
 
64
 
65
+ # def push_annotations():
66
+ # """Push annotations to Hugging Face"""
67
+ # for idx, sample in enumerate(dataset):
68
+ # if sample["comment_id"] in annotations:
69
+ # dataset[idx]["manual_annotation"] = annotations[sample["comment_id"]]
70
 
71
+ # dataset.push_to_hub(ds_name)
72
+ # return "Annotations successfully uploaded to Hugging Face!"
73
+
74
+
75
+ # Gradio Interface
76
+ with gr.Blocks() as demo:
77
+ gr.Markdown("## Annotate Generated Texts")
78
+ gr.Markdown("#### Rate each generation and select the best one")
79
+
80
+ textboxes = []
81
+ grammar_radios = []
82
+ coherence_radios = []
83
+
84
+ for col in generation_columns:
85
  with gr.Row():
 
86
  with gr.Column(scale=3):
87
+ gr.Markdown(f"### {col}")
88
+ text_input = gr.Textbox(label=f"Generated Text ({col})", lines=5)
89
+ textboxes.append(text_input)
90
 
91
+ with gr.Column(scale=1):
 
 
92
  grammar = gr.Radio(
93
+ choices=["-2", "-1", "0", "1", "2"],
94
+ label="Grammar",
95
  interactive=True
96
  )
97
  coherence = gr.Radio(
98
+ choices=["-2", "-1", "0", "1", "2"],
99
+ label="Coherence",
100
  interactive=True
101
  )
102
+ grammar_radios.append(grammar)
103
+ coherence_radios.append(coherence)
104
 
105
+ preferred_text = gr.Dropdown(choices=generation_columns, label="Select Best Generation")
 
 
 
 
 
 
 
 
 
 
106
 
 
107
  with gr.Row():
108
+ submit_btn = gr.Button("Submit Annotation")
109
+ next_btn = gr.Button("Skip (Next)")
110
+ push_btn = gr.Button("Push to Hugging Face")
111
 
112
+ output_message = gr.Textbox(label="Status", interactive=False)
 
113
 
114
+ # Set initial values
115
+ demo.load(update_ui, outputs=textboxes)
116
+
117
+ # Actions
118
+ submit_btn.click(annotate_text, inputs=grammar_radios + coherence_radios + [preferred_text], outputs=textboxes + [output_message])
119
+ next_btn.click(lambda: update_ui(), outputs=textboxes + [output_message])
120
+ # push_btn.click(push_annotations, outputs=output_message)
121
+
122
+ # Launch the app
123
  demo.launch()
124
 
125
+
126
  # import gradio as gr
127
 
128
+ # # Load dataset from Hugging Face
129
+ # ds_name = "anna-tch/generation-results"
130
+ # dataset = datasets.load_dataset(ds_name)["train"]
131
+
132
+ # # Identify generation columns (all except 'comment_id' and 'manual_annotation')
133
+ # generation_columns = [col for col in dataset.column_names if col not in ["comment_id", "manual_annotation"]]
134
+
135
+ # # Track annotation progress
136
+ # current_index = 0
137
+ # annotations = {} # Store annotations locally before pushing to the dataset
138
+
139
+
140
+ # # Function to handle evaluation
141
  # def evaluate_texts(*args):
142
  # num_columns = (len(args) - 1) // 2 # Exclude preference from the count
143
  # evaluations = []
 
146
  # grammar = args[i * 2]
147
  # coherence = args[i * 2 + 1]
148
 
149
+ # # Ensure required fields are filled
150
  # if grammar is None or coherence is None:
151
  # return "Error: Please fill out all fields before submitting."
152
 
153
  # evaluations.append(f"Adapter {i+1}: Grammar {grammar}, Coherence {coherence}")
154
 
155
+ # # Preferred adapter
156
  # preference = args[-1]
157
  # evaluations.append(f"Preferred Text: {preference}")
158
 
159
  # return "\n".join(evaluations)
160
 
161
+ # # Define the number of adapters (change dynamically)
162
+ # num_columns = 3 # Change to 2, 3, or 4 as needed
163
 
164
  # with gr.Blocks() as demo:
165
  # gr.Markdown("## Compare and Rate Generated Texts")
166
 
167
+ # # Description of scoring system
168
  # gr.Markdown("""
169
  # **Scoring System**
170
  # - **Grammar Score**: -2 (Poor), -1 (Fair), 0 (Neutral), 1 (Good), 2 (Excellent)
 
172
  # """)
173
 
174
  # adapters = [] # Store inputs dynamically
175
+
176
  # for i in range(num_columns):
177
  # with gr.Row():
178
+ # # Left: Generated text (in a larger column)
179
+ # with gr.Column(scale=3):
180
  # gr.Markdown(f"### Adapter {i+1}")
181
  # adapter_text = gr.Textbox(label=f"Generated Text by Adapter {i+1}", lines=5, max_lines=6)
182
+
183
+ # # Right: Scoring section with **extra padding for spacing**
184
+ # with gr.Column(scale=1, min_width=150):
185
+ # gr.Markdown("### Scoring") # Adds a title for clarity
186
  # grammar = gr.Radio(
187
  # choices=["-2", "-1", "0", "1", "2"],
188
+ # label="Grammar",
189
  # interactive=True
190
  # )
191
  # coherence = gr.Radio(
192
  # choices=["-2", "-1", "0", "1", "2"],
193
+ # label="Coherence",
194
  # interactive=True
195
  # )
196
 
197
+ # # Store inputs for later
198
  # adapters.append(adapter_text)
199
  # adapters.append(grammar)
200
  # adapters.append(coherence)
201
 
202
+ # # User preference dropdown
203
  # preference = gr.Dropdown(
204
  # choices=[f"Adapter {i+1}" for i in range(num_columns)],
205
  # label="Choose Your Preferred Text",
206
  # value=f"Adapter 1"
207
  # )
208
 
209
+ # # Submit button + result box
210
  # with gr.Row():
211
  # submit_btn = gr.Button("Submit Ratings")
212
  # result_output = gr.Textbox(label="Evaluation Results", lines=6)
213
 
214
+ # # Connect function to UI
215
  # submit_btn.click(evaluate_texts, inputs=adapters + [preference], outputs=result_output)
216
 
217
+ # # Launch the Gradio app
218
  # demo.launch()
219
+
220
+ # # import gradio as gr
221
+
222
+ # # # Define the function to display and rate multiple generations
223
+ # # def evaluate_texts(*args):
224
+ # # num_columns = (len(args) - 1) // 2 # Exclude preference from the count
225
+ # # evaluations = []
226
+
227
+ # # for i in range(num_columns):
228
+ # # grammar = args[i * 2]
229
+ # # coherence = args[i * 2 + 1]
230
+
231
+ # # # Validate required fields
232
+ # # if grammar is None or coherence is None:
233
+ # # return "Error: Please fill out all fields before submitting."
234
+
235
+ # # evaluations.append(f"Adapter {i+1}: Grammar {grammar}, Coherence {coherence}")
236
+
237
+ # # # Get preferred adapter
238
+ # # preference = args[-1]
239
+ # # evaluations.append(f"Preferred Text: {preference}")
240
+
241
+ # # return "\n".join(evaluations)
242
+
243
+ # # # Define the number of generated texts (adapters)
244
+ # # num_columns = 3 # Change this to 2, 3, or 4 dynamically
245
+
246
+ # # with gr.Blocks() as demo:
247
+ # # gr.Markdown("## Compare and Rate Generated Texts")
248
+
249
+ # # # Description of scores before the interface
250
+ # # gr.Markdown("""
251
+ # # **Scoring System**
252
+ # # - **Grammar Score**: -2 (Poor), -1 (Fair), 0 (Neutral), 1 (Good), 2 (Excellent)
253
+ # # - **Coherence Score**: -2 (Poor), -1 (Fair), 0 (Neutral), 1 (Good), 2 (Excellent)
254
+ # # """)
255
+
256
+ # # adapters = [] # Store inputs dynamically
257
+ # # for i in range(num_columns):
258
+ # # with gr.Row():
259
+ # # # Left: Generated text
260
+ # # with gr.Column(scale=3): # Adjust width ratio (3:1)
261
+ # # gr.Markdown(f"### Adapter {i+1}")
262
+ # # adapter_text = gr.Textbox(label=f"Generated Text by Adapter {i+1}", lines=5, max_lines=6)
263
+
264
+ # # # Right: Scoring section
265
+ # # with gr.Column(scale=1):
266
+ # # grammar = gr.Radio(
267
+ # # choices=["-2", "-1", "0", "1", "2"],
268
+ # # label="Grammar Score",
269
+ # # interactive=True
270
+ # # )
271
+ # # coherence = gr.Radio(
272
+ # # choices=["-2", "-1", "0", "1", "2"],
273
+ # # label="Coherence Score",
274
+ # # interactive=True
275
+ # # )
276
+
277
+ # # # Store inputs for submission
278
+ # # adapters.append(adapter_text)
279
+ # # adapters.append(grammar)
280
+ # # adapters.append(coherence)
281
+
282
+ # # # Preference dropdown
283
+ # # preference = gr.Dropdown(
284
+ # # choices=[f"Adapter {i+1}" for i in range(num_columns)],
285
+ # # label="Choose Your Preferred Text",
286
+ # # value=f"Adapter 1"
287
+ # # )
288
+
289
+ # # # Submit button and output
290
+ # # with gr.Row():
291
+ # # submit_btn = gr.Button("Submit Ratings")
292
+ # # result_output = gr.Textbox(label="Evaluation Results", lines=6)
293
+
294
+ # # # Link function to interface
295
+ # # submit_btn.click(evaluate_texts, inputs=adapters + [preference], outputs=result_output)
296
+
297
+ # # # Launch the app
298
+ # # demo.launch()