|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +from safetensors import safe_open |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +def ensure_dependencies(): |
| 7 | + """Ensure that required dependencies are installed.""" |
| 8 | + try: |
| 9 | + import torch |
| 10 | + except ImportError: |
| 11 | + print("PyTorch is not installed. Installing now...") |
| 12 | + subprocess.check_call(["pip", "install", "torch"]) |
| 13 | + |
| 14 | +def get_safe_tensor_location(): |
| 15 | + """Prompt the user for the location of the safe tensor file.""" |
| 16 | + while True: |
| 17 | + location = input("Enter the path to your safe tensor file: ").strip() |
| 18 | + if os.path.isfile(location): |
| 19 | + return location |
| 20 | + else: |
| 21 | + print("Invalid file path. Please try again.") |
| 22 | + |
| 23 | +def display_tensor_keys(file_path): |
| 24 | + """Display the keys of tensors in the safe tensor file.""" |
| 25 | + try: |
| 26 | + with safe_open(file_path, framework="pt") as f: |
| 27 | + keys = f.keys() |
| 28 | + print("\nAvailable Tensor Keys:") |
| 29 | + for key in keys: |
| 30 | + print(f"- {key}") |
| 31 | + return keys |
| 32 | + except Exception as e: |
| 33 | + print(f"Error reading tensor keys: {e}") |
| 34 | + return [] |
| 35 | + |
| 36 | +def display_tensor_metadata(file_path, key): |
| 37 | + """Display metadata and a preview of the tensor for a specific key.""" |
| 38 | + try: |
| 39 | + with safe_open(file_path, framework="pt") as f: |
| 40 | + tensor = f.get_tensor(key) |
| 41 | + print(f"\nKey: {key}") |
| 42 | + print(f"Shape: {tensor.shape}") |
| 43 | + print(f"Dtype: {tensor.dtype}") |
| 44 | + print(f"Device: {tensor.device}") |
| 45 | + print("First 5 Values:", tensor.flatten()[:5]) |
| 46 | + except Exception as e: |
| 47 | + print(f"Error reading tensor metadata: {e}") |
| 48 | + |
| 49 | +def main(): |
| 50 | + """Main function to orchestrate the script.""" |
| 51 | + print("\n=== Safe Tensor Inspector ===") |
| 52 | + |
| 53 | + # Ensure dependencies are installed |
| 54 | + ensure_dependencies() |
| 55 | + |
| 56 | + # Step 1: Get the file location |
| 57 | + file_path = get_safe_tensor_location() |
| 58 | + |
| 59 | + # Step 2: Display tensor keys |
| 60 | + keys = display_tensor_keys(file_path) |
| 61 | + if not keys: |
| 62 | + print("No tensors found or unable to read the file.") |
| 63 | + return |
| 64 | + |
| 65 | + # Step 3: Interactive selection |
| 66 | + while True: |
| 67 | + print("\nMenu:") |
| 68 | + print("1. View tensor keys") |
| 69 | + print("2. Inspect a tensor") |
| 70 | + print("3. Exit") |
| 71 | + |
| 72 | + choice = input("Enter your choice: ").strip() |
| 73 | + |
| 74 | + if choice == "1": |
| 75 | + display_tensor_keys(file_path) |
| 76 | + elif choice == "2": |
| 77 | + key = input("Enter the tensor key to inspect: ").strip() |
| 78 | + if key in keys: |
| 79 | + display_tensor_metadata(file_path, key) |
| 80 | + else: |
| 81 | + print("Invalid key. Use option 1 to view available keys.") |
| 82 | + elif choice == "3": |
| 83 | + print("Exiting the inspector. Goodbye!") |
| 84 | + break |
| 85 | + else: |
| 86 | + print("Invalid choice. Please select a valid option.") |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + main() |
0 commit comments