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

rug logo and codegen fixes

Browse files
Files changed (3) hide show
  1. contents.py +2 -2
  2. img/rug_logo_white_contour.png +0 -0
  3. presets.py +48 -43
contents.py CHANGED
@@ -56,7 +56,7 @@ example_explanation = """
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. This is useful to understand how to use the Inseq API and quickly get up to speed with running PECoRe on your own models and data.</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
 
@@ -107,7 +107,7 @@ If you use the Inseq implementation of PECoRe (<a href="https://inseq.org/en/lat
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>With the support of</b> <a href='https://projects.illc.uva.nl/indeep/' target='_blank'><img src="file/img/indeep_logo_white_contour.png" width=120px /></a><a href='https://www.esciencecenter.nl/' target='_blank'><img src="file/img/escience_logo_white_contour.png" width=160px /></a></div>"""
111
 
112
  examples = [
113
  [
 
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
 
 
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
  [
img/rug_logo_white_contour.png ADDED
presets.py CHANGED
@@ -105,9 +105,31 @@ def update_code_snippets_fn(
105
  generation_kwargs: str,
106
  attribution_kwargs: str,
107
  ) -> tuple[str, str]:
108
- def get_kwargs_str(kwargs: str, name: str, pad: str = " " * 4) -> str:
 
 
109
  kwargs_dict = json.loads(kwargs)
110
  return nl + pad + name + '=' + str(kwargs_dict) + ',' if kwargs_dict else ''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  nl = "\n"
112
  tq = "\"\"\""
113
  # Python
@@ -117,59 +139,42 @@ from inseq.commands.attribute_context import attribute_context_with_model
117
 
118
  inseq_model = inseq.load_model(
119
  "{model_name_or_path}",
120
- "{attribution_method}",{get_kwargs_str(model_kwargs, "model_kwargs")}{get_kwargs_str(tokenizer_kwargs, "tokenizer_kwargs")}
121
  )
122
 
123
  pecore_args = AttributeContextArgs(
124
- save_path="pecore_output.json",
125
- viz_path="pecore_output.html",
126
  model_name_or_path="{model_name_or_path}",
127
  attribution_method="{attribution_method}",
128
  attributed_fn="{attributed_fn}",
129
  context_sensitivity_metric="{context_sensitivity_metric}",
130
- special_tokens_to_keep={special_tokens_to_keep},
131
- context_sensitivity_std_threshold={context_sensitivity_std_threshold},
132
- attribution_std_threshold={attribution_std_threshold},
133
- input_current_text=\"\"\"{input_current_text}\"\"\",
134
- input_template=\"\"\"{input_template}\"\"\",
135
- output_template="{output_template}",
136
  contextless_input_current_text=\"\"\"{contextless_input_template}\"\"\",
 
137
  contextless_output_current_text=\"\"\"{contextless_output_template}\"\"\",
138
- context_sensitivity_topk={context_sensitivity_topk if context_sensitivity_topk > 0 else None},
139
- attribution_topk={attribution_topk if attribution_topk > 0 else None},
140
- input_context_text={tq + input_context_text + tq if input_context_text else None},
141
- output_context_text={tq + output_context_text + tq if output_context_text else None},
142
- output_current_text={tq + output_current_text + tq if output_current_text else None},
143
- decoder_input_output_separator={tq + decoder_input_output_separator + tq if decoder_input_output_separator else None},{get_kwargs_str(model_kwargs, "model_kwargs")}{get_kwargs_str(tokenizer_kwargs, "tokenizer_kwargs")}{get_kwargs_str(generation_kwargs, "generation_kwargs")}{get_kwargs_str(attribution_kwargs, "attribution_kwargs")}
144
  )
 
145
  out = attribute_context_with_model(pecore_args, loaded_model)"""
146
  # Bash
147
- bash = f"""pip install inseq
148
- inseq attribute-context \\
149
- --save-path pecore_output.json \\
150
- --viz-path pecore_output.html \\
151
- --model-name-or-path "{model_name_or_path}" \\
152
- --attribution-method "{attribution_method}" \\
153
- --attributed-fn "{attributed_fn}" \\
154
- --context-sensitivity-metric "{context_sensitivity_metric}" \\
155
- --special-tokens-to-keep {" ".join(special_tokens_to_keep)} \\
156
- --context-sensitivity-std-threshold {context_sensitivity_std_threshold} \\
157
- --attribution-std-threshold {attribution_std_threshold} \\
158
- --input-current-text "{input_current_text}" \\
159
- --input-template "{input_template}" \\
160
- --output-template "{output_template}" \\
161
- --contextless-input-current-text "{contextless_input_template}" \\
162
- --contextless-output-current-text "{contextless_output_template}" \\
163
- --context-sensitivity-topk {context_sensitivity_topk if context_sensitivity_topk > 0 else None} \\
164
- --attribution-topk {attribution_topk if attribution_topk > 0 else None} \\
165
- --input-context-text "{input_context_text}" \\
166
- --output-context-text "{output_context_text}" \\
167
- --output-current-text "{output_current_text}" \\
168
- --decoder-input-output-separator "{decoder_input_output_separator}" \\
169
- --model-kwargs "{str(model_kwargs).replace(nl, "")}" \\
170
- --tokenizer-kwargs "{str(tokenizer_kwargs).replace(nl, "")} \\
171
- --generation-kwargs "{str(generation_kwargs).replace(nl, "")}" \\
172
- --attribution-kwargs "{str(attribution_kwargs).replace(nl, "")}"
173
- """
174
  return python, bash
175
 
 
105
  generation_kwargs: str,
106
  attribution_kwargs: str,
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 ''
113
+ def py_get_if_specified(arg: str | int | float | list | None, name: str, pad: str = " " * 4) -> str:
114
+ if arg is None or (isinstance(arg, (str, list)) and not arg) or (isinstance(arg, (int, float)) and arg <= 0):
115
+ return ""
116
+ elif isinstance(arg, str):
117
+ return nl + pad + name + "=" + tq + arg + tq + ","
118
+ elif isinstance(arg, list):
119
+ return nl + pad + name + "=" + str(arg) + ","
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
 
139
 
140
  inseq_model = inseq.load_model(
141
  "{model_name_or_path}",
142
+ "{attribution_method}",{py_get_kwargs_str(model_kwargs, "model_kwargs")}{py_get_kwargs_str(tokenizer_kwargs, "tokenizer_kwargs")}
143
  )
144
 
145
  pecore_args = AttributeContextArgs(
 
 
146
  model_name_or_path="{model_name_or_path}",
147
  attribution_method="{attribution_method}",
148
  attributed_fn="{attributed_fn}",
149
  context_sensitivity_metric="{context_sensitivity_metric}",
150
+ context_sensitivity_std_threshold={context_sensitivity_std_threshold},{py_get_if_specified(context_sensitivity_topk, "context_sensitivity_topk")}
151
+ attribution_std_threshold={attribution_std_threshold},{py_get_if_specified(attribution_topk, "attribution_topk")}
152
+ input_current_text=\"\"\"{input_current_text}\"\"\",{py_get_if_specified(input_context_text, "input_context_text")}
 
 
 
153
  contextless_input_current_text=\"\"\"{contextless_input_template}\"\"\",
154
+ input_template=\"\"\"{input_template}\"\"\",{py_get_if_specified(output_current_text, "output_current_text")}{py_get_if_specified(output_context_text, "output_context_text")}
155
  contextless_output_current_text=\"\"\"{contextless_output_template}\"\"\",
156
+ output_template="{output_template}",{py_get_if_specified(special_tokens_to_keep, "special_tokens_to_keep")}{py_get_if_specified(decoder_input_output_separator, "decoder_input_output_separator")}
157
+ save_path="pecore_output.json",
158
+ viz_path="pecore_output.html",{py_get_kwargs_str(model_kwargs, "model_kwargs")}{py_get_kwargs_str(tokenizer_kwargs, "tokenizer_kwargs")}{py_get_kwargs_str(generation_kwargs, "generation_kwargs")}{py_get_kwargs_str(attribution_kwargs, "attribution_kwargs")}
 
 
 
159
  )
160
+
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