Setting the file. One moment.
Get Deployable Models · AWS AI ML · aws/agent-toolkit-for-aws · Skills Docs
Issue No. 14 · AWS AI ML
↖ Back to the coverMessaging And Streaming Skills
Migration And Modernization Skills
Networking And Content Delivery Skills
Security And Identity Skills
Web And Mobile Development
120 chapters · 648 min
ContentsBack to the top of the page 10
Setup DevOps Agent
24.7
Code Output Guide · references
RDS Oracle
references/model-selection/scripts/get_deployable_models.py
references/model-selection/scripts/ get_deployable_models.py
Python · 197 lines · 7 KB
17 import json
18 import sys
19
20 import boto3
21 import botocore.exceptions
22
23
24 def get_deployable_models (hub_name, region_name = None ):
25 """Query a SageMaker Hub and return structured model metadata.
26
27 Args:
28 hub_name: Name of the SageMaker Hub.
29 region_name: Optional AWS region override.
30
31 Returns:
32 List of model dicts with extracted metadata fields.
33 """
34 sm_client = boto3.client( "sagemaker" , region_name = region_name)
35
36 # Retrieve all models with pagination
37 all_contents = []
38 next_token = None
39
40 while True :
41 params = {
42 "HubName" : hub_name,
43 "HubContentType" : "Model" ,
44 "MaxResults" : 100 ,
45 }
46
47 if next_token:
48 params[ "NextToken" ] = next_token
49
50 response = sm_client.list_hub_contents( ** params)
51 all_contents.extend(response.get( "HubContentSummaries" , []))
52
53 next_token = response.get( "NextToken" )
54 if not next_token:
55 break
56
57 # All hub models are deployable to SageMaker endpoints.
58 # Extract structured metadata from search keywords for filtering.
59 results = []
60 for model in all_contents:
61 keywords = model.get( "HubContentSearchKeywords" , [])
62 entry = {
63 "name" : model.get( "HubContentName" ),
64 }
65
66 for kw in keywords:
67 if kw.startswith( "@data-type:" ):
68 entry.setdefault( "data_types" , []).append(kw.split( ":" , 1 )[ 1 ])
69 elif kw.startswith( "@input-modality:" ):
70 entry.setdefault( "input_modalities" , []).append(kw.split( ":" , 1 )[ 1 ])
71 elif kw.startswith( "@output-modality:" ):
72 entry.setdefault( "output_modalities" , []).append(kw.split( ":" , 1 )[ 1 ])
73 elif kw.startswith( "@model-size:" ):
74 entry[ "size" ] = kw.split( ":" , 1 )[ 1 ]
75 elif kw.startswith( "@license:" ):
76 entry[ "license" ] = kw.split( ":" , 1 )[ 1 ]
77 elif kw.startswith( "@language:" ):
78 entry.setdefault( "languages" , []).append(kw.split( ":" , 1 )[ 1 ])
79 elif kw.startswith( "@context-window:" ):
80 entry[ "context_window" ] = kw.split( ":" , 1 )[ 1 ]
81 elif kw.startswith( "@model-type:" ):
82 entry[ "model_type" ] = kw.split( ":" , 1 )[ 1 ]
83 elif kw.startswith( "@framework:" ):
84 # Framework (e.g., "pytorch", "tensorflow") stored separately from provider.
85 # The filter script matches against 'provider' field only; framework is
86 # preserved for informational purposes.
87 entry[ "framework" ] = kw.split( ":" , 1 )[ 1 ]
88 elif kw.startswith( "@provider:" ):
89 entry[ "provider" ] = kw.split( ":" , 1 )[ 1 ]
90 elif kw.startswith( "@task:" ):
91 entry.setdefault( "tasks" , []).append(kw.split( ":" , 1 )[ 1 ])
92
93 # Bedrock eligibility
94 entry[ "bedrock_eligible" ] = "@capability:bedrock_console" in keywords
95
96 # Original creation time for sorting by recency
97 original_creation_time = model.get( "OriginalCreationTime" )
98 if original_creation_time:
99 entry[ "original_creation_time" ] = (
100 original_creation_time.isoformat()
101 if hasattr (original_creation_time, "isoformat" )
102 else str (original_creation_time)
103 )
104
105 results.append(entry)
106
107 return results
108
109
110 def list_available_values (models):
111 """Extract all unique values for each metadata field across all models.
112
113 Returns a dict of field_name -> sorted list of unique values.
114 """
115 values: dict[ str , set[ str ]] = {
116 "tasks" : set (),
117 "data_types" : set (),
118 "input_modalities" : set (),
119 "output_modalities" : set (),
120 "sizes" : set (),
121 "licenses" : set (),
122 "languages" : set (),
123 "context_windows" : set (),
124 "model_types" : set (),
125 "providers" : set (),
126 "frameworks" : set (),
127 }
128
129 for model in models:
130 for task in model.get( "tasks" , []):
131 values[ "tasks" ].add(task)
132 for dt in model.get( "data_types" , []):
133 values[ "data_types" ].add(dt)
134 for im in model.get( "input_modalities" , []):
135 values[ "input_modalities" ].add(im)
136 for om in model.get( "output_modalities" , []):
137 values[ "output_modalities" ].add(om)
138 if "size" in model:
139 values[ "sizes" ].add(model[ "size" ])
140 if "license" in model:
141 values[ "licenses" ].add(model[ "license" ])
142 for lang in model.get( "languages" , []):
143 values[ "languages" ].add(lang)
144 if "context_window" in model:
145 values[ "context_windows" ].add(model[ "context_window" ])
146 if "model_type" in model:
147 values[ "model_types" ].add(model[ "model_type" ])
148 if "provider" in model:
149 values[ "providers" ].add(model[ "provider" ])
150 if "framework" in model:
151 values[ "frameworks" ].add(model[ "framework" ])
152
153 return {k: sorted (v) for k, v in values.items()}
154
155
156 if __name__ == "__main__" :
157 if len (sys.argv) < 2 :
158 print ( "Usage: python get_deployable_models.py <hub-name> [--list-values] [region]" )
159 sys.exit( 1 )
160
161 args = sys.argv[ 1 :]
162 list_values_mode = False
163 if "--list-values" in args:
164 list_values_mode = True
165 args.remove( "--list-values" )
166
167 if not args:
168 print ( "Usage: python get_deployable_models.py <hub-name> [--list-values] [region]" )
169 sys.exit( 1 )
170
171 hub_name = args[ 0 ]
172 region_name = args[ 1 ] if len (args) > 1 else None
173
174 try :
175 results = get_deployable_models(hub_name, region_name)
176 except botocore.exceptions.ClientError as e:
177 error_code = e.response[ "Error" ][ "Code" ]
178 error_msg = e.response[ "Error" ][ "Message" ]
179 print ( f "Error: AWS API call failed ( { error_code } ): { error_msg } " , file = sys.stderr)
180 sys.exit( 1 )
181 except botocore.exceptions.NoCredentialsError:
182 print (
183 "Error: No AWS credentials found. Configure credentials via 'aws configure' or environment variables." ,
184 file = sys.stderr,
185 )
186 sys.exit( 1 )
187 except botocore.exceptions.EndpointConnectionError as e:
188 print ( f "Error: Could not connect to SageMaker endpoint: { e } " , file = sys.stderr)
189 sys.exit( 1 )
190 except Exception as e:
191 print ( f "Error: { e } " , file = sys.stderr)
192 sys.exit( 1 )
193
194 if list_values_mode:
195 print (json.dumps(list_available_values(results)))
196 else :
197 print (json.dumps(results))