File size: 13,624 Bytes
8152a82
 
2fa693e
8152a82
 
 
d2ac459
2fa693e
8152a82
 
 
d2ac459
8152a82
317211f
8152a82
 
 
 
 
 
 
 
 
 
317211f
8152a82
 
 
 
 
 
 
 
317211f
d2ac459
8152a82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fa693e
 
 
 
 
 
 
8152a82
 
 
 
 
 
 
 
 
 
 
2fa693e
 
 
8152a82
 
 
2fa693e
 
 
 
 
8152a82
 
 
2fa693e
8152a82
 
2fa693e
 
 
 
 
 
8152a82
 
 
2fa693e
8152a82
 
 
 
 
2fa693e
 
 
 
 
 
 
 
 
 
 
 
 
 
8152a82
2fa693e
8152a82
 
 
 
 
 
 
2fa693e
8152a82
 
2fa693e
 
8152a82
 
2fa693e
 
8152a82
2fa693e
 
 
 
 
 
8152a82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ee1e49
 
 
8152a82
 
 
 
0ee1e49
8152a82
 
 
 
 
 
 
0ee1e49
 
8152a82
 
 
 
 
 
 
 
 
 
 
8ed1654
8152a82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fa693e
 
 
 
8152a82
 
 
 
 
 
 
2fa693e
8152a82
2fa693e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8152a82
 
 
 
 
 
 
 
2fa693e
8152a82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fa693e
9f4ab8c
2fa693e
 
8152a82
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import base64
import os
import re
from io import BytesIO
from pathlib import Path

import gradio as gr
from langchain.schema.output_parser import OutputParserException
from PIL import Image

import categories
from categories import Category
from main import process_image, process_pdf

HF_TOKEN = os.getenv("HF_TOKEN")
PDF_IFRAME = """
<div style="border-radius: 10px; width: 100%; overflow: hidden;">
    <iframe
        src="data:application/pdf;base64,{0}"
        width="100%"
        height="400"
        type="application/pdf">
    </iframe>
</div>"""

hf_writer_normal = gr.HuggingFaceDatasetSaver(
    HF_TOKEN, "automatic-reimbursement-tool-demo", separate_dirs=False
)
hf_writer_incorrect = gr.HuggingFaceDatasetSaver(
    HF_TOKEN, "automatic-reimbursement-tool-demo-incorrect", separate_dirs=False
)
# with open("examples/example1.pdf", "rb") as pdf_file:
#     base64_pdf = base64.b64encode(pdf_file.read())


# example_paths = []
# current_file_path = None

# def ignore_examples(function):
#     def new_function(*args, **kwargs):
#         global example_paths, current_file_path
#         if current_file_path not in example_paths:
#             return function(*args, **kwargs)


def display_file(input_file):
    global current_file_path
    current_file_path = input_file.name if input_file else None
    if not input_file:
        return gr.HTML.update(visible=False), gr.Image.update(visible=False)
    if input_file.name.endswith(".pdf"):
        with open(input_file.name, "rb") as input_file:
            pdf_base64 = base64.b64encode(input_file.read()).decode()
        return gr.HTML.update(
            PDF_IFRAME.format(pdf_base64), visible=True
        ), gr.Image.update(visible=False)
    else:
        # image = Image.open(input_file.name)
        return gr.HTML.update(visible=False), gr.Image.update(
            input_file.name, visible=True
        )


def show_intermediate_outputs(show_intermediate):
    if show_intermediate:
        return gr.Accordion.update(visible=True)
    else:
        return gr.Accordion.update(visible=False)


def show_share_contact(share_result):
    return gr.Textbox.update(visible=share_result)


def clear_inputs():
    return gr.File.update(value=None)


def clear_outputs(input_file):
    if input_file:
        return None, None, None, None


def extract_text(input_file):
    """Takes the input file and updates the extracted text"""
    if not input_file:
        gr.Error("Please upload a file to continue!")
        return gr.Textbox.update()
    # Send change to preprocessed image or to extracted text
    if input_file.name.endswith(".pdf"):
        text = process_pdf(Path(input_file.name), extract_only=True)
    else:
        text = process_image(Path(input_file.name), extract_only=True)
    return text


