Python code for ollama get model size #29533
Closed
jsmith173
announced in
Ask Dosu (Archived)
Replies: 1 comment
-
To get the size of an Ollama model on disk in gigabytes, you can use the import os
def get_source_size(source_path: str) -> int:
"""Fetch size of source path. Source can be a directory or a file.
Args:
source_path (str): Local path of data source.
Returns:
int: Source size in bytes.
"""
if not source_path:
return 0
size = 0
if os.path.isfile(source_path):
size = os.path.getsize(source_path)
elif os.path.isdir(source_path):
total_size = 0
for dirpath, _, filenames in os.walk(source_path):
for f in filenames:
fp = os.path.join(dirpath, f)
if not os.path.islink(fp):
total_size += os.path.getsize(fp)
size = total_size
return size
def get_model_size_in_gb(model_name: str) -> float:
# Define the base directory for Ollama models
base_dir = os.path.expanduser("~/.ollama/models")
model_path = os.path.join(base_dir, model_name)
# Get the size in bytes
size_in_bytes = get_source_size(model_path)
# Convert bytes to gigabytes
size_in_gb = size_in_bytes / (1024 ** 3)
return size_in_gb
# Example usage
model_name = 'llama3.1'
size_in_gb = get_model_size_in_gb(model_name)
print(f"The size of the model '{model_name}' is {size_in_gb:.2f} GB") This script assumes that the models are stored in the To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Write me a Python code:
Input parameter: name of an Ollama model (for example 'llama3.1')
Result: the size of the model on the disk in gigabyte
Beta Was this translation helpful? Give feedback.
All reactions