import gguf
import sys
import os
def load_gguf_file(file_path):
"""Load and return a GGUF reader object"""
if not os.path.exists(file_path):
print(f"Error: File '{file_path}' not found.")
return None
try:
reader = gguf.GGUFReader(file_path)
return reader
except Exception as e:
print(f"Error loading GGUF file: {e}")
return None
def show_metadata(reader):
"""Display model metadata"""
print("\n" + "=" * 50)
print("MODEL METADATA")
print("=" * 50)
if hasattr(reader, 'fields') and reader.fields:
for key, field in reader.fields.items():
# Handle different field types
if hasattr(field, 'parts'):
value = field.parts
elif hasattr(field, 'data'):
value = field.data
else:
value = str(field)
# Truncate very long values
if isinstance(value, (list, tuple)) and len(value) > 10:
print(f"{key}: [{len(value)} items] {value[:3]}...{value[-2:]}")
elif isinstance(value, str) and len(value) > 100:
print(f"{key}: {value[:100]}...")
else:
print(f"{key}: {value}")
else:
print("No metadata found or metadata format not recognized.")
def show_tokenizer_data(reader):
"""Display tokenizer information"""
print("\n" + "=" * 50)
print("TOKENIZER DATA")
print("=" * 50)
tokenizer_fields = {}
# Look for tokenizer-related fields
if hasattr(reader, 'fields'):
for key, field in reader.fields.items():
key_lower = key.lower()
if any(token_key in key_lower for token_key in ['token', 'vocab', 'bos', 'eos', 'pad', 'unk']):
if hasattr(field, 'parts'):
value = field.parts
elif hasattr(field, 'data'):
value = field.data
else:
value = str(field)
tokenizer_fields[key] = value
if tokenizer_fields:
for key, value in tokenizer_fields.items():
if isinstance(value, (list, tuple)):
if len(value) > 20:
print(f"{key}: [{len(value)} items]")
print(f" First 10: {value[:10]}")
print(f" Last 10: {value[-10:]}")
else:
print(f"{key}: {value}")
else:
print(f"{key}: {value}")
else:
print("No tokenizer data found in metadata fields.")
def show_model_weights(reader, limit=100):
"""Display first N model weights/tensors"""
print("\n" + "=" * 50)
print(f"MODEL WEIGHTS (First {limit})")
print("=" * 50)
if hasattr(reader, 'tensors') and reader.tensors:
tensors_shown = 0
total_tensors = len(reader.tensors)
print(f"Total tensors in model: {total_tensors}")
print("-" * 50)
for i, tensor in enumerate(reader.tensors):
if tensors_shown >= limit:
break
print(f"Tensor {i + 1}:")
print(f" Name: {tensor.name}")
print(f" Shape: {tensor.shape}")
print(f" Type: {tensor.tensor_type}")
print(f" Size: {tensor.n_elements} elements")
print()
tensors_shown += 1
if total_tensors > limit:
print(f"... and {total_tensors - limit} more tensors")
else:
print("No tensor data found.")
def show_configuration(reader):
"""Display model configuration"""
print("\n" + "=" * 50)
print("MODEL CONFIGURATION")
print("=" * 50)
config_fields = {}
# Look for configuration-related fields
if hasattr(reader, 'fields'):
for key, field in reader.fields.items():
key_lower = key.lower()
# Common config field patterns
if any(config_key in key_lower for config_key in [
'arch', 'dim', 'layer', 'head', 'context', 'vocab_size',
'hidden', 'intermediate', 'norm', 'rope', 'attention'
]):
if hasattr(field, 'parts'):
value = field.parts
elif hasattr(field, 'data'):
value = field.data
else:
value = str(field)
config_fields[key] = value
if config_fields:
for key, value in config_fields.items():
print(f"{key}: {value}")
else:
print("No obvious configuration fields found.")
print("All available fields:")
if hasattr(reader, 'fields'):
for key in reader.fields.keys():
print(f" - {key}")
def main():
# Get file path
if len(sys.argv) > 1:
file_path = sys.argv[1]
else:
#file_path = input("Enter the path to your .gguf file: ").strip()
file_path = "C:/AI/llama3_2/Llama-3.2-3B-Instruct-IQ3_M.gguf"
# Load the GGUF file
reader = load_gguf_file(file_path)
if not reader:
return
print(f"\nSuccessfully loaded: {file_path}")
while True:
print("\n" + "=" * 50)
print("GGUF FILE EXPLORER")
print("=" * 50)
print("1. Show Metadata")
print("2. Show Tokenizer Data")
print("3. Show 100 Model Weights")
print("4. Show Configuration")
print("5. Exit")
print("-" * 50)
choice = input("Select an option (1-5): ").strip()
if choice == '1':
show_metadata(reader)
elif choice == '2':
show_tokenizer_data(reader)
elif choice == '3':
show_model_weights(reader, 100)
elif choice == '4':
show_configuration(reader)
elif choice == '5':
print("Goodbye!")
break
else:
print("Invalid choice. Please select 1-5.")
input("\nPress Enter to continue...")
if __name__ == "__main__":
# Check if gguf is installed
try:
import gguf
except ImportError:
print("Error: gguf library not found.")
print("Please install it with: pip install gguf")
sys.exit(1)
main()