Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,9 +2,6 @@ import streamlit as st
|
|
2 |
import matplotlib.pyplot as plt
|
3 |
import pandas as pd
|
4 |
import torch
|
5 |
-
import plotly.express as px
|
6 |
-
from sklearn.decomposition import PCA
|
7 |
-
from sklearn.manifold import TSNE
|
8 |
from transformers import AutoConfig, AutoTokenizer
|
9 |
|
10 |
# Page configuration
|
@@ -49,38 +46,18 @@ st.markdown("""
|
|
49 |
</style>
|
50 |
""", unsafe_allow_html=True)
|
51 |
|
52 |
-
#
|
53 |
MODELS = {
|
54 |
-
"BERT": {"model_name": "bert-base-uncased", "type": "Encoder", "layers": 12, "heads": 12,
|
55 |
-
|
56 |
-
|
57 |
-
"
|
58 |
-
|
59 |
-
|
60 |
-
"
|
61 |
-
|
62 |
-
|
63 |
-
"
|
64 |
-
"params": 125, "downloads": "7M+", "release_year": 2019, "gpu_req": "5GB+",
|
65 |
-
"cpu_req": "4 cores+", "ram_req": "10GB+"},
|
66 |
-
"DistilBERT": {"model_name": "distilbert-base-uncased", "type": "Encoder", "layers": 6,
|
67 |
-
"heads": 12, "params": 66, "downloads": "9M+", "release_year": 2019,
|
68 |
-
"gpu_req": "2GB+", "cpu_req": "2 cores+", "ram_req": "4GB+"},
|
69 |
-
"ALBERT": {"model_name": "albert-base-v2", "type": "Encoder", "layers": 12, "heads": 12,
|
70 |
-
"params": 11.8, "downloads": "3M+", "release_year": 2019, "gpu_req": "1GB+",
|
71 |
-
"cpu_req": "1 core+", "ram_req": "2GB+"},
|
72 |
-
"ELECTRA": {"model_name": "google/electra-small-discriminator", "type": "Encoder",
|
73 |
-
"layers": 12, "heads": 12, "params": 13.5, "downloads": "2M+",
|
74 |
-
"release_year": 2020, "gpu_req": "2GB+", "cpu_req": "2 cores+", "ram_req": "4GB+"},
|
75 |
-
"XLNet": {"model_name": "xlnet-base-cased", "type": "AutoRegressive", "layers": 12,
|
76 |
-
"heads": 12, "params": 110, "downloads": "4M+", "release_year": 2019,
|
77 |
-
"gpu_req": "5GB+", "cpu_req": "4 cores+", "ram_req": "8GB+"},
|
78 |
-
"BART": {"model_name": "facebook/bart-base", "type": "Seq2Seq", "layers": 6, "heads": 16,
|
79 |
-
"params": 139, "downloads": "6M+", "release_year": 2020, "gpu_req": "6GB+",
|
80 |
-
"cpu_req": "4 cores+", "ram_req": "12GB+"},
|
81 |
-
"DeBERTa": {"model_name": "microsoft/deberta-base", "type": "Encoder", "layers": 12,
|
82 |
-
"heads": 12, "params": 139, "downloads": "3M+", "release_year": 2021,
|
83 |
-
"gpu_req": "8GB+", "cpu_req": "6 cores+", "ram_req": "16GB+"}
|
84 |
}
|
85 |
|
86 |
def get_model_config(model_name):
|
@@ -109,7 +86,7 @@ def plot_model_comparison(selected_model):
|
|
109 |
def visualize_architecture(model_info):
|
110 |
architecture = []
|
111 |
model_type = model_info["type"]
|
112 |
-
layers = model_info
|
113 |
heads = model_info["heads"]
|
114 |
|
115 |
architecture.append("Input")
|
@@ -178,86 +155,13 @@ def visualize_attention_patterns():
|
|
178 |
fig.patch.set_facecolor('#2c2c2c')
|
179 |
st.pyplot(fig)
|
180 |
|
181 |
-
def
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
"neural", "network", "language", "processing"]
|
187 |
-
|
188 |
-
# Create dummy embeddings (3D for visualization)
|
189 |
-
embeddings = torch.randn(len(words), 256)
|
190 |
-
|
191 |
-
# Dimensionality reduction
|
192 |
-
method = st.selectbox("Reduction Method", ["PCA", "t-SNE"])
|
193 |
-
|
194 |
-
if method == "PCA":
|
195 |
-
reduced = PCA(n_components=3).fit_transform(embeddings)
|
196 |
else:
|
197 |
-
|
198 |
-
|
199 |
-
# Create interactive 3D plot
|
200 |
-
fig = px.scatter_3d(
|
201 |
-
x=reduced[:,0], y=reduced[:,1], z=reduced[:,2],
|
202 |
-
text=words,
|
203 |
-
title=f"Word Embeddings ({method})"
|
204 |
-
)
|
205 |
-
fig.update_traces(marker=dict(size=5), textposition='top center')
|
206 |
-
st.plotly_chart(fig, use_container_width=True)
|
207 |
-
|
208 |
-
def hardware_recommendations(model_info):
|
209 |
-
st.subheader("💻 Hardware Recommendations")
|
210 |
-
|
211 |
-
col1, col2, col3 = st.columns(3)
|
212 |
-
with col1:
|
213 |
-
st.metric("Minimum GPU", model_info.get("gpu_req", "4GB+"))
|
214 |
-
with col2:
|
215 |
-
st.metric("CPU Recommendation", model_info.get("cpu_req", "4 cores+"))
|
216 |
-
with col3:
|
217 |
-
st.metric("RAM Requirement", model_info.get("ram_req", "8GB+"))
|
218 |
-
|
219 |
-
st.markdown("""
|
220 |
-
**Cloud Recommendations:**
|
221 |
-
- AWS: g4dn.xlarge instance
|
222 |
-
- GCP: n1-standard-4 with T4 GPU
|
223 |
-
- Azure: Standard_NC4as_T4_v3
|
224 |
-
""")
|
225 |
-
|
226 |
-
def model_zoo_statistics():
|
227 |
-
st.subheader("📊 Model Zoo Statistics")
|
228 |
-
|
229 |
-
df = pd.DataFrame.from_dict(MODELS, orient='index')
|
230 |
-
st.dataframe(
|
231 |
-
df[["release_year", "downloads", "params"]],
|
232 |
-
column_config={
|
233 |
-
"release_year": "Release Year",
|
234 |
-
"downloads": "Downloads",
|
235 |
-
"params": "Params (M)"
|
236 |
-
},
|
237 |
-
use_container_width=True,
|
238 |
-
height=400
|
239 |
-
)
|
240 |
-
|
241 |
-
fig = px.bar(df, x=df.index, y="params", title="Model Parameters Comparison")
|
242 |
-
st.plotly_chart(fig, use_container_width=True)
|
243 |
-
|
244 |
-
def memory_usage_estimator(model_info):
|
245 |
-
st.subheader("🧮 Memory Usage Estimator")
|
246 |
-
|
247 |
-
precision = st.selectbox("Precision", ["FP32", "FP16", "INT8"])
|
248 |
-
batch_size = st.slider("Batch size", 1, 128, 8)
|
249 |
-
|
250 |
-
# Memory calculation
|
251 |
-
bytes_map = {"FP32": 4, "FP16": 2, "INT8": 1}
|
252 |
-
estimated_memory = (model_info["params"] * 1e6 * bytes_map[precision] * batch_size) / (1024**3)
|
253 |
-
|
254 |
-
col1, col2 = st.columns(2)
|
255 |
-
with col1:
|
256 |
-
st.metric("Estimated VRAM", f"{estimated_memory:.1f} GB")
|
257 |
-
with col2:
|
258 |
-
st.metric("Recommended GPU", "RTX 3090" if estimated_memory > 24 else "RTX 3060")
|
259 |
-
|
260 |
-
st.progress(min(estimated_memory/40, 1.0), text="GPU Memory Utilization (of 40GB GPU)")
|
261 |
|
262 |
def main():
|
263 |
st.title("🧠 Transformer Model Visualizer")
|
@@ -271,16 +175,15 @@ def main():
|
|
271 |
with col1:
|
272 |
st.metric("Model Type", model_info["type"])
|
273 |
with col2:
|
274 |
-
st.metric("Layers", model_info
|
275 |
with col3:
|
276 |
st.metric("Attention Heads", model_info["heads"])
|
277 |
with col4:
|
278 |
st.metric("Parameters", f"{model_info['params']}M")
|
279 |
|
280 |
-
|
281 |
-
tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs([
|
282 |
"Model Structure", "Comparison", "Model Attention",
|
283 |
-
"Tokenization", "
|
284 |
])
|
285 |
|
286 |
with tab1:
|
@@ -303,10 +206,6 @@ def main():
|
|
303 |
with tab3:
|
304 |
st.subheader("Model-specific Visualizations")
|
305 |
visualize_attention_patterns()
|
306 |
-
if selected_model == "BERT":
|
307 |
-
st.write("BERT-specific visualization example")
|
308 |
-
elif selected_model == "GPT-2":
|
309 |
-
st.write("GPT-2 attention mask visualization")
|
310 |
|
311 |
with tab4:
|
312 |
st.subheader("📝 Tokenization Visualization")
|
@@ -317,7 +216,6 @@ def main():
|
|
317 |
st.markdown("**Tokenized Output**")
|
318 |
tokens = tokenizer.tokenize(input_text)
|
319 |
st.write(tokens)
|
320 |
-
|
321 |
with col2:
|
322 |
st.markdown("**Token IDs**")
|
323 |
encoded_ids = tokenizer.encode(input_text)
|
@@ -328,15 +226,7 @@ def main():
|
|
328 |
"Token": tokens,
|
329 |
"ID": encoded_ids[1:-1] if tokenizer.cls_token else encoded_ids
|
330 |
})
|
331 |
-
st.dataframe(
|
332 |
-
token_data,
|
333 |
-
height=150,
|
334 |
-
use_container_width=True,
|
335 |
-
column_config={
|
336 |
-
"Token": "Token",
|
337 |
-
"ID": {"header": "ID", "help": "Numerical representation of the token"}
|
338 |
-
}
|
339 |
-
)
|
340 |
|
341 |
st.markdown(f"""
|
342 |
**Tokenizer Info:**
|
@@ -345,16 +235,40 @@ def main():
|
|
345 |
- Padding token: `{tokenizer.pad_token}`
|
346 |
- Max length: `{tokenizer.model_max_length}`
|
347 |
""")
|
348 |
-
|
349 |
with tab5:
|
350 |
-
|
351 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
352 |
with tab6:
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
358 |
|
359 |
if __name__ == "__main__":
|
360 |
main()
|
|
|
2 |
import matplotlib.pyplot as plt
|
3 |
import pandas as pd
|
4 |
import torch
|
|
|
|
|
|
|
5 |
from transformers import AutoConfig, AutoTokenizer
|
6 |
|
7 |
# Page configuration
|
|
|
46 |
</style>
|
47 |
""", unsafe_allow_html=True)
|
48 |
|
49 |
+
# Model database
|
50 |
MODELS = {
|
51 |
+
"BERT": {"model_name": "bert-base-uncased", "type": "Encoder", "layers": 12, "heads": 12, "params": 109.48},
|
52 |
+
"GPT-2": {"model_name": "gpt2", "type": "Decoder", "layers": 12, "heads": 12, "params": 117},
|
53 |
+
"T5-Small": {"model_name": "t5-small", "type": "Seq2Seq", "layers": 6, "heads": 8, "params": 60},
|
54 |
+
"RoBERTa": {"model_name": "roberta-base", "type": "Encoder", "layers": 12, "heads": 12, "params": 125},
|
55 |
+
"DistilBERT": {"model_name": "distilbert-base-uncased", "type": "Encoder", "layers": 6, "heads": 12, "params": 66},
|
56 |
+
"ALBERT": {"model_name": "albert-base-v2", "type": "Encoder", "layers": 12, "heads": 12, "params": 11.8},
|
57 |
+
"ELECTRA": {"model_name": "google/electra-small-discriminator", "type": "Encoder", "layers": 12, "heads": 12, "params": 13.5},
|
58 |
+
"XLNet": {"model_name": "xlnet-base-cased", "type": "AutoRegressive", "layers": 12, "heads": 12, "params": 110},
|
59 |
+
"BART": {"model_name": "facebook/bart-base", "type": "Seq2Seq", "layers": 6, "heads": 16, "params": 139},
|
60 |
+
"DeBERTa": {"model_name": "microsoft/deberta-base", "type": "Encoder", "layers": 12, "heads": 12, "params": 139}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
}
|
62 |
|
63 |
def get_model_config(model_name):
|
|
|
86 |
def visualize_architecture(model_info):
|
87 |
architecture = []
|
88 |
model_type = model_info["type"]
|
89 |
+
layers = model_info.get("layers", model_info.get("layers", 12)) # Handle key variations
|
90 |
heads = model_info["heads"]
|
91 |
|
92 |
architecture.append("Input")
|
|
|
155 |
fig.patch.set_facecolor('#2c2c2c')
|
156 |
st.pyplot(fig)
|
157 |
|
158 |
+
def get_hardware_recommendation(params):
|
159 |
+
if params < 100:
|
160 |
+
return "CPU or Entry-level GPU (e.g., GTX 1060)"
|
161 |
+
elif 100 <= params < 200:
|
162 |
+
return "Mid-range GPU (e.g., RTX 2080, RTX 3060)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
else:
|
164 |
+
return "High-end GPU (e.g., RTX 3090, A100) or TPU"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
def main():
|
167 |
st.title("🧠 Transformer Model Visualizer")
|
|
|
175 |
with col1:
|
176 |
st.metric("Model Type", model_info["type"])
|
177 |
with col2:
|
178 |
+
st.metric("Layers", model_info.get("layers", model_info.get("layers", "N/A")))
|
179 |
with col3:
|
180 |
st.metric("Attention Heads", model_info["heads"])
|
181 |
with col4:
|
182 |
st.metric("Parameters", f"{model_info['params']}M")
|
183 |
|
184 |
+
tab1, tab2, tab3, tab4, tab5, tab6 = st.tabs([
|
|
|
185 |
"Model Structure", "Comparison", "Model Attention",
|
186 |
+
"Tokenization", "Hardware", "Memory"
|
187 |
])
|
188 |
|
189 |
with tab1:
|
|
|
206 |
with tab3:
|
207 |
st.subheader("Model-specific Visualizations")
|
208 |
visualize_attention_patterns()
|
|
|
|
|
|
|
|
|
209 |
|
210 |
with tab4:
|
211 |
st.subheader("📝 Tokenization Visualization")
|
|
|
216 |
st.markdown("**Tokenized Output**")
|
217 |
tokens = tokenizer.tokenize(input_text)
|
218 |
st.write(tokens)
|
|
|
219 |
with col2:
|
220 |
st.markdown("**Token IDs**")
|
221 |
encoded_ids = tokenizer.encode(input_text)
|
|
|
226 |
"Token": tokens,
|
227 |
"ID": encoded_ids[1:-1] if tokenizer.cls_token else encoded_ids
|
228 |
})
|
229 |
+
st.dataframe(token_data, height=150, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
230 |
|
231 |
st.markdown(f"""
|
232 |
**Tokenizer Info:**
|
|
|
235 |
- Padding token: `{tokenizer.pad_token}`
|
236 |
- Max length: `{tokenizer.model_max_length}`
|
237 |
""")
|
238 |
+
|
239 |
with tab5:
|
240 |
+
st.subheader("🖥️ Hardware Recommendation")
|
241 |
+
params = model_info["params"]
|
242 |
+
recommendation = get_hardware_recommendation(params)
|
243 |
+
|
244 |
+
st.markdown(f"**Recommended hardware for {selected_model}:**")
|
245 |
+
st.info(recommendation)
|
246 |
+
|
247 |
+
st.markdown("""
|
248 |
+
**Recommendation Criteria:**
|
249 |
+
- <100M parameters: Suitable for CPU or entry-level GPUs
|
250 |
+
- 100-200M parameters: Requires mid-range GPUs
|
251 |
+
- >200M parameters: Needs high-end GPUs/TPUs
|
252 |
+
""")
|
253 |
+
|
254 |
with tab6:
|
255 |
+
st.subheader("💾 Memory Usage Estimation")
|
256 |
+
params = model_info["params"]
|
257 |
+
memory_mb = params * 4 # 1M params ≈ 4MB in FP32
|
258 |
+
memory_gb = memory_mb / 1024
|
259 |
+
|
260 |
+
st.metric("Estimated Memory (FP32)",
|
261 |
+
f"{memory_mb:.1f} MB / {memory_gb:.2f} GB")
|
262 |
+
|
263 |
+
st.markdown("""
|
264 |
+
**Memory Notes:**
|
265 |
+
- Based on 4 bytes per parameter (FP32 precision)
|
266 |
+
- Actual usage varies with:
|
267 |
+
- Batch size
|
268 |
+
- Sequence length
|
269 |
+
- Precision (FP16/FP32)
|
270 |
+
- Optimizer states (training)
|
271 |
+
""")
|
272 |
|
273 |
if __name__ == "__main__":
|
274 |
main()
|