victor HF Staff commited on
Commit
2de81b2
·
1 Parent(s): 9532898

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

Files changed (2) hide show
  1. .DS_Store +0 -0
  2. app.py +34 -66
.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("### Quick Examples - Click to load")
459
-
460
- with gr.Row():
461
- example_1_btn = gr.Button("Text Detection")
462
- example_2_btn = gr.Button("Document Parsing")
463
- example_3_btn = gr.Button("Info Extraction")
464
- example_4_btn = gr.Button("Visual Q&A")
465
- example_5_btn = gr.Button("Translation")
466
-
467
- task_history = gr.State([])
468
-
469
-
470
- # Example 1: Text Detection
471
- def load_example_1(history, task_hist):
472
- prompt = "Detect and recognize all text in this image. Output the text with bounding box coordinates."
473
- image_path = EXAMPLE_IMAGES["spotting"]
474
- history = [((image_path,), None)]
475
- task_hist = [((image_path,), None)]
476
- return history, task_hist, prompt
477
-
478
- # Example 2: Document Parsing
479
- def load_example_2(history, task_hist):
480
- prompt = "Extract all text from this document in markdown format. Use HTML for tables and LaTeX for equations. Parse in reading order."
481
- image_path = EXAMPLE_IMAGES["parsing"]
482
- history = [((image_path,), None)]
483
- task_hist = [((image_path,), None)]
484
- return history, task_hist, prompt
485
-
486
- # Example 3: Information Extraction
487
- def load_example_3(history, task_hist):
488
- prompt = "Extract the following fields from this receipt and return as JSON: ['total', 'subtotal', 'tax', 'date', 'items']"
489
- image_path = EXAMPLE_IMAGES["ie"]
490
- history = [((image_path,), None)]
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():