1
- import boto3 ,os ,base64 ,json
1
+ #
2
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
5
+ # with the License. A copy of the License is located at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
10
+ # OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
11
+ # and limitations under the License.
12
+ #
2
13
import requests as reqs
3
- from aws_lambda_powertools import Logger , Tracer , Metrics
4
- from requests_aws4auth import AWS4Auth
14
+ import boto3 ,os ,base64 ,json
5
15
from datetime import datetime
16
+ from requests_aws4auth import AWS4Auth
17
+ from aws_lambda_powertools import Logger , Tracer , Metrics
6
18
7
- logger = Logger (service = "IMAGE_GENERATION " )
8
- tracer = Tracer (service = "IMAGE_GENERATION " )
9
- metrics = Metrics (namespace = "image_generation " , service = "IMAGE_GENERATION " )
19
+ logger = Logger (service = "CONTENT_GENERATION " )
20
+ tracer = Tracer (service = "CONTENT_GENERATION " )
21
+ metrics = Metrics (namespace = "content_generation " , service = "CONTENT_GENERATION " )
10
22
11
23
12
24
s3 = boto3 .resource ('s3' )
@@ -116,7 +128,7 @@ def generate_image(self,input_params):
116
128
117
129
118
130
input_text = self .input_text
119
- print (f' input_params :: { input_params } ' )
131
+ logger . info (f' input_params :: { input_params } ' )
120
132
# add default negative prompts
121
133
if 'negative_prompts' in input_params and input_params ['negative_prompts' ] is None :
122
134
sample_string_bytes = base64 .b64decode (input_params ['negative_prompts' ])
@@ -133,7 +145,6 @@ def generate_image(self,input_params):
133
145
134
146
135
147
body = get_model_payload (model_id ,params ,input_text ,negative_prompts )
136
- print (f' body :: { body } ' )
137
148
try :
138
149
return self .bedrock_client .invoke_model (
139
150
modelId = model_id ,
@@ -228,56 +239,17 @@ def get_model_payload(modelid,params,input_text,negative_prompts):
228
239
229
240
def get_inference_parameters (model_kwargs ):
230
241
""" Read inference parameters and set default values"""
231
- if 'seed' in model_kwargs :
232
- seed = model_kwargs ['seed' ]
233
- else :
234
- seed = 452345
235
- if 'cfg_scale' in model_kwargs :
236
- cfg_scale = model_kwargs ['cfg_scale' ]
237
- else :
238
- cfg_scale = 10
239
- if 'steps' in model_kwargs :
240
- steps = model_kwargs ['steps' ]
241
- else :
242
- steps = 10
243
- if 'style_preset' in model_kwargs :
244
- style_preset = model_kwargs ['style_preset' ]
245
- else :
246
- style_preset = 'photographic'
247
- if 'clip_guidance_preset' in model_kwargs :
248
- clip_guidance_preset = model_kwargs ['clip_guidance_preset' ]
249
- else :
250
- clip_guidance_preset = 'FAST_GREEN'
251
- if 'width' in model_kwargs :
252
- width = model_kwargs ['width' ]
253
- else :
254
- width = 512
255
- if 'height' in model_kwargs :
256
- height = model_kwargs ['height' ]
257
- else :
258
- height = 512
259
- if 'sampler' in model_kwargs :
260
- sampler = model_kwargs ['sampler' ]
261
- else :
262
- sampler = 'K_DPMPP_2S_ANCESTRAL'
263
- if 'numberOfImages' in model_kwargs :
264
- numberOfImages = model_kwargs ['numberOfImages' ]
265
- else :
266
- numberOfImages = 1
267
- if 'quality' in model_kwargs :
268
- quality = model_kwargs ['quality' ]
269
- else :
270
- quality = "standard"
271
242
272
243
return {
273
- "cfg_scale" : cfg_scale ,
274
- "seed" : seed ,
275
- "steps" : steps ,
276
- "style_preset" : style_preset ,
277
- "clip_guidance_preset" : clip_guidance_preset ,
278
- "sampler" : sampler ,
279
- "width" : width ,
280
- "height" : height ,
281
- "numberOfImages" : numberOfImages ,
282
- "quality" : quality
244
+ "cfg_scale" : model_kwargs .get ('cfg_scale' ,10 ),
245
+ "seed" : model_kwargs .get ('seed' ,452345 ),
246
+ "steps" : model_kwargs .get ('steps' ,10 ),
247
+ "style_preset" : model_kwargs .get ('style_preset' ,'photographic' ),
248
+ "clip_guidance_preset" : model_kwargs .get ('clip_guidance_preset' ,'FAST_GREEN' ),
249
+ "sampler" : model_kwargs .get ('sampler' ,'K_DPMPP_2S_ANCESTRAL' ),
250
+ "width" : model_kwargs .get ('width' ,512 ),
251
+ "height" : model_kwargs .get ('height' ,512 ),
252
+ "numberOfImages" : model_kwargs .get ('numberOfImages' ,1 ),
253
+ "quality" : model_kwargs .get ('quality' ,'standard' ),
254
+
283
255
}
0 commit comments