gsarti commited on
Commit
645be2c
β€’
1 Parent(s): 2574e16

Final style update

Browse files
Files changed (5) hide show
  1. app.py +19 -13
  2. contents.py +11 -23
  3. presets.py +22 -21
  4. requirements.txt +2 -1
  5. style.py +1 -1
app.py CHANGED
@@ -4,7 +4,8 @@ import os
4
  import gradio as gr
5
  import spaces
6
  from contents import (
7
- citation,
 
8
  description,
9
  examples,
10
  how_it_works_intro,
@@ -12,12 +13,14 @@ from contents import (
12
  cci_explanation,
13
  how_to_use,
14
  example_explanation,
 
15
  subtitle,
16
  title,
17
  powered_by,
18
  support,
19
  )
20
  from gradio_highlightedtextbox import HighlightedTextbox
 
21
  from presets import (
22
  set_chatml_preset,
23
  set_cora_preset,
@@ -178,7 +181,9 @@ with gr.Blocks(css=custom_css) as demo:
178
  input_current_text = gr.Textbox(
179
  label="Input query", placeholder="Your input query..."
180
  )
181
- attribute_input_button = gr.Button("Run PECoRe", variant="primary")
 
 
182
  with gr.Column():
183
  pecore_output_highlights = HighlightedTextbox(
184
  value=[
@@ -215,7 +220,6 @@ with gr.Blocks(css=custom_css) as demo:
215
  attribute_input_examples = gr.Examples(
216
  examples,
217
  inputs=[input_current_text, input_context_text],
218
- outputs=pecore_output_highlights,
219
  examples_per_page=1,
220
  )
221
  with gr.Tab("βš™οΈ Parameters") as params_tab:
@@ -478,7 +482,16 @@ with gr.Blocks(css=custom_css) as demo:
478
  with gr.Tab("πŸ”§ Usage Guide"):
479
  gr.Markdown(how_to_use)
480
  gr.Markdown(example_explanation)
481
- update_code_snippets = gr.Button("Update code snippets", variant="primary")
 
 
 
 
 
 
 
 
 
482
  with gr.Row(equal_height=True):
483
  python_code_snippet = gr.Code(
484
  value="""Generate Python code snippet by pressing the button.""",
@@ -494,12 +507,6 @@ with gr.Blocks(css=custom_css) as demo:
494
  interactive=False,
495
  show_label=True,
496
  )
497
- with gr.Tab("πŸ“š Citing PECoRe"):
498
- gr.Markdown(citation)
499
- with gr.Row(elem_classes="footer-container"):
500
- gr.Markdown(powered_by)
501
- gr.Markdown(support)
502
-
503
 
504
  # Main logic
505
 
@@ -650,11 +657,10 @@ with gr.Blocks(css=custom_css) as demo:
650
  ],
651
  ).success(preload_model, inputs=load_model_args, cancels=load_model_event)
652
 
653
- update_code_snippets.click(
654
  update_code_snippets_fn,
655
  inputs=pecore_args,
656
  outputs=[python_code_snippet, shell_code_snippet],
657
-
658
- )
659
 
660
  demo.launch(allowed_paths=["outputs/", "img/"])
 
4
  import gradio as gr
5
  import spaces
6
  from contents import (
7
+ pecore_citation,
8
+ inseq_citation,
9
  description,
10
  examples,
11
  how_it_works_intro,
 
13
  cci_explanation,
14
  how_to_use,
15
  example_explanation,
16
+ show_code_modal,
17
  subtitle,
18
  title,
19
  powered_by,
20
  support,
21
  )
22
  from gradio_highlightedtextbox import HighlightedTextbox
23
+ from gradio_modal import Modal
24
  from presets import (
25
  set_chatml_preset,
26
  set_cora_preset,
 
181
  input_current_text = gr.Textbox(
182
  label="Input query", placeholder="Your input query..."
183
  )
184
+ with gr.Row(equal_height=True):
185
+ show_code_btn = gr.Button("Show code", variant="secondary")
186
+ attribute_input_button = gr.Button("Run PECoRe", variant="primary")
187
  with gr.Column():
188
  pecore_output_highlights = HighlightedTextbox(
189
  value=[
 
220
  attribute_input_examples = gr.Examples(
221
  examples,
222
  inputs=[input_current_text, input_context_text],
 
223
  examples_per_page=1,
224
  )
225
  with gr.Tab("βš™οΈ Parameters") as params_tab:
 
482
  with gr.Tab("πŸ”§ Usage Guide"):
483
  gr.Markdown(how_to_use)
484
  gr.Markdown(example_explanation)
485
+ with gr.Tab("πŸ“š Citing PECoRe"):
486
+ gr.Markdown("To refer to the PECoRe framework for context usage detection, cite:")
487
+ gr.Code(pecore_citation, interactive=False, label="PECoRe (Sarti et al., 2024)")
488
+ gr.Markdown("If you use the Inseq implementation of PECoRe (<a href=\"https://inseq.org/en/latest/main_classes/cli.html#attribute-context\"><code>inseq attribute-context</code></a>, including this demo), please also cite:")
489
+ gr.Code(inseq_citation, interactive=False, label="Inseq (Sarti et al., 2023)")
490
+ with gr.Row(elem_classes="footer-container"):
491
+ gr.Markdown(powered_by)
492
+ gr.Markdown(support)
493
+ with Modal(visible=False) as code_modal:
494
+ gr.Markdown(show_code_modal)
495
  with gr.Row(equal_height=True):
496
  python_code_snippet = gr.Code(
497
  value="""Generate Python code snippet by pressing the button.""",
 
507
  interactive=False,
508
  show_label=True,
509
  )
 
 
 
 
 
 
510
 
511
  # Main logic
512
 
 
657
  ],
658
  ).success(preload_model, inputs=load_model_args, cancels=load_model_event)
659
 
660
+ show_code_btn.click(
661
  update_code_snippets_fn,
662
  inputs=pecore_args,
663
  outputs=[python_code_snippet, shell_code_snippet],
664
+ ).then(lambda: Modal(visible=True), None, code_modal)
 
665
 
666
  demo.launch(allowed_paths=["outputs/", "img/"])
contents.py CHANGED
@@ -54,19 +54,15 @@ example_explanation = """
54
  <li>When using a model, make sure that the <b>contextual and contextless templates are set to match the expected format</b>. You can use presets to auto-fill these for the provided models.</li>
55
  <li>If you are using an encoder-decoder expecting an output context (e.g. the multilingual MT preset), the <b>output context should be provided manually</b> before running PECoRe in the <code>Generation context</code> parameter. This is a requirement for the demo because the splitting between output context and current cannot be reliably performed in an automatic way. However, the <code>inseq attribute-context</code> CLI command actually support various strategies, including prompting users for a split and/or trying an automatic source-target alignment. </li>
56
  </ol>
57
- <h2>Using PECoRe from Python with Inseq</h3>
 
 
 
58
  <p>This demo is useful for testing out various models and methods for PECoRe attribution, but the <a href="https://inseq.org/en/latest/main_classes/cli.html#attribute-context"><code>inseq attribute-context</code></a> CLI command is the way to go if you want to run experiments on several examples, or if you want to exploit the full customizability of the Inseq API.</p>
59
- <p>The utility we provide in this section allows you to generate Python and Shell code calling the Inseq CLI with the parameters you set in the interface. <b>We recommend using the Python version for repeated evaluation, since it allows for model-preloading.</b></p>
60
- <p>Once you are satisfied with the parameters you set (including context/query strings in the <code>πŸ‘ Demo</code> tab), just press the button and get your code snippets ready for usage! πŸ€—</p>
61
  """
62
 
63
- citation = r"""
64
- <p>To refer to the PECoRe framework for context usage detection, cite:</p>
65
- <div class="code_wrap"><button class="copy_code_button" title="copy">
66
- <span class="copy-text"><svg viewBox="0 0 32 32" height="100%" width="100%" xmlns="http://www.w3.org/2000/svg"><path d="M28 10v18H10V10h18m0-2H10a2 2 0 0 0-2 2v18a2 2 0 0 0 2 2h18a2 2 0 0 0 2-2V10a2 2 0 0 0-2-2Z" fill="currentColor"></path><path d="M4 18H2V4a2 2 0 0 1 2-2h14v2H4Z" fill="currentColor"></path></svg></span>
67
- <span class="check"><svg stroke-linejoin="round" stroke-linecap="round" stroke-width="3" stroke="currentColor" fill="none" viewBox="0 0 24 24" height="100%" width="100%" xmlns="http://www.w3.org/2000/svg"><polyline points="20 6 9 17 4 12"></polyline></svg></span>
68
- </button><pre><code>
69
- @inproceedings{sarti-etal-2023-quantifying,
70
  title = "Quantifying the Plausibility of Context Reliance in Neural Machine Translation",
71
  author = "Sarti, Gabriele and
72
  Chrupa{\l}a, Grzegorz and
@@ -78,15 +74,9 @@ citation = r"""
78
  address = "Vienna, Austria",
79
  publisher = "OpenReview",
80
  url = "https://openreview.net/forum?id=XTHfNGI3zT"
81
- }
82
- </code></pre></div>
83
-
84
- If you use the Inseq implementation of PECoRe (<a href="https://inseq.org/en/latest/main_classes/cli.html#attribute-context"><code>inseq attribute-context</code></a>, including this demo), please also cite:
85
- <div class="code_wrap"><button class="copy_code_button" title="copy">
86
- <span class="copy-text"><svg viewBox="0 0 32 32" height="100%" width="100%" xmlns="http://www.w3.org/2000/svg"><path d="M28 10v18H10V10h18m0-2H10a2 2 0 0 0-2 2v18a2 2 0 0 0 2 2h18a2 2 0 0 0 2-2V10a2 2 0 0 0-2-2Z" fill="currentColor"></path><path d="M4 18H2V4a2 2 0 0 1 2-2h14v2H4Z" fill="currentColor"></path></svg></span>
87
- <span class="check"><svg stroke-linejoin="round" stroke-linecap="round" stroke-width="3" stroke="currentColor" fill="none" viewBox="0 0 24 24" height="100%" width="100%" xmlns="http://www.w3.org/2000/svg"><polyline points="20 6 9 17 4 12"></polyline></svg></span>
88
- </button><pre><code>
89
- @inproceedings{sarti-etal-2023-inseq,
90
  title = "Inseq: An Interpretability Toolkit for Sequence Generation Models",
91
  author = "Sarti, Gabriele and
92
  Feldhus, Nils and
@@ -101,13 +91,11 @@ If you use the Inseq implementation of PECoRe (<a href="https://inseq.org/en/lat
101
  publisher = "Association for Computational Linguistics",
102
  url = "https://aclanthology.org/2023.acl-demo.40",
103
  pages = "421--435",
104
- }
105
- </code></pre></div>
106
- """
107
 
108
  powered_by = """<div class="footer-custom-block"><b>Powered by</b> <a href='https://github.com/inseq-team/inseq' target='_blank'><img src="file/img/inseq_logo_white_contour.png" width=150px /></a></div>"""
109
 
110
- support = """<div class="footer-custom-block"><b>Built by <a href="https://gsarti.com" target="_blank">Gabriele Sarti</a> with the support of</b> <a href='https://www.rug.nl/research/clcg/research/cl/' target='_blank'><img src="file/img/rug_logo_white_contour.png" width=170px /></a><a href='https://projects.illc.uva.nl/indeep/' target='_blank'><img src="file/img/indeep_logo_white_contour.png" width=100px /></a><a href='https://www.esciencecenter.nl/' target='_blank'><img src="file/img/escience_logo_white_contour.png" width=120px /></a></div>"""
111
 
112
  examples = [
113
  [
 
54
  <li>When using a model, make sure that the <b>contextual and contextless templates are set to match the expected format</b>. You can use presets to auto-fill these for the provided models.</li>
55
  <li>If you are using an encoder-decoder expecting an output context (e.g. the multilingual MT preset), the <b>output context should be provided manually</b> before running PECoRe in the <code>Generation context</code> parameter. This is a requirement for the demo because the splitting between output context and current cannot be reliably performed in an automatic way. However, the <code>inseq attribute-context</code> CLI command actually support various strategies, including prompting users for a split and/or trying an automatic source-target alignment. </li>
56
  </ol>
57
+ """
58
+
59
+ show_code_modal = """
60
+ <h2>Using PECoRe CLI</h3>
61
  <p>This demo is useful for testing out various models and methods for PECoRe attribution, but the <a href="https://inseq.org/en/latest/main_classes/cli.html#attribute-context"><code>inseq attribute-context</code></a> CLI command is the way to go if you want to run experiments on several examples, or if you want to exploit the full customizability of the Inseq API.</p>
62
+ <p>The snippets provided below are updated based on the current parameter configuration of the demo, and allow you to use Python and Shell code to call the Inseq CLI. <b>We recommend using the Python version for repeated evaluation, since it allows for model-preloading.</b></p>
 
63
  """
64
 
65
+ pecore_citation = """@inproceedings{sarti-etal-2023-quantifying,
 
 
 
 
 
 
66
  title = "Quantifying the Plausibility of Context Reliance in Neural Machine Translation",
67
  author = "Sarti, Gabriele and
68
  Chrupa{\l}a, Grzegorz and
 
74
  address = "Vienna, Austria",
75
  publisher = "OpenReview",
76
  url = "https://openreview.net/forum?id=XTHfNGI3zT"
77
+ }"""
78
+
79
+ inseq_citation = """@inproceedings{sarti-etal-2023-inseq,
 
 
 
 
 
 
80
  title = "Inseq: An Interpretability Toolkit for Sequence Generation Models",
81
  author = "Sarti, Gabriele and
82
  Feldhus, Nils and
 
91
  publisher = "Association for Computational Linguistics",
92
  url = "https://aclanthology.org/2023.acl-demo.40",
93
  pages = "421--435",
94
+ }"""
 
 
95
 
96
  powered_by = """<div class="footer-custom-block"><b>Powered by</b> <a href='https://github.com/inseq-team/inseq' target='_blank'><img src="file/img/inseq_logo_white_contour.png" width=150px /></a></div>"""
97
 
98
+ support = """<div class="footer-custom-block"><b>Built by <a href="https://gsarti.com" target="_blank">Gabriele Sarti</a><br> with the support of</b> <a href='https://www.rug.nl/research/clcg/research/cl/' target='_blank'><img src="file/img/rug_logo_white_contour.png" width=170px /></a><a href='https://projects.illc.uva.nl/indeep/' target='_blank'><img src="file/img/indeep_logo_white_contour.png" width=100px /></a><a href='https://www.esciencecenter.nl/' target='_blank'><img src="file/img/escience_logo_white_contour.png" width=120px /></a></div>"""
99
 
100
  examples = [
101
  [
presets.py CHANGED
@@ -107,6 +107,10 @@ def update_code_snippets_fn(
107
  ) -> tuple[str, str]:
108
  if not input_current_text:
109
  input_current_text = "<MISSING INPUT CURRENT TEXT, REQUIRED>"
 
 
 
 
110
  def py_get_kwargs_str(kwargs: str, name: str, pad: str = " " * 4) -> str:
111
  kwargs_dict = json.loads(kwargs)
112
  return nl + pad + name + '=' + str(kwargs_dict) + ',' if kwargs_dict else ''
@@ -120,18 +124,16 @@ def update_code_snippets_fn(
120
  else:
121
  return nl + pad + name + "=" + str(arg) + ","
122
  def sh_get_kwargs_str(kwargs: str, name: str, pad: str = " " * 4) -> str:
123
- return nl + pad + f"--{name} " + '"' + str(kwargs).replace("\n", "").replace('"', '\\"') + '"' + " \\\\" if json.loads(kwargs) else ''
124
  def sh_get_if_specified(arg: str | int | float | list | None, name: str, pad: str = " " * 4) -> str:
125
  if arg is None or (isinstance(arg, (str, list)) and not arg) or (isinstance(arg, (int, float)) and arg <= 0):
126
  return ""
127
  elif isinstance(arg, str):
128
- return nl + pad + f"--{name} " + '"' + arg.replace('"', '\\"') + '"' + " \\\\"
129
  elif isinstance(arg, list):
130
- return nl + pad + f"--{name} " + " ".join(str(arg)) + " \\\\"
131
  else:
132
- return nl + pad + f"--{name} " + str(arg) + " \\\\"
133
- nl = "\n"
134
- tq = "\"\"\""
135
  # Python
136
  python = f"""#!pip install inseq
137
  import inseq
@@ -161,20 +163,19 @@ pecore_args = AttributeContextArgs(
161
  out = attribute_context_with_model(pecore_args, loaded_model)"""
162
  # Bash
163
  bash = f"""# pip install inseq
164
- inseq attribute-context \\\\
165
- --model-name-or-path "{model_name_or_path}" \\\\
166
- --attribution-method "{attribution_method}" \\\\
167
- --attributed-fn "{attributed_fn}" \\\\
168
- --context-sensitivity-metric "{context_sensitivity_metric}" \\\\
169
- --context-sensitivity-std-threshold {context_sensitivity_std_threshold} \\\\{sh_get_if_specified(context_sensitivity_topk, "context-sensitivity-topk")}
170
- --attribution-std-threshold {attribution_std_threshold} \\\\{sh_get_if_specified(attribution_topk, "attribution-topk")}
171
- --input-current-text "{input_current_text}" \\\\{sh_get_if_specified(input_context_text, "input-context-text")}
172
- --contextless-input-current-text "{contextless_input_template}" \\\\
173
- --input-template "{input_template}" \\\\{sh_get_if_specified(output_current_text, "output-current-text")}{sh_get_if_specified(output_context_text, "output-context-text")}
174
- --contextless-output-current-text "{contextless_output_template}" \\\\
175
- --output-template "{output_template}" \\\\{sh_get_if_specified(special_tokens_to_keep, "special_tokens_to_keep")}{sh_get_if_specified(decoder_input_output_separator, "decoder-input-output-separator")}
176
- --save-path pecore_output.json \\\\
177
- --viz-path pecore_output.html \\\\{sh_get_kwargs_str(model_kwargs, "model-kwargs")}{sh_get_kwargs_str(tokenizer_kwargs, "tokenizer-kwargs")}{sh_get_kwargs_str(generation_kwargs, "generation-kwargs")}{sh_get_kwargs_str(attribution_kwargs, "attribution-kwargs")}
178
- """
179
  return python, bash
180
 
 
107
  ) -> tuple[str, str]:
108
  if not input_current_text:
109
  input_current_text = "<MISSING INPUT CURRENT TEXT, REQUIRED>"
110
+ nl = "\n"
111
+ tq = "\"\"\""
112
+ def escape_quotes(s: str) -> str:
113
+ return s.replace('"', '\\"')
114
  def py_get_kwargs_str(kwargs: str, name: str, pad: str = " " * 4) -> str:
115
  kwargs_dict = json.loads(kwargs)
116
  return nl + pad + name + '=' + str(kwargs_dict) + ',' if kwargs_dict else ''
 
124
  else:
125
  return nl + pad + name + "=" + str(arg) + ","
126
  def sh_get_kwargs_str(kwargs: str, name: str, pad: str = " " * 4) -> str:
127
+ return nl + pad + f"--{name} " + '"' + escape_quotes("".join(x.strip() for x in str(kwargs).split("\n"))) + '"' + " \\" if json.loads(kwargs) else ''
128
  def sh_get_if_specified(arg: str | int | float | list | None, name: str, pad: str = " " * 4) -> str:
129
  if arg is None or (isinstance(arg, (str, list)) and not arg) or (isinstance(arg, (int, float)) and arg <= 0):
130
  return ""
131
  elif isinstance(arg, str):
132
+ return nl + pad + f"--{name} " + '"' + escape_quotes(arg) + '"' + " \\"
133
  elif isinstance(arg, list):
134
+ return nl + pad + f"--{name} " + " ".join(str(arg)) + " \\"
135
  else:
136
+ return nl + pad + f"--{name} " + str(arg) + " \\"
 
 
137
  # Python
138
  python = f"""#!pip install inseq
139
  import inseq
 
163
  out = attribute_context_with_model(pecore_args, loaded_model)"""
164
  # Bash
165
  bash = f"""# pip install inseq
166
+ inseq attribute-context \\
167
+ --model_name_or_path "{model_name_or_path}" \\
168
+ --attribution_method "{attribution_method}" \\
169
+ --attributed_fn "{attributed_fn}" \\
170
+ --context_sensitivity_metric "{context_sensitivity_metric}" \\
171
+ --context_sensitivity_std_threshold {context_sensitivity_std_threshold} \\{sh_get_if_specified(context_sensitivity_topk, "context_sensitivity_topk")}
172
+ --attribution_std_threshold {attribution_std_threshold} \\{sh_get_if_specified(attribution_topk, "attribution_topk")}
173
+ --input_current_text "{escape_quotes(input_current_text)}" \\{sh_get_if_specified(input_context_text, "input_context_text")}
174
+ --contextless_input_current_text "{escape_quotes(contextless_input_template)}" \\
175
+ --input_template "{escape_quotes(input_template)}" \\{sh_get_if_specified(output_current_text, "output_current_text")}{sh_get_if_specified(output_context_text, "output_context_text")}
176
+ --contextless_output_current_text "{escape_quotes(contextless_output_template)}" \\
177
+ --output_template "{escape_quotes(output_template)}" \\{sh_get_if_specified(special_tokens_to_keep, "special_tokens_to_keep")}{sh_get_if_specified(decoder_input_output_separator, "decoder_input_output_separator")}
178
+ --save_path "pecore_output.json" \\
179
+ --viz_path "pecore_output.html" \\{sh_get_kwargs_str(model_kwargs, "model_kwargs")}{sh_get_kwargs_str(tokenizer_kwargs, "tokenizer_kwargs")}{sh_get_kwargs_str(generation_kwargs, "generation_kwargs")}{sh_get_kwargs_str(attribution_kwargs, "attribution_kwargs")}""".strip("\\")
 
180
  return python, bash
181
 
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  spaces
2
  git+https://github.com/inseq-team/inseq.git@main
3
- gradio_highlightedtextbox
 
 
1
  spaces
2
  git+https://github.com/inseq-team/inseq.git@main
3
+ gradio_highlightedtextbox
4
+ gradio_modal
style.py CHANGED
@@ -35,7 +35,7 @@ custom_css = """
35
  margin-right: 10px;
36
  }
37
 
38
- .footer-custom-block a {
39
  margin-right: 15px;
40
  }
41
 
 
35
  margin-right: 10px;
36
  }
37
 
38
+ .footer-custom-block img {
39
  margin-right: 15px;
40
  }
41