Shanshan Wang commited on
Commit
80d1b7a
·
1 Parent(s): e753774

added a preset prompts

Browse files
Files changed (1) hide show
  1. app.py +55 -15
app.py CHANGED
@@ -21,6 +21,14 @@ model_paths = {
21
  }
22
 
23
 
 
 
 
 
 
 
 
 
24
  def load_model_and_set_image_function(model_name):
25
  # Get the model path from the model_paths dictionary
26
  model_path = model_paths[model_name]
@@ -227,6 +235,28 @@ with gr.Blocks() as demo:
227
  label="Select Model",
228
  value="H2OVL-Mississippi-2B"
229
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
 
231
  # When the model selection changes, load the new model
232
  model_dropdown.change(
@@ -235,26 +265,28 @@ with gr.Blocks() as demo:
235
  outputs=[model_state]
236
  )
237
 
238
- # Load the default model when the app starts
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
  demo.load(
240
  fn=load_model_and_set_image_function,
241
  inputs=[model_dropdown],
242
  outputs=[model_state]
243
  )
244
 
245
- with gr.Row(equal_height=True):
246
- # First column with image input
247
- with gr.Column(scale=1):
248
- image_input = gr.Image(type="filepath", label="Upload an Image")
249
-
250
-
251
- # Second column with chatbot and user input
252
- with gr.Column(scale=2):
253
- chatbot = gr.Chatbot(label="Conversation")
254
- user_input = gr.Textbox(label="What is your question",
255
- placeholder="Type your message here",
256
- interactive=True,
257
- lines=1)
258
 
259
  with gr.Accordion('Parameters', open=False):
260
  with gr.Row():
@@ -329,7 +361,13 @@ with gr.Blocks() as demo:
329
  fn=clear_all,
330
  inputs=None,
331
  outputs=[chatbot, state, image_input, user_input]
332
- )
 
 
 
 
 
 
333
  gr.Examples(
334
  examples=[
335
  ["assets/driver_license.png", "Extract the text from the image and fill the following json {'license_number':'',\n'full_name':'',\n'date_of_birth':'',\n'address':'',\n'issue_date':'',\n'expiration_date':'',\n}"],
@@ -338,6 +376,8 @@ with gr.Blocks() as demo:
338
  ["assets/CBA-1H23-Results-Presentation_wheel.png", "What is the efficiency of H2O.AI in document processing?"],
339
  ],
340
  inputs = [image_input, user_input],
 
 
341
  label = "examples",
342
  )
343
  demo.queue()
 
21
  }
22
 
23
 
24
+ example_prompts = [
25
+ "Read the text and provide word by word ocr for the document. <doc>",
26
+ "Extract the text from the image.",
27
+ "Extract the text from the image and fill the following json {'license_number':'',\n'full_name':'',\n'date_of_birth':'',\n'address':'',\n'issue_date':'',\n'expiration_date':'',\n}",
28
+ "Please extract the following fields, and return the result in JSON format: supplier_name, supplier_address, customer_name, customer_address, invoice_number, invoice_total_amount, invoice_tax_amount",
29
+ ]
30
+
31
+
32
  def load_model_and_set_image_function(model_name):
33
  # Get the model path from the model_paths dictionary
34
  model_path = model_paths[model_name]
 
235
  label="Select Model",
236
  value="H2OVL-Mississippi-2B"
237
  )
238
+
239
+
240
+ with gr.Row(equal_height=True):
241
+ # First column with image input
242
+ with gr.Column(scale=1):
243
+ image_input = gr.Image(type="filepath", label="Upload an Image")
244
+
245
+
246
+ # Second column with chatbot and user input
247
+ with gr.Column(scale=2):
248
+ chatbot = gr.Chatbot(label="Conversation")
249
+ user_input = gr.Dropdown(label="What is your question",
250
+ choices = example_prompts,
251
+ value=None,
252
+ allow_custom_value=True,
253
+ interactive=True)
254
+
255
+
256
+ def reset_chatbot_state():
257
+ # reset chatbot and state
258
+ return [], None
259
+
260
 
261
  # When the model selection changes, load the new model
262
  model_dropdown.change(
 
265
  outputs=[model_state]
266
  )
267
 
268
+ model_dropdown.change(
269
+ fn=reset_chatbot_state,
270
+ inputs=None,
271
+ outputs=[chatbot, state]
272
+ )
273
+
274
+
275
+ # Reset chatbot and state when image input changes
276
+ image_input.change(
277
+ fn=reset_chatbot_state,
278
+ inputs=None,
279
+ outputs=[chatbot, state]
280
+ )
281
+
282
+ # Load the default model when the app starts
283
  demo.load(
284
  fn=load_model_and_set_image_function,
285
  inputs=[model_dropdown],
286
  outputs=[model_state]
287
  )
288
 
289
+
 
 
 
 
 
 
 
 
 
 
 
 
290
 
291
  with gr.Accordion('Parameters', open=False):
292
  with gr.Row():
 
361
  fn=clear_all,
362
  inputs=None,
363
  outputs=[chatbot, state, image_input, user_input]
364
+ )
365
+
366
+ def example_clicked(example):
367
+ image_value, user_input_value = example
368
+ return image_value, user_input_value, [], None # Reset chatbot and state
369
+
370
+
371
  gr.Examples(
372
  examples=[
373
  ["assets/driver_license.png", "Extract the text from the image and fill the following json {'license_number':'',\n'full_name':'',\n'date_of_birth':'',\n'address':'',\n'issue_date':'',\n'expiration_date':'',\n}"],
 
376
  ["assets/CBA-1H23-Results-Presentation_wheel.png", "What is the efficiency of H2O.AI in document processing?"],
377
  ],
378
  inputs = [image_input, user_input],
379
+ outputs = [image_input, user_input, chatbot, state],
380
+ fn=example_clicked,
381
  label = "examples",
382
  )
383
  demo.queue()