File size: 14,220 Bytes
92f3006
 
51fbfa6
f2965eb
 
 
2f12302
 
 
 
c6d3d04
7221fd1
 
51fbfa6
2f12302
 
51fbfa6
5bfb443
 
 
 
 
 
 
 
2f12302
 
 
 
 
51fbfa6
2f12302
 
 
5bfb443
 
 
51fbfa6
92f3006
2f12302
 
 
 
 
 
 
 
 
 
 
7221fd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25be378
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7221fd1
 
 
 
 
 
 
 
 
 
 
b2ecf83
 
2f12302
 
92f3006
 
25be378
92f3006
2f12302
 
92f3006
 
 
 
2f12302
f2965eb
2f12302
 
 
 
 
 
799b85e
27b17df
2f12302
 
84e6019
2f12302
 
 
 
 
 
 
 
 
92f3006
51fbfa6
2f12302
 
 
799b85e
 
 
 
92f3006
f2965eb
 
b2ecf83
799b85e
f2965eb
2f12302
 
 
c77919f
b2ecf83
c77919f
f2965eb
7221fd1
f2965eb
51fbfa6
2f12302
25be378
 
2f12302
92f3006
2f12302
92f3006
2f12302
9b2e531
 
2f12302
9b2e531
 
 
2f12302
9b2e531
 
7221fd1
 
 
 
 
 
 
 
b2ecf83
 
7221fd1
 
 
 
 
b2ecf83
 
9b2e531
 
 
c6d3d04
9b2e531
2f12302
9b2e531
25be378
c6d3d04
2f12302
 
f2965eb
 
2f12302
f2965eb
b2ecf83
f2965eb
 
bf0c174
 
7221fd1
 
 
 
 
bf0c174
 
 
 
 
7221fd1
 
 
 
 
bf0c174
 
 
 
2f12302
 
 
c6d3d04
2f12302
 
 
 
c6d3d04
2f12302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27b17df
2f12302
 
 
 
 
 
799b85e
 
 
 
2f12302
 
 
 
7221fd1
2f12302
 
 
 
c6d3d04
7221fd1
c6d3d04
2f12302
7221fd1
 
2f12302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6d3d04
 
 
 
 
2f12302
 
 
 
 
c6d3d04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f12302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d9f8d7
 
 
 
2f12302
 
 
 
 
 
 
 
 
 
c6d3d04
 
 
92f3006
 
 
 
 
 
 
 
 
 
 
 
 
 
51fbfa6
92f3006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d9f8d7
92f3006
 
 
 
 
 
 
 
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import os

import pypdfium2 as pdfium
import gradio as gr

from langchain.chat_models import ChatOpenAI
from src.summarization import (
    parallel_summarization,
    PARALLEL_SUMMARIZATION_MAPPING,
)
from src.legal_implications import parallel_legal_implications
from src.mailing_utils import update_email_content, construct_url
import urllib


def _file_render_helper(file):
    pdf = pdfium.PdfDocument(file.name)
    images = []
    for page_index in range(len(pdf)):
        page = pdf.get_page(page_index)
        bitmap = page.render(
            scale=200 / 72,  # 200dpi resolution
            rotation=0,  # no additional rotation
            # ... further rendering options
        )
        images.append(
            (bitmap.to_pil(), f"{file.name.split('/')[-1]} Seite {page_index+1}")
        )
    return images


# Function to render a specific page of a PDF file as an image
def render_file(file):
    images = _file_render_helper(file)
    return gr.update(
        value=images,
    )


def render_files(files):
    all_images = []
    for file in files:
        images = _file_render_helper(file)
        all_images.extend(images)

    return gr.update(
        value=all_images,
    )


def create_email_textbox() -> gr.Textbox:
    return gr.Textbox(
        label="Email Instructions",
        placeholder="Write Email Instructions here.",
        value=(
            "Dear Recipient\n\n"
            "Please find the summarization of the uploaded document below.\n\n"
            "<TEXT_FROM_LLM>\n\n"
            "Kind regards,\n"
            "Your Legal Assistant"
        ),
        lines=9,
    )


def switch_buttons(interactive: bool):
    """This switches the buttons to interactive or not interactive.

    Args:
        interactive (bool): Whether the buttons should be interactive or not.
    """
    return (
        gr.update(
            interactive=interactive,
        ),
        gr.update(
            interactive=interactive,
        ),
        gr.update(
            interactive=interactive,
        ),
    )


def update_email_url(address: str, subject: str, email_instructions_summary: str):
    body = urllib.parse.quote(email_instructions_summary)
    return gr.update(link=construct_url(address=address, subject=subject, body=body))


def update_email_instructions_summary(email_wrapping_content: str, summary: str):
    return gr.update(
        value=update_email_content(
            email_wrapping_content=email_wrapping_content, summary=summary
        )
    )


