vibs08 commited on
Commit
c54df6c
·
verified ·
1 Parent(s): a67e3bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -23
app.py CHANGED
@@ -39,10 +39,10 @@ model.renderer.set_chunk_size(131072)
39
  model.to(device)
40
 
41
  rembg_session = rembg.new_session()
42
- # my_aws_access_key_id = os.getenv("ACCESS")
43
- # my_aws_secret_access_key = os.getenv("SECRET")
44
- # bedrock_runtime = boto3.client(service_name='bedrock-runtime', aws_access_key_id = my_aws_access_key_id, aws_secret_access_key = my_aws_secret_access_key, region_name='us-east-1')
45
-
46
  # def generate_image_from_text(pos_prompt):
47
  # # bedrock_runtime = boto3.client(region_name = 'us-east-1', service_name='bedrock-runtime')
48
  # parameters = {'text_prompts': [{'text': pos_prompt , 'weight':1},
@@ -55,11 +55,99 @@ rembg_session = rembg.new_session()
55
 
56
  # return Image.open(io.BytesIO(base64_image_data))
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  def check_input_image(input_image):
59
  if input_image is None:
60
  raise gr.Error("No image uploaded!")
61
 
62
-
63
  def preprocess(input_image, do_remove_background, foreground_ratio):
64
  def fill_background(image):
65
  image = np.array(image).astype(np.float32) / 255.0
@@ -78,8 +166,7 @@ def preprocess(input_image, do_remove_background, foreground_ratio):
78
  image = fill_background(image)
79
  return image
80
 
81
-
82
- @spaces.GPU
83
  def generate(image, mc_resolution, formats=["obj", "glb"]):
84
  scene_codes = model(image, device=device)
85
  mesh = model.extract_mesh(scene_codes, resolution=mc_resolution)[0]
@@ -94,9 +181,10 @@ def generate(image, mc_resolution, formats=["obj", "glb"]):
94
 
95
  return mesh_path_obj.name, mesh_path_glb.name
96
 
97
- def run_example(image_pil):
98
- preprocessed = preprocess(image_pil, False, 0.9)
99
- mesh_name_obj, mesh_name_glb = generate(preprocessed, 256, ["obj", "glb"])
 
100
  return preprocessed, mesh_name_obj, mesh_name_glb
101
 
102
  with gr.Blocks() as demo:
@@ -104,14 +192,20 @@ with gr.Blocks() as demo:
104
  with gr.Row(variant="panel"):
105
  with gr.Column():
106
  with gr.Row():
 
107
  input_image = gr.Image(
108
- label="Input Image",
109
  image_mode="RGBA",
110
  sources="upload",
111
  type="pil",
112
  elem_id="content_image",
 
 
 
 
 
113
  )
114
- processed_image = gr.Image(label="Processed Image", interactive=False)
115
  with gr.Row():
116
  with gr.Group():
117
  do_remove_background = gr.Checkbox(
@@ -151,22 +245,19 @@ with gr.Blocks() as demo:
151
  examples=[
152
  os.path.join("examples", img_name) for img_name in sorted(os.listdir("examples"))
153
  ],
154
- inputs=[input_image],
155
  outputs=[processed_image, output_model_obj, output_model_glb],
156
  cache_examples=True,
157
- fn=partial(run_example),
158
  label="Examples",
159
  examples_per_page=20
160
  )
161
- submit.click(fn=check_input_image, inputs=[input_image]).success(
162
- fn=preprocess,
163
- inputs=[input_image, do_remove_background, foreground_ratio],
164
- outputs=[processed_image],
165
- ).success(
166
- fn=generate,
167
- inputs=[processed_image, mc_resolution],
168
- outputs=[output_model_obj, output_model_glb],
169
  )
170
 
171
  demo.queue(max_size=10)
172
- demo.launch()
 
39
  model.to(device)
40
 
41
  rembg_session = rembg.new_session()
