Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,77 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
from typing import Dict, Tuple, List
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
def get_features(text: str) -> Dict:
|
7 |
"""Get neural features from the API using the exact website parameters."""
|
@@ -25,10 +95,11 @@ def get_features(text: str) -> Dict:
|
|
25 |
|
26 |
def create_feature_html(feature_id: int, activation: float, selected: bool = False) -> str:
|
27 |
"""Create HTML for an individual feature card."""
|
28 |
-
|
29 |
return f"""
|
30 |
-
<div class="feature-card
|
31 |
-
data-feature-id="{feature_id}"
|
|
|
32 |
<div class="flex justify-between items-center">
|
33 |
<div>
|
34 |
<span class="font-semibold">Feature {feature_id}</span>
|
@@ -54,15 +125,16 @@ def create_token_section(token: str, features: List[Dict], initial_count: int =
|
|
54 |
])
|
55 |
show_more = f"""
|
56 |
<div class="hidden" id="more-features-{token}">{hidden_features}</div>
|
57 |
-
<button
|
58 |
-
|
|
|
59 |
Show {remaining} More Features
|
60 |
</button>
|
61 |
"""
|
62 |
|
63 |
return f"""
|
64 |
<div class="mb-6">
|
65 |
-
<h2 class="text-xl
|
66 |
<div id="features-{token}">
|
67 |
{features_html}
|
68 |
</div>
|
@@ -73,8 +145,10 @@ def create_token_section(token: str, features: List[Dict], initial_count: int =
|
|
73 |
def create_dashboard_html(feature_id: int, activation: float) -> str:
|
74 |
"""Create HTML for the feature dashboard."""
|
75 |
return f"""
|
76 |
-
<div class="
|
77 |
-
<h3 class="text-lg font-semibold mb-4">
|
|
|
|
|
78 |
<iframe
|
79 |
src="https://www.neuronpedia.org/gemma-2-2b/20-gemmascope-res-16k/{feature_id}?embed=true&embedexplanation=true&embedplots=true&embedtest=true&height=300"
|
80 |
width="100%"
|
@@ -86,41 +160,55 @@ def create_dashboard_html(feature_id: int, activation: float) -> str:
|
|
86 |
"""
|
87 |
|
88 |
def create_interface_html(data: Dict) -> str:
|
89 |
-
"""Create the complete interface HTML."""
|
90 |
js_code = """
|
91 |
<script>
|
92 |
-
function
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
document.querySelectorAll('.feature-card').forEach(card => {
|
95 |
-
card.classList.remove('
|
96 |
-
card.classList.add('border', 'border-gray-200');
|
97 |
});
|
98 |
-
|
99 |
-
// Add selection to clicked feature
|
100 |
-
element.classList.remove('border', 'border-gray-200');
|
101 |
-
element.classList.add('border-blue-500', 'border-2');
|
102 |
|
103 |
// Update dashboard
|
104 |
-
|
105 |
-
`<iframe src="https://www.neuronpedia.org/gemma-2-2b/20-gemmascope-res-16k/${featureId}?embed=true&embedexplanation=true&embedplots=true&embedtest=true&height=300"
|
106 |
-
width="100%" height="600" frameborder="0" class="rounded-lg"></iframe>`;
|
107 |
}
|
108 |
|
109 |
-
function toggleFeatures(token
|
110 |
const moreFeatures = document.getElementById(`more-features-${token}`);
|
111 |
const featuresContainer = document.getElementById(`features-${token}`);
|
|
|
112 |
|
113 |
if (moreFeatures.classList.contains('hidden')) {
|
|
|
114 |
moreFeatures.classList.remove('hidden');
|
115 |
-
|
116 |
-
|
|
|
117 |
} else {
|
|
|
118 |
const allFeatures = featuresContainer.querySelectorAll('.feature-card');
|
119 |
-
|
120 |
-
allFeatures[i].remove();
|
121 |
-
}
|
122 |
moreFeatures.classList.add('hidden');
|
123 |
-
|
124 |
}
|
125 |
}
|
126 |
</script>
|
@@ -168,23 +256,34 @@ def analyze_features(text: str) -> Tuple[str, str, str]:
|
|
168 |
return interface_html, "", ""
|
169 |
|
170 |
def create_interface():
|
171 |
-
with gr.Blocks(css=
|
172 |
-
gr.Markdown(
|
173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
with gr.Row():
|
176 |
with gr.Column():
|
177 |
input_text = gr.Textbox(
|
178 |
lines=5,
|
179 |
placeholder="Enter text to analyze...",
|
180 |
-
label="Input Text"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
)
|
182 |
-
analyze_btn = gr.Button("Analyze Features", variant="primary")
|
183 |
-
gr.Examples([
|
184 |
-
"WordLift",
|
185 |
-
"Think Different",
|
186 |
-
"Just Do It"
|
187 |
-
], inputs=input_text)
|
188 |
|
189 |
with gr.Column():
|
190 |
output_html = gr.HTML()
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
from typing import Dict, Tuple, List
|
4 |
+
|
5 |
+
# Define custom CSS with Open Sans font and color theme
|
6 |
+
css = """
|
7 |
+
@import url('https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600;700&display=swap');
|
8 |
+
|
9 |
+
body {
|
10 |
+
font-family: 'Open Sans', sans-serif !important;
|
11 |
+
}
|
12 |
+
|
13 |
+
.primary-btn {
|
14 |
+
background-color: #3452db !important;
|
15 |
+
}
|
16 |
+
|
17 |
+
.primary-btn:hover {
|
18 |
+
background-color: #2a41af !important;
|
19 |
+
}
|
20 |
+
|
21 |
+
.feature-card {
|
22 |
+
border: 1px solid #e0e5ff;
|
23 |
+
background-color: #ffffff;
|
24 |
+
transition: all 0.2s ease;
|
25 |
+
}
|
26 |
+
|
27 |
+
.feature-card:hover {
|
28 |
+
border-color: #3452db;
|
29 |
+
box-shadow: 0 2px 4px rgba(52, 82, 219, 0.1);
|
30 |
+
}
|
31 |
+
|
32 |
+
.feature-card.selected {
|
33 |
+
border: 2px solid #3452db;
|
34 |
+
background-color: #eef1ff;
|
35 |
+
}
|
36 |
+
|
37 |
+
.show-more-btn {
|
38 |
+
color: #3452db;
|
39 |
+
font-weight: 600;
|
40 |
+
}
|
41 |
+
|
42 |
+
.show-more-btn:hover {
|
43 |
+
color: #2a41af;
|
44 |
+
}
|
45 |
+
|
46 |
+
.token-header {
|
47 |
+
color: #152156;
|
48 |
+
font-weight: 700;
|
49 |
+
}
|
50 |
+
|
51 |
+
.dashboard-container {
|
52 |
+
border: 1px solid #e0e5ff;
|
53 |
+
border-radius: 8px;
|
54 |
+
background-color: #ffffff;
|
55 |
+
}
|
56 |
+
"""
|
57 |
+
|
58 |
+
# Create custom theme
|
59 |
+
theme = gr.themes.Soft(
|
60 |
+
primary_hue=gr.themes.colors.Color(
|
61 |
+
name="blue",
|
62 |
+
c50="#eef1ff",
|
63 |
+
c100="#e0e5ff",
|
64 |
+
c200="#c3cbff",
|
65 |
+
c300="#a5b2ff",
|
66 |
+
c400="#8798ff",
|
67 |
+
c500="#6a7eff",
|
68 |
+
c600="#3452db",
|
69 |
+
c700="#2a41af",
|
70 |
+
c800="#1f3183",
|
71 |
+
c900="#152156",
|
72 |
+
c950="#0a102b",
|
73 |
+
)
|
74 |
+
)
|
75 |
|
76 |
def get_features(text: str) -> Dict:
|
77 |
"""Get neural features from the API using the exact website parameters."""
|
|
|
95 |
|
96 |
def create_feature_html(feature_id: int, activation: float, selected: bool = False) -> str:
|
97 |
"""Create HTML for an individual feature card."""
|
98 |
+
selected_class = "selected" if selected else ""
|
99 |
return f"""
|
100 |
+
<div class="feature-card {selected_class} p-4 rounded-lg mb-4"
|
101 |
+
data-feature-id="{feature_id}"
|
102 |
+
onclick="selectFeature(this, {feature_id}, {activation})">
|
103 |
<div class="flex justify-between items-center">
|
104 |
<div>
|
105 |
<span class="font-semibold">Feature {feature_id}</span>
|
|
|
125 |
])
|
126 |
show_more = f"""
|
127 |
<div class="hidden" id="more-features-{token}">{hidden_features}</div>
|
128 |
+
<button id="toggle-btn-{token}"
|
129 |
+
class="show-more-btn text-sm mt-2"
|
130 |
+
onclick="toggleFeatures('{token}')">
|
131 |
Show {remaining} More Features
|
132 |
</button>
|
133 |
"""
|
134 |
|
135 |
return f"""
|
136 |
<div class="mb-6">
|
137 |
+
<h2 class="token-header text-xl mb-4">Token: {token}</h2>
|
138 |
<div id="features-{token}">
|
139 |
{features_html}
|
140 |
</div>
|
|
|
145 |
def create_dashboard_html(feature_id: int, activation: float) -> str:
|
146 |
"""Create HTML for the feature dashboard."""
|
147 |
return f"""
|
148 |
+
<div class="dashboard-container p-4">
|
149 |
+
<h3 class="text-lg font-semibold mb-4 text-gray-900">
|
150 |
+
Feature {feature_id} Dashboard (Activation: {activation:.2f})
|
151 |
+
</h3>
|
152 |
<iframe
|
153 |
src="https://www.neuronpedia.org/gemma-2-2b/20-gemmascope-res-16k/{feature_id}?embed=true&embedexplanation=true&embedplots=true&embedtest=true&height=300"
|
154 |
width="100%"
|
|
|
160 |
"""
|
161 |
|
162 |
def create_interface_html(data: Dict) -> str:
|
163 |
+
"""Create the complete interface HTML with JavaScript functionality."""
|
164 |
js_code = """
|
165 |
<script>
|
166 |
+
function updateDashboard(featureId, activation) {
|
167 |
+
const dashboardContainer = document.getElementById('dashboard-container');
|
168 |
+
dashboardContainer.innerHTML = `
|
169 |
+
<div class="dashboard-container p-4">
|
170 |
+
<h3 class="text-lg font-semibold mb-4 text-gray-900">
|
171 |
+
Feature ${featureId} Dashboard (Activation: ${activation.toFixed(2)})
|
172 |
+
</h3>
|
173 |
+
<iframe
|
174 |
+
src="https://www.neuronpedia.org/gemma-2-2b/20-gemmascope-res-16k/${featureId}?embed=true&embedexplanation=true&embedplots=true&embedtest=true&height=300"
|
175 |
+
width="100%"
|
176 |
+
height="600"
|
177 |
+
frameborder="0"
|
178 |
+
class="rounded-lg"
|
179 |
+
></iframe>
|
180 |
+
</div>
|
181 |
+
`;
|
182 |
+
}
|
183 |
+
|
184 |
+
function selectFeature(element, featureId, activation) {
|
185 |
+
// Update selected state visually
|
186 |
document.querySelectorAll('.feature-card').forEach(card => {
|
187 |
+
card.classList.remove('selected');
|
|
|
188 |
});
|
189 |
+
element.classList.add('selected');
|
|
|
|
|
|
|
190 |
|
191 |
// Update dashboard
|
192 |
+
updateDashboard(featureId, activation);
|
|
|
|
|
193 |
}
|
194 |
|
195 |
+
function toggleFeatures(token) {
|
196 |
const moreFeatures = document.getElementById(`more-features-${token}`);
|
197 |
const featuresContainer = document.getElementById(`features-${token}`);
|
198 |
+
const toggleButton = document.getElementById(`toggle-btn-${token}`);
|
199 |
|
200 |
if (moreFeatures.classList.contains('hidden')) {
|
201 |
+
// Show additional features
|
202 |
moreFeatures.classList.remove('hidden');
|
203 |
+
const additionalFeatures = moreFeatures.innerHTML;
|
204 |
+
featuresContainer.insertAdjacentHTML('beforeend', additionalFeatures);
|
205 |
+
toggleButton.textContent = 'Show Less';
|
206 |
} else {
|
207 |
+
// Hide additional features
|
208 |
const allFeatures = featuresContainer.querySelectorAll('.feature-card');
|
209 |
+
Array.from(allFeatures).slice(3).forEach(card => card.remove());
|
|
|
|
|
210 |
moreFeatures.classList.add('hidden');
|
211 |
+
toggleButton.textContent = `Show ${moreFeatures.children.length} More Features`;
|
212 |
}
|
213 |
}
|
214 |
</script>
|
|
|
256 |
return interface_html, "", ""
|
257 |
|
258 |
def create_interface():
|
259 |
+
with gr.Blocks(theme=theme, css=css) as interface:
|
260 |
+
gr.Markdown(
|
261 |
+
"# Neural Feature Analyzer",
|
262 |
+
elem_classes="text-2xl font-bold text-gray-900 mb-2"
|
263 |
+
)
|
264 |
+
gr.Markdown(
|
265 |
+
"*Analyze text using Gemma's interpretable neural features*",
|
266 |
+
elem_classes="text-gray-600 mb-6"
|
267 |
+
)
|
268 |
|
269 |
with gr.Row():
|
270 |
with gr.Column():
|
271 |
input_text = gr.Textbox(
|
272 |
lines=5,
|
273 |
placeholder="Enter text to analyze...",
|
274 |
+
label="Input Text",
|
275 |
+
elem_classes="mb-4"
|
276 |
+
)
|
277 |
+
analyze_btn = gr.Button(
|
278 |
+
"Analyze Features",
|
279 |
+
variant="primary",
|
280 |
+
elem_classes="primary-btn"
|
281 |
+
)
|
282 |
+
gr.Examples(
|
283 |
+
["WordLift", "Think Different", "Just Do It"],
|
284 |
+
inputs=input_text,
|
285 |
+
elem_classes="mt-4"
|
286 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
287 |
|
288 |
with gr.Column():
|
289 |
output_html = gr.HTML()
|