vibs08 commited on
Commit
e4ffaa9
·
verified ·
1 Parent(s): a5ad85c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -27
app.py CHANGED
@@ -23,6 +23,8 @@ import io
23
  from io import BytesIO
24
  from botocore.exceptions import NoCredentialsError, PartialCredentialsError
25
  import datetime
 
 
26
 
27
  app = FastAPI()
28
 
@@ -54,6 +56,7 @@ ACCESS = os.getenv("ACCESS")
54
  SECRET = os.getenv("SECRET")
55
  bedrock = boto3.client(service_name='bedrock', aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
56
  bedrock_runtime = boto3.client(service_name='bedrock-runtime', aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
 
57
 
58
 
59
  def gen_pos_prompt(text):
@@ -114,7 +117,7 @@ def generate_image_from_text(encoded_image, seed, pos_prompt=None):
114
  },
115
  'imageGenerationConfig': {
116
  "cfgScale": 8,
117
- "seed": int(seed),
118
  "width": 512,
119
  "height": 512,
120
  "numberOfImages": 1
@@ -130,7 +133,7 @@ def generate_image_from_text(encoded_image, seed, pos_prompt=None):
130
  },
131
  'imageGenerationConfig': {
132
  "cfgScale": 8,
133
- "seed": int(seed),
134
  "width": 512,
135
  "height": 512,
136
  "numberOfImages": 1
@@ -147,20 +150,25 @@ def check_input_image(input_image):
147
  raise gr.Error("No image uploaded!")
148
 
149
  def preprocess(input_image, do_remove_background, foreground_ratio):
150
- torch.cuda.synchronize()
151
  def fill_background(image):
 
 
152
  image = np.array(image).astype(np.float32) / 255.0
153
  image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
154
  image = Image.fromarray((image * 255.0).astype(np.uint8))
155
  return image
156
 
157
  if do_remove_background:
 
 
158
  image = input_image.convert("RGB")
159
  image = remove_background(image, rembg_session)
160
  image = resize_foreground(image, foreground_ratio)
161
  image = fill_background(image)
162
- print("do_remove_back")
 
163
  torch.cuda.empty_cache()
 
164
  else:
165
  image = input_image
166
  if image.mode == "RGBA":
@@ -175,40 +183,29 @@ def generate(image, mc_resolution, formats=["obj", "glb"]):
175
  scene_codes = model(image, device=device)
176
  torch.cuda.synchronize()
177
  mesh = model.extract_mesh(scene_codes, resolution=mc_resolution)[0]
 
178
  mesh = to_gradio_3d_orientation(mesh)
 
179
 
180
  mesh_path_glb = tempfile.NamedTemporaryFile(suffix=f".glb", delete=False)
 
181
  mesh.export(mesh_path_glb.name)
 
182
 
183
  mesh_path_obj = tempfile.NamedTemporaryFile(suffix=f".obj", delete=False)
 
184
  mesh.apply_scale([-1, 1, 1]) # Otherwise the visualized .obj will be flipped
185
  mesh.export(mesh_path_obj.name)
186
  torch.cuda.synchronize() # Ensure all CUDA operations are complete before clearing cache
187
  torch.cuda.empty_cache()
188
  return mesh_path_obj.name, mesh_path_glb.name
189
 
190
- def upload_file_to_s3(file_path, bucket_name, object_name=None):
191
- s3_client = boto3.client('s3',aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
192
-
193
- if object_name is None:
194
- object_name = file_path
195
 
196
- try:
197
- s3_client.upload_file(file_path, bucket_name, object_name)
198
- except FileNotFoundError:
199
- print(f"The file {file_path} was not found.")
200
- return False
201
- except NoCredentialsError:
202
- print("Credentials not available.")
203
- return False
204
- except PartialCredentialsError:
205
- print("Incomplete credentials provided.")
206
- return False
207
- except Exception as e:
208
- print(f"An error occurred: {e}")
209
- return False
210
-
211
- print(f"File {file_path} uploaded successfully to {bucket_name}/{object_name}.")
212
  return True
213
 
214
 
@@ -220,8 +217,8 @@ async def process_image(
220
  enhance_image: bool = Form(False), # Default enhance_image value
221
  do_remove_background: bool = Form(True), # Default do_remove_background value
222
  foreground_ratio: float = Form(0.85, gt=0.0, lt=1.0), # Ratio must be between 0.0 and 1.0 (exclusive)
223
- mc_resolution: int = Form(285, ge=32, le=320), # Resolution must be between 256 and 4096
224
- auth: str = Form("default_auth_token"), # Default auth token
225
  text_prompt: Optional[str] = Form(None)
226
  ):
227
 
 
23
  from io import BytesIO
24
  from botocore.exceptions import NoCredentialsError, PartialCredentialsError
25
  import datetime
26
+ from transformers.utils import move_cache
27
+ move_cache()
28
 
29
  app = FastAPI()
30
 
 
56
  SECRET = os.getenv("SECRET")
57
  bedrock = boto3.client(service_name='bedrock', aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
58
  bedrock_runtime = boto3.client(service_name='bedrock-runtime', aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
59
+ s3_client = boto3.client('s3',aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
60
 
61
 
62
  def gen_pos_prompt(text):
 
117
  },
118
  'imageGenerationConfig': {
119
  "cfgScale": 8,
120
+ "seed": seed,
121
  "width": 512,
122
  "height": 512,
123
  "numberOfImages": 1
 
133
  },
134
  'imageGenerationConfig': {
135
  "cfgScale": 8,
136
+ "seed": seed,
137
  "width": 512,
138
  "height": 512,
139
  "numberOfImages": 1
 
150
  raise gr.Error("No image uploaded!")
151
 
152
  def preprocess(input_image, do_remove_background, foreground_ratio):
 
153
  def fill_background(image):
154
+ torch.cuda.synchronize()
155
+ torch.cuda.empty_cache()
156
  image = np.array(image).astype(np.float32) / 255.0
157
  image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
158
  image = Image.fromarray((image * 255.0).astype(np.uint8))
159
  return image
160
 
161
  if do_remove_background:
162
+ torch.cuda.synchronize()
163
+ torch.cuda.empty_cache()
164
  image = input_image.convert("RGB")
165
  image = remove_background(image, rembg_session)
166
  image = resize_foreground(image, foreground_ratio)
167
  image = fill_background(image)
168
+
169
+ torch.cuda.synchronize()
170
  torch.cuda.empty_cache()
171
+ print("Background Removed")
172
  else:
173
  image = input_image
174
  if image.mode == "RGBA":
 
183
  scene_codes = model(image, device=device)
184
  torch.cuda.synchronize()
185
  mesh = model.extract_mesh(scene_codes, resolution=mc_resolution)[0]
186
+ torch.cuda.synchronize()
187
  mesh = to_gradio_3d_orientation(mesh)
188
+ torch.cuda.synchronize()
189
 
190
  mesh_path_glb = tempfile.NamedTemporaryFile(suffix=f".glb", delete=False)
191
+ torch.cuda.synchronize()
192
  mesh.export(mesh_path_glb.name)
193
+ torch.cuda.synchronize()
194
 
195
  mesh_path_obj = tempfile.NamedTemporaryFile(suffix=f".obj", delete=False)
196
+ torch.cuda.synchronize()
197
  mesh.apply_scale([-1, 1, 1]) # Otherwise the visualized .obj will be flipped
198
  mesh.export(mesh_path_obj.name)
199
  torch.cuda.synchronize() # Ensure all CUDA operations are complete before clearing cache
200
  torch.cuda.empty_cache()
201
  return mesh_path_obj.name, mesh_path_glb.name
202
 
203
+ def upload_file_to_s3(file_path, bucket_name, object_name=None):
204
+ s3_client.upload_file(file_path, bucket_name, object_name)
 
 
 
205
 
206
+ # print(f"File {file_path} uploaded successfully to {bucket_name}/{object_name}.")
207
+ torch.cuda.synchronize() # Wait for all CUDA operations to complete
208
+ torch.cuda.empty_cache()
 
 
 
 
 
 
 
 
 
 
 
 
 
209
  return True
210
 
211
 
 
217
  enhance_image: bool = Form(False), # Default enhance_image value
218
  do_remove_background: bool = Form(True), # Default do_remove_background value
219
  foreground_ratio: float = Form(0.85, gt=0.0, lt=1.0), # Ratio must be between 0.0 and 1.0 (exclusive)
220
+ mc_resolution: int = Form(286, ge=32, le=320), # Resolution must be between 256 and 4096
221
+ auth: str = Form(...),
222
  text_prompt: Optional[str] = Form(None)
223
  ):
224