vibs08 commited on
Commit
3db2f15
·
verified ·
1 Parent(s): 8024103

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -4
app.py CHANGED
@@ -20,6 +20,7 @@ from PIL import Image
20
  from functools import partial
21
  import io
22
  from io import BytesIO
 
23
 
24
  app = FastAPI()
25
 
@@ -187,11 +188,37 @@ def generate(image, mc_resolution, formats=["obj", "glb"]):
187
 
188
  return mesh_path_obj.name, mesh_path_glb.name
189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  @app.post("/process_image/")
191
  async def process_image(
192
  file: UploadFile = File(...),
193
  seed: int = Form(...),
194
- use_image: bool = Form(...),
195
  do_remove_background: bool = Form(...),
196
  foreground_ratio: float = Form(...),
197
  mc_resolution: int = Form(...),
@@ -200,7 +227,7 @@ async def process_image(
200
  image_bytes = await file.read()
201
  input_image = Image.open(BytesIO(image_bytes))
202
 
203
- if use_image:
204
  image_pil = generate_image_from_text(encoded_image=input_image, seed=seed, pos_prompt=text_prompt)
205
  else:
206
  image_pil = input_image
@@ -209,8 +236,8 @@ async def process_image(
209
  mesh_name_obj, mesh_name_glb = generate(preprocessed, mc_resolution)
210
 
211
  return {
212
- "obj_path": mesh_name_obj,
213
- "glb_path": mesh_name_glb
214
  }
215
 
216
  if __name__ == "__main__":
 
20
  from functools import partial
21
  import io
22
  from io import BytesIO
23
+ from botocore.exceptions import NoCredentialsError, PartialCredentialsError
24
 
25
  app = FastAPI()
26
 
 
188
 
189
  return mesh_path_obj.name, mesh_path_glb.name
190
 
191
+ def upload_file_to_s3(file_path, bucket_name, object_name=None):
192
+ s3_client = boto3.client('s3',aws_access_key_id = ACCESS, aws_secret_access_key = SECRET, region_name='us-east-1')
193
+
194
+ if object_name is None:
195
+ object_name = file_path
196
+
197
+ try:
198
+ s3_client.upload_file(file_path, bucket_name, object_name)
199
+ except FileNotFoundError:
200
+ print(f"The file {file_path} was not found.")
201
+ return False
202
+ except NoCredentialsError:
203
+ print("Credentials not available.")
204
+ return False
205
+ except PartialCredentialsError:
206
+ print("Incomplete credentials provided.")
207
+ return False
208
+ except Exception as e:
209
+ print(f"An error occurred: {e}")
210
+ return False
211
+
212
+ print(f"File {file_path} uploaded successfully to {bucket_name}/{object_name}.")
213
+ return True
214
+
215
+
216
+
217
  @app.post("/process_image/")
218
  async def process_image(
219
  file: UploadFile = File(...),
220
  seed: int = Form(...),
221
+ enhance_image: bool = Form(...),
222
  do_remove_background: bool = Form(...),
223
  foreground_ratio: float = Form(...),
224
  mc_resolution: int = Form(...),
 
227
  image_bytes = await file.read()
228
  input_image = Image.open(BytesIO(image_bytes))
229
 
230
+ if enhance_image:
231
  image_pil = generate_image_from_text(encoded_image=input_image, seed=seed, pos_prompt=text_prompt)
232
  else:
233
  image_pil = input_image
 
236
  mesh_name_obj, mesh_name_glb = generate(preprocessed, mc_resolution)
237
 
238
  return {
239
+ "obj_path": upload_file_to_s3(mesh_name_obj, 'framebucket3d')
240
+ "glb_path": upload_file_to_s3(mesh_name_glb, 'framebucket3d')
241
  }
242
 
243
  if __name__ == "__main__":