def load_summary_section(llm: ChatOpenAI):
    """Load the summary section

    Args:
        llm (ChatOpenAI): Language model.

    Returns:
        gr.Blocks: The summarization section
    """

    with gr.Blocks(
        theme="soft",
    ) as summary_section:
        with gr.Row().style(equal_height=True):
            with gr.Column(scale=1):
                file_upload_summary = gr.File(
                    file_count="single",
                    file_types=[".pdf", ".txt"],
                    label="Upload PDF",
                )
                summary_parallel_button = gr.Button("Summarize", interactive=False)
                clear_button = gr.Button("Clear All")
            with gr.Column(scale=2):
                sections_to_select = [
                    i for i in PARALLEL_SUMMARIZATION_MAPPING.keys() if "II." not in i
                ]
                summary_sections_dropdown = gr.Dropdown(
                    sections_to_select,
                    value=sections_to_select,
                    interactive=True,
                    multiselect=True,
                    label="Sections for Summarization",
                    info="Select the sections you want to include in the summarization.",
                )
        with gr.Row().style(equal_height=True):
            with gr.Column(scale=1):
                summary_show_pdf = gr.Gallery(label="Uploaded PDF").style(
                    object_fit="contain"
                )
            with gr.Column(scale=1):
                summary_output = gr.Textbox(label="Summary", lines=9).style(
                    show_copy_button=True
                )

        with gr.Row().style(equal_height=True):
            with gr.Column(scale=1):
                recipient_email_summary = gr.Textbox(
                    label="Recipient Email", placeholder="Enter Email"
                )
                subject_email_summary = gr.Textbox(
                    label="Subject", placeholder="Enter Subject"
                )
                send_email_button = gr.Button(
                    "TEST email", link="mailto:", interactive=False, variant="primary"
                )
            with gr.Column(scale=3):
                email_instructions_summary = create_email_textbox()

        # Once a file is uploaded, enable the summarization buttons and visualize the uploaded file
        file_upload_summary.upload(
            switch_buttons,
            [gr.State(True)],
            [summary_parallel_button, gr.State(None), gr.State(None)],
            queue=False,
        ).then(fn=render_file, inputs=[file_upload_summary], outputs=[summary_show_pdf])

        summary_parallel_button.click(
            switch_buttons,
            [gr.State(False)],
            [summary_parallel_button, gr.State(None), gr.State(None)],
            queue=False,
        ).then(
            parallel_summarization,
            [file_upload_summary, summary_sections_dropdown, gr.State([llm])],
            [summary_output],
            queue=False,
        ).then(
            fn=update_email_instructions_summary,
            inputs=[
                email_instructions_summary,
                summary_output,
            ],
            outputs=[email_instructions_summary],
            queue=False,
        ).then(
            fn=update_email_url,
            inputs=[
                recipient_email_summary,
                subject_email_summary,
                email_instructions_summary,
            ],
            outputs=[send_email_button],
            queue=False,
        ).then(
            switch_buttons,
            [gr.State(True)],
            [summary_parallel_button, send_email_button, gr.State(None)],
            queue=False,
        ).then

        # The clear button clears the dashboard
        clear_button.click(lambda: None, None, summary_output, queue=False).then(
            lambda: None, None, file_upload_summary, queue=False
        ).then(lambda: None, None, summary_show_pdf, queue=False).then(
            lambda: None, None, send_email_button, queue=False
        ).then(
            lambda: None, None, email_instructions_summary, queue=False
        ).then(
            lambda: None, None, recipient_email_summary, queue=False
        )

        recipient_email_summary.input(
            fn=update_email_url,
            inputs=[
                recipient_email_summary,
                subject_email_summary,
                email_instructions_summary,
            ],
            outputs=[send_email_button],
            queue=False,
        )
        subject_email_summary.input(
            fn=update_email_url,
            inputs=[
                recipient_email_summary,
                subject_email_summary,
                email_instructions_summary,
            ],
            outputs=[send_email_button],
            queue=False,
        )

    return summary_section


