cyberandy commited on
Commit
8d771a6
·
verified ·
1 Parent(s): e8c8adb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -19
app.py CHANGED
@@ -21,32 +21,29 @@ def get_features(text: str) -> Dict:
21
  except Exception as e:
22
  return None
23
 
24
- def process_features(text: str) -> Tuple[List[gr.Tab], str]:
 
25
  if not text:
26
- return [], ""
27
 
28
  features_data = get_features(text)
29
  if not features_data:
30
- return [], ""
31
 
32
- tabs = []
33
  for result in features_data['results']:
34
  if result['token'] == '<bos>':
35
  continue
36
 
37
  token = result['token']
38
- features = result['top_features']
39
-
40
- with gr.Tab(token):
41
- feature_list = []
42
- for feature in features[:3]:
43
- gr.Button(
44
- f"Feature {feature['feature_index']} (Activation: {feature['activation_value']:.2f})",
45
- variant="secondary"
46
- )
47
- tabs.append(gr.Tab)
48
 
49
- # Create initial dashboard for first feature
50
  if features_data['results']:
51
  first_feature = features_data['results'][0]['top_features'][0]
52
  dashboard_html = create_dashboard({
@@ -56,7 +53,7 @@ def process_features(text: str) -> Tuple[List[gr.Tab], str]:
56
  else:
57
  dashboard_html = ""
58
 
59
- return tabs, dashboard_html
60
 
61
  def create_dashboard(feature: Dict) -> str:
62
  if not feature:
@@ -118,16 +115,16 @@ def create_interface():
118
  )
119
 
120
  with gr.Column(scale=2):
121
- token_tabs = gr.Tabs()
122
  dashboard = gr.HTML()
123
 
124
  analyze_btn.click(
125
  fn=process_features,
126
  inputs=[input_text],
127
- outputs=[token_tabs, dashboard]
128
  )
129
 
130
  return interface
131
 
132
  if __name__ == "__main__":
133
- create_interface().launch()
 
21
  except Exception as e:
22
  return None
23
 
24
+ def process_features(text: str) -> Tuple[Dict[str, list], str]:
25
+ """Returns a dictionary of token: [features] and dashboard HTML"""
26
  if not text:
27
+ return {}, ""
28
 
29
  features_data = get_features(text)
30
  if not features_data:
31
+ return {}, ""
32
 
33
+ token_features = {}
34
  for result in features_data['results']:
35
  if result['token'] == '<bos>':
36
  continue
37
 
38
  token = result['token']
39
+ features = []
40
+ for feature in result['top_features'][:3]:
41
+ features.append(
42
+ f"Feature {feature['feature_index']} (Activation: {feature['activation_value']:.2f})"
43
+ )
44
+ token_features[token] = features
 
 
 
 
45
 
46
+ # Create initial dashboard
47
  if features_data['results']:
48
  first_feature = features_data['results'][0]['top_features'][0]
49
  dashboard_html = create_dashboard({
 
53
  else:
54
  dashboard_html = ""
55
 
56
+ return token_features, dashboard_html
57
 
58
  def create_dashboard(feature: Dict) -> str:
59
  if not feature:
 
115
  )
116
 
117
  with gr.Column(scale=2):
118
+ features_text = gr.JSON()
119
  dashboard = gr.HTML()
120
 
121
  analyze_btn.click(
122
  fn=process_features,
123
  inputs=[input_text],
124
+ outputs=[features_text, dashboard]
125
  )
126
 
127
  return interface
128
 
129
  if __name__ == "__main__":
130
+ create_interface().launch(share=True)