def categorize_text(text):
    """Takes the extracted text and updates the category"""
    category = categories.categorize_text(text)
    return category


def query(category, text):
    """Takes the extracted text and category and updates the chatbot in two steps:
    1. Construct a prompt
    2. Generate a response
    """
    category = Category[category]
    chain = categories.category_modules[category].chain
    formatted_prompt = chain.prompt.format_prompt(
        text=text,
        format_instructions=chain.output_parser.get_format_instructions(),
    )
    question = f""
    if len(formatted_prompt.messages) > 1:
        question += f"**System:**\n{formatted_prompt.messages[0].content}"
    question += f"\n\n**Human:**\n{formatted_prompt.messages[1].content}"
    yield gr.Chatbot.update([[question, "Generating..."]])

    result = chain.generate(
        input_list=[
            {
                "text": text,
                "format_instructions": chain.output_parser.get_format_instructions(),
            }
        ]
    )
    answer = result.generations[0][0].text
    yield gr.Chatbot.update([[question, answer]])


PARSING_REGEXP = r"\*\*System:\*\*\n([\s\S]+)\n\n\*\*Human:\*\*\n([\s\S]+)"


def parse(category, chatbot):
    """Takes the chatbot prompt and response and updates the extracted information"""
    global PARSING_REGEXP

    answer = chatbot[0][1]
    category = Category[category]
    chain = categories.category_modules[category].chain
    yield {"status": "Parsing response..."}
    try:
        information = chain.output_parser.parse(answer)
        information = information.json() if information else {}
    except OutputParserException as e:
        information = {
            "error": "Unable to parse chatbot output",
            "details": str(e),
            "output": e.llm_output,
        }
    yield information


def activate_flags():
    return gr.Button.update(interactive=True), gr.Button.update(interactive=True)


def deactivate_flags():
    return gr.Button.update(interactive=False), gr.Button.update(interactive=False)


def flag_if_shared(flag_method):
    def proxy(share_result, request: gr.Request, *args, **kwargs):
        if share_result:
            return flag_method(request, *args, **kwargs)
    return proxy


