SecureLLMSys commited on
Commit
011baea
Β·
1 Parent(s): dd36917
Files changed (2) hide show
  1. app.py +233 -121
  2. app_config.py β†’ app_no_config.py +12 -91
app.py CHANGED
@@ -18,13 +18,11 @@ from src.prompts import wrap_prompt
18
  from gradio_highlightedtextbox import HighlightedTextbox
19
  from examples import run_example_1, run_example_2, run_example_3, run_example_4, run_example_5, run_example_6
20
  from functools import partial
21
- os.makedirs("/home/user/nltk_data", exist_ok=True)
22
- # Download punkt to a known path
23
- nltk.download("punkt", download_dir="/home/user/nltk_data")
24
- # Tell nltk where to find it
25
- nltk.data.path.append("/home/user/nltk_data")
26
  from nltk.tokenize import sent_tokenize
27
 
 
28
  # Load original app constants
29
  APP_TITLE = '<div class="app-title"><span class="brand">AttnTrace: </span><span class="subtitle">Attention-based Context Traceback for Long-Context LLMs</span></div>'
30
  APP_DESCRIPTION = """AttnTrace traces a model's generated statements back to specific parts of the context using attention-based traceback. Try it out with Meta-Llama-3.1-8B-Instruct here! See the [[paper](https://arxiv.org/abs/2506.04202)] and [[code](https://github.com/Wang-Yanting/TracLLM-Kit)] for more!
@@ -82,10 +80,43 @@ current_attr = None
82
  current_model_path = None
83
  current_explanation_level = None
84
  current_api_key = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  def initialize_model_and_attr():
87
  """Initialize model and attribution with default configuration"""
88
- global current_llm, current_attr, current_model_path, current_explanation_level, current_api_key
89
 
90
  try:
91
  # Check if we need to reinitialize the model
@@ -95,7 +126,7 @@ def initialize_model_and_attr():
95
 
96
  # Check if we need to update attribution
97
  need_attr_update = (current_attr is None or
98
- current_explanation_level != DEFAULT_EXPLANATION_LEVEL or
99
  need_model_update)
100
 
101
  if need_model_update:
@@ -106,15 +137,20 @@ def initialize_model_and_attr():
106
  current_api_key = effective_api_key
107
 
108
  if need_attr_update:
109
- print(f"Initializing context traceback with explanation level: {DEFAULT_EXPLANATION_LEVEL}")
 
 
 
 
 
110
  current_attr = AttnTraceAttribution(
111
  current_llm,
112
- explanation_level=DEFAULT_EXPLANATION_LEVEL,
113
- K=3,
114
  q=0.4,
115
  B=30
116
  )
117
- current_explanation_level = DEFAULT_EXPLANATION_LEVEL
118
 
119
  return current_llm, current_attr, None
120
 
@@ -207,9 +243,9 @@ def generate_model_response(state: State):
207
  print("❌ Validation failed: No query")
208
  return state, gr.update(value=[("❌ Please enter a query before generating response! If you just changed configuration, try reloading an example.", None)], visible=True)
209
 
210
- # Initialize model and attribution with default configuration
211
- print(f"πŸ”§ Generating response with explanation_level: {DEFAULT_EXPLANATION_LEVEL}")
212
- #llm, attr, error_msg = initialize_model_and_attr()
213
 
214
  if llm is None or attr is None:
215
  error_text = error_msg if error_msg else "Model initialization failed!"
@@ -338,7 +374,7 @@ def unified_response_handler(response_text: str, state: State):
338
  )
339
 
340
  # Initialize model and generate response
341
- #llm, attr, error_msg = initialize_model_and_attr()
342
 
343
  if llm is None:
344
  error_text = error_msg if error_msg else "Model initialization failed!"
@@ -436,7 +472,7 @@ def basic_get_scores_and_sources_full_response(state: State):
436
  state.explained_response_part = state.full_response
437
 
438
  # Attribution using default configuration
439
- #_, attr, error_msg = initialize_model_and_attr()
440
 
441
  if attr is None:
442
  error_text = error_msg if error_msg else "Traceback initialization failed!"
@@ -651,7 +687,7 @@ def basic_get_scores_and_sources(
651
  state.explained_response_part = selected_text
652
 
653
  # Attribution using default configuration
654
- #_, attr, error_msg = initialize_model_and_attr()
655
 
656
  if attr is None:
657
  error_text = error_msg if error_msg else "Traceback initialization failed!"
@@ -946,111 +982,167 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
946
  value=clear_state()
947
  )
948
 
949
- basic_tab = gr.Tab("Demo")
950
- with basic_tab:
951
- # gr.Markdown("## Demo")
952
- gr.Markdown(
953
- "Enter your context and instruction below to try out AttnTrace! You can also click on the example buttons above to load pre-configured examples."
954
- )
955
-
956
- gr.Markdown(
957
- '**Color Legend for Context Traceback (by ranking):** <span style="background-color: #FF4444; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Red</span> = 1st (most important) | <span style="background-color: #FF8C42; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Orange</span> = 2nd | <span style="background-color: #FFD93D; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Golden</span> = 3rd | <span style="background-color: #FFF280; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Yellow</span> = 4th-5th | <span style="background-color: #FFF9C4; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Light</span> = 6th+'
958
- )
959
-
960
-
961
- # Top section: Wide Context box with tabs
962
- with gr.Row():
963
- with gr.Column(scale=1):
964
- with gr.Tabs() as basic_context_tabs:
965
- with gr.TabItem("Context", id=0):
966
- basic_context_box = gr.Textbox(
967
- placeholder="Enter context...",
968
- show_label=False,
969
- value="",
970
- lines=6,
971
- max_lines=6,
972
- elem_id="basic_context_box",
973
- autoscroll=False,
974
- )
975
- with gr.TabItem("Context with highlighted traceback results", id=1, visible=True) as basic_sources_in_context_tab:
976
- basic_sources_in_context_box = HighlightedTextbox(
977
- value=[("Click on a sentence in the response below to see highlighted traceback results here.", None)],
978
- show_legend_label=False,
979
- show_label=False,
980
- show_legend=False,
981
- interactive=False,
982
- elem_id="basic_sources_in_context_box",
983
- )
984
-
985
- # Error messages
986
- basic_generate_error_box = HighlightedTextbox(
987
- show_legend_label=False,
988
- show_label=False,
989
- show_legend=False,
990
- visible=False,
991
- interactive=False,
992
- container=False,
993
- )
994
 
995
- # Bottom section: Left (instruction + button + response), Right (response selection)
996
- with gr.Row(equal_height=True):
997
- # Left: Instruction + Button + Response
998
- with gr.Column(scale=1):
999
- basic_query_box = gr.Textbox(
1000
- label="Instruction",
1001
- placeholder="Enter an instruction...",
1002
- value="",
1003
- lines=3,
1004
- max_lines=3,
1005
- )
1006
-
1007
- unified_response_button = gr.Button(
1008
- "Generate/Use Response",
1009
- variant="primary",
1010
- size="lg"
1011
- )
 
 
 
 
 
 
 
 
1012
 
1013
- response_input_box = gr.Textbox(
1014
- label="Response (Editable)",
1015
- placeholder="Response will appear here after generation, or type your own response for traceback...",
1016
- lines=8,
1017
- max_lines=8,
1018
- info="Leave empty and click button to generate from LLM, or type your own response to use for traceback"
1019
- )
 
 
 
 
 
 
 
 
 
 
 
 
1020
 
1021
- # Right: Response for attribution selection
1022
- with gr.Column(scale=1):
1023
- basic_response_box = gr.HighlightedText(
1024
- label="Click to select text for traceback!",
1025
- value=[("Click the 'Generate/Use Response' button on the left to see response text here for traceback analysis.", None)],
1026
- interactive=False,
1027
- combine_adjacent=False,
1028
- show_label=True,
1029
- show_legend=False,
1030
- elem_id="basic_response_box",
1031
- visible=True,
1032
- )
1033
-
1034
- # Button for full response traceback
1035
- full_response_traceback_button = gr.Button(
1036
- "πŸ” Traceback Entire Response",
1037
- variant="secondary",
1038
- size="sm"
1039
- )
1040
-
1041
- # Hidden error box and dummy elements
1042
- basic_attribute_error_box = HighlightedTextbox(
1043
- show_legend_label=False,
1044
- show_label=False,
1045
- show_legend=False,
1046
- visible=False,
1047
- interactive=False,
1048
- container=False,
1049
- )
1050
- dummy_basic_sources_box = gr.Textbox(
1051
- visible=False, interactive=False, container=False
1052
- )
1053
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1054
 
1055
  # Only a single (AttnTrace) method and model in this simplified version
1056
 
@@ -1065,9 +1157,23 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
1065
  state,
1066
  )
1067
 
1068
- # Defining behavior of various interactions for the basic tab
1069
- basic_tab.select(
1070
- fn=basic_clear_state,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1071
  inputs=[],
1072
  outputs=[
1073
  basic_context_box,
@@ -1209,6 +1315,12 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
1209
  outputs=[state, response_input_box, basic_response_box, basic_generate_error_box]
1210
  )
1211
 
 
 
 
 
 
 
1212
 
1213
  # gr.Markdown(
1214
  # "Please do not interact with elements while generation/attribution is in progress. This may cause errors. You can refresh the page if you run into issues because of this."
 
18
  from gradio_highlightedtextbox import HighlightedTextbox
19
  from examples import run_example_1, run_example_2, run_example_3, run_example_4, run_example_5, run_example_6
20
  from functools import partial
21
+
22
+
 
 
 
23
  from nltk.tokenize import sent_tokenize
24
 
25
+
26
  # Load original app constants
27
  APP_TITLE = '<div class="app-title"><span class="brand">AttnTrace: </span><span class="subtitle">Attention-based Context Traceback for Long-Context LLMs</span></div>'
28
  APP_DESCRIPTION = """AttnTrace traces a model's generated statements back to specific parts of the context using attention-based traceback. Try it out with Meta-Llama-3.1-8B-Instruct here! See the [[paper](https://arxiv.org/abs/2506.04202)] and [[code](https://github.com/Wang-Yanting/TracLLM-Kit)] for more!
 
