1
1
# SPDX-License-Identifier: Apache-2.0
2
2
3
+ import json
3
4
from functools import cache
4
5
from os import PathLike
5
6
from pathlib import Path
@@ -51,6 +52,26 @@ def modelscope_list_repo_files(
51
52
return files
52
53
53
54
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
+
54
75
@cache
55
76
def maybe_model_redirect (model : str ) -> str :
56
77
"""
@@ -68,16 +89,10 @@ def maybe_model_redirect(model: str) -> str:
68
89
if not Path (model_redirect_path ).exists ():
69
90
return model
70
91
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
82
97
83
98
return model
0 commit comments