Spaces:
Running
on
Zero
Running
on
Zero
Update src/app.py
Browse files- src/app.py +55 -3
src/app.py
CHANGED
@@ -15,6 +15,9 @@ from refiners.foundationals.latent_diffusion import Solver, solvers
|
|
15 |
import requests
|
16 |
from enhancer import ESRGANUpscaler, ESRGANUpscalerCheckpoints
|
17 |
import time
|
|
|
|
|
|
|
18 |
|
19 |
pillow_heif.register_heif_opener()
|
20 |
pillow_heif.register_avif_opener()
|
@@ -101,6 +104,28 @@ enhancer.to(device=DEVICE, dtype=DTYPE)
|
|
101 |
|
102 |
|
103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
class calculateDuration:
|
105 |
def __init__(self, activity_name=""):
|
106 |
self.activity_name = activity_name
|
@@ -134,7 +159,12 @@ def process(
|
|
134 |
denoise_strength: float = 0.35,
|
135 |
num_inference_steps: int = 18,
|
136 |
solver: str = "DDIM",
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
138 |
manual_seed(seed)
|
139 |
|
140 |
if image_url:
|
@@ -164,7 +194,14 @@ def process(
|
|
164 |
solver_type=solver_type,
|
165 |
)
|
166 |
print("enhancer finish")
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
with gr.Blocks() as demo:
|
170 |
gr.HTML(TITLE)
|
@@ -176,7 +213,10 @@ with gr.Blocks() as demo:
|
|
176 |
run_button = gr.ClearButton(components=None, value="Enhance Image")
|
177 |
with gr.Column():
|
178 |
output_slider = ImageSlider(label="Generate image", type="pil", slider_color="pink")
|
|
|
|
|
179 |
run_button.add(output_slider)
|
|
|
180 |
|
181 |
with gr.Accordion("Advanced Options", open=False):
|
182 |
prompt = gr.Textbox(
|
@@ -256,6 +296,13 @@ with gr.Blocks() as demo:
|
|
256 |
label="Solver",
|
257 |
)
|
258 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
run_button.click(
|
260 |
fn=process,
|
261 |
inputs=[
|
@@ -273,8 +320,13 @@ with gr.Blocks() as demo:
|
|
273 |
denoise_strength,
|
274 |
num_inference_steps,
|
275 |
solver,
|
|
|
|
|
|
|
|
|
|
|
276 |
],
|
277 |
-
outputs=output_slider,
|
278 |
)
|
279 |
|
280 |
demo.launch(share=False)
|
|
|
15 |
import requests
|
16 |
from enhancer import ESRGANUpscaler, ESRGANUpscalerCheckpoints
|
17 |
import time
|
18 |
+
import boto3
|
19 |
+
from datetime import datetime
|
20 |
+
import json
|
21 |
|
22 |
pillow_heif.register_heif_opener()
|
23 |
pillow_heif.register_avif_opener()
|
|
|
104 |
|
105 |
|
106 |
|
107 |
+
def upload_image_to_r2(image, account_id, access_key, secret_key, bucket_name):
|
108 |
+
print("upload_image_to_s3", account_id, access_key, secret_key, bucket_name)
|
109 |
+
connectionUrl = f"https://{account_id}.r2.cloudflarestorage.com"
|
110 |
+
|
111 |
+
s3 = boto3.client(
|
112 |
+
's3',
|
113 |
+
endpoint_url=connectionUrl,
|
114 |
+
region_name='auto',
|
115 |
+
aws_access_key_id=access_key,
|
116 |
+
aws_secret_access_key=secret_key
|
117 |
+
)
|
118 |
+
|
119 |
+
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
|
120 |
+
image_file = f"generated_images/{current_time}_{random.randint(0, MAX_SEED)}.png"
|
121 |
+
buffer = BytesIO()
|
122 |
+
image.save(buffer, "PNG")
|
123 |
+
buffer.seek(0)
|
124 |
+
s3.upload_fileobj(buffer, bucket_name, image_file)
|
125 |
+
print("upload finish", image_file)
|
126 |
+
return image_file
|
127 |
+
|
128 |
+
|
129 |
class calculateDuration:
|
130 |
def __init__(self, activity_name=""):
|
131 |
self.activity_name = activity_name
|
|
|
159 |
denoise_strength: float = 0.35,
|
160 |
num_inference_steps: int = 18,
|
161 |
solver: str = "DDIM",
|
162 |
+
upload_to_r2: bool = True,
|
163 |
+
account_id = "",
|
164 |
+
access_key = "",
|
165 |
+
secret_key = "",
|
166 |
+
bucket = ""
|
167 |
+
) -> Tuple[Tuple[Image.Image, Image.Image], str]:
|
168 |
manual_seed(seed)
|
169 |
|
170 |
if image_url:
|
|
|
194 |
solver_type=solver_type,
|
195 |
)
|
196 |
print("enhancer finish")
|
197 |
+
|
198 |
+
if upload_to_r2:
|
199 |
+
url = upload_image_to_r2(enhanced_image, account_id, access_key, secret_key, bucket_name)
|
200 |
+
result = {"status": "success", "url": url}
|
201 |
+
else:
|
202 |
+
result = {"status": "success", "message": "Image generated but not uploaded"}
|
203 |
+
return [input_image, enhanced_image], json.dumps(result)
|
204 |
+
|
205 |
|
206 |
with gr.Blocks() as demo:
|
207 |
gr.HTML(TITLE)
|
|
|
213 |
run_button = gr.ClearButton(components=None, value="Enhance Image")
|
214 |
with gr.Column():
|
215 |
output_slider = ImageSlider(label="Generate image", type="pil", slider_color="pink")
|
216 |
+
logs = gr.Textbox(label="logs")
|
217 |
+
|
218 |
run_button.add(output_slider)
|
219 |
+
|
220 |
|
221 |
with gr.Accordion("Advanced Options", open=False):
|
222 |
prompt = gr.Textbox(
|
|
|
296 |
label="Solver",
|
297 |
)
|
298 |
|
299 |
+
upload_to_r2 = gr.Checkbox(label="Upload generated image to R2", value=False)
|
300 |
+
account_id = gr.Textbox(label="Account Id", placeholder="Enter R2 account id")
|
301 |
+
access_key = gr.Textbox(label="Access Key", placeholder="Enter R2 access key here")
|
302 |
+
secret_key = gr.Textbox(label="Secret Key", placeholder="Enter R2 secret key here")
|
303 |
+
bucket = gr.Textbox(label="Bucket Name", placeholder="Enter R2 bucket name here")
|
304 |
+
|
305 |
+
|
306 |
run_button.click(
|
307 |
fn=process,
|
308 |
inputs=[
|
|
|
320 |
denoise_strength,
|
321 |
num_inference_steps,
|
322 |
solver,
|
323 |
+
upload_to_r2,
|
324 |
+
account_id,
|
325 |
+
access_key,
|
326 |
+
secret_key,
|
327 |
+
bucket
|
328 |
],
|
329 |
+
outputs=[output_slider, logs]
|
330 |
)
|
331 |
|
332 |
demo.launch(share=False)
|