Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,61 +9,19 @@ def get_features(text: str) -> Dict:
|
|
9 |
"text": text,
|
10 |
"layer": "20-gemmascope-res-16k"
|
11 |
}
|
12 |
-
|
13 |
try:
|
14 |
-
response = requests.post(
|
15 |
-
url,
|
16 |
-
headers={"Content-Type": "application/json"},
|
17 |
-
json=payload
|
18 |
-
)
|
19 |
response.raise_for_status()
|
20 |
return response.json()
|
21 |
except Exception as e:
|
22 |
return None
|
23 |
|
24 |
-
def
|
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({
|
50 |
-
'feature_id': first_feature['feature_index'],
|
51 |
-
'activation': first_feature['activation_value']
|
52 |
-
})
|
53 |
-
else:
|
54 |
-
dashboard_html = ""
|
55 |
-
|
56 |
-
return token_features, dashboard_html
|
57 |
-
|
58 |
-
def create_dashboard(feature: Dict) -> str:
|
59 |
-
if not feature:
|
60 |
-
return ""
|
61 |
-
|
62 |
return f"""
|
63 |
<div class="dashboard-container p-4">
|
64 |
-
<h3 class="text-lg font-semibold mb-4">Feature {
|
65 |
<iframe
|
66 |
-
src="https://www.neuronpedia.org/gemma-2-2b/20-gemmascope-res-16k/{
|
67 |
width="100%"
|
68 |
height="600"
|
69 |
frameborder="0"
|
@@ -72,13 +30,38 @@ def create_dashboard(feature: Dict) -> str:
|
|
72 |
</div>
|
73 |
"""
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
css = """
|
76 |
@import url('https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600;700&display=swap');
|
77 |
-
|
78 |
-
body {
|
79 |
-
font-family: 'Open Sans', sans-serif !important;
|
80 |
-
}
|
81 |
-
|
82 |
.dashboard-container {
|
83 |
border: 1px solid #e0e5ff;
|
84 |
border-radius: 8px;
|
@@ -96,35 +79,32 @@ theme = gr.themes.Soft(
|
|
96 |
)
|
97 |
)
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
with gr.
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
)
|
116 |
-
|
117 |
-
with gr.Column(scale=2):
|
118 |
-
features_text = gr.JSON()
|
119 |
-
dashboard = gr.HTML()
|
120 |
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
outputs=[features_text, dashboard]
|
125 |
-
)
|
126 |
|
127 |
-
|
|
|
|
|
|
|
|
|
128 |
|
129 |
if __name__ == "__main__":
|
130 |
-
|
|
|
9 |
"text": text,
|
10 |
"layer": "20-gemmascope-res-16k"
|
11 |
}
|
|
|
12 |
try:
|
13 |
+
response = requests.post(url, headers={"Content-Type": "application/json"}, json=payload)
|
|
|
|
|
|
|
|
|
14 |
response.raise_for_status()
|
15 |
return response.json()
|
16 |
except Exception as e:
|
17 |
return None
|
18 |
|
19 |
+
def create_dashboard(feature_id: int) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
return f"""
|
21 |
<div class="dashboard-container p-4">
|
22 |
+
<h3 class="text-lg font-semibold mb-4">Feature {feature_id} Dashboard</h3>
|
23 |
<iframe
|
24 |
+
src="https://www.neuronpedia.org/gemma-2-2b/20-gemmascope-res-16k/{feature_id}?embed=true&embedexplanation=true&embedplots=true&embedtest=true&height=300"
|
25 |
width="100%"
|
26 |
height="600"
|
27 |
frameborder="0"
|
|
|
30 |
</div>
|
31 |
"""
|
32 |
|
33 |
+
def process_features(text: str) -> Tuple[gr.Column, str]:
|
34 |
+
if not text:
|
35 |
+
return gr.Column(), ""
|
36 |
+
|
37 |
+
features_data = get_features(text)
|
38 |
+
if not features_data:
|
39 |
+
return gr.Column(), ""
|
40 |
+
|
41 |
+
first_feature_id = None
|
42 |
+
with gr.Column() as col:
|
43 |
+
for result in features_data['results']:
|
44 |
+
if result['token'] == '<bos>':
|
45 |
+
continue
|
46 |
+
|
47 |
+
gr.Markdown(f"### {result['token']}")
|
48 |
+
for i, feature in enumerate(result['top_features'][:3]):
|
49 |
+
feature_id = feature['feature_index']
|
50 |
+
if first_feature_id is None:
|
51 |
+
first_feature_id = feature_id
|
52 |
+
gr.Button(
|
53 |
+
f"Feature {feature_id} (Activation: {feature['activation_value']:.2f})",
|
54 |
+
elem_id=str(feature_id)
|
55 |
+
).click(
|
56 |
+
fn=lambda fid=feature_id: create_dashboard(fid),
|
57 |
+
outputs=dashboard
|
58 |
+
)
|
59 |
+
|
60 |
+
return col, create_dashboard(first_feature_id) if first_feature_id else ""
|
61 |
+
|
62 |
css = """
|
63 |
@import url('https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600;700&display=swap');
|
64 |
+
body { font-family: 'Open Sans', sans-serif !important; }
|
|
|
|
|
|
|
|
|
65 |
.dashboard-container {
|
66 |
border: 1px solid #e0e5ff;
|
67 |
border-radius: 8px;
|
|
|
79 |
)
|
80 |
)
|
81 |
|
82 |
+
with gr.Blocks(theme=theme, css=css) as demo:
|
83 |
+
gr.Markdown("# Brand Analyzer", elem_classes="text-2xl font-bold mb-2")
|
84 |
+
gr.Markdown("*Analyze text using Gemma's interpretable neural features*", elem_classes="text-gray-600 mb-6")
|
85 |
+
|
86 |
+
with gr.Row():
|
87 |
+
with gr.Column(scale=1):
|
88 |
+
input_text = gr.Textbox(
|
89 |
+
lines=5,
|
90 |
+
placeholder="Enter text to analyze...",
|
91 |
+
label="Input Text"
|
92 |
+
)
|
93 |
+
analyze_btn = gr.Button("Analyze Features", variant="primary")
|
94 |
+
gr.Examples(
|
95 |
+
examples=["WordLift", "Think Different", "Just Do It"],
|
96 |
+
inputs=input_text
|
97 |
+
)
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
+
with gr.Column(scale=2):
|
100 |
+
features_col = gr.Column()
|
101 |
+
dashboard = gr.HTML()
|
|
|
|
|
102 |
|
103 |
+
analyze_btn.click(
|
104 |
+
fn=process_features,
|
105 |
+
inputs=[input_text],
|
106 |
+
outputs=[features_col, dashboard]
|
107 |
+
)
|
108 |
|
109 |
if __name__ == "__main__":
|
110 |
+
demo.launch(share=True)
|