File size: 4,108 Bytes
79904b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import traceback
import gradio as gr
from utils.get_RGB_image import get_RGB_image, is_online_file, steam_online_file
from pdf2image import convert_from_path, convert_from_bytes

def similarity_fn(document_image_1, document_image_2):
    xxx = 'BOTH' if document_image_1 and document_image_2 else 'ONE' if document_image_1 or document_image_2 else 'NONE'
    return xxx

def load_image(filename, page = 0):
    try:
        image = None
        try:
            if (is_online_file(filename)):
                image = get_RGB_image(convert_from_bytes(steam_online_file(filename))[page])
            else:
                image = get_RGB_image(convert_from_path(filename)[page])
        except:
            image = get_RGB_image(filename)
        return [
            gr.Image(value=image, visible=True), 
            None
        ]
    except:
        error = traceback.format_exc()
        return [None, gr.HTML(value=error, visible=True)]
    
def preview_url(url, page = 0):
    [image, error] = load_image(url, page = page)
    if image:
        return [gr.Tabs(selected=0), image, error]
    else:
        return [gr.Tabs(selected=1), image, error]
    
def document_view(document_number: int):
    gr.HTML(value=f'<h4>Load the {"first" if document_number == 1 else "second"} PDF or Document Image<h4>', elem_classes=['center'])
    with gr.Tabs() as document_tabs:
        with gr.Tab("From Image", id=0):
            document = gr.Image(type="pil", label=f"Document {document_number}", visible=False)
            document_error_message = gr.HTML(label="Error Message", visible=False)
            document_preview = gr.UploadButton(
                "Click to PDF or Document Image", 
                file_types=["image", ".pdf"], 
                file_count="single")
        with gr.Tab("From URL", id=1):
            document_url = gr.Textbox(
                label=f"Document {document_number} URL",
                info="Paste a Link/URL to PDF or Document Image",
                placeholder="https://datasets-server.huggingface.co/.../image.jpg")
            document_url_error_message = gr.HTML(label="Error Message", visible=False)
            document_url_preview = gr.Button(value="Preview", variant="primary")
    document_preview.upload(
         fn = lambda file: load_image(file.name), 
         inputs = [document_preview], 
         outputs = [document, document_error_message])
    document_url_preview.click(
        fn = preview_url, 
        inputs = [document_url], 
        outputs = [document_tabs, document, document_url_error_message])
    return document

def app():
    title = 'Document Similarity Search Using Visual Layout Features'
    description = "<h2>Document Similarity Search using Detectron2<h2>"
    article = "<h4>More details, Links about this! - Document Similarity Search using Detectron2<h4>"
    css = '''
    image { max-height="86vh" !important; }
    .center { display: flex; flex: 1 1 auto; align-items: center; align-content: center; justify-content: center; justify-items: center; }
  '''
    with gr.Blocks(title=title, css=css) as app:
        with gr.Row():
            gr.HTML(value=description, elem_classes=['center'])
        with gr.Row(equal_height = False):
            with gr.Column():
                document_1_image = document_view(1)
            with gr.Column():
                document_2_image = document_view(2)
        with gr.Row():
            # gr.HTML(value=article, elem_classes=['center'])
            submit = gr.Button(value="Preview", variant="primary")
            similarity_output = gr.HTML(value=article, elem_classes=['center'], visible=False)
            submit.click(
                fn=lambda image: similarity_fn(image, just_image=False),
                inputs=[document_1_image, document_2_image],
                outputs=[similarity_output])
        
        # threshold.change(
        #     fn=lambda image, threshold: similarity_fn(image, threshold, just_image=False),
        #     inputs=[document_1, threshold],
        #     outputs=[annotated_document_image, message])
    return app.launch(debug=True)