Spaces:
Running
Running
Use Gradio Examples component with image thumbnails
Browse files- Replace button-based examples with gr.Examples
- Show image thumbnails for each example
- Click to load image and prompt into chatbot
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
app.py
CHANGED
|
@@ -454,72 +454,40 @@ def _launch_demo(args, model, processor):
|
|
| 454 |
regen_btn = gr.Button('Regenerate')
|
| 455 |
empty_bin = gr.Button('Clear')
|
| 456 |
|
| 457 |
-
# Examples section
|
| 458 |
-
gr.Markdown("###
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
task_hist = [((image_path,), None)]
|
| 492 |
-
return history, task_hist, prompt
|
| 493 |
-
|
| 494 |
-
# Example 4: Visual Q&A
|
| 495 |
-
def load_example_4(history, task_hist):
|
| 496 |
-
prompt = "Look at this chart and answer: Which quarter had the highest revenue? What was the Sales value in Q4?"
|
| 497 |
-
image_path = EXAMPLE_IMAGES["vqa"]
|
| 498 |
-
history = [((image_path,), None)]
|
| 499 |
-
task_hist = [((image_path,), None)]
|
| 500 |
-
return history, task_hist, prompt
|
| 501 |
-
|
| 502 |
-
# Example 5: Translation
|
| 503 |
-
def load_example_5(history, task_hist):
|
| 504 |
-
prompt = "Translate all text in this image to English."
|
| 505 |
-
image_path = EXAMPLE_IMAGES["translation"]
|
| 506 |
-
history = [((image_path,), None)]
|
| 507 |
-
task_hist = [((image_path,), None)]
|
| 508 |
-
return history, task_hist, prompt
|
| 509 |
-
|
| 510 |
-
# Bind events
|
| 511 |
-
example_1_btn.click(load_example_1, [chatbot, task_history], [chatbot, task_history, query])
|
| 512 |
-
example_2_btn.click(load_example_2, [chatbot, task_history], [chatbot, task_history, query])
|
| 513 |
-
example_3_btn.click(load_example_3, [chatbot, task_history], [chatbot, task_history, query])
|
| 514 |
-
example_4_btn.click(load_example_4, [chatbot, task_history], [chatbot, task_history, query])
|
| 515 |
-
example_5_btn.click(load_example_5, [chatbot, task_history], [chatbot, task_history, query])
|
| 516 |
-
|
| 517 |
-
submit_btn.click(add_text, [chatbot, task_history, query],
|
| 518 |
-
[chatbot, task_history]).then(predict, [chatbot, task_history], [chatbot], show_progress=True)
|
| 519 |
-
submit_btn.click(reset_user_input, [], [query])
|
| 520 |
-
empty_bin.click(reset_state, [chatbot, task_history], [chatbot], show_progress=True)
|
| 521 |
-
regen_btn.click(regenerate, [chatbot, task_history], [chatbot], show_progress=True)
|
| 522 |
-
addfile_btn.upload(add_file, [chatbot, task_history, addfile_btn], [chatbot, task_history], show_progress=True)
|
| 523 |
|
| 524 |
# Feature descriptions
|
| 525 |
with gr.Row():
|
|
|
|
| 454 |
regen_btn = gr.Button('Regenerate')
|
| 455 |
empty_bin = gr.Button('Clear')
|
| 456 |
|
| 457 |
+
# Examples section with image thumbnails
|
| 458 |
+
gr.Markdown("### Examples - Click to load")
|
| 459 |
+
|
| 460 |
+
# Hidden image input for examples
|
| 461 |
+
example_image = gr.Image(type="filepath", visible=False)
|
| 462 |
+
|
| 463 |
+
def load_example(image_path, prompt):
|
| 464 |
+
"""Load example into chatbot"""
|
| 465 |
+
history = [((image_path,), None)]
|
| 466 |
+
return history, history, prompt
|
| 467 |
+
|
| 468 |
+
gr.Examples(
|
| 469 |
+
examples=[
|
| 470 |
+
[EXAMPLE_IMAGES["spotting"], "Detect and recognize all text in this image. Output the text with bounding box coordinates."],
|
| 471 |
+
[EXAMPLE_IMAGES["parsing"], "Extract all text from this document in markdown format. Use HTML for tables and LaTeX for equations."],
|
| 472 |
+
[EXAMPLE_IMAGES["ie"], "Extract the following fields from this receipt and return as JSON: ['total', 'subtotal', 'tax', 'date', 'items']"],
|
| 473 |
+
[EXAMPLE_IMAGES["vqa"], "Look at this chart and answer: Which quarter had the highest revenue? What was the Sales value in Q4?"],
|
| 474 |
+
[EXAMPLE_IMAGES["translation"], "Translate all text in this image to English."],
|
| 475 |
+
],
|
| 476 |
+
inputs=[example_image, query],
|
| 477 |
+
outputs=[chatbot, task_history, query],
|
| 478 |
+
fn=load_example,
|
| 479 |
+
cache_examples=False,
|
| 480 |
+
examples_per_page=5,
|
| 481 |
+
)
|
| 482 |
+
|
| 483 |
+
task_history = gr.State([])
|
| 484 |
+
|
| 485 |
+
submit_btn.click(add_text, [chatbot, task_history, query],
|
| 486 |
+
[chatbot, task_history]).then(predict, [chatbot, task_history], [chatbot], show_progress=True)
|
| 487 |
+
submit_btn.click(reset_user_input, [], [query])
|
| 488 |
+
empty_bin.click(reset_state, [chatbot, task_history], [chatbot], show_progress=True)
|
| 489 |
+
regen_btn.click(regenerate, [chatbot, task_history], [chatbot], show_progress=True)
|
| 490 |
+
addfile_btn.upload(add_file, [chatbot, task_history, addfile_btn], [chatbot, task_history], show_progress=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 491 |
|
| 492 |
# Feature descriptions
|
| 493 |
with gr.Row():
|