Spaces:
Runtime error
Runtime error
File size: 15,578 Bytes
38f8c33 6502808 38f8c33 6502808 38f8c33 196b397 38f8c33 196b397 38f8c33 07fceec 38f8c33 196b397 38f8c33 196b397 38f8c33 07fceec 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 196b397 6502808 367e9b2 38f8c33 6502808 38f8c33 6502808 07fceec 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 07fceec 6502808 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 07fceec 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 367e9b2 38f8c33 6502808 367e9b2 6502808 367e9b2 38f8c33 6502808 367e9b2 6502808 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 38f8c33 6502808 |
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 |
from __future__ import annotations
from typing import Iterable
import gradio as gr
from base.legal_document_utils import (
summarize,
question_answer,
load_gpl_license,
load_pokemon_license,
)
from base.document_search import cross_lingual_document_search, translate_search_result
from gradio.themes.base import Base
from gradio.themes import Soft
from gradio.themes.utils import colors, fonts, sizes
class CustomTheme(Base):
def __init__(
self,
*,
primary_hue: colors.Color | str = colors.blue,
secondary_hue: colors.Color | str = colors.cyan,
neutral_hue: colors.Color | str = colors.zinc,
spacing_size: sizes.Size | str = sizes.spacing_md,
radius_size: sizes.Size | str = sizes.radius_md,
text_size: sizes.Size | str = sizes.text_md,
):
super().__init__(
primary_hue=primary_hue,
secondary_hue=secondary_hue,
neutral_hue=neutral_hue,
spacing_size=spacing_size,
radius_size=radius_size,
text_size=text_size,
)
self.name = "custom_theme"
super().set(
# Colors
background_fill_primary="*neutral_50",
slider_color="*primary_500",
slider_color_dark="*primary_600",
# Shadows
shadow_drop="0 1px 4px 0 rgb(0 0 0 / 0.1)",
shadow_drop_lg="0 2px 5px 0 rgb(0 0 0 / 0.1)",
# Block Labels
block_background_fill="white",
block_label_padding="*spacing_sm *spacing_md",
block_label_background_fill="*primary_100",
block_label_background_fill_dark="*primary_600",
block_label_radius="*radius_md",
block_label_text_size="*text_md",
block_label_text_weight="600",
block_label_text_color="*primary_500",
block_label_text_color_dark="*white",
block_title_radius="*block_label_radius",
block_title_padding="*block_label_padding",
block_title_background_fill="*block_label_background_fill",
block_title_text_weight="600",
block_title_text_color="*primary_500",
block_title_text_color_dark="*white",
block_label_margin="*spacing_md",
block_shadow="*shadow_drop_lg",
# Inputs
input_border_color="*neutral_50",
input_shadow="*shadow_drop",
input_shadow_focus="*shadow_drop_lg",
checkbox_shadow="none",
# Buttons
shadow_spread="6px",
button_shadow="*shadow_drop_lg",
button_shadow_hover="*shadow_drop_lg",
button_shadow_active="*shadow_inset",
button_primary_background_fill="linear-gradient(90deg, *primary_300, *secondary_400)",
button_primary_background_fill_hover="linear-gradient(90deg, *primary_200, *secondary_300)",
button_primary_text_color="white",
button_primary_background_fill_dark="linear-gradient(90deg, *primary_600, *secondary_800)",
button_primary_background_fill_hover_dark="*primary_500",
button_secondary_background_fill="white",
button_secondary_background_fill_hover="*neutral_100",
button_secondary_background_fill_hover_dark="*primary_500",
button_secondary_text_color="*neutral_800",
button_cancel_background_fill="*button_secondary_background_fill",
button_cancel_background_fill_hover="*button_secondary_background_fill_hover",
button_cancel_background_fill_hover_dark="*button_secondary_background_fill_hover",
button_cancel_text_color="*button_secondary_text_color",
#checkboxes
checkbox_label_shadow="*shadow_drop_lg",
checkbox_label_background_fill_selected="*primary_500",
checkbox_label_background_fill_selected_dark="*primary_600",
checkbox_border_width="1px",
checkbox_border_color="*neutral_100",
checkbox_border_color_dark="*neutral_600",
checkbox_background_color_selected="*primary_600",
checkbox_background_color_selected_dark="*primary_700",
checkbox_border_color_focus="*primary_500",
checkbox_border_color_focus_dark="*primary_600",
checkbox_border_color_selected="*primary_600",
checkbox_border_color_selected_dark="*primary_700",
checkbox_label_text_color_selected="white",
# Borders
block_border_width="0px",
panel_border_width="1px",
)
custom_theme = CustomTheme()
max_search_results = 3
def reset_chatbot():
return gr.update(value="")
def get_user_input(input_question, history):
return "", history + [[input_question, None]]
def legal_doc_qa_bot(input_document, history):
bot_message = question_answer(input_document, history)
history[-1][1] = bot_message
return history
with gr.Blocks(theme=custom_theme) as demo:
gr.HTML(
"""<html><center><img src='file/logo/flc_design4.png', alt='Legal-ease logo', width=250, height=250 /></center><br></html>"""
)
qa_bot_state = gr.State(value=[])
with gr.Tabs():
with gr.TabItem("Q&A"):
gr.HTML(
"""<p style="text-align:center;"><b>Legal documents can be difficult to comprehend and understand. Add a legal document below and ask any questions related to it.</p>"""
)
with gr.Row():
with gr.Column():
input_document = gr.Text(label="Copy your document here", lines=10)
with gr.Column():
chatbot = gr.Chatbot(label="Chat History")
input_question = gr.Text(
label="Ask a question",
placeholder="Type a question here and hit enter.",
)
clear = gr.Button("Clear", variant="primary")
with gr.Row():
with gr.Accordion("Show example inputs I can load:", open=False):
example_1 = gr.Button(
"Load GPL License Document", variant="primary"
)
example_2 = gr.Button(
"Load Pokemon Go Terms of Service", variant="primary"
)
with gr.TabItem("Summarize"):
gr.HTML(
"""<p style="text-align:center;"><b>Legal documents can be very lengthy. Add a legal document below and generate a quick summary for it.</p>"""
)
with gr.Row():
with gr.Column():
summary_input = gr.Text(label="Document", lines=10)
generate_summary = gr.Button("Generate Summary", variant="primary")
with gr.Column():
summary_output = gr.Text(label="Summary", lines=10)
invisible_comp = gr.Text(label="Dummy Component", visible=False)
with gr.Row():
with gr.Accordion("Advanced Settings:", open=False):
summary_length = gr.Radio(
["short", "medium", "long"],
label="Summary Length",
value="long",
)
summary_format = gr.Radio(
["paragraph", "bullets"],
label="Summary Format",
value="bullets",
)
extractiveness = gr.Radio(
["low", "medium", "high"],
label="Extractiveness",
info="Controls how close to the original text the summary is.",
visible=False,
value="high",
)
temperature = gr.Slider(
minimum=0,
maximum=5.0,
value=0.64,
step=0.1,
interactive=True,
visible=False,
label="Temperature",
info="Controls the randomness of the output. Lower values tend to generate more “predictable” output, while higher values tend to generate more “creative” output.",
)
with gr.Row():
with gr.Accordion("Show example inputs I can load:", open=False):
example_3 = gr.Button(
"Load GPL License Document", variant="primary"
)
example_4 = gr.Button(
"Load Pokemon Go Terms of Service", variant="primary"
)
with gr.TabItem("Document Search"):
gr.HTML(
"""<p style="text-align:center;"><b>Search across a set of legal documents in any language or even a mix of languages. Query them using any one of over 100 supported languages.</p>"""
)
gr.HTML(
"""<p style="text-align:center; font-style:italic;">Get started with a pre-indexed set of documents from eight European countries (Belgium, France, Hungary, Italy, Netherlands, Norway, Poland, UK) in seven languages, outlining legislation passed during the COVID-19 pandemic.</p>"""
)
with gr.Row():
text_match = gr.CheckboxGroup(
["Full Text Search"], label="find exact text in documents"
)
with gr.Row():
lang_choices = gr.CheckboxGroup(
[
"English",
"French",
"Italian",
"Dutch",
"Polish",
"Hungarian",
"Norwegian",
],
label="Filter results based on language",
)
with gr.Row():
with gr.Column():
user_query = gr.Text(
label="Enter query here",
placeholder="Search through all your documents",
)
num_search_results = gr.Slider(
1,
max_search_results,
visible=False,
value=max_search_results,
step=1,
interactive=True,
label="How many search results to show:",
)
with gr.Row():
with gr.Column():
query_match_out_1 = gr.Textbox(label=f"Search Result 1")
with gr.Column():
with gr.Accordion("Translate Search Result", open=False):
translate_1 = gr.Button(
label="Translate",
value="Translate",
variant="primary",
)
translate_res_1 = gr.Textbox(
label=f"Translation Result 1"
)
with gr.Row():
with gr.Column():
query_match_out_2 = gr.Textbox(label=f"Search Result 2")
with gr.Column():
with gr.Accordion("Translate Search Result", open=False):
translate_2 = gr.Button(
label="Translate",
value="Translate",
variant="primary",
)
translate_res_2 = gr.Textbox(
label=f"Translation Result 2"
)
with gr.Row():
with gr.Column():
query_match_out_3 = gr.Textbox(label=f"Search Result 3")
with gr.Column():
with gr.Accordion("Translate Search Result", open=False):
translate_3 = gr.Button(
label="Translate",
value="Translate",
variant="primary",
)
translate_res_3 = gr.Textbox(
label=f"Translation Result 3"
)
# fetch answer for submitted question corresponding to input document
input_question.submit(
get_user_input,
[input_question, chatbot],
[input_question, chatbot],
queue=False,
).then(legal_doc_qa_bot, [input_document, chatbot], chatbot)
# reset the chatbot Q&A history when input document changes
input_document.change(fn=reset_chatbot, inputs=[], outputs=chatbot)
# Loading examples on click for Q&A module
example_1.click(
load_gpl_license,
[],
[input_document, input_question],
queue=False,
)
example_2.click(
load_pokemon_license,
[],
[input_document, input_question],
queue=False,
)
# Loading examples on click for Q&A module
example_3.click(
load_gpl_license,
[],
[summary_input, invisible_comp],
queue=False,
)
example_4.click(
load_pokemon_license,
[],
[summary_input, invisible_comp],
queue=False,
)
# generate summary corresponding to document submitted by the user.
generate_summary.click(
summarize,
[summary_input, summary_length, summary_format, extractiveness, temperature],
[summary_output],
queue=False,
)
# clear the chatbot Q&A history when this button is clicked by the user
clear.click(lambda: None, None, chatbot, queue=False)
# run search as user is typing the query
user_query.change(
cross_lingual_document_search,
[user_query, num_search_results, lang_choices, text_match],
[query_match_out_1, query_match_out_2, query_match_out_3],
queue=False,
)
# run search if user submits query
user_query.submit(
cross_lingual_document_search,
[user_query, num_search_results, lang_choices, text_match],
[query_match_out_1, query_match_out_2, query_match_out_3],
queue=False,
)
# translate results corresponding to 1st search result obtained if user clicks 'Translate'
translate_1.click(
translate_search_result,
[query_match_out_1, user_query],
[translate_res_1],
queue=False,
)
# translate results corresponding to 2nd search result obtained if user clicks 'Translate'
translate_2.click(
translate_search_result,
[query_match_out_2, user_query],
[translate_res_2],
queue=False,
)
# translate results corresponding to 3rd search result obtained if user clicks 'Translate'
translate_3.click(
translate_search_result,
[query_match_out_3, user_query],
[translate_res_3],
queue=False,
)
if __name__ == "__main__":
demo.launch(debug=True)
|