80
  current_model_path = None
81
  current_explanation_level = None
82
  current_api_key = None
83
+ current_top_k = 3 # Add top-k tracking
84
+
85
+ def update_configuration(explanation_level, top_k):
86
+ """Update the global configuration and reinitialize attribution if needed"""
87
+ global current_explanation_level, current_top_k, current_attr, current_llm
88
+
89
+ # Convert top_k to int
90
+ top_k = int(top_k)
91
+
92
+ # Check if configuration has changed
93
+ config_changed = (current_explanation_level != explanation_level or
94
+ current_top_k != top_k)
95
+
96
+ if config_changed:
97
+ print(f"πŸ”„ Updating configuration: explanation_level={explanation_level}, top_k={top_k}")
98
+ current_explanation_level = explanation_level
99
+ current_top_k = top_k
100
+
101
+ # Reset both model and attribution to force complete reinitialization
102
+ current_llm = None
103
+ current_attr = None
104
+
105
+ # Reinitialize with new configuration
106
+ try:
107
+ llm, attr, error_msg = initialize_model_and_attr()
108
+ if llm is not None and attr is not None:
109
+ return gr.update(value=f"βœ… Configuration updated: {explanation_level} level, top-{top_k}")
110
+ else:
111
+ return gr.update(value=f"❌ Error reinitializing: {error_msg}")
112
+ except Exception as e:
113
+ return gr.update(value=f"❌ Error updating configuration: {str(e)}")
114
+ else:
115
+ return gr.update(value="ℹ️ Configuration unchanged")
116
 
