Spaces:
Runtime error
Runtime error
File size: 19,340 Bytes
c71f5c6 8430c55 bb7473f ffc03fb 6dc6bb2 5e22042 8430c55 c71f5c6 1581622 8430c55 c71f5c6 1581622 65c2b88 c71f5c6 1581622 0d68c58 1581622 0d68c58 1581622 0d68c58 1581622 0d68c58 1581622 0d68c58 1581622 0d68c58 1581622 0d68c58 1581622 0d68c58 1581622 0d68c58 1581622 0d68c58 1581622 c71f5c6 1581622 5e22042 c71f5c6 5e22042 bb7473f 1581622 bb7473f 5e22042 bb7473f 5e22042 bb7473f 1581622 c71f5c6 5e22042 c71f5c6 5e22042 bb7473f 5e22042 c71f5c6 1581622 c71f5c6 1581622 c71f5c6 1581622 c71f5c6 1581622 c71f5c6 5e22042 8430c55 c71f5c6 1581622 c71f5c6 bb7473f ffc03fb bb7473f c71f5c6 ffc03fb c71f5c6 bb7473f ffc03fb bb7473f 6dc6bb2 1581622 6dc6bb2 ffc03fb 6dc6bb2 c71f5c6 bb7473f ffc03fb 90a8fd5 c71f5c6 5e22042 90a8fd5 ffc03fb 6dc6bb2 ffc03fb 5e22042 6dc6bb2 90a8fd5 c71f5c6 0d5c2c1 c71f5c6 8430c55 bb7473f ffc03fb 90a8fd5 8430c55 bb7473f 8430c55 bb7473f ffc03fb c71f5c6 bb7473f ffc03fb c71f5c6 1581622 c71f5c6 1581622 c71f5c6 8430c55 bb7473f 8430c55 90a8fd5 8430c55 bb7473f 8430c55 90a8fd5 bb7473f 8430c55 90a8fd5 8430c55 90a8fd5 8430c55 bb7473f 8430c55 bb7473f f04497e 90a8fd5 f04497e 90a8fd5 8430c55 90a8fd5 8430c55 90a8fd5 f04497e 90a8fd5 f04497e 90a8fd5 f04497e 8430c55 c71f5c6 8430c55 bb7473f 0d5c2c1 8430c55 90a8fd5 0d68c58 1581622 0d68c58 c5f9cd8 65c2b88 1581622 65c2b88 0d68c58 1581622 0d68c58 1581622 0d68c58 c71f5c6 9f76d30 90a8fd5 9f76d30 90a8fd5 9f76d30 6dc6bb2 9f76d30 bb7473f f04497e bb7473f 2c3c98c 0d5c2c1 2c3c98c 0d5c2c1 90a8fd5 5e22042 90a8fd5 bb7473f 90a8fd5 c71f5c6 0d68c58 1581622 0d68c58 1581622 0d68c58 1581622 0d68c58 c71f5c6 1581622 c71f5c6 1581622 bb7473f 8430c55 bb7473f 8430c55 bb7473f 8430c55 bb7473f 8430c55 c71f5c6 1581622 c71f5c6 58cfe8f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | import json
import tempfile
import os
import glob
import shutil
import io
import time
import threading
import sys
import gradio as gr
import torch
from huggingface_hub import hf_hub_download, scan_cache_dir, whoami
from safetensors import safe_open
# Default token from HF_TOKEN environment variable (for HuggingFace Spaces)
DEFAULT_HF_TOKEN = os.environ.get("HF_TOKEN")
def hf_login(token: str, session_token: str):
"""Login to Hugging Face with provided token (per-user session)."""
if not token:
return "β Please provide a token", "Not logged in", session_token
try:
user_info = whoami(token=token)
username = user_info.get('name', 'Unknown')
return f"β
Successfully logged in as: {username}", f"β
Logged in as {username}", token
except Exception as e:
return f"β Login failed: {str(e)}", "β Not logged in", session_token
def hf_logout(session_token: str):
"""Logout from Hugging Face (clear session token)."""
return "β
Successfully logged out", "Not logged in", None
def check_hf_status(session_token: str):
"""Check current HF login status for this session."""
# Check session token first, then fall back to default token
token = session_token or DEFAULT_HF_TOKEN
if not token:
return "βΉοΈ Not logged in", "Not logged in", session_token
try:
user_info = whoami(token=token)
username = user_info.get('name', 'Unknown')
source = "(session)" if session_token else "(default HF_TOKEN)"
return f"β
Currently logged in as: {username} {source}", f"β
Logged in as {username}", session_token
except Exception:
return "βΉοΈ Not logged in", "Not logged in", session_token
def get_param(model_id: str, param_key: str, log_buffer: io.StringIO, progress: gr.Progress, token: str = None):
"""
Download and return a specific parameter tensor from a Hugging Face model.
"""
# Use session token or fall back to default token
auth_token = token or DEFAULT_HF_TOKEN
# Redirect stderr to log buffer for real-time tqdm updates
original_stderr = sys.stderr
sys.stderr = log_buffer
try:
# Try to download the index file (for sharded models)
try:
log_buffer.write(f"π₯ Downloading index file for {model_id}...\n")
progress(0.1, desc="Downloading index...")
index_path = hf_hub_download(
model_id, "model.safetensors.index.json", token=auth_token)
log_buffer.write(f"β Index file found: {index_path}\n")
with open(index_path, "r", encoding="utf-8") as f:
index = json.load(f)
weight_map = index["weight_map"]
if param_key not in weight_map:
raise KeyError(
f"Parameter '{param_key}' not found in model. Available keys: {list(weight_map.keys())[:10]}..."
)
shard_file = weight_map[param_key]
log_buffer.write(f"β Parameter found in shard: {shard_file}\n")
except Exception as e:
if "404" in str(e) or "not found" in str(e).lower():
log_buffer.write("βΉοΈ No index file, trying single model file...\n")
shard_file = "model.safetensors"
else:
raise
log_buffer.write(f"π₯ Downloading shard: {shard_file}...\n")
progress(0.3, desc=f"Downloading {shard_file}...")
shard_path = hf_hub_download(model_id, shard_file, token=auth_token)
log_buffer.write(f"\nβ Shard downloaded: {shard_path}\n")
progress(0.7, desc="Loading tensor...")
log_buffer.write(f"π Loading tensor '{param_key}'...\n")
with safe_open(shard_path, framework="pt") as f:
tensor = f.get_tensor(param_key)
log_buffer.write(f"β Tensor loaded successfully\n")
progress(0.9, desc="Finalizing...")
return tensor
finally:
# Restore original stderr
sys.stderr = original_stderr
def get_available_keys(model_id: str, token: str = None):
"""Get all available parameter keys from a model."""
# Use session token or fall back to default token
auth_token = token or DEFAULT_HF_TOKEN
try:
index_path = hf_hub_download(model_id, "model.safetensors.index.json", token=auth_token)
with open(index_path, "r", encoding="utf-8") as f:
index = json.load(f)
return sorted(index["weight_map"].keys())
except Exception:
# Try single file
try:
shard_path = hf_hub_download(model_id, "model.safetensors", token=auth_token)
with safe_open(shard_path, framework="pt") as f:
return sorted(f.keys())
except Exception as e:
return []
def format_tensor_info(tensor: torch.Tensor) -> str:
"""Format tensor information for display."""
info = []
info.append(f"**Shape:** {list(tensor.shape)}")
info.append(f"**Dtype:** {tensor.dtype}")
info.append(f"**Device:** {tensor.device}")
info.append(f"**Numel:** {tensor.numel():,}")
# Handle special dtypes that don't support statistical operations
try:
# Convert FP8 and other special dtypes to float32 for stats
if str(tensor.dtype) in ['torch.float8_e4m3fn', 'torch.float8_e5m2']:
stats_tensor = tensor.to(torch.float32)
else:
stats_tensor = tensor
info.append(f"**Min:** {stats_tensor.min().item():.6f}")
info.append(f"**Max:** {stats_tensor.max().item():.6f}")
info.append(f"**Mean:** {stats_tensor.float().mean().item():.6f}")
info.append(f"**Std:** {stats_tensor.float().std().item():.6f}")
except Exception as e:
info.append(f"**Stats:** Unable to compute (dtype not supported)")
return "<br>".join(info)
def fetch_param(model_id: str, param_key: str, session_token: str, progress=gr.Progress()):
"""Fetch parameter and return formatted info and tensor preview."""
log_buffer = io.StringIO()
last_log_value = ""
if not model_id or not param_key:
yield "Please provide both model ID and parameter key.", "", None, "β Missing required inputs"
return
try:
log_buffer.write(f"π Starting download for {model_id}\n")
log_buffer.write(f"π― Target parameter: {param_key}\n\n")
progress(0, desc="Initializing...")
yield "", "", None, log_buffer.getvalue()
time.sleep(0.5)
# Start download in background thread
download_complete = threading.Event()
download_error = [None] # Use list to store exception from thread
result_tensor = [None] # Use list to store result from thread
def download_thread():
try:
result_tensor[0] = get_param(model_id, param_key, log_buffer, progress, session_token)
except Exception as e:
download_error[0] = e
finally:
download_complete.set()
thread = threading.Thread(target=download_thread, daemon=True)
thread.start()
# Poll log buffer every 1 second while download is running
while not download_complete.is_set():
current_log = log_buffer.getvalue()
if current_log != last_log_value:
yield "", "", None, current_log
last_log_value = current_log
time.sleep(1)
# Final log update after download completes
current_log = log_buffer.getvalue()
if current_log != last_log_value:
yield "", "", None, current_log
last_log_value = current_log
# Check for errors
if download_error[0]:
raise download_error[0]
tensor = result_tensor[0]
info = format_tensor_info(tensor)
# Create tensor preview (first few elements)
log_buffer.write(f"\nπ Creating preview...\n")
yield "", "", None, log_buffer.getvalue()
flat = tensor.flatten()
preview_size = min(100, flat.numel())
# Convert to float32 for FP8 types for display
if str(tensor.dtype) in ['torch.float8_e4m3fn', 'torch.float8_e5m2']:
preview = flat[:preview_size].to(torch.float32).tolist()
else:
preview = flat[:preview_size].tolist()
# Format preview in multiple lines (10 values per line)
# Adapt to different data types
preview_lines = []
for i in range(0, len(preview), 10):
line_values = preview[i:i+10]
if tensor.dtype in [torch.float32, torch.float64, torch.float16, torch.bfloat16] or str(tensor.dtype) in ['torch.float8_e4m3fn', 'torch.float8_e5m2']:
preview_lines.append(", ".join(f"{v:.6f}" for v in line_values))
elif tensor.dtype in [torch.int8, torch.int16, torch.int32, torch.int64, torch.uint8]:
preview_lines.append(", ".join(f"{v}" for v in line_values))
elif tensor.dtype == torch.bool:
preview_lines.append(", ".join(f"{v}" for v in line_values))
else:
preview_lines.append(", ".join(str(v) for v in line_values))
preview_str = f"**First {preview_size} values:**\n```\n" + \
"\n".join(preview_lines) + "\n```"
# if flat.numel() > preview_size:
# preview_str += f"\n\n... and {flat.numel() - preview_size:,} more values"
# Save tensor for download
log_buffer.write(f"πΎ Saving tensor for download...\n")
yield info, preview_str, None, log_buffer.getvalue()
temp_dir = tempfile.gettempdir()
safe_param_key = param_key.replace("/", "_").replace(".", "_")
download_path = os.path.join(temp_dir, f"{safe_param_key}.pt")
torch.save(tensor, download_path)
log_buffer.write(f"β Saved to: {download_path}\n")
progress(1.0, desc="Complete!")
log_buffer.write(f"\nβ
All operations completed successfully!\n")
yield info, preview_str, download_path, log_buffer.getvalue()
except Exception as e:
log_buffer.write(f"\nβ Error: {str(e)}\n")
yield f"**Error:** {str(e)}", "", None, log_buffer.getvalue()
def list_keys(model_id: str, session_token: str):
"""List all available keys for a model."""
if not model_id:
return "Please provide a model ID."
try:
keys = get_available_keys(model_id, session_token)
if not keys:
return "No keys found or failed to load model."
return "\n".join(keys)
except Exception as e:
return f"**Error:** {str(e)}"
def clear_temp_files():
"""Clear all .pt files from temp directory."""
try:
temp_dir = tempfile.gettempdir()
pt_files = glob.glob(os.path.join(temp_dir, "*.pt"))
count = len(pt_files)
deleted_files = []
for file in pt_files:
try:
os.remove(file)
deleted_files.append(os.path.basename(file))
except Exception:
pass
if deleted_files:
files_list = "\n".join(deleted_files)
return f"β
Cleared {count} temporary file(s):\n\n{files_list}"
else:
return "β
No temporary files to clear"
except Exception as e:
return f"β Error: {str(e)}"
def clear_hf_cache():
"""Clear Hugging Face cache directory."""
try:
cache_info = scan_cache_dir()
total_size = cache_info.size_on_disk
total_repos = len(cache_info.repos)
if total_repos == 0:
return "β
Hugging Face cache is already empty"
# Get cache directory and clear it
cache_dir = os.path.expanduser("~/.cache/huggingface/hub")
if os.path.exists(cache_dir):
shutil.rmtree(cache_dir)
os.makedirs(cache_dir)
size_mb = total_size / (1024 * 1024)
return f"β
Cleared Hugging Face cache: {total_repos} repo(s), {size_mb:.2f} MB freed"
else:
return "β
Hugging Face cache directory not found"
except Exception as e:
return f"β Error: {str(e)}"
def get_cache_info():
"""Get size information about caches."""
try:
# Temp files
temp_dir = tempfile.gettempdir()
pt_files = glob.glob(os.path.join(temp_dir, "*.pt"))
temp_size = sum(os.path.getsize(f)
for f in pt_files if os.path.exists(f))
temp_size_mb = temp_size / (1024 * 1024)
info = f"π Cache Info:\n\n"
info += f"βββ Temp .pt files: {len(pt_files)} file(s), {temp_size_mb:.2f} MB βββ\n"
if pt_files:
for file in pt_files:
size = os.path.getsize(file) / (1024 * 1024)
filename = os.path.basename(file)
info += f" β’ {filename} ({size:.2f} MB)\n"
else:
info += " (empty)\n"
# HF cache
info += f"\nβββ Hugging Face Cache βββ\n"
try:
cache_info = scan_cache_dir()
hf_size_mb = cache_info.size_on_disk / (1024 * 1024)
hf_repos = len(cache_info.repos)
info += f"Total: {hf_repos} repo(s), {hf_size_mb:.2f} MB\n\n"
if hf_repos > 0:
for repo in cache_info.repos:
repo_size = repo.size_on_disk / (1024 * 1024)
info += f" π¦ {repo.repo_id}\n"
info += f" Size: {repo_size:.2f} MB, Revisions: {len(repo.revisions)}\n"
info += f" Last accessed: {repo.last_accessed}\n"
else:
info += " (empty)\n"
except Exception as e:
info += f" Error reading HF cache: {str(e)}\n"
info += f"\nβββ Total: {temp_size_mb + (hf_size_mb if 'hf_size_mb' in locals() else 0):.2f} MB βββ"
return info
except Exception as e:
return f"β Error: {str(e)}"
# Create Gradio interface
custom_css = """
* {
font-family: Consolas, Monaco, 'Courier New', monospace !important;
}
.compact-row {
gap: 0.5rem !important;
}
.tensor-preview pre {
font-size: 0.75rem !important;
line-height: 1.0 !important;
}
.compact-file {
max-height: 80px !important;
}
.compact-file > div {
min-height: 60px !important;
}
"""
with gr.Blocks(title="Hugging Face Model Weight Inspector") as demo:
gr.Markdown("# π Hugging Face Model Weight Inspector")
# Session state for per-user token
session_token = gr.State(None)
# HF Login section
with gr.Accordion("π Hugging Face Login (Per-User Session) [β οΈβ οΈβ οΈWIP, Do not useβ οΈβ οΈβ οΈ]", open=False):
gr.Markdown("""
**Note:** This Space uses the default `HF_TOKEN` secret for all users if no session token is provided.
Login below with your own token for per-user authentication (affects only your session).
""")
with gr.Row():
with gr.Column(scale=3):
hf_token_input = gr.Textbox(
label="HF Token",
placeholder="hf_...",
type="password",
)
with gr.Column(scale=2):
initial_status = "β
Using default HF_TOKEN" if DEFAULT_HF_TOKEN else "Not logged in"
hf_status = gr.Textbox(
label="Status",
value=initial_status,
interactive=False,
)
with gr.Row():
login_btn = gr.Button("π Login", variant="primary", scale=1)
logout_btn = gr.Button("πͺ Logout", variant="secondary", scale=1)
check_status_btn = gr.Button("βΉοΈ Check Status", variant="secondary", scale=1)
login_output = gr.Textbox(label="Login Status", interactive=False, lines=2)
with gr.Row():
with gr.Column(scale=1):
model_id_input = gr.Textbox(
label="Model ID",
placeholder="e.g., meta-llama/Llama-2-7b-hf",
value="Qwen/Qwen3-Coder-Next-FP8",
)
param_key_input = gr.Textbox(
label="Parameter Key",
placeholder="e.g., model.norm.weight",
value="model.norm.weight",
)
with gr.Row():
list_keys_btn = gr.Button(
"π List Keys", variant="secondary", scale=1)
fetch_btn = gr.Button("π Fetch", variant="primary", scale=1)
with gr.Column(scale=1):
keys_output = gr.Textbox(
label="Available Parameter Keys",
lines=5,
max_lines=8,
)
with gr.Tabs():
with gr.Tab("Results"):
with gr.Row():
with gr.Column(scale=3):
preview_output = gr.Markdown(label="Tensor Preview", elem_classes="tensor-preview")
with gr.Column(scale=1):
info_output = gr.Markdown(label="Tensor Info")
download_output = gr.File(label="Download Tensor (.pt file)", elem_classes="compact-file")
log_output = gr.Textbox(
label="π Download Log", lines=1, interactive=False)
with gr.Tab("Cache Management"):
with gr.Row():
get_info_btn = gr.Button(
"π Get Cache Info", variant="secondary", scale=1)
clear_temp_btn = gr.Button(
"ποΈ Clear Temp Folder", variant="secondary", scale=1)
clear_hf_btn = gr.Button(
"ποΈ Clear HF Cache", variant="secondary", scale=1)
clear_status = gr.Textbox(
label="Status", interactive=False, lines=6)
# Event handlers
login_btn.click(
fn=hf_login,
inputs=[hf_token_input, session_token],
outputs=[login_output, hf_status, session_token],
)
logout_btn.click(
fn=hf_logout,
inputs=[session_token],
outputs=[login_output, hf_status, session_token],
)
check_status_btn.click(
fn=check_hf_status,
inputs=[session_token],
outputs=[login_output, hf_status, session_token],
)
list_keys_btn.click(
fn=list_keys,
inputs=[model_id_input, session_token],
outputs=[keys_output],
)
fetch_btn.click(
fn=fetch_param,
inputs=[model_id_input, param_key_input, session_token],
outputs=[info_output, preview_output, download_output, log_output],
)
clear_temp_btn.click(
fn=clear_temp_files,
inputs=[],
outputs=[clear_status],
)
clear_hf_btn.click(
fn=clear_hf_cache,
inputs=[],
outputs=[clear_status],
)
get_info_btn.click(
fn=get_cache_info,
inputs=[],
outputs=[clear_status],
)
# Auto-check status on load
demo.load(
fn=check_hf_status,
inputs=[session_token],
outputs=[login_output, hf_status, session_token],
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", css=custom_css)
|