Beamlnwza commited on
Commit
d99687a
·
1 Parent(s): 6588d03

extract s3uploadimage from all and index methods

Browse files
Files changed (1) hide show
  1. src/endpoints/generate.py +20 -24
src/endpoints/generate.py CHANGED
@@ -48,17 +48,8 @@ async def generate(user: User, index: int | None = None) -> GenerateResult:
48
  async def generate_index(user: User, index: int) -> GenerateResult:
49
  s3 = s3client()
50
 
51
- output_path = os.path.join(IMAGE_STORE_PATH, f"{user.uuid}-{str(index).zfill(2)}.png")
52
- used_model = model.model_cols[get_model(index)]
53
- image = generate_img_index(reloaded_model=used_model, index=index % 11)
54
- save_img(image, output_path)
55
-
56
- s3_path: str = f"{user.uuid}/{str(index).zfill(2)}.png"
57
- s3.upload_file(output_path, BUCKET_NAME, s3_path)
58
- img_url: str = f'https://{BUCKET_NAME}.s3.amazonaws.com/{s3_path}'
59
-
60
- result: List[ImageResult] = [ImageResult(index=index, image_url=img_url)]
61
- os.remove(output_path)
62
 
63
  s3.close()
64
 
@@ -70,21 +61,26 @@ async def generate_all(user: User):
70
  result: List[ImageResult] = []
71
 
72
  for index in range(0, 88):
73
- output_path = os.path.join(IMAGE_STORE_PATH, f"{user.uuid}-{str(index).zfill(2)}.png")
74
- used_model = model.model_cols[get_model(index)]
75
- image = generate_img_index(reloaded_model=used_model, index=index % 11)
76
- save_img(image, output_path)
77
-
78
- s3_path: str = f"{user.uuid}/{str(index).zfill(2)}.png"
79
- s3.upload_file(output_path, BUCKET_NAME, s3_path)
80
- image_url = f'https://{BUCKET_NAME}.s3.amazonaws.com/{s3_path}'
81
-
82
- img_detail = ImageResult(index=index,
83
- image_url=image_url)
84
  result.append(img_detail)
85
 
86
- os.remove(output_path)
87
-
88
  s3.close()
89
 
90
  return GenerateResult(user=user, method=Method.all, result=result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  async def generate_index(user: User, index: int) -> GenerateResult:
49
  s3 = s3client()
50
 
51
+ img_detail = s3uploadimage(user, s3, index)
52
+ result: List[ImageResult] = [img_detail]
 
 
 
 
 
 
 
 
 
53
 
54
  s3.close()
55
 
 
61
  result: List[ImageResult] = []
62
 
63
  for index in range(0, 88):
64
+ img_detail = s3uploadimage(user, s3, index)
 
 
 
 
 
 
 
 
 
 
65
  result.append(img_detail)
66
 
 
 
67
  s3.close()
68
 
69
  return GenerateResult(user=user, method=Method.all, result=result)
70
+
71
+
72
+ def s3uploadimage(user, s3, index):
73
+ output_path = os.path.join(
74
+ IMAGE_STORE_PATH, f"{user.uuid}-{str(index).zfill(2)}.png")
75
+ used_model = model.model_cols[get_model(index)]
76
+ image = generate_img_index(reloaded_model=used_model, index=index % 11)
77
+ save_img(image, output_path)
78
+
79
+ s3_path: str = f"{user.uuid}/{str(index).zfill(2)}.png"
80
+ s3.upload_file(output_path, BUCKET_NAME, s3_path)
81
+ image_url = f'https://{BUCKET_NAME}.s3.amazonaws.com/{s3_path}'
82
+
83
+ img_detail = ImageResult(index=index,
84
+ image_url=image_url)
85
+ os.remove(output_path)
86
+ return img_detail