hysts commited on
Commit
525ed27
1 Parent(s): 7728909

Check if models exist

Browse files
Files changed (1) hide show
  1. app.py +22 -0
app.py CHANGED
@@ -41,6 +41,19 @@ examples = [
41
  api = HfApi()
42
 
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def save_space_info(dirname: str, filename: str, content: str) -> None:
45
  with open(f'{dirname}/{filename}', 'w') as f:
46
  f.write(content)
@@ -51,6 +64,15 @@ def run(space_name: str, model_names_str: str, hf_token: str, title: str,
51
  model_names = [name.strip() for name in model_names_str.split(',')]
52
  model_names_str = '\n'.join(model_names)
53
 
 
 
 
 
 
 
 
 
 
54
  try:
55
  space_url = api.create_repo(repo_id=space_name,
56
  repo_type='space',
 
41
  api = HfApi()
42
 
43
 
44
+ def check_if_model_exists(model_name: str) -> bool:
45
+ return any(info.modelId == model_name
46
+ for info in api.list_models(search=model_name))
47
+
48
+
49
+ def list_missing_models(model_names: list[str]) -> list[str]:
50
+ missing_models = []
51
+ for model_name in model_names:
52
+ if not check_if_model_exists(model_name):
53
+ missing_models.append(model_name)
54
+ return missing_models
55
+
56
+
57
  def save_space_info(dirname: str, filename: str, content: str) -> None:
58
  with open(f'{dirname}/{filename}', 'w') as f:
59
  f.write(content)
 
64
  model_names = [name.strip() for name in model_names_str.split(',')]
65
  model_names_str = '\n'.join(model_names)
66
 
67
+ if len(model_names) == 0:
68
+ return 'Model Name cannot be empty.'
69
+ missing_models = list_missing_models(model_names)
70
+ if len(missing_models) > 0:
71
+ message = 'The following model were not found: '
72
+ for model_name in missing_models:
73
+ message += f'\n{model_name}'
74
+ return message
75
+
76
  try:
77
  space_url = api.create_repo(repo_id=space_name,
78
  repo_type='space',