yaleh commited on
Commit
71ddd7f
·
1 Parent(s): 6730ecf

Updated deleting behavior.

Browse files
Files changed (1) hide show
  1. app/gradio_sample_generator.py +43 -12
app/gradio_sample_generator.py CHANGED
@@ -125,11 +125,26 @@ def generate_examples_directly(
125
  raise gr.Error(f"An error occurred: {str(e)}")
126
 
127
 
128
- def format_selected_example(evt: gr.SelectData, examples):
129
  if evt.index[0] < len(examples):
130
  selected_example = examples.iloc[evt.index[0]]
131
- return selected_example.iloc[0], selected_example.iloc[1]
132
- return "", ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
  def import_json(file, input_df):
135
  if file is not None:
@@ -163,6 +178,13 @@ def append_example_to_input(new_example_input, new_example_output, input_df):
163
  except KeyError:
164
  raise gr.Error("Invalid input or output")
165
 
 
 
 
 
 
 
 
166
  with gr.Blocks(title="Task Description Generator") as demo:
167
  gr.Markdown("# Task Description Generator")
168
  gr.Markdown(
@@ -181,7 +203,10 @@ with gr.Blocks(title="Task Description Generator") as demo:
181
  with gr.Row():
182
  selected_example_input = gr.Textbox(label="Selected Example Input", lines=2, show_copy_button=True)
183
  selected_example_output = gr.Textbox(label="Selected Example Output", lines=2, show_copy_button=True)
184
- append_example_button = gr.Button("Append to Input Examples", variant="secondary")
 
 
 
185
  with gr.Row():
186
  submit_button = gr.Button("Generate", variant="primary")
187
  with gr.Accordion("Import/Export JSON", open=False):
@@ -354,27 +379,27 @@ with gr.Blocks(title="Task Description Generator") as demo:
354
  )
355
 
356
  input_df.select(
357
- fn=format_selected_example,
358
  inputs=[input_df],
359
- outputs=[selected_example_input, selected_example_output],
360
  )
361
 
362
  examples_directly_output.select(
363
- fn=format_selected_example,
364
  inputs=[examples_directly_output],
365
- outputs=[selected_example_input, selected_example_output],
366
  )
367
 
368
  examples_from_briefs_output.select(
369
- fn=format_selected_example,
370
  inputs=[examples_from_briefs_output],
371
- outputs=[selected_example_input, selected_example_output],
372
  )
373
 
374
  examples_output.select(
375
- fn=format_selected_example,
376
  inputs=[examples_output],
377
- outputs=[selected_example_input, selected_example_output],
378
  )
379
 
380
  gr.Markdown("### Manual Flagging", visible=False)
@@ -402,5 +427,11 @@ with gr.Blocks(title="Task Description Generator") as demo:
402
  outputs=[input_df],
403
  )
404
 
 
 
 
 
 
 
405
  if __name__ == "__main__":
406
  demo.launch()
 
125
  raise gr.Error(f"An error occurred: {str(e)}")
126
 
127
 
128
+ def format_selected_example_input_df(evt: gr.SelectData, examples):
129
  if evt.index[0] < len(examples):
130
  selected_example = examples.iloc[evt.index[0]]
131
+ return (
132
+ selected_example.iloc[0],
133
+ selected_example.iloc[1],
134
+ evt.index[0] + 1,
135
+ gr.update(interactive=True) # Enable the delete button
136
+ )
137
+ return "", "", None, gr.update(interactive=False) # Disable the delete button
138
+
139
+ def format_selected_example_other(evt: gr.SelectData, examples):
140
+ if evt.index[0] < len(examples):
141
+ selected_example = examples.iloc[evt.index[0]]
142
+ return (
143
+ selected_example.iloc[0],
144
+ selected_example.iloc[1],
145
+ gr.update(interactive=False) # Disable the delete button
146
+ )
147
+ return "", "", gr.update(interactive=False) # Disable the delete button
148
 
149
  def import_json(file, input_df):
150
  if file is not None:
 
178
  except KeyError:
179
  raise gr.Error("Invalid input or output")
180
 
181
+ def delete_selected_row(row_index, input_df):
182
+ if row_index is not None and row_index > 0:
183
+ # Subtract 1 from row_index because it's 1-indexed for display
184
+ input_df = input_df.drop(index=row_index - 1).reset_index(drop=True)
185
+ return input_df, None, "", "", gr.update(interactive=False) # Return updated df, clear row index and selected example, disable delete button
186
+ return input_df, row_index, "", "", gr.update(interactive=False) # Return unchanged if no valid row index, disable delete button
187
+
188
  with gr.Blocks(title="Task Description Generator") as demo:
189
  gr.Markdown("# Task Description Generator")
190
  gr.Markdown(
 
203
  with gr.Row():
204
  selected_example_input = gr.Textbox(label="Selected Example Input", lines=2, show_copy_button=True)
205
  selected_example_output = gr.Textbox(label="Selected Example Output", lines=2, show_copy_button=True)
206
+ with gr.Row():
207
+ selected_row_index = gr.Number(label="Selected Row Index", value=None, precision=0, interactive=False)
208
+ delete_row_button = gr.Button("Delete Selected Row", variant="secondary", interactive=False) # Disable by default
209
+ append_example_button = gr.Button("Append to Input Examples", variant="secondary")
210
  with gr.Row():
211
  submit_button = gr.Button("Generate", variant="primary")
212
  with gr.Accordion("Import/Export JSON", open=False):
 
379
  )
380
 
381
  input_df.select(
382
+ fn=format_selected_example_input_df,
383
  inputs=[input_df],
384
+ outputs=[selected_example_input, selected_example_output, selected_row_index, delete_row_button],
385
  )
386
 
387
  examples_directly_output.select(
388
+ fn=format_selected_example_other,
389
  inputs=[examples_directly_output],
390
+ outputs=[selected_example_input, selected_example_output, delete_row_button],
391
  )
392
 
393
  examples_from_briefs_output.select(
394
+ fn=format_selected_example_other,
395
  inputs=[examples_from_briefs_output],
396
+ outputs=[selected_example_input, selected_example_output, delete_row_button],
397
  )
398
 
399
  examples_output.select(
400
+ fn=format_selected_example_other,
401
  inputs=[examples_output],
402
+ outputs=[selected_example_input, selected_example_output, delete_row_button],
403
  )
404
 
405
  gr.Markdown("### Manual Flagging", visible=False)
 
427
  outputs=[input_df],
428
  )
429
 
430
+ delete_row_button.click(
431
+ fn=delete_selected_row,
432
+ inputs=[selected_row_index, input_df],
433
+ outputs=[input_df, selected_row_index, selected_example_input, selected_example_output, delete_row_button],
434
+ )
435
+
436
  if __name__ == "__main__":
437
  demo.launch()