1- # Copyright 2023 Amazon.com and its affiliates; all rights reserved.
1+ # Copyright 2023 Amazon.com and its affiliates; all rights reserved.
22# This file is Amazon Web Services Content and may not be duplicated or distributed without permission.
33AWSTemplateFormatVersion : " 2010-09-09"
44Description : SageMaker Foundational Models Workshop
55
6-
76Parameters :
87 UserProfileName :
98 Type : String
109 Description : The user profile name for the SageMaker workshop
11- Default : ' workshop-builder'
10+ Default : " workshop-builder"
1211 DomainName :
1312 Type : String
14- Description : The domain name of the Sagemaker studio instance
15- Default : ' d-aigensagemaker'
16- DockerImage :
17- Type : String
18- Description : TODO you must build the image, in the repo and push to your ECR -https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-ecr-image.html
19- Default : ' TODO'
13+ Description : The domain name of the SageMaker studio instance
14+ Default : " d-aigensagemaker"
2015
2116Mappings :
2217 RegionMap :
@@ -78,7 +73,7 @@ Mappings:
7873 datascience : " arn:aws:sagemaker:sa-east-1:782484402741:image/datascience-1.0"
7974 datawrangler : " arn:aws:sagemaker:sa-east-1:424196993095:image/sagemaker-data-wrangler-1.0"
8075
81- Resources :
76+ Resources :
8277 LambdaExecutionRole :
8378 Type : " AWS::IAM::Role"
8479 Properties :
@@ -93,11 +88,11 @@ Resources:
9388 - " sts:AssumeRole"
9489 Path : /
9590 ManagedPolicyArns :
96- - ' arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess'
97- - ' arn:aws:iam::aws:policy/AmazonS3FullAccess'
98- - ' arn:aws:iam::aws:policy/IAMFullAccess'
99- - ' arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
100- - ' arn:aws:iam::aws:policy/AmazonSageMakerFullAccess'
91+ - " arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess"
92+ - " arn:aws:iam::aws:policy/AmazonS3FullAccess"
93+ - " arn:aws:iam::aws:policy/IAMFullAccess"
94+ - " arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
95+ - " arn:aws:iam::aws:policy/AmazonSageMakerFullAccess"
10196
10297 SageMakerExecutionRole :
10398 Type : AWS::IAM::Role
@@ -107,8 +102,7 @@ Resources:
107102 PolicyDocument :
108103 Version : 2012-10-17
109104 Statement :
110- -
111- Effect : Allow
105+ - Effect : Allow
112106 Action :
113107 - s3:GetObject
114108 - s3:PutObject
@@ -119,42 +113,40 @@ Resources:
119113 PolicyDocument :
120114 Version : 2012-10-17
121115 Statement :
122- -
123- Effect : Allow
116+ - Effect : Allow
124117 Action :
125118 - iam:GetRole
126119 - iam:GetRolePolicy
127- Resource : ' * '
120+ Resource : " * "
128121 AssumeRolePolicyDocument :
129122 Version : 2012-10-17
130123 Statement :
131- -
132- Effect : Allow
124+ - Effect : Allow
133125 Principal :
134126 Service :
135127 - sagemaker.amazonaws.com
136128 Action :
137129 - sts:AssumeRole
138130 ManagedPolicyArns :
139- - ' arn:aws:iam::aws:policy/AmazonSageMakerFullAccess'
140- - ' arn:aws:iam::aws:policy/AmazonS3FullAccess'
141- - ' arn:aws:iam::aws:policy/AWSCloudFormationFullAccess'
142- - ' arn:aws:iam::aws:policy/AWSCodePipeline_FullAccess'
131+ - " arn:aws:iam::aws:policy/AmazonSageMakerFullAccess"
132+ - " arn:aws:iam::aws:policy/AmazonS3FullAccess"
133+ - " arn:aws:iam::aws:policy/AWSCloudFormationFullAccess"
134+ - " arn:aws:iam::aws:policy/AWSCodePipeline_FullAccess"
143135
144136 DomainDeploymentDelay :
145- Type : ' Custom::Delay'
137+ Type : " Custom::Delay"
146138 DependsOn : SageMakerExecutionRole
147139 Properties :
148140 ServiceToken : !GetAtt DelayLambda.Arn
149141 TimeToWait : 300
150142
151143 DelayLambda :
152- Type : ' AWS::Lambda::Function'
144+ Type : " AWS::Lambda::Function"
153145 Properties :
154146 Handler : " index.handler"
155147 Timeout : 660
156148 Role : !GetAtt LambdaExecutionRole.Arn
157- Runtime : python3.7
149+ Runtime : python3.12
158150 Code :
159151 ZipFile : |
160152 import json
@@ -211,14 +203,13 @@ Resources:
211203 Handler : index.lambda_handler
212204 MemorySize : 512
213205 Role : !GetAtt LambdaExecutionRole.Arn
214- Runtime : python3.7
206+ Runtime : python3.12
215207 Timeout : 5
216208 DefaultVpcFinder :
217209 Type : Custom::ResourceForFindingDefaultVpc
218210 Properties :
219211 ServiceToken : !GetAtt DefaultVpcLambda.Arn
220212
221-
222213 FmCreateImage :
223214 Type : AWS::Lambda::Function
224215 Properties :
@@ -228,29 +219,19 @@ Resources:
228219 import json
229220 import boto3
230221
231- def parse_response(query_response):
232- """Parse response and return generated image and the prompt"""
233-
234- response_dict = json.loads(query_response)
235- return response_dict["generated_image"], response_dict["prompt"]
236-
237-
238222 def lambda_handler(event, context):
239223
240224 client = boto3.client('sagemaker-runtime')
241225
242226 data_string = event["body"]
243227 text = json.loads(data_string)["text"]
244-
245- encoded_text = text.encode("utf-8")
246-
247-
228+
248229 CONTENT_TYPE = 'application/x-text'
249- endpoint_name = "YOUR_MODEL_ENDPOINT " # Replace with your model endpoint
250-
251- response = client.invoke_endpoint(EndpointName=endpoint_name,
252- ContentType=CONTENT_TYPE,
253- Body=encoded_text)
230+ endpoint_name = "REPLACE_WITH_MODEL_ENDPOINT " # Replace with your model endpoint
231+
232+ encoded_text = text.encode("utf-8")
233+ response = client.invoke_endpoint(EndpointName=endpoint_name, InferenceComponentName='model-txt2img-stabilityai-stable-diffusion-v2-1-20250203-192659', ContentType='application/x-text', Body=encoded_text, Accept='application/json')
234+
254235
255236 print(response)
256237
@@ -267,7 +248,7 @@ Resources:
267248 Handler : index.lambda_handler
268249 MemorySize : 512
269250 Role : !GetAtt LambdaExecutionRole.Arn
270- Runtime : python3.9
251+ Runtime : python3.12
271252 Timeout : 200
272253
273254 FmCreateSummary :
@@ -286,11 +267,11 @@ Resources:
286267 data_string = event["body"]
287268 text = json.loads(data_string)["text"]
288269
289- query = 'write a summary'
290-
291- prompt = f'{text }\n{query} '
270+ query = '<|im_start|>system\nyou are an expert summary writter<|im_end|>\n<|im_start|>user\nwrite a summary of the following text: '
271+
272+ prompt = f'{query }\n{text}<|im_end|>\n<|im_start|>assistant: '
292273
293- MAX_LENGTH = 150 # 256
274+ MAX_LENGTH = 1000
294275 NUM_RETURN_SEQUENCES = 1
295276 TOP_K = 0
296277 TOP_P = 0.7
@@ -308,39 +289,39 @@ Resources:
308289
309290 payload = json.dumps(payload).encode('utf-8')
310291
311- endpoint_name = "YOUR_MODEL_ENDPOINT " # Replace with your model endpoint
292+ endpoint_name = "REPLACE_WITH_MODEL_ENDPOINT " # Replace with your model endpoint
312293
313294 response = client.invoke_endpoint(EndpointName=endpoint_name,
314295 ContentType=CONTENT_TYPE,
315296 Body=payload)
316297 model_predictions = json.loads(response['Body'].read())
317298 generated_text = model_predictions[0]["generated_text"]
318299 print(f'Response: {generated_text}')
300+ final_response = generated_text.split("assistant:")[1].strip()
319301
320- resp = json.dumps({"text":generated_text })
302+ resp = json.dumps({"text":final_response })
321303
322304
323305 return {
324306 'statusCode': 200,
325307 'body': resp
326308 }
309+
327310 Description : Call model to generate summary
328311 Handler : index.lambda_handler
329312 MemorySize : 512
330313 Role : !GetAtt LambdaExecutionRole.Arn
331- Runtime : python3.9
314+ Runtime : python3.12
332315 Timeout : 200
333316
334-
335-
336317 StudioDomain :
337318 Type : AWS::SageMaker::Domain
338319 DependsOn : DomainDeploymentDelay
339320 Properties :
340321 AppNetworkAccessType : PublicInternetOnly
341322 AuthMode : IAM
342323 DefaultUserSettings :
343- ExecutionRole : !GetAtt SageMakerExecutionRole.Arn
324+ ExecutionRole : !GetAtt SageMakerExecutionRole.Arn
344325 DomainName : !Ref DomainName
345326 SubnetIds : !GetAtt DefaultVpcFinder.Subnets
346327 VpcId : !GetAtt DefaultVpcFinder.VpcId
@@ -362,21 +343,6 @@ Resources:
362343 DomainId : !GetAtt StudioDomain.DomainId
363344 UserProfileName : !Ref UserProfileName
364345
365- DataScienceApp :
366- Type : AWS::SageMaker::App
367- DependsOn : UserProfile
368- Properties :
369- AppName : instance-event-engine-datascience-ml-t3-medium
370- AppType : KernelGateway
371- DomainId : !GetAtt StudioDomain.DomainId
372- ResourceSpec :
373- InstanceType : ml.t3.medium
374- SageMakerImageArn : !FindInMap
375- - RegionMap
376- - !Ref ' AWS::Region'
377- - datascience
378- UserProfileName : !Ref UserProfileName
379-
380346 ContainerCluster :
381347 Type : AWS::ECS::Cluster
382348 Properties :
@@ -396,11 +362,11 @@ Resources:
396362 - Effect : Allow
397363 Principal :
398364 Service : ecs-tasks.amazonaws.com
399- Action : ' sts:AssumeRole'
365+ Action : " sts:AssumeRole"
400366 ManagedPolicyArns :
401- - ' arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy'
402- - ' arn:aws:iam::aws:policy/AmazonElasticContainerRegistryPublicReadOnly'
403- - ' arn:aws:iam::aws:policy/AWSLambda_FullAccess'
367+ - " arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
368+ - " arn:aws:iam::aws:policy/AmazonElasticContainerRegistryPublicReadOnly"
369+ - " arn:aws:iam::aws:policy/AWSLambda_FullAccess"
404370
405371 TaskRole :
406372 Type : AWS::IAM::Role
@@ -411,12 +377,11 @@ Resources:
411377 - Effect : Allow
412378 Principal :
413379 Service : ecs-tasks.amazonaws.com
414- Action : ' sts:AssumeRole'
380+ Action : " sts:AssumeRole"
415381 ManagedPolicyArns :
416- - ' arn:aws:iam::aws:policy/AWSGlueSchemaRegistryFullAccess'
417- - ' arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy'
418- - ' arn:aws:iam::aws:policy/AWSLambda_FullAccess'
419-
382+ - " arn:aws:iam::aws:policy/AWSGlueSchemaRegistryFullAccess"
383+ - " arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
384+ - " arn:aws:iam::aws:policy/AWSLambda_FullAccess"
420385
421386 BuildOnTaskDefinition :
422387 Type : AWS::ECS::TaskDefinition
@@ -431,12 +396,12 @@ Resources:
431396 ExecutionRoleArn : !Ref ExecutionRole
432397 ContainerDefinitions :
433398 - Name : build-on-poster
434- Image : !Ref DockerImage
399+ Image : public.ecr.aws/o3s1z9h7/build-on-poster:latest
435400 Environment :
436401 - Name : IMAGE_API
437- Value : ' '
402+ Value : " "
438403 - Name : TEXT_API
439- Value : ' '
404+ Value : " "
440405
441406 LogConfiguration :
442407 LogDriver : awslogs
0 commit comments