117
  def initialize_model_and_attr():
118
  """Initialize model and attribution with default configuration"""
119
+ global current_llm, current_attr, current_model_path, current_explanation_level, current_api_key, current_top_k
120
 
121
  try:
122
  # Check if we need to reinitialize the model
 
126
 
127
  # Check if we need to update attribution
128
  need_attr_update = (current_attr is None or
129
+ current_explanation_level != (current_explanation_level or DEFAULT_EXPLANATION_LEVEL) or
130
  need_model_update)
131
 
132
  if need_model_update:
 
137
  current_api_key = effective_api_key
138
 
139
  if need_attr_update:
140
+ # Use current configuration or defaults
141
+ explanation_level = current_explanation_level or DEFAULT_EXPLANATION_LEVEL
142
+ top_k = current_top_k or 3
143
+ if "segment" in explanation_level:
144
+ explanation_level = "segment"
145
+ print(f"Initializing context traceback with explanation level: {explanation_level}, top_k: {top_k}")
146
  current_attr = AttnTraceAttribution(
147
  current_llm,
148
+ explanation_level= explanation_level,
149
+ K=top_k,
150
  q=0.4,
151
  B=30
152
  )
153
+ current_explanation_level = explanation_level
154
 
155
  return current_llm, current_attr, None
156
 
 
243
  print("❌ Validation failed: No query")
