Fix file handle leaks and redundant re-read in get_model_metadata (#7422)

This commit is contained in:
Alvin Tang 2026-03-18 09:06:05 +08:00 committed by GitHub
parent f0014ab01c
commit 73a094a657
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -34,7 +34,8 @@ def get_model_metadata(model):
path = model_path / 'config.json'
if path.exists():
hf_metadata = json.loads(open(path, 'r', encoding='utf-8').read())
with open(path, 'r', encoding='utf-8') as f:
hf_metadata = json.loads(f.read())
else:
hf_metadata = None
@ -93,7 +94,7 @@ def get_model_metadata(model):
else:
# Transformers metadata
if hf_metadata is not None:
metadata = json.loads(open(path, 'r', encoding='utf-8').read())
metadata = hf_metadata
if 'pretrained_config' in metadata:
metadata = metadata['pretrained_config']
@ -134,7 +135,8 @@ def get_model_metadata(model):
# 3. Fall back to tokenizer_config.json metadata
if path.exists():
metadata = json.loads(open(path, 'r', encoding='utf-8').read())
with open(path, 'r', encoding='utf-8') as f:
metadata = json.loads(f.read())
# Only read from metadata if we haven't already loaded from .jinja or .json
if template is None and 'chat_template' in metadata: