Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
3 |
from typing import Dict, List, Tuple
|
4 |
|
5 |
BRAND_EXAMPLES = [
|
@@ -7,52 +8,74 @@ BRAND_EXAMPLES = [
|
|
7 |
"Apple - Think Different. Innovation through unique perspectives.",
|
8 |
"McDonald's - I'm Lovin' It. Creating moments of joy and satisfaction.",
|
9 |
"L'Oréal - Because You're Worth It. Embracing self-worth and beauty.",
|
10 |
-
"BMW - The Ultimate Driving Machine. Engineering excellence in motion."
|
|
|
|
|
|
|
11 |
]
|
12 |
|
13 |
-
def
|
14 |
response = requests.post(
|
15 |
-
"https://www.neuronpedia.org/api/
|
16 |
headers={"Content-Type": "application/json"},
|
17 |
json={
|
18 |
-
"
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
"
|
24 |
-
"numResults": 50
|
25 |
}
|
26 |
)
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
def format_output(features: List[Dict]) -> Tuple[str, str, str]:
|
30 |
if not features:
|
31 |
return "No significant neural activations detected", "", ""
|
32 |
|
33 |
output = "# Neural Analysis Results\n\n"
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
output += f"
|
38 |
-
output += f"- Max Activation: {feature['maxValue']:.2f}\n"
|
39 |
-
output += f"- Layer: {feature['layer']}\n\n"
|
40 |
|
41 |
-
|
42 |
-
dashboard_url = f"https://www.neuronpedia.org/gemma-2-2b/0-gemmascope-mlp-16k/{top_feature['index']}?embed=true&embedexplanation=true&embedplots=true&embedtest=true&height=300"
|
43 |
iframe = f'<iframe src="{dashboard_url}" width="100%" height="600px" frameborder="0"></iframe>'
|
44 |
|
45 |
-
return output, iframe, f"Feature {
|
46 |
|
47 |
def create_interface():
|
48 |
with gr.Blocks() as interface:
|
49 |
gr.Markdown("# Brand Message Neural Analyzer")
|
|
|
50 |
|
51 |
with gr.Row():
|
52 |
with gr.Column():
|
53 |
input_text = gr.Textbox(
|
54 |
lines=5,
|
55 |
-
placeholder="Enter brand message to analyze...",
|
56 |
label="Brand Message"
|
57 |
)
|
58 |
analyze_btn = gr.Button("Analyze Neural Patterns")
|
@@ -64,7 +87,7 @@ def create_interface():
|
|
64 |
dashboard = gr.HTML()
|
65 |
|
66 |
analyze_btn.click(
|
67 |
-
fn=lambda text: format_output(
|
68 |
inputs=input_text,
|
69 |
outputs=[output_text, dashboard, feature_label]
|
70 |
)
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
from concurrent.futures import ThreadPoolExecutor
|
4 |
from typing import Dict, List, Tuple
|
5 |
|
6 |
BRAND_EXAMPLES = [
|
|
|
8 |
"Apple - Think Different. Innovation through unique perspectives.",
|
9 |
"McDonald's - I'm Lovin' It. Creating moments of joy and satisfaction.",
|
10 |
"L'Oréal - Because You're Worth It. Embracing self-worth and beauty.",
|
11 |
+
"BMW - The Ultimate Driving Machine. Engineering excellence in motion.",
|
12 |
+
"Mastercard - There are some things money can't buy. For everything else, there's MasterCard.",
|
13 |
+
"Google - Don't be evil. Building technology for a better world.",
|
14 |
+
"Amazon - Work Hard. Have Fun. Make History. Revolutionizing how the world shops."
|
15 |
]
|
16 |
|
17 |
+
def get_feature_activation(text: str, feature_id: int) -> Dict:
|
18 |
response = requests.post(
|
19 |
+
"https://www.neuronpedia.org/api/activation/new",
|
20 |
headers={"Content-Type": "application/json"},
|
21 |
json={
|
22 |
+
"feature": {
|
23 |
+
"modelId": "gemma-2-2b",
|
24 |
+
"layer": "0-gemmascope-mlp-16k",
|
25 |
+
"index": str(feature_id)
|
26 |
+
},
|
27 |
+
"customText": text
|
|
|
28 |
}
|
29 |
)
|
30 |
+
result = response.json()
|
31 |
+
values = result.get('values', [0])
|
32 |
+
return {
|
33 |
+
'id': feature_id,
|
34 |
+
'activation': max(values),
|
35 |
+
'token': result['tokens'][result['maxValueTokenIndex']] if values else None
|
36 |
+
}
|
37 |
+
|
38 |
+
def analyze_text(text: str, batch_size: int = 50) -> List[Dict]:
|
39 |
+
features = []
|
40 |
+
with ThreadPoolExecutor(max_workers=10) as executor:
|
41 |
+
futures = []
|
42 |
+
for i in range(0, 16384, batch_size):
|
43 |
+
futures.extend([
|
44 |
+
executor.submit(get_feature_activation, text, idx)
|
45 |
+
for idx in range(i, min(i + batch_size, 16384))
|
46 |
+
])
|
47 |
+
|
48 |
+
for future in executor.map(lambda f: f.result(), futures):
|
49 |
+
if future['activation'] > 1.0:
|
50 |
+
features.append(future)
|
51 |
+
|
52 |
+
return sorted(features, key=lambda x: x['activation'], reverse=True)[:10]
|
53 |
|
54 |
def format_output(features: List[Dict]) -> Tuple[str, str, str]:
|
55 |
if not features:
|
56 |
return "No significant neural activations detected", "", ""
|
57 |
|
58 |
output = "# Neural Analysis Results\n\n"
|
59 |
+
for f in features:
|
60 |
+
output += f"### Feature {f['id']}\n"
|
61 |
+
output += f"- Activation: {f['activation']:.2f}\n"
|
62 |
+
output += f"- Peak Token: {f['token']}\n\n"
|
|
|
|
|
63 |
|
64 |
+
dashboard_url = f"https://www.neuronpedia.org/gemma-2-2b/0-gemmascope-mlp-16k/{features[0]['id']}?embed=true&embedexplanation=true&embedplots=true&embedtest=true&height=300"
|
|
|
65 |
iframe = f'<iframe src="{dashboard_url}" width="100%" height="600px" frameborder="0"></iframe>'
|
66 |
|
67 |
+
return output, iframe, f"Feature {features[0]['id']} Dashboard"
|
68 |
|
69 |
def create_interface():
|
70 |
with gr.Blocks() as interface:
|
71 |
gr.Markdown("# Brand Message Neural Analyzer")
|
72 |
+
gr.Markdown("Analyze brand messages and taglines using Gemma's neural features")
|
73 |
|
74 |
with gr.Row():
|
75 |
with gr.Column():
|
76 |
input_text = gr.Textbox(
|
77 |
lines=5,
|
78 |
+
placeholder="Enter brand message or tagline to analyze...",
|
79 |
label="Brand Message"
|
80 |
)
|
81 |
analyze_btn = gr.Button("Analyze Neural Patterns")
|
|
|
87 |
dashboard = gr.HTML()
|
88 |
|
89 |
analyze_btn.click(
|
90 |
+
fn=lambda text: format_output(analyze_text(text)),
|
91 |
inputs=input_text,
|
92 |
outputs=[output_text, dashboard, feature_label]
|
93 |
)
|