Spaces:
Running
on
Zero
Running
on
Zero
Commit
Β·
9783844
1
Parent(s):
6b9f4b3
update
Browse files
app.py
CHANGED
@@ -81,22 +81,30 @@ 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
|
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
|
@@ -106,7 +114,7 @@ def update_configuration(explanation_level, top_k):
|
|
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:
|
@@ -116,7 +124,7 @@ def update_configuration(explanation_level, top_k):
|
|
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
|
@@ -140,15 +148,17 @@ def initialize_model_and_attr():
|
|
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=
|
151 |
-
B=
|
152 |
)
|
153 |
current_explanation_level = explanation_level
|
154 |
|
@@ -1108,6 +1118,26 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
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(
|
@@ -1134,6 +1164,14 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
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 |
|
@@ -1318,7 +1356,7 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
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 |
|
|
|
81 |
current_explanation_level = None
|
82 |
current_api_key = None
|
83 |
current_top_k = 3 # Add top-k tracking
|
84 |
+
current_B = 30 # Add B parameter tracking
|
85 |
+
current_q = 0.4 # Add q parameter tracking
|
86 |
|
87 |
+
def update_configuration(explanation_level, top_k, B, q):
|
88 |
"""Update the global configuration and reinitialize attribution if needed"""
|
89 |
+
global current_explanation_level, current_top_k, current_B, current_q, current_attr, current_llm
|
90 |
|
91 |
+
# Convert parameters to appropriate types
|
92 |
top_k = int(top_k)
|
93 |
+
B = int(B)
|
94 |
+
q = float(q)
|
95 |
|
96 |
# Check if configuration has changed
|
97 |
config_changed = (current_explanation_level != explanation_level or
|
98 |
+
current_top_k != top_k or
|
99 |
+
current_B != B or
|
100 |
+
current_q != q)
|
101 |
|
102 |
if config_changed:
|
103 |
+
print(f"π Updating configuration: explanation_level={explanation_level}, top_k={top_k}, B={B}, q={q}")
|
104 |
current_explanation_level = explanation_level
|
105 |
current_top_k = top_k
|
106 |
+
current_B = B
|
107 |
+
current_q = q
|
108 |
|
109 |
# Reset both model and attribution to force complete reinitialization
|
110 |
current_llm = None
|
|
|
114 |
try:
|
115 |
llm, attr, error_msg = initialize_model_and_attr()
|
116 |
if llm is not None and attr is not None:
|
117 |
+
return gr.update(value=f"β
Configuration updated: {explanation_level} level, top-{top_k}, B={B}, q={q}")
|
118 |
else:
|
119 |
return gr.update(value=f"β Error reinitializing: {error_msg}")
|
120 |
except Exception as e:
|
|
|
124 |
|
125 |
def initialize_model_and_attr():
|
126 |
"""Initialize model and attribution with default configuration"""
|
127 |
+
global current_llm, current_attr, current_model_path, current_explanation_level, current_api_key, current_top_k, current_B, current_q
|
128 |
|
129 |
try:
|
130 |
# Check if we need to reinitialize the model
|
|
|
148 |
# Use current configuration or defaults
|
149 |
explanation_level = current_explanation_level or DEFAULT_EXPLANATION_LEVEL
|
150 |
top_k = current_top_k or 3
|
151 |
+
B = current_B or 30
|
152 |
+
q = current_q or 0.4
|
153 |
if "segment" in explanation_level:
|
154 |
explanation_level = "segment"
|
155 |
+
print(f"Initializing context traceback with explanation level: {explanation_level}, top_k: {top_k}, B: {B}, q: {q}")
|
156 |
current_attr = AttnTraceAttribution(
|
157 |
current_llm,
|
158 |
explanation_level= explanation_level,
|
159 |
K=top_k,
|
160 |
+
q=q,
|
161 |
+
B=B
|
162 |
)
|
163 |
current_explanation_level = explanation_level
|
164 |
|
|
|
1118 |
info="Number of most important text segments to highlight"
|
1119 |
)
|
1120 |
|
1121 |
+
with gr.Row():
|
1122 |
+
with gr.Column(scale=1):
|
1123 |
+
B_slider = gr.Slider(
|
1124 |
+
minimum=10,
|
1125 |
+
maximum=100,
|
1126 |
+
value=30,
|
1127 |
+
step=5,
|
1128 |
+
label="B Parameter",
|
1129 |
+
info="Number of subsamples (higher = more accurate but slower)"
|
1130 |
+
)
|
1131 |
+
with gr.Column(scale=1):
|
1132 |
+
q_slider = gr.Slider(
|
1133 |
+
minimum=0.1,
|
1134 |
+
maximum=0.9,
|
1135 |
+
value=0.4,
|
1136 |
+
step=0.1,
|
1137 |
+
label="q Parameter",
|
1138 |
+
info="Sub-sampling ratio (0.1-0.9)"
|
1139 |
+
)
|
1140 |
+
|
1141 |
with gr.Row():
|
1142 |
with gr.Column(scale=1):
|
1143 |
apply_config_button = gr.Button(
|
|
|
1164 |
- Higher values show more context but may be less focused
|
1165 |
- Lower values provide more focused results but may miss some context
|
1166 |
|
1167 |
+
- **B Parameter**: Number of subsamples
|
1168 |
+
- Higher values (50-100): More thorough analysis but slower
|
1169 |
+
- Lower values (10-30): Faster analysis but may miss some important segments
|
1170 |
+
- Default: 30 (good balance of speed and accuracy)
|
1171 |
+
|
1172 |
+
- **q Parameter**: Sub-sampling ratio (0.1-0.9)
|
1173 |
+
|
1174 |
+
|
1175 |
**Note**: Configuration changes will take effect immediately for new traceback operations.
|
1176 |
""")
|
1177 |
|
|
|
1356 |
# Configuration update handler
|
1357 |
apply_config_button.click(
|
1358 |
fn=update_configuration,
|
1359 |
+
inputs=[explanation_level_dropdown, top_k_dropdown, B_slider, q_slider],
|
1360 |
outputs=[config_status_text]
|
1361 |
)
|
1362 |
|