244
  return state, gr.update(value=[("❌ Please enter a query before generating response! If you just changed configuration, try reloading an example.", None)], visible=True)
245
 
246
+ # Initialize model and attribution with current configuration
247
+ print(f"πŸ”§ Generating response with explanation_level: {current_explanation_level or DEFAULT_EXPLANATION_LEVEL}, top_k: {current_top_k or 3}")
248
+ llm, attr, error_msg = initialize_model_and_attr()
249
 
250
  if llm is None or attr is None:
251
  error_text = error_msg if error_msg else "Model initialization failed!"
 
374
  )
375
 
376
  # Initialize model and generate response
377
+ llm, attr, error_msg = initialize_model_and_attr()
378
 
379
  if llm is None:
380
  error_text = error_msg if error_msg else "Model initialization failed!"
 
472
  state.explained_response_part = state.full_response
473
 
474
  # Attribution using default configuration
475
+ llm, attr, error_msg = initialize_model_and_attr()
476
 
477
  if attr is None:
478
  error_text = error_msg if error_msg else "Traceback initialization failed!"
 
687
  state.explained_response_part = selected_text
688
 
689
  # Attribution using default configuration
690
+ llm, attr, error_msg = initialize_model_and_attr()
691
 
692
  if attr is None:
693
  error_text = error_msg if error_msg else "Traceback initialization failed!"
 
982
  value=clear_state()
983
  )
984
 
985
+ # Create tabs for Demo and Configuration
986
+ with gr.Tabs() as main_tabs:
987
+ # Demo Tab
988
+ with gr.Tab("Demo", id="demo_tab"):
989
+ gr.Markdown(
990
+ "Enter your context and instruction below to try out AttnTrace! You can also click on the example buttons above to load pre-configured examples."
991
+ )
992
+
993
+ gr.Markdown(
994
+ '**Color Legend for Context Traceback (by ranking):** <span style="background-color: #FF4444; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Red</span> = 1st (most important) | <span style="background-color: #FF8C42; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Orange</span> = 2nd | <span style="background-color: #FFD93D; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Golden</span> = 3rd | <span style="background-color: #FFF280; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Yellow</span> = 4th-5th | <span style="background-color: #FFF9C4; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Light</span> = 6th+'
995
+ )
996
+
997
+ # Top section: Wide Context box with tabs
998
+ with gr.Row():
999
+ with gr.Column(scale=1):
1000
+ with gr.Tabs() as basic_context_tabs:
1001
+ with gr.TabItem("Context", id=0):
1002
+ basic_context_box = gr.Textbox(
1003
+ placeholder="Enter context...",
1004
+ show_label=False,
1005
+ value="",
1006
+ lines=6,
1007
+ max_lines=6,
1008
+ elem_id="basic_context_box",
1009
+ autoscroll=False,
1010
+ )
1011
+ with gr.TabItem("Context with highlighted traceback results", id=1, visible=True) as basic_sources_in_context_tab:
1012
+ basic_sources_in_context_box = HighlightedTextbox(
1013
+ value=[("Click on a sentence in the response below to see highlighted traceback results here.", None)],
1014
+ show_legend_label=False,
1015
+ show_label=False,
1016
+ show_legend=False,
1017
+ interactive=False,
1018
+ elem_id="basic_sources_in_context_box",
1019
+ )
1020
+
1021
+ # Error messages
1022
+ basic_generate_error_box = HighlightedTextbox(
1023
+ show_legend_label=False,
1024
+ show_label=False,
1025
+ show_legend=False,
1026
+ visible=False,
1027
+ interactive=False,
1028
+ container=False,
1029
+ )
1030
 
1031
+ # Bottom section: Left (instruction + button + response), Right (response selection)
1032
+ with gr.Row(equal_height=True):
1033
+ # Left: Instruction + Button + Response
1034
+ with gr.Column(scale=1):
1035
+ basic_query_box = gr.Textbox(
1036
+ label="Instruction",
1037
+ placeholder="Enter an instruction...",
1038
+ value="",
1039
+ lines=3,
1040
+ max_lines=3,
1041
+ )
1042
+
1043
+ unified_response_button = gr.Button(
1044
+ "Generate/Use Response",
1045
+ variant="primary",
1046
+ size="lg"
1047
+ )
1048
+
1049
+ response_input_box = gr.Textbox(
1050
+ label="Response (Editable)",
1051
+ placeholder="Response will appear here after generation, or type your own response for traceback...",
1052
+ lines=8,
1053
+ max_lines=8,
1054
+ info="Leave empty and click button to generate from LLM, or type your own response to use for traceback"
1055
+ )
1056
 
