11# SPDX-License-Identifier: Apache-2.0
22
3+ import json
34from functools import cache
45from os import PathLike
56from pathlib import Path
@@ -51,6 +52,26 @@ def modelscope_list_repo_files(
5152 return files
5253
5354
55+ def _maybe_json_dict (path : Union [str , PathLike ]) -> dict [str , str ]:
56+ with open (path ) as f :
57+ try :
58+ return json .loads (f .read ())
59+ except Exception :
60+ return dict [str , str ]()
61+
62+
63+ def _maybe_space_split_dict (path : Union [str , PathLike ]) -> dict [str , str ]:
64+ parsed_dict = dict [str , str ]()
65+ with open (path ) as f :
66+ for line in f .readlines ():
67+ try :
68+ model_name , redirect_name = line .strip ().split ()
69+ parsed_dict [model_name ] = redirect_name
70+ except Exception :
71+ pass
72+ return parsed_dict
73+
74+
5475@cache
5576def maybe_model_redirect (model : str ) -> str :
5677 """
@@ -68,16 +89,10 @@ def maybe_model_redirect(model: str) -> str:
6889 if not Path (model_redirect_path ).exists ():
6990 return model
7091
71- with open (model_redirect_path ) as f :
72- for line in f .readlines ():
73- try :
74- model_name , redirect_name = line .split ("\t " )
75- if model == model_name :
76- redirect_name = redirect_name .strip ()
77- logger .info ("model redirect: [ %s ] -> [ %s ]" , model ,
78- redirect_name )
79- return redirect_name
80- except Exception :
81- pass
92+ redirect_dict = (_maybe_json_dict (model_redirect_path )
93+ or _maybe_space_split_dict (model_redirect_path ))
94+ if (redirect_model := redirect_dict .get (model )):
95+ logger .info ("model redirect: [ %s ] -> [ %s ]" , model , redirect_model )
96+ return redirect_model
8297
8398 return model
0 commit comments