FagerholmEmil commited on
Commit
7706468
·
1 Parent(s): ab3ddc2

Simplify Neuroscope UI layout and styling

Browse files
Files changed (1) hide show
  1. app.py +17 -68
app.py CHANGED
@@ -23,25 +23,7 @@ def calculate_color(val, max_val, min_val):
23
 
24
  style_string = """<style>
25
  span.token {
26
- border: 1px solid #ddd;
27
- padding: 2px 4px;
28
- margin: 1px;
29
- border-radius: 3px;
30
- font-family: monospace;
31
- }
32
- .neuron-info {
33
- background: #f5f5f5;
34
- padding: 15px;
35
- border-radius: 8px;
36
- margin-bottom: 15px;
37
- box-shadow: 0 2px 4px rgba(0,0,0,0.1);
38
- }
39
- .token-container {
40
- line-height: 2;
41
- background: white;
42
- padding: 20px;
43
- border-radius: 8px;
44
- box-shadow: 0 2px 4px rgba(0,0,0,0.1);
45
  }
46
  </style>"""
47
 
@@ -59,24 +41,19 @@ def basic_neuron_vis(text, layer, neuron_index, max_val=None, min_val=None):
59
  min_val = act_min
60
 
61
  htmls = [style_string]
62
- htmls.append('<div class="neuron-info">')
63
- htmls.append(f"<h3 style='margin-top: 0;'>Neuron Information</h3>")
64
- htmls.append(f"<p>Layer: <b>{layer}</b> | Neuron Index: <b>{neuron_index}</b></p>")
65
- htmls.append(f"<p>Max Range: <b>{max_val:.4f}</b> | Min Range: <b>{min_val:.4f}</b></p>")
66
 
67
  if act_max != max_val or act_min != min_val:
68
  htmls.append(
69
- f"<p>Custom Range Set | Max Act: <b>{act_max:.4f}</b> | Min Act: <b>{act_min:.4f}</b></p>"
70
  )
71
- htmls.append('</div>')
72
 
73
- htmls.append('<div class="token-container">')
74
  str_tokens = model.to_str_tokens(text)
75
  for tok, act in zip(str_tokens, acts):
76
  htmls.append(
77
  f"<span class='token' style='background-color:{calculate_color(act, max_val, min_val)}' >{tok}</span>"
78
  )
79
- htmls.append('</div>')
80
 
81
  return "".join(htmls)
82
 
@@ -146,48 +123,21 @@ default_neuron_index = 1
146
  default_max_val = 4.0
147
  default_min_val = 0.0
148
 
149
- with gr.Blocks(css="footer {display: none}") as demo:
150
- gr.HTML(
151
- value=f"<h1 style='text-align: center; margin-bottom: 1rem;'>Neuroscope for {model_name}</h1>"
152
- )
153
  with gr.Row():
154
- with gr.Column(scale=1):
155
- with gr.Box():
156
- gr.HTML("<h3>Controls</h3>")
157
- text = gr.Textbox(
158
- label="Input Text",
159
- value=default_text,
160
- lines=10
161
- )
162
- with gr.Row():
163
- layer = gr.Number(
164
- label="Layer",
165
- value=default_layer,
166
- precision=0,
167
- container=True
168
- )
169
- neuron_index = gr.Number(
170
- label="Neuron Index",
171
- value=default_neuron_index,
172
- precision=0,
173
- container=True
174
- )
175
- with gr.Row():
176
- max_val = gr.Number(
177
- label="Max Value",
178
- value=default_max_val,
179
- container=True
180
- )
181
- min_val = gr.Number(
182
- label="Min Value",
183
- value=default_min_val,
184
- container=True
185
- )
186
- inputs = [text, layer, neuron_index, max_val, min_val]
187
-
188
- with gr.Column(scale=1):
189
  out = gr.HTML(
190
- label="Neuron Activation Visualization",
191
  value=basic_neuron_vis(
192
  default_text,
193
  default_layer,
@@ -196,7 +146,6 @@ with gr.Blocks(css="footer {display: none}") as demo:
196
  default_min_val,
197
  ),
198
  )
199
-
200
  for inp in inputs:
201
  inp.change(basic_neuron_vis, inputs, out)
202
 
 
23
 
24
  style_string = """<style>
25
  span.token {
26
+ border: 1px solid rgb(123, 123, 123)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  }
28
  </style>"""
29
 
 
41
  min_val = act_min
42
 
43
  htmls = [style_string]
44
+ htmls.append(f"<h4>Layer: <b>{layer}</b>. Neuron Index: <b>{neuron_index}</b></h4>")
45
+ htmls.append(f"<h4>Max Range: <b>{max_val:.4f}</b>. Min Range: <b>{min_val:.4f}</b></h4>")
 
 
46
 
47
  if act_max != max_val or act_min != min_val:
48
  htmls.append(
49
+ f"<h4>Custom Range Set. Max Act: <b>{act_max:.4f}</b>. Min Act: <b>{act_min:.4f}</b></h4>"
50
  )
 
51
 
 
52
  str_tokens = model.to_str_tokens(text)
53
  for tok, act in zip(str_tokens, acts):
54
  htmls.append(
55
  f"<span class='token' style='background-color:{calculate_color(act, max_val, min_val)}' >{tok}</span>"
56
  )
 
57
 
58
  return "".join(htmls)
59
 
 
123
  default_max_val = 4.0
124
  default_min_val = 0.0
125
 
126
+ with gr.Blocks() as demo:
127
+ gr.HTML(value=f"Neuroscope for {model_name}")
 
 
128
  with gr.Row():
129
+ with gr.Column():
130
+ text = gr.Textbox(label="Text", value=default_text)
131
+ layer = gr.Number(label="Layer", value=default_layer, precision=0)
132
+ neuron_index = gr.Number(
133
+ label="Neuron Index", value=default_neuron_index, precision=0
134
+ )
135
+ max_val = gr.Number(label="Max Value", value=default_max_val)
136
+ min_val = gr.Number(label="Min Value", value=default_min_val)
137
+ inputs = [text, layer, neuron_index, max_val, min_val]
138
+ with gr.Column():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  out = gr.HTML(
140
+ label="Neuron Acts",
141
  value=basic_neuron_vis(
142
  default_text,
143
  default_layer,
 
146
  default_min_val,
147
  ),
148
  )
 
149
  for inp in inputs:
150
  inp.change(basic_neuron_vis, inputs, out)
151