with gr.Blocks(title="Automatic Reimbursement Tool Demo") as page:
    gr.Markdown("<center><h1>Automatic Reimbursement Tool Demo</h1></center>")
    gr.Markdown("<h2>Description</h2>")
    gr.Markdown(
        "The reimbursement filing process can be time-consuming and cumbersome, causing "
        "frustration for faculty members and finance departments. Our project aims to "
        "automate the information extraction involved in the process by feeding "
        "extracted text to language models such as ChatGPT. This demo showcases the "
        "categorization and extraction parts of the pipeline. Categorization is done "
        "to identify the relevant details associated with the text, after which "
        "extraction is done for those details using a language model."
    )
    gr.Markdown("<h2>Try it out!</h2>")
    with gr.Box() as demo:
        with gr.Row():
            with gr.Column(variant="panel"):
                gr.HTML(
                    '<div><center style="color:rgb(200, 200, 200);">Input</center></div>'
                )
                pdf_preview = gr.HTML(label="Preview", show_label=True, visible=False)
                image_preview = gr.Image(
                    label="Preview", show_label=True, visible=False, height=350
                )
                input_file = gr.File(
                    label="Input receipt",
                    show_label=True,
                    type="file",
                    file_count="single",
                    file_types=["image", ".pdf"],
                )
                input_file.change(
                    display_file, input_file, [pdf_preview, image_preview]
                )

                with gr.Row():
                    clear = gr.Button("Clear", variant="secondary")
                    submit_button = gr.Button("Submit", variant="primary")

                show_intermediate = gr.Checkbox(
                    False,
                    label="Show intermediate outputs",
                    info="There are several intermediate steps in the process such as "
                         "preprocessing, OCR, chatbot interaction. You can choose to "
                         "show their results here.",
                )
                share_result = gr.Checkbox(
                    True,
                    label="Share results",
                    info="Sharing your result with us will help us improve this tool.",
                    interactive=True,
                )
                contact = gr.Textbox(
                    type="email",
                    label="Contact",
                    interactive=True,
                    placeholder="Enter your email address",
                    info="Optionally, enter your email address to allow us to contact "
                         "you regarding your result.",
                    visible=True,
                )
                share_result.change(show_share_contact, share_result, [contact])

            with gr.Column(variant="panel"):
                gr.HTML(
                    '<div><center style="color:rgb(200, 200, 200);">Output</center></div>'
                )
                category = gr.Dropdown(
                    value=None,
                    choices=Category.__members__.keys(),
                    label=f"Recognized category ({', '.join(Category.__members__.keys())})",
                    show_label=True,
                    interactive=False,
                )
                intermediate_outputs = gr.Accordion(
                    "Intermediate outputs", open=True, visible=False
                )
                with intermediate_outputs:
                    extracted_text = gr.Textbox(
                        label="Extracted text",
                        show_label=True,
                        max_lines=5,
                        show_copy_button=True,
                        lines=5,
                        interactive=False,
                    )
                    chatbot = gr.Chatbot(
                        None,
                        label="Chatbot interaction",
                        show_label=True,
                        interactive=False,
                        height=240,
                    )
                information = gr.JSON(label="Extracted information")
                with gr.Row():
                    flag_incorrect_button = gr.Button(
                        "Flag as incorrect", variant="stop", interactive=True
                    )
                    flag_irrelevant_button = gr.Button(
                        "Flag as irrelevant", variant="stop", interactive=True
                    )
            show_intermediate.change(
                show_intermediate_outputs, show_intermediate, [intermediate_outputs]
            )

            clear.click(clear_inputs, None, [input_file]).then(
                deactivate_flags,
                None,
                [flag_incorrect_button, flag_irrelevant_button],
            )

            hf_writer_normal.setup(
                [input_file, extracted_text, category, chatbot, information, contact],
                flagging_dir="flagged",
            )
            flag_method = gr.flagging.FlagMethod(
                hf_writer_normal, "", "", visual_feedback=False
            )

            submit_button.click(
                clear_outputs,
                [input_file],
                [extracted_text, category, chatbot, information],
            ).then(
                extract_text,
                [input_file],
                [extracted_text],
            ).then(
                categorize_text,
                [extracted_text],
                [category],
            ).then(
                query,
                [category, extracted_text],
                [chatbot],
                queue=True,
            ).then(
                parse,
                [category, chatbot],
                [information],
            ).then(
                activate_flags,
                None,
                [flag_incorrect_button, flag_irrelevant_button],
            ).then(
                flag_if_shared(flag_method),
                [
                    share_result,
                    input_file,
                    extracted_text,
                    category,
                    chatbot,
                    information,
                    contact,
                ],
                None,
                preprocess=False,
            )

            hf_writer_incorrect.setup(
                [input_file, extracted_text, category, chatbot, information, contact],
                flagging_dir="flagged_incorrect",
            )
            flag_incorrect_method = gr.flagging.FlagMethod(
                hf_writer_incorrect,
                "Flag as incorrect",
                "Incorrect",
                visual_feedback=True,
            )
            flag_incorrect_button.click(
                lambda: gr.Button.update(value="Saving...", interactive=False),
                None,
                flag_incorrect_button,
                queue=False,
            )
            flag_incorrect_button.click(
                flag_incorrect_method,
                inputs=[
                    input_file,
                    extracted_text,
                    category,
                    chatbot,
                    information,
                    contact,
                ],
                outputs=[flag_incorrect_button],
                preprocess=False,
                queue=False,
            )

            flag_irrelevant_method = gr.flagging.FlagMethod(
                hf_writer_incorrect,
                "Flag as irrelevant",
                "Irrelevant",
                visual_feedback=True,
            )
            flag_irrelevant_button.click(
                lambda: gr.Button.update(value="Saving...", interactive=False),
                None,
                flag_irrelevant_button,
                queue=False,
            )
            flag_irrelevant_button.click(
                flag_irrelevant_method,
                inputs=[
                    input_file,
                    extracted_text,
                    category,
                    chatbot,
                    information,
                    contact,
                ],
                outputs=[flag_irrelevant_button],
                preprocess=False,
                queue=False,
            )

page.queue(
    concurrency_count=20,
    max_size=1,
)
page.launch(show_api=True, show_error=True, debug=True)