Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,13 +4,18 @@ import torch
|
|
| 4 |
import json
|
| 5 |
from collections import defaultdict, OrderedDict
|
| 6 |
|
| 7 |
-
def analyze_model_parameters(model_path, show_layer_details=False):
|
| 8 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Load model configuration first
|
| 10 |
-
config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
|
| 11 |
|
| 12 |
# Load model on CPU
|
| 13 |
-
model = AutoModel.from_pretrained(model_path, device_map="cpu", trust_remote_code=True)
|
| 14 |
|
| 15 |
# Initialize counters
|
| 16 |
total_params = 0
|
|
@@ -169,15 +174,21 @@ def analyze_model_parameters(model_path, show_layer_details=False):
|
|
| 169 |
return summary + layer_details
|
| 170 |
|
| 171 |
except Exception as e:
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
-
def count_parameters_basic(model_path):
|
| 175 |
"""Basic parameter counting without layer details"""
|
| 176 |
-
return analyze_model_parameters(model_path, show_layer_details=False)
|
| 177 |
|
| 178 |
-
def count_parameters_detailed(model_path):
|
| 179 |
"""Detailed parameter counting with layer-by-layer breakdown"""
|
| 180 |
-
return analyze_model_parameters(model_path, show_layer_details=True)
|
| 181 |
|
| 182 |
# Create Gradio interface with multiple outputs
|
| 183 |
with gr.Blocks(title="π€ Advanced HuggingFace Model Parameter Analyzer", theme=gr.themes.Soft()) as demo:
|
|
@@ -189,6 +200,7 @@ with gr.Blocks(title="π€ Advanced HuggingFace Model Parameter Analyzer", theme
|
|
| 189 |
- **Embedding vs non-embedding breakdown**
|
| 190 |
- **Layer-by-layer analysis**
|
| 191 |
- **Weight sharing detection**
|
|
|
|
| 192 |
""")
|
| 193 |
|
| 194 |
with gr.Row():
|
|
@@ -200,8 +212,16 @@ with gr.Blocks(title="π€ Advanced HuggingFace Model Parameter Analyzer", theme
|
|
| 200 |
)
|
| 201 |
|
| 202 |
with gr.Column(scale=1):
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
output_text = gr.Textbox(
|
| 207 |
label="π Analysis Results",
|
|
@@ -213,13 +233,13 @@ with gr.Blocks(title="π€ Advanced HuggingFace Model Parameter Analyzer", theme
|
|
| 213 |
# Event handlers
|
| 214 |
analyze_btn.click(
|
| 215 |
fn=count_parameters_basic,
|
| 216 |
-
inputs=model_input,
|
| 217 |
outputs=output_text
|
| 218 |
)
|
| 219 |
|
| 220 |
detailed_btn.click(
|
| 221 |
fn=count_parameters_detailed,
|
| 222 |
-
inputs=model_input,
|
| 223 |
outputs=output_text
|
| 224 |
)
|
| 225 |
|
|
@@ -244,6 +264,8 @@ with gr.Blocks(title="π€ Advanced HuggingFace Model Parameter Analyzer", theme
|
|
| 244 |
- **Weight tying detection**: Automatically handles shared parameters (e.g., input/output embeddings)
|
| 245 |
- **Layer categorization**: Groups parameters by transformer layers, embeddings, etc.
|
| 246 |
- **Detailed analysis**: Click "Detailed Analysis" for parameter-by-parameter breakdown
|
|
|
|
|
|
|
| 247 |
- **Model compatibility**: Works with most HuggingFace transformer models
|
| 248 |
""")
|
| 249 |
|
|
|
|
| 4 |
import json
|
| 5 |
from collections import defaultdict, OrderedDict
|
| 6 |
|
| 7 |
+
def analyze_model_parameters(model_path, hf_token=None, show_layer_details=False):
|
| 8 |
try:
|
| 9 |
+
# Prepare token parameter
|
| 10 |
+
token_kwargs = {}
|
| 11 |
+
if hf_token and hf_token.strip():
|
| 12 |
+
token_kwargs['token'] = hf_token.strip()
|
| 13 |
+
|
| 14 |
# Load model configuration first
|
| 15 |
+
config = AutoConfig.from_pretrained(model_path, trust_remote_code=True, **token_kwargs)
|
| 16 |
|
| 17 |
# Load model on CPU
|
| 18 |
+
model = AutoModel.from_pretrained(model_path, device_map="cpu", trust_remote_code=True, **token_kwargs)
|
| 19 |
|
| 20 |
# Initialize counters
|
| 21 |
total_params = 0
|
|
|
|
| 174 |
return summary + layer_details
|
| 175 |
|
| 176 |
except Exception as e:
|
| 177 |
+
error_msg = str(e)
|
| 178 |
+
if "401" in error_msg or "authentication" in error_msg.lower():
|
| 179 |
+
return f"π **Authentication Error:** This model requires a valid HuggingFace token.\n\nPlease provide your HuggingFace token in the token field above.\n\nOriginal error: {error_msg}"
|
| 180 |
+
elif "404" in error_msg or "not found" in error_msg.lower():
|
| 181 |
+
return f"π **Model Not Found:** The model '{model_path}' was not found.\n\nPlease check:\n- Model path is correct\n- Model exists on HuggingFace Hub\n- You have access to the model (use token if private)\n\nOriginal error: {error_msg}"
|
| 182 |
+
else:
|
| 183 |
+
return f"β **Error loading model:** {error_msg}\n\nPlease check that the model path is correct and accessible."
|
| 184 |
|
| 185 |
+
def count_parameters_basic(model_path, hf_token=None):
|
| 186 |
"""Basic parameter counting without layer details"""
|
| 187 |
+
return analyze_model_parameters(model_path, hf_token, show_layer_details=False)
|
| 188 |
|
| 189 |
+
def count_parameters_detailed(model_path, hf_token=None):
|
| 190 |
"""Detailed parameter counting with layer-by-layer breakdown"""
|
| 191 |
+
return analyze_model_parameters(model_path, hf_token, show_layer_details=True)
|
| 192 |
|
| 193 |
# Create Gradio interface with multiple outputs
|
| 194 |
with gr.Blocks(title="π€ Advanced HuggingFace Model Parameter Analyzer", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 200 |
- **Embedding vs non-embedding breakdown**
|
| 201 |
- **Layer-by-layer analysis**
|
| 202 |
- **Weight sharing detection**
|
| 203 |
+
- **Private model access** with HuggingFace token
|
| 204 |
""")
|
| 205 |
|
| 206 |
with gr.Row():
|
|
|
|
| 212 |
)
|
| 213 |
|
| 214 |
with gr.Column(scale=1):
|
| 215 |
+
hf_token_input = gr.Textbox(
|
| 216 |
+
label="π HuggingFace Token (Optional)",
|
| 217 |
+
placeholder="hf_...",
|
| 218 |
+
type="password",
|
| 219 |
+
info="Required for private models or gated models"
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
with gr.Row():
|
| 223 |
+
analyze_btn = gr.Button("π Analyze Model", variant="primary")
|
| 224 |
+
detailed_btn = gr.Button("π Detailed Analysis", variant="secondary")
|
| 225 |
|
| 226 |
output_text = gr.Textbox(
|
| 227 |
label="π Analysis Results",
|
|
|
|
| 233 |
# Event handlers
|
| 234 |
analyze_btn.click(
|
| 235 |
fn=count_parameters_basic,
|
| 236 |
+
inputs=[model_input, hf_token_input],
|
| 237 |
outputs=output_text
|
| 238 |
)
|
| 239 |
|
| 240 |
detailed_btn.click(
|
| 241 |
fn=count_parameters_detailed,
|
| 242 |
+
inputs=[model_input, hf_token_input],
|
| 243 |
outputs=output_text
|
| 244 |
)
|
| 245 |
|
|
|
|
| 264 |
- **Weight tying detection**: Automatically handles shared parameters (e.g., input/output embeddings)
|
| 265 |
- **Layer categorization**: Groups parameters by transformer layers, embeddings, etc.
|
| 266 |
- **Detailed analysis**: Click "Detailed Analysis" for parameter-by-parameter breakdown
|
| 267 |
+
- **Private models**: Use your HuggingFace token to access private or gated models
|
| 268 |
+
- **Token security**: Token is only used for this session and not stored
|
| 269 |
- **Model compatibility**: Works with most HuggingFace transformer models
|
| 270 |
""")
|
| 271 |
|