File size: 1,616 Bytes
0bd5bed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import gradio as gr
import warnings
import os
import pix2struct, layoutlm, donut
warnings.filterwarnings('ignore')


def process_image_and_generate_output(image, model_selection, question):
    result = ''
    if image is None:
        return "Please select an image", None

    if model_selection == "LayoutLM":
        result = layoutlm.get_result(image, question)
        return result
    if model_selection == 'Pix2Struct':
        result = pix2struct.get_result(image, question)
        return result
    if model_selection == 'Donut':
        result = donut.get_result(image, question)
        return result

    return result


sample_images = [
    [os.path.join(os.path.dirname(__file__), "images/1.png"), "LayoutLM", "What is the NIC Code?"],
    [os.path.join(os.path.dirname(__file__), "images/1.png"), "Pix2Struct", "What is the NIC Code?"],
    [os.path.join(os.path.dirname(__file__), "images/1.png"), "Donut", "What is the NIC Code?"]
]

# Create a dropdown to select sample image
image_input = gr.Image(label="Upload Image", type='filepath')

# Create a dropdown to choose the model
model_selection_input = gr.Radio(["LayoutLM", "Pix2Struct", "Donut"],
                                 label="Choose Model")
question_input = gr.Text(label="Question")

iface = gr.Interface(fn=process_image_and_generate_output,
                     inputs=[image_input, model_selection_input, question_input],
                     outputs=gr.Text(label="Result"),
                     allow_flagging='never',
                     examples=sample_images,
                     title="DocVQA Sanctum")

iface.launch()