1057
+ # Right: Response for attribution selection
1058
+ with gr.Column(scale=1):
1059
+ basic_response_box = gr.HighlightedText(
1060
+ label="Click to select text for traceback!",
1061
+ value=[("Click the 'Generate/Use Response' button on the left to see response text here for traceback analysis.", None)],
1062
+ interactive=False,
1063
+ combine_adjacent=False,
1064
+ show_label=True,
1065
+ show_legend=False,
1066
+ elem_id="basic_response_box",
1067
+ visible=True,
1068
+ )
1069
+
1070
+ # Button for full response traceback
1071
+ full_response_traceback_button = gr.Button(
1072
+ "πŸ” Traceback Entire Response",
1073
+ variant="secondary",
1074
+ size="sm"
1075
+ )
1076
 
1077
+ # Hidden error box and dummy elements
1078
+ basic_attribute_error_box = HighlightedTextbox(
1079
+ show_legend_label=False,
1080
+ show_label=False,
1081
+ show_legend=False,
1082
+ visible=False,
1083
+ interactive=False,
1084
+ container=False,
1085
+ )
1086
+ dummy_basic_sources_box = gr.Textbox(
1087
+ visible=False, interactive=False, container=False
1088
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1089
 
1090
+ # Configuration Tab
1091
+ with gr.Tab("Config", id="config_tab"):
1092
+ gr.Markdown("## βš™οΈ AttnTrace Configuration")
1093
+ gr.Markdown("Configure the traceback analysis parameters to customize how AttnTrace processes your context and generates results.")
1094
+
1095
+ with gr.Row():
1096
+ with gr.Column(scale=1):
1097
+ explanation_level_dropdown = gr.Dropdown(
1098
+ choices=["sentence", "paragraph", "text segment"],
1099
+ value="sentence",
1100
+ label="Explanation Level",
1101
+ info="How to segment the context for traceback analysis"
1102
+ )
1103
+ with gr.Column(scale=1):
1104
+ top_k_dropdown = gr.Dropdown(
1105
+ choices=["3", "5", "10"],
1106
+ value="5",
1107
+ label="Top-K Value",
1108
+ info="Number of most important text segments to highlight"
1109
+ )
1110
+
1111
+ with gr.Row():
1112
+ with gr.Column(scale=1):
1113
+ apply_config_button = gr.Button(
1114
+ "Apply Configuration",
1115
+ variant="primary",
1116
+ size="lg"
1117
+ )
1118
+ with gr.Column(scale=2):
1119
+ config_status_text = gr.Textbox(
1120
+ label="Configuration Status",
1121
+ value="Ready to apply configuration",
1122
+ interactive=False,
1123
+ lines=2
1124
+ )
1125
+
1126
+ gr.Markdown("### πŸ“‹ Current Configuration")
1127
+ gr.Markdown("""
1128
+ - **Explanation Level**: Determines how the context is segmented for analysis
1129
+ - `sentence`: Analyze at sentence level (recommended for most cases)
1130
+ - `paragraph`: Analyze at paragraph level (good for longer documents)
1131
+ - `text segment`: Analyze at custom text segments (advanced usage)
1132
+
1133
+ - **Top-K Value**: Number of most important text segments to highlight in results
1134
+ - Higher values show more context but may be less focused
1135
+ - Lower values provide more focused results but may miss some context
1136
+
1137
+ **Note**: Configuration changes will take effect immediately for new traceback operations.
1138
+ """)
1139
+
1140
+ gr.Markdown("### πŸ”„ Model Information")
1141
+ gr.Markdown(f"""
1142
+ - **Current Model**: {DEFAULT_MODEL_PATH}
1143
+ - **Max Tokens**: {get_max_tokens(DEFAULT_MODEL_PATH):,}
1144
+ - **Device**: CUDA (GPU accelerated)
1145
+ """)
1146
 
1147
  # Only a single (AttnTrace) method and model in this simplified version
1148
 
 
1157
  state,
1158
  )
1159
 
