-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from duanemoody/main
Add context manager protocol support, rudimentary tags reader
- Loading branch information
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import json, sys | ||
from safetensors_file import SafeTensorsFile | ||
from safetensors_worker import _ParseMore | ||
""" | ||
This script extracts the JSON header's ss_tag_frequency from a safetensors file, then outputs it. | ||
TODO: gracefully error out on safetensors files without ["__metadata__"]["ss_tag_frequency"] | ||
""" | ||
|
||
def get_tags(tensorsfile: str) -> str: | ||
s = SafeTensorsFile.open_file(tensorsfile, quiet=True) # omit the first non-JSON line | ||
js = s.get_header() | ||
md = js["__metadata__"] | ||
_ParseMore(md) # pretty print the metadata | ||
stf = md["ss_tag_frequency"] | ||
return json.dumps(stf, ensure_ascii=False, separators=(', ', ': '), indent=4) | ||
|
||
tensorsfile = sys.argv[1] | ||
hdata = get_tags(tensorsfile) | ||
print(hdata) |