Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -84,25 +84,41 @@ def generate_image(prompt, adapter_names, steps, seed, cfg_scale, width, height,
|
|
84 |
|
85 |
|
86 |
def upload_image_to_r2(image, account_id, access_key, secret_key, bucket_name):
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
current_time = datetime.now().strftime("%Y/%m/%d/%H%M%S")
|
99 |
-
image_file = f"generated_images/{current_time}_{random.randint(0, MAX_SEED)}.png"
|
100 |
-
buffer = BytesIO()
|
101 |
-
image.save(buffer, "PNG")
|
102 |
-
buffer.seek(0)
|
103 |
-
s3.upload_fileobj(buffer, bucket_name, image_file)
|
104 |
-
print("upload finish", image_file)
|
105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
return image_file
|
107 |
|
108 |
def run_lora(prompt, lora_strings_json, cfg_scale, steps, randomize_seed, seed, width, height, upload_to_r2, account_id, access_key, secret_key, bucket, progress=gr.Progress(track_tqdm=True)):
|
@@ -164,9 +180,8 @@ def run_lora(prompt, lora_strings_json, cfg_scale, steps, randomize_seed, seed,
|
|
164 |
|
165 |
if final_image:
|
166 |
if upload_to_r2:
|
167 |
-
|
168 |
-
|
169 |
-
result = {"status": "success", "message": "upload image success", "url": url}
|
170 |
else:
|
171 |
result = {"status": "success", "message": "Image generated but not uploaded"}
|
172 |
else:
|
|
|
84 |
|
85 |
|
86 |
def upload_image_to_r2(image, account_id, access_key, secret_key, bucket_name):
|
87 |
+
with calculateDuration("Upload images"):
|
88 |
+
print("upload_image_to_r2", account_id, access_key, secret_key, bucket_name)
|
89 |
+
connectionUrl = f"https://{account_id}.r2.cloudflarestorage.com"
|
90 |
+
|
91 |
+
s3 = boto3.client(
|
92 |
+
's3',
|
93 |
+
endpoint_url=connectionUrl,
|
94 |
+
region_name='auto',
|
95 |
+
aws_access_key_id=access_key,
|
96 |
+
aws_secret_access_key=secret_key
|
97 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
+
current_time = datetime.now().strftime("%Y/%m/%d/%H%M%S")
|
100 |
+
image_file = f"generated_images/{current_time}_{random.randint(0, MAX_SEED)}.png"
|
101 |
+
buffer = BytesIO()
|
102 |
+
image.save(buffer, "PNG")
|
103 |
+
buffer.seek(0)
|
104 |
+
s3.upload_fileobj(buffer, bucket_name, image_file)
|
105 |
+
print("upload finish", image_file)
|
106 |
+
# start to generate thumbnail
|
107 |
+
thumbnail = image.copy()
|
108 |
+
thumbnail_width = 256
|
109 |
+
aspect_ratio = image.height / image.width
|
110 |
+
thumbnail_height = int(thumbnail_width * aspect_ratio)
|
111 |
+
thumbnail = thumbnail.resize((thumbnail_width, thumbnail_height), Image.LANCZOS)
|
112 |
+
|
113 |
+
# Generate the thumbnail image filename
|
114 |
+
thumbnail_file = image_file.replace(".png", "_thumbnail.png")
|
115 |
+
|
116 |
+
# Save thumbnail to buffer and upload
|
117 |
+
thumbnail_buffer = BytesIO()
|
118 |
+
thumbnail.save(thumbnail_buffer, "PNG")
|
119 |
+
thumbnail_buffer.seek(0)
|
120 |
+
s3.upload_fileobj(thumbnail_buffer, bucket_name, thumbnail_file)
|
121 |
+
print("upload thumbnail finish", thumbnail_file)
|
122 |
return image_file
|
123 |
|
124 |
def run_lora(prompt, lora_strings_json, cfg_scale, steps, randomize_seed, seed, width, height, upload_to_r2, account_id, access_key, secret_key, bucket, progress=gr.Progress(track_tqdm=True)):
|
|
|
180 |
|
181 |
if final_image:
|
182 |
if upload_to_r2:
|
183 |
+
url = upload_image_to_r2(final_image, account_id, access_key, secret_key, bucket)
|
184 |
+
result = {"status": "success", "message": "upload image success", "url": url}
|
|
|
185 |
else:
|
186 |
result = {"status": "success", "message": "Image generated but not uploaded"}
|
187 |
else:
|