def load_legal_implications_section(llm: ChatOpenAI, summarization_kwargs: dict = {}):
    """Load the legal implications section

    Args:
        llm (ChatOpenAI): Language model.
        summarization_kwargs (dict, optional): Keyword arguments for the summarization. Defaults to {}.

    Returns:
        gr.Block: Legal Implications Section
    """
    with gr.Blocks(theme="soft") as legal_implications_section:
        with gr.Row().style(equal_height=True):
            with gr.Column(scale=3):
                file_upload_legal_implications = gr.File(
                    file_count="multiple",
                    file_types=[".pdf", ".txt"],
                    label="Upload PDF",
                )
            with gr.Column(scale=1):
                extract_legal_implications_button = gr.Button(
                    "Extract Legal Implications", interactive=False
                )
                clear_legal_implications_button = gr.Button("Clear All")

        with gr.Row().style(equal_height=True):
            with gr.Column(scale=1):
                legal_implications_show_pdf = gr.Gallery(label="Uploaded PDF").style(
                    object_fit="contain"
                )
            with gr.Column(scale=1):
                legal_implications_output = gr.Textbox(
                    label="Legal Implications", lines=9
                ).style(show_copy_button=True)

        with gr.Row().style(equal_height=True):
            with gr.Column(scale=1):
                recipiant_email_legal_implications = gr.Textbox(
                    label="Recipient Email", placeholder="Enter Email"
                )
                subject_email_legal_implications = gr.Textbox(
                    label="Subject", placeholder="Enter Subject"
                )
                send_email_button_legal_implications = gr.Button(
                    "Open Email", link="mailto:", interactive=False
                )
            with gr.Column(scale=3):
                email_instructions_legal_implications = create_email_textbox()

        # Once a file is uploaded, enable the summarization buttons and visualize the uploaded file
        file_upload_legal_implications.upload(
            switch_buttons,
            [gr.State(True)],
            [extract_legal_implications_button, gr.State(None), gr.State(None)],
            queue=False,
        ).then(
            fn=render_files,
            inputs=[file_upload_legal_implications],
            outputs=[legal_implications_show_pdf],
        )

        extract_legal_implications_button.click(
            switch_buttons,
            [gr.State(False)],
            [extract_legal_implications_button, gr.State(None), gr.State(None)],
            queue=False,
        ).then(
            parallel_legal_implications,
            [
                file_upload_legal_implications,
                gr.State([llm]),
                gr.State(summarization_kwargs),
            ],
            [legal_implications_output],
            queue=False,
        ).then(
            switch_buttons,
            [gr.State(True)],
            [
                extract_legal_implications_button,
                send_email_button_legal_implications,
                gr.State(None),
            ],
            queue=False,
        )
        # The clear button clears the dashboard
        clear_legal_implications_button.click(
            lambda: None, None, legal_implications_output, queue=False
        ).then(lambda: None, None, file_upload_legal_implications, queue=False).then(
            lambda: None, None, legal_implications_show_pdf, queue=False
        ).then(
            lambda: None, None, send_email_button_legal_implications, queue=False
        ).then(
            lambda: None, None, email_instructions_legal_implications, queue=False
        ).then(
            lambda: None, None, recipiant_email_legal_implications, queue=False
        )


def run_summarization_model_gradio(
    llm: ChatOpenAI,
    share_gradio_via_link: bool = False,
    summarization_kwargs: dict = {},
    run_local: bool = False,
):
    """Run the Summarization assistant with gradio

    Args:
        llm (ChatOpenAI): Language model.
        share_gradio_via_link (bool, optional): Whether to launch the gradio app via a public link. Defaults to False.
        summarization_kwargs (dict, optional): Keyword arguments for the summarization. Defaults to {}.
        run_local (bool, optional): Whether to run the gradio app locally. Defaults to False.

    """
    title = "Legal Document Assistant"
    description = (
        f"This is a proof of concept. Don't upload any confidential documents."
    )

    with gr.Blocks(
        theme="soft",
        title=title,
    ) as webui:
        with gr.Row().style(equal_height=True):
            Header_box = generate_title(title=title, description=description)
        with gr.Tab("Summarize Verdict"):
            load_summary_section(llm=llm)
        with gr.Tab("Legal Implications"):
            load_legal_implications_section(
                llm=llm, summarization_kwargs=summarization_kwargs
            )

    webui.queue()

    if run_local:
        webui.launch(share=share_gradio_via_link)
    else:
        basic_auth = (
            "lenox",
            os.environ["PROTOTYPE_PASSWORD"],
        )
        webui.launch(auth=basic_auth)


def generate_title(title: str, description: str):
    """THIS IS AN EXAMPLE HTML CODE FOR A TITLE"""
    return gr.HTML(
        (
            """
            <html>
            <head>
            <style>
            .awesome-title {
                background: linear-gradient(27deg, rgba(252,92,125,1) 19%, rgba(79,70,229,1) 45%);
                color: #FFFFFF;
                text-align: center;
                padding: 20px;
                border-radius: 10px;
                height: 10%;
                box-shadow: 0px 0px 20px 0px rgba(0,0,0,0.1);
            }
            
            .awesome-title h1 {
                font-family: 'Arial', sans-serif;
                font-size: 32px;
                margin: 0;
            }
            
            .awesome-title p {
                font-family: 'Arial', sans-serif;
                font-size: 16px;
                margin: 10px 0 0;
            }
            </style>
            </head>
            <body>
            """
            + f"""
            <div class="awesome-title">
                <img src="https://aeiljuispo.cloudimg.io/v7/https://cdn-uploads.huggingface.co/production/uploads/64e3560ad5e75575cd641c9c/WpgaNwGDfaTMjsimt8i87.png" alt="Lenox AI" style="height: 80%; position:absolute;">
                <h1>{title}</h1>
                <p>{description}</p>
            </div>
            </body>
            </html>
            """
        ).strip(),
    )