Spaces:
Sleeping
Sleeping
Refactored selected group with gr.State.
Browse files- app/gradio_sample_generator.py +196 -83
app/gradio_sample_generator.py
CHANGED
@@ -124,35 +124,22 @@ def generate_examples_from_description(
|
|
124 |
except Exception as e:
|
125 |
raise gr.Error(f"An error occurred: {str(e)}")
|
126 |
|
127 |
-
|
128 |
def format_selected_input_example_dataframe(evt: gr.SelectData, examples):
|
129 |
if evt.index[0] < len(examples):
|
130 |
selected_example = examples.iloc[evt.index[0]]
|
131 |
-
return
|
132 |
-
|
133 |
-
selected_example.iloc[1],
|
134 |
-
evt.index[0] + 1,
|
135 |
-
gr.update(visible=True), # Show selected_example_group
|
136 |
-
gr.update(visible=True), # Show selected_row_index
|
137 |
-
gr.update(visible=True), # Show delete_row_button
|
138 |
-
gr.update(visible=True), # Show update_row_button
|
139 |
-
gr.update(visible=False), # Hide append_example_button
|
140 |
-
)
|
141 |
-
return "", "", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
142 |
|
143 |
def format_selected_example(evt: gr.SelectData, examples):
|
144 |
if evt.index[0] < len(examples):
|
145 |
selected_example = examples.iloc[evt.index[0]]
|
146 |
return (
|
|
|
|
|
147 |
selected_example.iloc[0],
|
148 |
selected_example.iloc[1],
|
149 |
-
gr.update(visible=True), # Show selected_example_group
|
150 |
-
gr.update(visible=False), # Hide selected_row_index
|
151 |
-
gr.update(visible=False), # Hide delete_row_button
|
152 |
-
gr.update(visible=False), # Hide update_row_button
|
153 |
-
gr.update(visible=True), # Show append_example_button
|
154 |
)
|
155 |
-
return
|
156 |
|
157 |
def import_json_data(file, input_dataframe):
|
158 |
if file is not None:
|
@@ -178,32 +165,52 @@ def export_json_data(dataframe):
|
|
178 |
return temp_file_path
|
179 |
return None
|
180 |
|
181 |
-
|
|
|
|
|
|
|
182 |
try:
|
183 |
-
new_row = pd.DataFrame(
|
|
|
|
|
184 |
updated_df = pd.concat([input_dataframe, new_row], ignore_index=True)
|
185 |
-
return updated_df,
|
186 |
except KeyError:
|
187 |
raise gr.Error("Invalid input or output")
|
188 |
|
|
|
189 |
def delete_selected_dataframe_row(row_index, input_dataframe):
|
190 |
if row_index is not None and row_index > 0:
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
|
|
195 |
|
196 |
-
|
|
|
|
|
|
|
197 |
if selected_row_index is not None and selected_row_index > 0:
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
|
208 |
with gr.Blocks(title="Task Description Generator") as demo:
|
209 |
gr.Markdown("# Task Description Generator")
|
@@ -211,8 +218,6 @@ with gr.Blocks(title="Task Description Generator") as demo:
|
|
211 |
"Enter a JSON object with 'input' and 'output' fields to generate a task description and additional examples."
|
212 |
)
|
213 |
|
214 |
-
|
215 |
-
|
216 |
input_dataframe = gr.DataFrame(
|
217 |
label="Input Examples",
|
218 |
headers=["Input", "Output"],
|
@@ -221,18 +226,121 @@ with gr.Blocks(title="Task Description Generator") as demo:
|
|
221 |
col_count=(2, "fixed"),
|
222 |
interactive=False
|
223 |
)
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
236 |
with gr.Row():
|
237 |
submit_button = gr.Button("Generate", variant="primary")
|
238 |
with gr.Accordion("Import/Export JSON", open=False):
|
@@ -318,9 +426,7 @@ with gr.Blocks(title="Task Description Generator") as demo:
|
|
318 |
input_analysis_output,
|
319 |
example_briefs_output,
|
320 |
examples_from_briefs_output_dataframe,
|
321 |
-
examples_output_dataframe
|
322 |
-
selected_example_input,
|
323 |
-
selected_example_output,
|
324 |
],
|
325 |
value="Clear All"
|
326 |
)
|
@@ -407,25 +513,45 @@ with gr.Blocks(title="Task Description Generator") as demo:
|
|
407 |
input_dataframe.select(
|
408 |
fn=format_selected_input_example_dataframe,
|
409 |
inputs=[input_dataframe],
|
410 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
411 |
)
|
412 |
|
413 |
examples_directly_output_dataframe.select(
|
414 |
fn=format_selected_example,
|
415 |
inputs=[examples_directly_output_dataframe],
|
416 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
417 |
)
|
418 |
|
419 |
examples_from_briefs_output_dataframe.select(
|
420 |
fn=format_selected_example,
|
421 |
inputs=[examples_from_briefs_output_dataframe],
|
422 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
423 |
)
|
424 |
|
425 |
examples_output_dataframe.select(
|
426 |
fn=format_selected_example,
|
427 |
inputs=[examples_output_dataframe],
|
428 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
429 |
)
|
430 |
|
431 |
gr.Markdown("### Manual Flagging", visible=False)
|
@@ -447,34 +573,21 @@ with gr.Blocks(title="Task Description Generator") as demo:
|
|
447 |
outputs=[],
|
448 |
)
|
449 |
|
450 |
-
append_example_button.click(
|
451 |
-
fn=append_example_to_input_dataframe,
|
452 |
-
inputs=[selected_example_input, selected_example_output, input_dataframe],
|
453 |
-
outputs=[input_dataframe, selected_example_input, selected_example_output, selected_example_group, selected_row_index, delete_row_button, update_row_button, append_example_button],
|
454 |
-
)
|
455 |
-
|
456 |
-
delete_row_button.click(
|
457 |
-
fn=delete_selected_dataframe_row,
|
458 |
-
inputs=[selected_row_index, input_dataframe],
|
459 |
-
outputs=[input_dataframe, selected_row_index, selected_example_input, selected_example_output, selected_example_group, selected_row_index, delete_row_button, update_row_button],
|
460 |
-
)
|
461 |
-
|
462 |
-
update_row_button.click(
|
463 |
-
fn=update_selected_dataframe_row,
|
464 |
-
inputs=[selected_example_input, selected_example_output, selected_row_index, input_dataframe],
|
465 |
-
outputs=[input_dataframe, selected_example_input, selected_example_output, selected_example_group, selected_row_index, delete_row_button, update_row_button],
|
466 |
-
)
|
467 |
-
|
468 |
-
close_button.click(
|
469 |
-
fn=lambda: gr.update(visible=False),
|
470 |
-
inputs=[],
|
471 |
-
outputs=[selected_example_group],
|
472 |
-
)
|
473 |
-
|
474 |
input_dataframe.change(
|
475 |
-
fn=
|
476 |
-
inputs=[
|
477 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
478 |
)
|
479 |
|
480 |
if __name__ == "__main__":
|
|
|
124 |
except Exception as e:
|
125 |
raise gr.Error(f"An error occurred: {str(e)}")
|
126 |
|
|
|
127 |
def format_selected_input_example_dataframe(evt: gr.SelectData, examples):
|
128 |
if evt.index[0] < len(examples):
|
129 |
selected_example = examples.iloc[evt.index[0]]
|
130 |
+
return "update", evt.index[0]+1, selected_example.iloc[0], selected_example.iloc[1]
|
131 |
+
return None, None, None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
def format_selected_example(evt: gr.SelectData, examples):
|
134 |
if evt.index[0] < len(examples):
|
135 |
selected_example = examples.iloc[evt.index[0]]
|
136 |
return (
|
137 |
+
"append",
|
138 |
+
None,
|
139 |
selected_example.iloc[0],
|
140 |
selected_example.iloc[1],
|
|
|
|
|
|
|
|
|
|
|
141 |
)
|
142 |
+
return None, None, None, None
|
143 |
|
144 |
def import_json_data(file, input_dataframe):
|
145 |
if file is not None:
|
|
|
165 |
return temp_file_path
|
166 |
return None
|
167 |
|
168 |
+
|
169 |
+
def append_example_to_input_dataframe(
|
170 |
+
new_example_input, new_example_output, input_dataframe
|
171 |
+
):
|
172 |
try:
|
173 |
+
new_row = pd.DataFrame(
|
174 |
+
[[new_example_input, new_example_output]], columns=["Input", "Output"]
|
175 |
+
)
|
176 |
updated_df = pd.concat([input_dataframe, new_row], ignore_index=True)
|
177 |
+
return updated_df, None, None, None, None
|
178 |
except KeyError:
|
179 |
raise gr.Error("Invalid input or output")
|
180 |
|
181 |
+
|
182 |
def delete_selected_dataframe_row(row_index, input_dataframe):
|
183 |
if row_index is not None and row_index > 0:
|
184 |
+
input_dataframe = input_dataframe.drop(index=row_index - 1).reset_index(
|
185 |
+
drop=True
|
186 |
+
)
|
187 |
+
return input_dataframe, None, None, None, None
|
188 |
+
return input_dataframe, None, None, None, None
|
189 |
|
190 |
+
|
191 |
+
def update_selected_dataframe_row(
|
192 |
+
selected_example_input, selected_example_output, selected_row_index, input_dataframe
|
193 |
+
):
|
194 |
if selected_row_index is not None and selected_row_index > 0:
|
195 |
+
input_dataframe.iloc[selected_row_index - 1] = [
|
196 |
+
selected_example_input,
|
197 |
+
selected_example_output,
|
198 |
+
]
|
199 |
+
return input_dataframe, None, None, None, None
|
200 |
+
return input_dataframe, None, None, None, None
|
201 |
+
|
202 |
+
|
203 |
+
def input_dataframe_change(
|
204 |
+
input_dataframe, selected_group_mode, selected_group_index, selected_group_input, selected_group_output
|
205 |
+
):
|
206 |
+
if len(input_dataframe) <= 1:
|
207 |
+
return None, None, None, None
|
208 |
+
return (
|
209 |
+
selected_group_mode,
|
210 |
+
selected_group_index,
|
211 |
+
selected_group_input,
|
212 |
+
selected_group_output,
|
213 |
+
)
|
214 |
|
215 |
with gr.Blocks(title="Task Description Generator") as demo:
|
216 |
gr.Markdown("# Task Description Generator")
|
|
|
218 |
"Enter a JSON object with 'input' and 'output' fields to generate a task description and additional examples."
|
219 |
)
|
220 |
|
|
|
|
|
221 |
input_dataframe = gr.DataFrame(
|
222 |
label="Input Examples",
|
223 |
headers=["Input", "Output"],
|
|
|
226 |
col_count=(2, "fixed"),
|
227 |
interactive=False
|
228 |
)
|
229 |
+
|
230 |
+
selected_group_mode = gr.State(None) # None, "update", "append"
|
231 |
+
selected_group_index = gr.State(None) # None, int
|
232 |
+
selected_group_input = gr.State("")
|
233 |
+
selected_group_output = gr.State("")
|
234 |
+
|
235 |
+
@gr.render(
|
236 |
+
inputs=[
|
237 |
+
selected_group_mode,
|
238 |
+
selected_group_index,
|
239 |
+
selected_group_input,
|
240 |
+
selected_group_output,
|
241 |
+
],
|
242 |
+
triggers=[selected_group_mode.change],
|
243 |
+
)
|
244 |
+
def selected_group(mode, index, input, output):
|
245 |
+
if mode is None:
|
246 |
+
return
|
247 |
+
with gr.Group():
|
248 |
+
if mode == "update":
|
249 |
+
with gr.Row():
|
250 |
+
selected_row_index = gr.Number(
|
251 |
+
label="Selected Row Index", value=index, precision=0, interactive=False
|
252 |
+
)
|
253 |
+
delete_row_button = gr.Button(
|
254 |
+
"Delete Selected Row", variant="secondary"
|
255 |
+
)
|
256 |
+
with gr.Row():
|
257 |
+
selected_example_input = gr.Textbox(
|
258 |
+
label="Selected Example Input",
|
259 |
+
lines=2,
|
260 |
+
show_copy_button=True,
|
261 |
+
value=input,
|
262 |
+
)
|
263 |
+
selected_example_output = gr.Textbox(
|
264 |
+
label="Selected Example Output",
|
265 |
+
lines=2,
|
266 |
+
show_copy_button=True,
|
267 |
+
value=output,
|
268 |
+
)
|
269 |
+
with gr.Row():
|
270 |
+
update_row_button = gr.Button(
|
271 |
+
"Update Selected Row", variant="secondary"
|
272 |
+
)
|
273 |
+
|
274 |
+
delete_row_button.click(
|
275 |
+
fn=delete_selected_dataframe_row,
|
276 |
+
inputs=[selected_row_index, input_dataframe],
|
277 |
+
outputs=[
|
278 |
+
input_dataframe,
|
279 |
+
selected_group_mode,
|
280 |
+
selected_group_index,
|
281 |
+
selected_group_input,
|
282 |
+
selected_group_output,
|
283 |
+
],
|
284 |
+
)
|
285 |
+
|
286 |
+
update_row_button.click(
|
287 |
+
fn=update_selected_dataframe_row,
|
288 |
+
inputs=[
|
289 |
+
selected_example_input,
|
290 |
+
selected_example_output,
|
291 |
+
selected_row_index,
|
292 |
+
input_dataframe,
|
293 |
+
],
|
294 |
+
outputs=[
|
295 |
+
input_dataframe,
|
296 |
+
selected_group_mode,
|
297 |
+
selected_group_index,
|
298 |
+
selected_group_input,
|
299 |
+
selected_group_output,
|
300 |
+
],
|
301 |
+
)
|
302 |
+
elif mode == "append":
|
303 |
+
with gr.Row():
|
304 |
+
selected_example_input = gr.Textbox(
|
305 |
+
label="Selected Example Input",
|
306 |
+
lines=2,
|
307 |
+
show_copy_button=True,
|
308 |
+
value=input,
|
309 |
+
)
|
310 |
+
selected_example_output = gr.Textbox(
|
311 |
+
label="Selected Example Output",
|
312 |
+
lines=2,
|
313 |
+
show_copy_button=True,
|
314 |
+
value=output,
|
315 |
+
)
|
316 |
+
with gr.Row():
|
317 |
+
append_example_button = gr.Button(
|
318 |
+
"Append to Input Examples", variant="secondary"
|
319 |
+
)
|
320 |
+
append_example_button.click(
|
321 |
+
fn=append_example_to_input_dataframe,
|
322 |
+
inputs=[
|
323 |
+
selected_example_input,
|
324 |
+
selected_example_output,
|
325 |
+
input_dataframe,
|
326 |
+
],
|
327 |
+
outputs=[
|
328 |
+
input_dataframe,
|
329 |
+
selected_group_mode,
|
330 |
+
selected_group_index,
|
331 |
+
selected_group_input,
|
332 |
+
selected_group_output,
|
333 |
+
],
|
334 |
+
)
|
335 |
+
|
336 |
+
with gr.Row():
|
337 |
+
close_button = gr.Button("Close", variant="secondary")
|
338 |
+
close_button.click(
|
339 |
+
fn=lambda: None,
|
340 |
+
inputs=[],
|
341 |
+
outputs=[selected_group_mode],
|
342 |
+
)
|
343 |
+
|
344 |
with gr.Row():
|
345 |
submit_button = gr.Button("Generate", variant="primary")
|
346 |
with gr.Accordion("Import/Export JSON", open=False):
|
|
|
426 |
input_analysis_output,
|
427 |
example_briefs_output,
|
428 |
examples_from_briefs_output_dataframe,
|
429 |
+
examples_output_dataframe
|
|
|
|
|
430 |
],
|
431 |
value="Clear All"
|
432 |
)
|
|
|
513 |
input_dataframe.select(
|
514 |
fn=format_selected_input_example_dataframe,
|
515 |
inputs=[input_dataframe],
|
516 |
+
outputs=[
|
517 |
+
selected_group_mode,
|
518 |
+
selected_group_index,
|
519 |
+
selected_group_input,
|
520 |
+
selected_group_output,
|
521 |
+
],
|
522 |
)
|
523 |
|
524 |
examples_directly_output_dataframe.select(
|
525 |
fn=format_selected_example,
|
526 |
inputs=[examples_directly_output_dataframe],
|
527 |
+
outputs=[
|
528 |
+
selected_group_mode,
|
529 |
+
selected_group_index,
|
530 |
+
selected_group_input,
|
531 |
+
selected_group_output,
|
532 |
+
],
|
533 |
)
|
534 |
|
535 |
examples_from_briefs_output_dataframe.select(
|
536 |
fn=format_selected_example,
|
537 |
inputs=[examples_from_briefs_output_dataframe],
|
538 |
+
outputs=[
|
539 |
+
selected_group_mode,
|
540 |
+
selected_group_index,
|
541 |
+
selected_group_input,
|
542 |
+
selected_group_output,
|
543 |
+
],
|
544 |
)
|
545 |
|
546 |
examples_output_dataframe.select(
|
547 |
fn=format_selected_example,
|
548 |
inputs=[examples_output_dataframe],
|
549 |
+
outputs=[
|
550 |
+
selected_group_mode,
|
551 |
+
selected_group_index,
|
552 |
+
selected_group_input,
|
553 |
+
selected_group_output,
|
554 |
+
],
|
555 |
)
|
556 |
|
557 |
gr.Markdown("### Manual Flagging", visible=False)
|
|
|
573 |
outputs=[],
|
574 |
)
|
575 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
576 |
input_dataframe.change(
|
577 |
+
fn=input_dataframe_change,
|
578 |
+
inputs=[
|
579 |
+
input_dataframe,
|
580 |
+
selected_group_mode,
|
581 |
+
selected_group_index,
|
582 |
+
selected_group_input,
|
583 |
+
selected_group_output,
|
584 |
+
],
|
585 |
+
outputs=[
|
586 |
+
selected_group_mode,
|
587 |
+
selected_group_index,
|
588 |
+
selected_group_input,
|
589 |
+
selected_group_output,
|
590 |
+
],
|
591 |
)
|
592 |
|
593 |
if __name__ == "__main__":
|