1160
+ # Defining behavior of various interactions for the demo tab only
1161
+ def handle_demo_tab_selection(evt: gr.SelectData):
1162
+ """Handle tab selection - only clear state when switching to demo tab"""
1163
+ if evt.index == 0: # Demo tab
1164
+ return basic_clear_state()
1165
+ else: # Configuration tab - no state change needed
1166
+ return (
1167
+ gr.update(), # basic_context_box
1168
+ gr.update(), # basic_query_box
1169
+ gr.update(), # response_input_box
1170
+ gr.update(), # basic_response_box
1171
+ gr.update(), # basic_context_tabs
1172
+ gr.update(), # state
1173
+ )
1174
+
1175
+ main_tabs.select(
1176
+ fn=handle_demo_tab_selection,
1177
  inputs=[],
1178
  outputs=[
1179
  basic_context_box,
 
1315
  outputs=[state, response_input_box, basic_response_box, basic_generate_error_box]
1316
  )
1317
 
1318
+ # Configuration update handler
1319
+ apply_config_button.click(
1320
+ fn=update_configuration,
1321
+ inputs=[explanation_level_dropdown, top_k_dropdown],
1322
+ outputs=[config_status_text]
1323
+ )
1324
 
1325
  # gr.Markdown(
1326
  # "Please do not interact with elements while generation/attribution is in progress. This may cause errors. You can refresh the page if you run into issues because of this."
app_config.py β†’ app_no_config.py RENAMED
@@ -25,8 +25,6 @@ nltk.download("punkt", download_dir="/home/user/nltk_data")
25
  nltk.data.path.append("/home/user/nltk_data")
26
  from nltk.tokenize import sent_tokenize
27
 
28
- DEFAULT_TOP_K = 3
29
-
30
  # Load original app constants
31
  APP_TITLE = '<div class="app-title"><span class="brand">AttnTrace: </span><span class="subtitle">Attention-based Context Traceback for Long-Context LLMs</span></div>'
32
  APP_DESCRIPTION = """AttnTrace traces a model's generated statements back to specific parts of the context using attention-based traceback. Try it out with Meta-Llama-3.1-8B-Instruct here! See the [[paper](https://arxiv.org/abs/2506.04202)] and [[code](https://github.com/Wang-Yanting/TracLLM-Kit)] for more!
@@ -84,46 +82,10 @@ current_attr = None
84
  current_model_path = None
85
  current_explanation_level = None
86
  current_api_key = None
87
- current_top_k = 3 # Add top-k tracking
88
-
89
- def update_configuration(explanation_level, top_k):
90
- """Update the global configuration and reinitialize attribution if needed"""
91
- global current_explanation_level, current_top_k, current_attr, current_llm
92
-
93
- # Convert top_k to int
94
- top_k = int(top_k)
95
-
96
- # Check if configuration has changed
97
- config_changed = (current_explanation_level != explanation_level or
98
- current_top_k != top_k)
99
-
100
- if config_changed:
101
- print(f"πŸ”„ Updating configuration: explanation_level={explanation_level}, top_k={top_k}")
102
- current_explanation_level = explanation_level
103
- current_top_k = top_k
104
-
105
- DEFAULT_EXPLANATION_LEVEL = explanation_level
106
- DEFAULT_TOP_K = top_k
107
-
108
- # Reset both model and attribution to force complete reinitialization
109
- current_llm = None
110
- current_attr = None
111
-
112
- # Reinitialize with new configuration
113
- try:
114
- llm, attr, error_msg = initialize_model_and_attr()
115
- if llm is not None and attr is not None:
116
- return gr.update(value=f"βœ… Configuration updated: {explanation_level} level, top-{top_k}")
117
- else:
118
- return gr.update(value=f"❌ Error reinitializing: {error_msg}")
119
- except Exception as e:
120
- return gr.update(value=f"❌ Error updating configuration: {str(e)}")
121
- else:
122
- return gr.update(value="ℹ️ Configuration unchanged")
123
 
124
  def initialize_model_and_attr():
125
  """Initialize model and attribution with default configuration"""
126
- global current_llm, current_attr, current_model_path, current_explanation_level, current_api_key, current_top_k
127
 
128
  try:
129
  # Check if we need to reinitialize the model
@@ -133,7 +95,7 @@ def initialize_model_and_attr():
133
 
134
  # Check if we need to update attribution
135
  need_attr_update = (current_attr is None or
136
- current_explanation_level != (current_explanation_level or DEFAULT_EXPLANATION_LEVEL) or
137
  need_model_update)
138
 
139
  if need_model_update:
@@ -144,20 +106,15 @@ def initialize_model_and_attr():
144
  current_api_key = effective_api_key
145
 
146
  if need_attr_update:
147
- # Use current configuration or defaults
148
- explanation_level = current_explanation_level or DEFAULT_EXPLANATION_LEVEL
149
- top_k = current_top_k or 3
150
- if "segment" in DEFAULT_EXPLANATION_LEVEL:
151
- DEFAULT_EXPLANATION_LEVEL = "segment"
152
- print(f"Initializing context traceback with explanation level: {explanation_level}, top_k: {top_k}")
153
  current_attr = AttnTraceAttribution(
154
  current_llm,
155
- explanation_level= DEFAULT_EXPLANATION_LEVEL,
156
- K=DEFAULT_TOP_K,
157
  q=0.4,
158
  B=30
159
  )
160
- current_explanation_level = explanation_level
161
 
162
  return current_llm, current_attr, None
163
 
@@ -250,9 +207,9 @@ def generate_model_response(state: State):
250
  print("❌ Validation failed: No query")
251
  return state, gr.update(value=[("❌ Please enter a query before generating response! If you just changed configuration, try reloading an example.", None)], visible=True)
252
 
253
- # Initialize model and attribution with current configuration
254
- print(f"πŸ”§ Generating response with explanation_level: {current_explanation_level or DEFAULT_EXPLANATION_LEVEL}, top_k: {current_top_k or 3}")
255
- llm, attr, error_msg = initialize_model_and_attr()
256
 
257
  if llm is None or attr is None:
258
  error_text = error_msg if error_msg else "Model initialization failed!"
@@ -381,7 +338,7 @@ def unified_response_handler(response_text: str, state: State):
381
  )
382
 
383
  # Initialize model and generate response
384
- llm, attr, error_msg = initialize_model_and_attr()
385
 
386
  if llm is None:
387
  error_text = error_msg if error_msg else "Model initialization failed!"
@@ -479,7 +436,7 @@ def basic_get_scores_and_sources_full_response(state: State):
479
  state.explained_response_part = state.full_response
480
 
481
  # Attribution using default configuration
482
- llm, attr, error_msg = initialize_model_and_attr()
483
 
484
  if attr is None:
485
  error_text = error_msg if error_msg else "Traceback initialization failed!"
@@ -694,7 +651,7 @@ def basic_get_scores_and_sources(
694
  state.explained_response_part = selected_text
695
 
696
  # Attribution using default configuration
697
- llm, attr, error_msg = initialize_model_and_attr()
698
 
699
  if attr is None:
700
  error_text = error_msg if error_msg else "Traceback initialization failed!"
@@ -1000,36 +957,6 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
1000
  '**Color Legend for Context Traceback (by ranking):** <span style="background-color: #FF4444; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Red</span> = 1st (most important) | <span style="background-color: #FF8C42; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Orange</span> = 2nd | <span style="background-color: #FFD93D; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Golden</span> = 3rd | <span style="background-color: #FFF280; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Yellow</span> = 4th-5th | <span style="background-color: #FFF9C4; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Light</span> = 6th+'
1001
  )
1002
 
1003
- # Configuration bar
1004
- with gr.Row():
1005
- with gr.Column(scale=1):
1006
- explanation_level_dropdown = gr.Dropdown(
1007
- choices=["sentence", "paragraph", "text segment"],
1008
- value="sentence",
1009
- label="Explanation Level",
1010
- info="How to segment the context for traceback analysis"
1011
- )
1012
- with gr.Column(scale=1):
1013
- top_k_dropdown = gr.Dropdown(
1014
- choices=["3", "5", "10"],
1015
- value="5",
1016
- label="Top-K Value",
1017
- info="Number of most important text segments to highlight"
1018
- )
1019
- with gr.Column(scale=1):
1020
- apply_config_button = gr.Button(
1021
- "Apply Configuration",
1022
- variant="secondary",
1023
- size="sm"
1024
- )
1025
- with gr.Column(scale=2):
1026
- config_status_text = gr.Textbox(
1027
- label="Configuration Status",
1028
- value="Ready to apply configuration",
1029
- interactive=False,
1030
- lines=1
1031
- )
1032
-
1033
 
1034
  # Top section: Wide Context box with tabs
1035
  with gr.Row():
@@ -1282,12 +1209,6 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
1282
  outputs=[state, response_input_box, basic_response_box, basic_generate_error_box]
1283
  )
1284
 
1285
- # Configuration update handler
1286
- apply_config_button.click(
1287
- fn=update_configuration,
1288
- inputs=[explanation_level_dropdown, top_k_dropdown],
1289
- outputs=[config_status_text]
1290
- )
1291
 
1292
  # gr.Markdown(
1293
  # "Please do not interact with elements while generation/attribution is in progress. This may cause errors. You can refresh the page if you run into issues because of this."
 
25
  nltk.data.path.append("/home/user/nltk_data")
26
  from nltk.tokenize import sent_tokenize
27
 
 
 
28
  # Load original app constants
29
  APP_TITLE = '<div class="app-title"><span class="brand">AttnTrace: </span><span class="subtitle">Attention-based Context Traceback for Long-Context LLMs</span></div>'
30
  APP_DESCRIPTION = """AttnTrace traces a model's generated statements back to specific parts of the context using attention-based traceback. Try it out with Meta-Llama-3.1-8B-Instruct here! See the [[paper](https://arxiv.org/abs/2506.04202)] and [[code](https://github.com/Wang-Yanting/TracLLM-Kit)] for more!
 
82
  current_model_path = None
83
  current_explanation_level = None
84
  current_api_key = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  def initialize_model_and_attr():
87
  """Initialize model and attribution with default configuration"""
88
+ global current_llm, current_attr, current_model_path, current_explanation_level, current_api_key
89
 
90
  try:
91
  # Check if we need to reinitialize the model
 
95
 
96
  # Check if we need to update attribution
97
  need_attr_update = (current_attr is None or
98
+ current_explanation_level != DEFAULT_EXPLANATION_LEVEL or
99
  need_model_update)
100
 
101
  if need_model_update:
 
106
  current_api_key = effective_api_key
107
 
108
  if need_attr_update:
109
+ print(f"Initializing context traceback with explanation level: {DEFAULT_EXPLANATION_LEVEL}")
 
 
 
 
 
110
  current_attr = AttnTraceAttribution(
111
  current_llm,
112
+ explanation_level=DEFAULT_EXPLANATION_LEVEL,
113
+ K=3,
114
  q=0.4,
115
  B=30
116
  )
117
+ current_explanation_level = DEFAULT_EXPLANATION_LEVEL
118
 
119
  return current_llm, current_attr, None
120
 
 
207
  print("❌ Validation failed: No query")
208
  return state, gr.update(value=[("❌ Please enter a query before generating response! If you just changed configuration, try reloading an example.", None)], visible=True)
209
 
210
+ # Initialize model and attribution with default configuration
211
+ print(f"πŸ”§ Generating response with explanation_level: {DEFAULT_EXPLANATION_LEVEL}")
212
+ #llm, attr, error_msg = initialize_model_and_attr()
213
 
214
  if llm is None or attr is None:
215
  error_text = error_msg if error_msg else "Model initialization failed!"
 
338
  )
