File size: 12,169 Bytes
cf3d1b1 fbd70bc cf3d1b1 fbd70bc cf3d1b1 fbd70bc cf3d1b1 fbd70bc cf3d1b1 fbd70bc cf3d1b1 |
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 |
import json
import os
import gradio as gr
import spaces
from contents import (
citation,
description,
examples,
how_it_works,
how_to_use,
subtitle,
title,
)
from gradio_highlightedtextbox import HighlightedTextbox
from style import custom_css
from utils import get_tuples_from_output
from inseq import list_feature_attribution_methods, list_step_functions
from inseq.commands.attribute_context.attribute_context import (
AttributeContextArgs,
attribute_context,
)
@spaces.GPU()
def pecore(
input_current_text: str,
input_context_text: str,
output_current_text: str,
output_context_text: str,
model_name_or_path: str,
attribution_method: str,
attributed_fn: str | None,
context_sensitivity_metric: str,
context_sensitivity_std_threshold: float,
context_sensitivity_topk: int,
attribution_std_threshold: float,
attribution_topk: int,
input_template: str,
input_current_text_template: str,
output_template: str,
special_tokens_to_keep: str | list[str] | None,
model_kwargs: str,
tokenizer_kwargs: str,
generation_kwargs: str,
attribution_kwargs: str,
):
formatted_input_current_text = input_current_text_template.format(
current=input_current_text
)
pecore_args = AttributeContextArgs(
show_intermediate_outputs=False,
save_path=os.path.join(os.path.dirname(__file__), "outputs/output.json"),
add_output_info=True,
viz_path=os.path.join(os.path.dirname(__file__), "outputs/output.html"),
show_viz=False,
model_name_or_path=model_name_or_path,
attribution_method=attribution_method,
attributed_fn=attributed_fn,
attribution_selectors=None,
attribution_aggregators=None,
normalize_attributions=True,
model_kwargs=json.loads(model_kwargs),
tokenizer_kwargs=json.loads(tokenizer_kwargs),
generation_kwargs=json.loads(generation_kwargs),
attribution_kwargs=json.loads(attribution_kwargs),
context_sensitivity_metric=context_sensitivity_metric,
align_output_context_auto=False,
prompt_user_for_contextless_output_next_tokens=False,
special_tokens_to_keep=special_tokens_to_keep,
context_sensitivity_std_threshold=context_sensitivity_std_threshold,
context_sensitivity_topk=context_sensitivity_topk
if context_sensitivity_topk > 0
else None,
attribution_std_threshold=attribution_std_threshold,
attribution_topk=attribution_topk if attribution_topk > 0 else None,
input_current_text=formatted_input_current_text,
input_context_text=input_context_text if input_context_text else None,
input_template=input_template,
output_current_text=output_current_text if output_current_text else None,
output_context_text=output_context_text if output_context_text else None,
output_template=output_template,
)
out = attribute_context(pecore_args)
return get_tuples_from_output(out), gr.Button(visible=True), gr.Button(visible=True)
with gr.Blocks(css=custom_css) as demo:
gr.Markdown(title)
gr.Markdown(subtitle)
gr.Markdown(description)
with gr.Tab("π Attributing Context"):
with gr.Row():
with gr.Column():
input_current_text = gr.Textbox(
label="Input query", placeholder="Your input query..."
)
input_context_text = gr.Textbox(
label="Input context", lines=4, placeholder="Your input context..."
)
attribute_input_button = gr.Button("Submit", variant="primary")
with gr.Column():
pecore_output_highlights = HighlightedTextbox(
value=[
("This output will contain ", None),
("context sensitive", "Context sensitive"),
(" generated tokens and ", None),
("influential context", "Influential context"),
(" tokens.", None),
],
color_map={
"Context sensitive": "green",
"Influential context": "blue",
},
show_legend=True,
label="PECoRe Output",
combine_adjacent=True,
interactive=False,
)
with gr.Row(equal_height=True):
download_output_file_button = gr.Button(
"β Download output",
visible=False,
link=os.path.join(
os.path.dirname(__file__), "/file=outputs/output.json"
),
)
download_output_html_button = gr.Button(
"π Download HTML",
visible=False,
link=os.path.join(
os.path.dirname(__file__), "/file=outputs/output.html"
),
)
attribute_input_examples = gr.Examples(
examples,
inputs=[input_current_text, input_context_text],
outputs=pecore_output_highlights,
)
with gr.Tab("βοΈ Parameters"):
gr.Markdown("## βοΈ PECoRe Parameters")
with gr.Row(equal_height=True):
model_name_or_path = gr.Textbox(
value="gsarti/cora_mgen",
label="Model",
info="Hugging Face Hub identifier of the model to analyze with PECoRe.",
interactive=True,
)
context_sensitivity_metric = gr.Dropdown(
value="kl_divergence",
label="Context sensitivity metric",
info="Metric to use to measure context sensitivity of generated tokens.",
choices=list_step_functions(),
interactive=True,
)
attribution_method = gr.Dropdown(
value="saliency",
label="Attribution method",
info="Attribution method identifier to identify relevant context tokens.",
choices=list_feature_attribution_methods(),
interactive=True,
)
attributed_fn = gr.Dropdown(
value="contrast_prob_diff",
label="Attributed function",
info="Function of model logits to use as target for the attribution method.",
choices=list_step_functions(),
interactive=True,
)
gr.Markdown("#### Results Selection Parameters")
with gr.Row(equal_height=True):
context_sensitivity_std_threshold = gr.Number(
value=1.0,
label="Context sensitivity threshold",
info="Select N to keep context sensitive tokens with scores above N * std. 0 = above mean.",
precision=1,
minimum=0.0,
maximum=5.0,
step=0.5,
interactive=True,
)
context_sensitivity_topk = gr.Number(
value=0,
label="Context sensitivity top-k",
info="Select N to keep top N context sensitive tokens. 0 = keep all.",
interactive=True,
precision=0,
minimum=0,
maximum=10,
)
attribution_std_threshold = gr.Number(
value=1.0,
label="Attribution threshold",
info="Select N to keep attributed tokens with scores above N * std. 0 = above mean.",
precision=1,
minimum=0.0,
maximum=5.0,
step=0.5,
interactive=True,
)
attribution_topk = gr.Number(
value=0,
label="Attribution top-k",
info="Select N to keep top N attributed tokens in the context. 0 = keep all.",
interactive=True,
precision=0,
minimum=0,
maximum=50,
)
gr.Markdown("#### Text Format Parameters")
with gr.Row(equal_height=True):
input_template = gr.Textbox(
value="{current} <P>:{context}",
label="Input template",
info="Template to format the input for the model. Use {current} and {context} placeholders.",
interactive=True,
)
output_template = gr.Textbox(
value="{current}",
label="Output template",
info="Template to format the output from the model. Use {current} and {context} placeholders.",
interactive=True,
)
input_current_text_template = gr.Textbox(
value="<Q>:{current}",
label="Input current text template",
info="Template to format the input query for the model. Use {current} placeholder.",
interactive=True,
)
special_tokens_to_keep = gr.Dropdown(
label="Special tokens to keep",
info="Special tokens to keep in the attribution. If empty, all special tokens are ignored.",
value=None,
multiselect=True,
allow_custom_value=True,
)
gr.Markdown("## βοΈ Generation Parameters")
with gr.Row(equal_height=True):
output_current_text = gr.Textbox(
label="Generation output",
info="Specifies an output to force-decoded during generation. If blank, the model will generate freely.",
interactive=True,
)
output_context_text = gr.Textbox(
label="Generation context",
info="If specified, this context is used as starting point for generation. Useful for e.g. chain-of-thought reasoning.",
interactive=True,
)
generation_kwargs = gr.Code(
value="{}",
language="json",
label="Generation kwargs",
interactive=True,
lines=1,
)
gr.Markdown("## βοΈ Other Parameters")
with gr.Row(equal_height=True):
model_kwargs = gr.Code(
value="{}",
language="json",
label="Model kwargs",
interactive=True,
lines=1,
)
tokenizer_kwargs = gr.Code(
value="{}",
language="json",
label="Tokenizer kwargs",
interactive=True,
lines=1,
)
attribution_kwargs = gr.Code(
value="{}",
language="json",
label="Attribution kwargs",
interactive=True,
lines=1,
)
gr.Markdown(how_it_works)
gr.Markdown(how_to_use)
gr.Markdown(citation)
attribute_input_button.click(
pecore,
inputs=[
input_current_text,
input_context_text,
output_current_text,
output_context_text,
model_name_or_path,
attribution_method,
attributed_fn,
context_sensitivity_metric,
context_sensitivity_std_threshold,
context_sensitivity_topk,
attribution_std_threshold,
attribution_topk,
input_template,
input_current_text_template,
output_template,
special_tokens_to_keep,
model_kwargs,
tokenizer_kwargs,
generation_kwargs,
attribution_kwargs,
],
outputs=[
pecore_output_highlights,
download_output_file_button,
download_output_html_button,
],
)
demo.launch(allowed_paths=["outputs/"])
|