Spaces:
Running
on
Zero
Running
on
Zero
add image url support
Browse files- requirements.txt +1 -0
- src/app.py +51 -18
requirements.txt
CHANGED
@@ -2,3 +2,4 @@ git+https://github.com/finegrain-ai/refiners@299217f45ab788bb7e670bcafb37a789a05
|
|
2 |
gradio_imageslider==0.0.20
|
3 |
spaces==0.28.3
|
4 |
numpy<2.0.0
|
|
|
|
2 |
gradio_imageslider==0.0.20
|
3 |
spaces==0.28.3
|
4 |
numpy<2.0.0
|
5 |
+
requests
|
src/app.py
CHANGED
@@ -6,6 +6,8 @@ import torch
|
|
6 |
def my_arange(*args, **kwargs):
|
7 |
return torch.arange(*args, **kwargs)
|
8 |
|
|
|
|
|
9 |
|
10 |
torch.arange = my_arange
|
11 |
|
@@ -17,8 +19,9 @@ from huggingface_hub import hf_hub_download
|
|
17 |
from PIL import Image
|
18 |
from refiners.fluxion.utils import manual_seed
|
19 |
from refiners.foundationals.latent_diffusion import Solver, solvers
|
20 |
-
|
21 |
from enhancer import ESRGANUpscaler, ESRGANUpscalerCheckpoints
|
|
|
22 |
|
23 |
TITLE = """
|
24 |
Image Enhancer
|
@@ -101,9 +104,28 @@ DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
101 |
enhancer.to(device=DEVICE, dtype=DTYPE)
|
102 |
|
103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
@spaces.GPU
|
105 |
def process(
|
106 |
input_image: Image.Image,
|
|
|
107 |
prompt: str = "masterpiece, best quality, highres",
|
108 |
negative_prompt: str = "worst quality, low quality, normal quality",
|
109 |
seed: int = 42,
|
@@ -118,33 +140,43 @@ def process(
|
|
118 |
solver: str = "DDIM",
|
119 |
) -> tuple[Image.Image, Image.Image]:
|
120 |
manual_seed(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
print("start", prompt, upscale_factor)
|
122 |
solver_type: type[Solver] = getattr(solvers, solver)
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
return [input_image, enhanced_image]
|
140 |
|
141 |
-
|
142 |
with gr.Blocks() as demo:
|
143 |
gr.HTML(TITLE)
|
144 |
|
145 |
with gr.Row():
|
146 |
with gr.Column():
|
147 |
input_image = gr.Image(type="pil", label="Input Image")
|
|
|
148 |
run_button = gr.ClearButton(components=None, value="Enhance Image")
|
149 |
with gr.Column():
|
150 |
output_slider = ImageSlider(label="Generate image", type="pil", slider_color="pink")
|
@@ -232,6 +264,7 @@ with gr.Blocks() as demo:
|
|
232 |
fn=process,
|
233 |
inputs=[
|
234 |
input_image,
|
|
|
235 |
prompt,
|
236 |
negative_prompt,
|
237 |
seed,
|
|
|
6 |
def my_arange(*args, **kwargs):
|
7 |
return torch.arange(*args, **kwargs)
|
8 |
|
9 |
+
from io import BytesIO
|
10 |
+
import PIL.Image
|
11 |
|
12 |
torch.arange = my_arange
|
13 |
|
|
|
19 |
from PIL import Image
|
20 |
from refiners.fluxion.utils import manual_seed
|
21 |
from refiners.foundationals.latent_diffusion import Solver, solvers
|
22 |
+
import requests
|
23 |
from enhancer import ESRGANUpscaler, ESRGANUpscalerCheckpoints
|
24 |
+
import time
|
25 |
|
26 |
TITLE = """
|
27 |
Image Enhancer
|
|
|
104 |
enhancer.to(device=DEVICE, dtype=DTYPE)
|
105 |
|
106 |
|
107 |
+
|
108 |
+
class calculateDuration:
|
109 |
+
def __init__(self, activity_name=""):
|
110 |
+
self.activity_name = activity_name
|
111 |
+
|
112 |
+
def __enter__(self):
|
113 |
+
self.start_time = time.time()
|
114 |
+
return self
|
115 |
+
|
116 |
+
def __exit__(self, exc_type, exc_value, traceback):
|
117 |
+
self.end_time = time.time()
|
118 |
+
self.elapsed_time = self.end_time - self.start_time
|
119 |
+
if self.activity_name:
|
120 |
+
print(f"Elapsed time for {self.activity_name}: {self.elapsed_time:.6f} seconds")
|
121 |
+
else:
|
122 |
+
print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
|
123 |
+
|
124 |
+
|
125 |
@spaces.GPU
|
126 |
def process(
|
127 |
input_image: Image.Image,
|
128 |
+
image_url:str,
|
129 |
prompt: str = "masterpiece, best quality, highres",
|
130 |
negative_prompt: str = "worst quality, low quality, normal quality",
|
131 |
seed: int = 42,
|
|
|
140 |
solver: str = "DDIM",
|
141 |
) -> tuple[Image.Image, Image.Image]:
|
142 |
manual_seed(seed)
|
143 |
+
|
144 |
+
if image_url:
|
145 |
+
# fetch image from url
|
146 |
+
with calculateDuration("Download Image"):
|
147 |
+
print("start to fetch image from url", image_url)
|
148 |
+
response = requests.get(image_url)
|
149 |
+
response.raise_for_status()
|
150 |
+
input_image = PIL.Image.open(BytesIO(response.content))
|
151 |
+
print("fetch image success")
|
152 |
+
|
153 |
print("start", prompt, upscale_factor)
|
154 |
solver_type: type[Solver] = getattr(solvers, solver)
|
155 |
+
with calculateDuration("enhancer"):
|
156 |
+
enhanced_image = enhancer.upscale(
|
157 |
+
image=input_image,
|
158 |
+
prompt=prompt,
|
159 |
+
negative_prompt=negative_prompt,
|
160 |
+
upscale_factor=upscale_factor,
|
161 |
+
controlnet_scale=controlnet_scale,
|
162 |
+
controlnet_scale_decay=controlnet_decay,
|
163 |
+
condition_scale=condition_scale,
|
164 |
+
tile_size=(tile_height, tile_width),
|
165 |
+
denoise_strength=denoise_strength,
|
166 |
+
num_inference_steps=num_inference_steps,
|
167 |
+
loras_scale=LORA_SCALES,
|
168 |
+
solver_type=solver_type,
|
169 |
+
)
|
170 |
+
print("enhancer finish")
|
171 |
return [input_image, enhanced_image]
|
172 |
|
|
|
173 |
with gr.Blocks() as demo:
|
174 |
gr.HTML(TITLE)
|
175 |
|
176 |
with gr.Row():
|
177 |
with gr.Column():
|
178 |
input_image = gr.Image(type="pil", label="Input Image")
|
179 |
+
image_url = gr.Textbox(label="Image Url", placeholder="Enter image URL here (optional)")
|
180 |
run_button = gr.ClearButton(components=None, value="Enhance Image")
|
181 |
with gr.Column():
|
182 |
output_slider = ImageSlider(label="Generate image", type="pil", slider_color="pink")
|
|
|
264 |
fn=process,
|
265 |
inputs=[
|
266 |
input_image,
|
267 |
+
image_url,
|
268 |
prompt,
|
269 |
negative_prompt,
|
270 |
seed,
|