339
 
340
  # Initialize model and generate response
341
+ #llm, attr, error_msg = initialize_model_and_attr()
342
 
343
  if llm is None:
344
  error_text = error_msg if error_msg else "Model initialization failed!"
 
436
  state.explained_response_part = state.full_response
437
 
438
  # Attribution using default configuration
439
+ #_, attr, error_msg = initialize_model_and_attr()
440
 
441
  if attr is None:
442
  error_text = error_msg if error_msg else "Traceback initialization failed!"
 
651
  state.explained_response_part = selected_text
652
 
653
  # Attribution using default configuration
654
+ #_, attr, error_msg = initialize_model_and_attr()
655
 
656
  if attr is None:
657
  error_text = error_msg if error_msg else "Traceback initialization failed!"
 
957
  '**Color Legend for Context Traceback (by ranking):** <span style="background-color: #FF4444; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Red</span> = 1st (most important) | <span style="background-color: #FF8C42; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Orange</span> = 2nd | <span style="background-color: #FFD93D; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Golden</span> = 3rd | <span style="background-color: #FFF280; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Yellow</span> = 4th-5th | <span style="background-color: #FFF9C4; color: black; padding: 2px 6px; border-radius: 4px; font-weight: 600;">Light</span> = 6th+'
958
  )
959
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
960
 
961
  # Top section: Wide Context box with tabs
962
  with gr.Row():
 
1209
  outputs=[state, response_input_box, basic_response_box, basic_generate_error_box]
1210
  )
1211
 
 
 
 
 
 
 
1212
 
1213
  # gr.Markdown(
1214
  # "Please do not interact with elements while generation/attribution is in progress. This may cause errors. You can refresh the page if you run into issues because of this."