vibs08 commited on
Commit
f7b6dac
·
verified ·
1 Parent(s): 86331df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -36
app.py CHANGED
@@ -95,44 +95,21 @@ def gen_pos_prompt(text):
95
  pos_prompt = json.loads(response.get('body').read())['results'][0]['outputText']
96
  return pos_prompt
97
 
98
- # def encode_image_to_base64(image_path):
99
- # with open(image_path, "rb") as image_file:
100
- # encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
101
- # return encoded_string
102
-
103
  def encode_image_to_base64(image):
104
- buffered = BytesIO()
105
- image.save(buffered, format="PNG") # Save the image to the buffer
106
- encoded_string = base64.b64encode(buffered.getvalue()).decode('utf-8')
107
- return encoded_string
108
 
109
 
110
  def generate_image_from_text(encoded_image,pos_prompt=None):
111
  neg_prompt = '''Detailed, complex textures, intricate patterns, realistic lighting, high contrast, reflections, fuzzy surface, realistic proportions, photographic quality, vibrant colors, detailed background, shadows, disfigured, deformed, ugly, multiple, duplicate.'''
112
  encoded_str = encode_image_to_base64(encoded_image)
113
- if pos_prompt:
114
- new_prompt = gen_pos_prompt(pos_prompt)
115
- parameters = {
116
- 'taskType': 'IMAGE_VARIATION',
117
- 'imageVariationParams': {
118
- 'images': [encoded_str],
119
- 'text': new_prompt,
120
- 'negativeText': neg_prompt,
121
- 'similarityStrength': 0.7
122
- },
123
- 'imageGenerationConfig': {
124
- "cfgScale": 8,
125
- "seed": 0,
126
- "width": 512,
127
- "height": 512,
128
- "numberOfImages": 1
129
- }
130
- }
131
- else:
132
- parameters = {
133
  'taskType': 'IMAGE_VARIATION',
134
  'imageVariationParams': {
135
  'images': [encoded_str],
 
136
  'negativeText': neg_prompt,
137
  'similarityStrength': 0.7
138
  },
@@ -143,8 +120,7 @@ def generate_image_from_text(encoded_image,pos_prompt=None):
143
  "height": 512,
144
  "numberOfImages": 1
145
  }
146
- }
147
-
148
  request_body = json.dumps(parameters)
149
  response = bedrock_runtime.invoke_model(body=request_body, modelId='amazon.titan-image-generator-v1')
150
  response_body = json.loads(response.get('body').read())
@@ -189,14 +165,12 @@ def generate(image, mc_resolution, formats=["obj", "glb"]):
189
  return mesh_path_obj.name, mesh_path_glb.name
190
 
191
  def run_example(image, do_remove_background, foreground_ratio, mc_resolution, text_prompt=None):
192
- if text_prompt:
193
- image_pil = generate_image_from_text(encoded_image=image, pos_prompt=text_prompt)
194
- else:
195
- image_pil = generate_image_from_text(encoded_image=image)
196
  preprocessed = preprocess(image_pil, False, 0.9)
197
  mesh_name_obj, mesh_name_glb = generate(preprocessed, 256, ["obj", "glb"])
198
  return preprocessed, mesh_name_obj, mesh_name_glb
199
 
 
200
  with gr.Blocks() as demo:
201
  gr.Markdown(HEADER)
202
  with gr.Row(variant="panel"):
@@ -265,7 +239,7 @@ with gr.Blocks() as demo:
265
  submit.click(fn=check_input_image, inputs=[input_image]).success(
266
  fn=run_example,
267
  inputs=[input_image, do_remove_background, foreground_ratio, mc_resolution, text_prompt],
268
- outputs=[processed_image, output_model_obj, output_model_glb],
269
  # outputs=[output_model_obj, output_model_glb],
270
  )
271
 
 
95
  pos_prompt = json.loads(response.get('body').read())['results'][0]['outputText']
96
  return pos_prompt
97
 
 
 
 
 
 
98
  def encode_image_to_base64(image):
99
+ with io.BytesIO() as buffered:
100
+ image.save(buffered, format="PNG")
101
+ return base64.b64encode(buffered.getvalue()).decode('utf-8')
 
102
 
103
 
104
  def generate_image_from_text(encoded_image,pos_prompt=None):
105
  neg_prompt = '''Detailed, complex textures, intricate patterns, realistic lighting, high contrast, reflections, fuzzy surface, realistic proportions, photographic quality, vibrant colors, detailed background, shadows, disfigured, deformed, ugly, multiple, duplicate.'''
106
  encoded_str = encode_image_to_base64(encoded_image)
107
+ new_prompt = gen_pos_prompt(pos_prompt)
108
+ parameters = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  'taskType': 'IMAGE_VARIATION',
110
  'imageVariationParams': {
111
  'images': [encoded_str],
112
+ 'text': gen_pos_prompt(pos_prompt) if pos_prompt else None,
113
  'negativeText': neg_prompt,
114
  'similarityStrength': 0.7
115
  },
 
120
  "height": 512,
121
  "numberOfImages": 1
122
  }
123
+ }
 
124
  request_body = json.dumps(parameters)
125
  response = bedrock_runtime.invoke_model(body=request_body, modelId='amazon.titan-image-generator-v1')
126
  response_body = json.loads(response.get('body').read())
 
165
  return mesh_path_obj.name, mesh_path_glb.name
166
 
167
  def run_example(image, do_remove_background, foreground_ratio, mc_resolution, text_prompt=None):
168
+ image_pil = generate_image_from_text(encoded_image=image, pos_prompt=text_prompt)
 
 
 
169
  preprocessed = preprocess(image_pil, False, 0.9)
170
  mesh_name_obj, mesh_name_glb = generate(preprocessed, 256, ["obj", "glb"])
171
  return preprocessed, mesh_name_obj, mesh_name_glb
172
 
173
+
174
  with gr.Blocks() as demo:
175
  gr.Markdown(HEADER)
176
  with gr.Row(variant="panel"):
 
239
  submit.click(fn=check_input_image, inputs=[input_image]).success(
240
  fn=run_example,
241
  inputs=[input_image, do_remove_background, foreground_ratio, mc_resolution, text_prompt],
242
+ outputs=[output_model_obj, output_model_glb],
243
  # outputs=[output_model_obj, output_model_glb],
244
  )
245