From a7aa8d2908296c3207849362a04b4e027481e335 Mon Sep 17 00:00:00 2001 From: Mishig Date: Mon, 8 Jan 2024 23:42:52 +0100 Subject: [PATCH] [docs] Use `hfoptions` for metadata (#424) * [docs] Use `hfoptions` for metadata * better wording --- docs/source/metadata_parsing.mdx | 46 +++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/docs/source/metadata_parsing.mdx b/docs/source/metadata_parsing.mdx index ed55796..eb7ac2a 100644 --- a/docs/source/metadata_parsing.mdx +++ b/docs/source/metadata_parsing.mdx @@ -20,8 +20,45 @@ There can be many potential use cases. For instance, we use it on the HuggingFac ## Usage -### JavaScript/TypeScript[[js]] + + +From [🤗 Hub](hf.co/models), you can get metadata of a model with [HTTP range requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests) instead of downloading the entire safetensors file with all the weights. In this example python script below (you can use any language that has HTTP requests support), we are parsing metadata of [gpt2](https://huggingface.co/gpt2/blob/main/model.safetensors). + +```python +import requests # pip install requests +import struct + +def parse_single_file(url): + # Fetch the first 8 bytes of the file + headers = {'Range': 'bytes=0-7'} + response = requests.get(url, headers=headers) + # Interpret the bytes as a little-endian unsigned 64-bit integer + length_of_header = struct.unpack(' + + Using [`huggingface.js`](https://huggingface.co/docs/huggingface.js) ```ts @@ -87,11 +124,10 @@ interface SafetensorsIndexJson { } export type SafetensorsShardedHeaders = Record; - ``` + -### Python - + [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub) provides a Python API to parse safetensors metadata. Use [`get_safetensors_metadata`](https://huggingface.co/docs/huggingface_hub/package_reference/hf_api#huggingface_hub.HfApi.get_safetensors_metadata) to get all safetensors metadata of a model. Depending on if the model is sharded or not, one or multiple safetensors files will be parsed. @@ -125,7 +161,9 @@ NotASafetensorsRepoError: 'runwayml/stable-diffusion-v1-5' is not a safetensors ``` To parse the metadata of a single safetensors file, use [`parse_safetensors_file_metadata`](https://huggingface.co/docs/huggingface_hub/package_reference/hf_api#huggingface_hub.HfApi.parse_safetensors_file_metadata). + + ## Example output -- 2.43.0