42
+ ACCESS = os.getenv("ACCESS")
43
+ SECRET = os.getenv("SECRET")
44
+ bedrock = boto3.client(service_name='bedrock', aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
45
+ bedrock_runtime = boto3.client(service_name='bedrock-runtime', aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
46
  # def generate_image_from_text(pos_prompt):
47
  # # bedrock_runtime = boto3.client(region_name = 'us-east-1', service_name='bedrock-runtime')
48
  # parameters = {'text_prompts': [{'text': pos_prompt , 'weight':1},
 
55
 
56
  # return Image.open(io.BytesIO(base64_image_data))
57
 
58
+
59
+ def gen_pos_prompt(text):
60
+ instruction = f'''Your task is to create a positive prompt for image generation.
61
+ Objective: Generate images that prioritize structural integrity and accurate shapes. The focus should be on the correct form and basic contours of objects, with minimal concern for colors.
62
+
63
+ Guidelines:
64
+
65
+ Complex Objects (e.g., animals, vehicles): For these, the image should resemble a toy object, emphasizing the correct shape and structure while minimizing details and color complexity.
66
+
67
+ Example Input: A sports bike
68
+ Example Positive Prompt: Simple sports bike with accurate shape and structure, minimal details, digital painting, concept art style, basic contours, soft lighting, clean lines, neutral or muted colors, toy-like appearance, low contrast.
69
+
70
+ Example Input: A lion
71
+ Example Positive Prompt: Toy-like depiction of a lion with a focus on structural accuracy, minimal details, digital painting, concept art style, basic contours, soft lighting, clean lines, neutral or muted colors, simplified features, low contrast.
72
+
73
+ Simple Objects (e.g., a tennis ball): For these, the prompt should specify a realistic depiction, focusing on the accurate shape and structure.
74
+
75
+ Example Input: A tennis ball
76
+ Example Positive Prompt: Realistic depiction of a tennis ball with accurate shape and texture, digital painting, clean lines, minimal additional details, soft lighting, neutral or muted colors, focus on structural integrity.
77
+
78
+ Prompt Structure:
79
+
80
+ Subject: Clearly describe the object and its essential shape and structure.
81
+ Medium: Specify the art style (e.g., digital painting, concept art).
82
+ Style: Include relevant style terms (e.g., simplified, toy-like for complex objects; realistic for simple objects).
83
+ Resolution: Mention resolution if necessary (e.g., basic resolution).
84
+ Lighting: Indicate the type of lighting (e.g., soft lighting).
85
+ Color: Use neutral or muted colors with minimal emphasis on color details.
86
+ Additional Details: Keep additional details minimal or specify if not desired.
87
+ Input: {text}
88
+ Positive Prompt:
89
+ '''
90
+
91
+ body = json.dumps({'inputText': instruction,
92
+ 'textGenerationConfig': {'temperature': 0.1, 'topP': 0.01, 'maxTokenCount':512}})
93
+ response = bedrock_runtime.invoke_model(body=body, modelId='amazon.titan-text-express-v1')
94
+ pos_prompt = json.loads(response.get('body').read())['results'][0]['outputText']
95
+ return pos_prompt
96
+
97
+ def encode_image_to_base64(image_path):
98
+ with open(image_path, "rb") as image_file:
99
+ encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
100
+ return encoded_string
101
+
102
+
103
+ def generate_image_from_text(encoded_image,pos_prompt=None):
104
+ 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.'''
105
+ encoded_str = encode_image_to_base64(encoded_image)
106
+ if pos_prompt:
107
+ new_prompt = gen_pos_prompt(pos_prompt)
108
+ parameters = {
109
+ 'taskType': 'IMAGE_VARIATION',
110
+ 'imageVariationParams': {
111
+ 'images': [encoded_str],
112
+ 'text': new_prompt,
113
+ 'negativeText': neg_prompt,
114
+ 'similarityStrength': 0.7
115
+ },
116
+ 'imageGenerationConfig': {
117
+ "cfgScale": 8,
118
+ "seed": 0,
119
+ "width": 512,
120
+ "height": 512,
121
+ "numberOfImages": 1
122
+ }
123
+ }
124
+ else:
125
+ parameters = {
126
+ 'taskType': 'IMAGE_VARIATION',
127
+ 'imageVariationParams': {
128
+ 'images': [encoded_str],
129
+ 'negativeText': neg_prompt,
130
+ 'similarityStrength': 0.7
131
+ },
132
+ 'imageGenerationConfig': {
133
+ "cfgScale": 8,
134
+ "seed": 0,
135
+ "width": 512,
136
+ "height": 512,
137
+ "numberOfImages": 1
138
+ }
139
+ }
140
+
141
+ request_body = json.dumps(parameters)
142
+ response = bedrock_runtime.invoke_model(body=request_body, modelId='amazon.titan-image-generator-v1')
143
+ response_body = json.loads(response.get('body').read())
144
+ base64_image_data = base64.b64decode(response_body['images'][0])
145
+ return Image.open(io.BytesIO(base64_image_data))
146
+
147
  def check_input_image(input_image):
148
  if input_image is None:
149
  raise gr.Error("No image uploaded!")
150
 
 
151
  def preprocess(input_image, do_remove_background, foreground_ratio):
152
  def fill_background(image):
153
  image = np.array(image).astype(np.float32) / 255.0
 
166
  image = fill_background(image)
167
  return image
168
 
169
+ # @spaces.GPU
 
170
  def generate(image, mc_resolution, formats=["obj", "glb"]):
171
  scene_codes = model(image, device=device)
172
  mesh = model.extract_mesh(scene_codes, resolution=mc_resolution)[0]
 
181
 
182
  return mesh_path_obj.name, mesh_path_glb.name
183
 
184
+ def run_example(text_prompt, do_remove_background, foreground_ratio, mc_resolution):
185
+ image_pil = generate_image_from_text(text_prompt)
186
+ preprocessed = preprocess(image_pil, do_remove_background, foreground_ratio)
187
+ mesh_name_obj, mesh_name_glb = generate(preprocessed, mc_resolution, ["obj", "glb"])
188
  return preprocessed, mesh_name_obj, mesh_name_glb
189
 
190
  with gr.Blocks() as demo:
 
192
  with gr.Row(variant="panel"):
193
  with gr.Column():
194
  with gr.Row():
195
+
196
  input_image = gr.Image(
197
+ label="Generated Image",
198
  image_mode="RGBA",
199
  sources="upload",
200
  type="pil",
201
  elem_id="content_image",
202
+ visible=True # Hidden since we generate the image from text
203
+ )
204
+ text_prompt = gr.Textbox(
205
+ label="Text Prompt",
206
+ placeholder="Enter Positive Prompt"
207
  )
208
+ processed_image = gr.Image(label="Processed Image", interactive=False, visible=False)
209
  with gr.Row():
210
  with gr.Group():
211
  do_remove_background = gr.Checkbox(
 
245
  examples=[
246
  os.path.join("examples", img_name) for img_name in sorted(os.listdir("examples"))
247
  ],
248
+ inputs=[text_prompt],
249
  outputs=[processed_image, output_model_obj, output_model_glb],
250
  cache_examples=True,
251
+ fn=partial(run_example, do_remove_background=True, foreground_ratio=0.85, mc_resolution=256),
252
  label="Examples",
253
  examples_per_page=20
254
  )
255
+ submit.click(fn=check_input_image, inputs=[input_image, text_prompt]).success(
256
+ fn=run_example,
257
+ inputs=[input_image,text_prompt, do_remove_background, foreground_ratio, mc_resolution],
258
+ outputs=[processed_image, output_model_obj, output_model_glb],
259
+ # outputs=[output_model_obj, output_model_glb],
 
 
 
260
  )
261
 
262
  demo.queue(max_size=10)
263
+ demo.launch(auth=(os.getenv("USERNAME"), os.getenv("PASSWORD")))