Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse filesapp.pyへコメント文の追加
app.py
CHANGED
@@ -88,6 +88,7 @@ SAMPLER_MAP = {
|
|
88 |
"Euler": lambda config: EulerDiscreteScheduler.from_config(config),
|
89 |
}
|
90 |
|
|
|
91 |
def center_crop_resize(img, output_size=(512, 512)):
|
92 |
width, height = img.size
|
93 |
|
@@ -104,6 +105,7 @@ def center_crop_resize(img, output_size=(512, 512)):
|
|
104 |
|
105 |
return img
|
106 |
|
|
|
107 |
def common_upscale(samples, width, height, upscale_method, crop=False):
|
108 |
if crop == "center":
|
109 |
old_width = samples.shape[3]
|
@@ -122,6 +124,7 @@ def common_upscale(samples, width, height, upscale_method, crop=False):
|
|
122 |
|
123 |
return torch.nn.functional.interpolate(s, size=(height, width), mode=upscale_method)
|
124 |
|
|
|
125 |
def upscale(samples, upscale_method, scale_by):
|
126 |
#s = samples.copy()
|
127 |
width = round(samples["images"].shape[3] * scale_by)
|
@@ -129,16 +132,19 @@ def upscale(samples, upscale_method, scale_by):
|
|
129 |
s = common_upscale(samples["images"], width, height, upscale_method, "disabled")
|
130 |
return (s)
|
131 |
|
|
|
132 |
def check_inputs(prompt: str, control_image: Image.Image):
|
133 |
if control_image is None:
|
134 |
raise gr.Error("Please select or upload an Input Illusion")
|
135 |
if prompt is None or prompt == "":
|
136 |
raise gr.Error("Prompt is required")
|
137 |
|
|
|
138 |
def convert_to_pil(base64_image):
|
139 |
pil_image = Image.open(base64_image)
|
140 |
return pil_image
|
141 |
|
|
|
142 |
def convert_to_base64(pil_image):
|
143 |
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp_file:
|
144 |
image.save(temp_file.name)
|
@@ -236,6 +242,8 @@ def inference(
|
|
236 |
# Gradio UIの構築
|
237 |
|
238 |
with gr.Blocks() as app:
|
|
|
|
|
239 |
gr.Markdown(
|
240 |
'''
|
241 |
<div style="text-align: center;">
|
@@ -248,9 +256,11 @@ with gr.Blocks() as app:
|
|
248 |
'''
|
249 |
)
|
250 |
|
251 |
-
|
252 |
state_img_input = gr.State()
|
253 |
state_img_output = gr.State()
|
|
|
|
|
254 |
with gr.Row():
|
255 |
with gr.Column():
|
256 |
control_image = gr.Image(label="Input Illusion", type="pil", elem_id="control_image")
|
@@ -274,6 +284,7 @@ with gr.Blocks() as app:
|
|
274 |
loading_icon = gr.HTML(loading_icon_html)
|
275 |
share_button = gr.Button("Share to community", elem_id="share-btn")
|
276 |
|
|
|
277 |
prompt.submit(
|
278 |
check_inputs,
|
279 |
inputs=[prompt, control_image],
|
@@ -282,7 +293,8 @@ with gr.Blocks() as app:
|
|
282 |
inference,
|
283 |
inputs=[control_image, prompt, negative_prompt, guidance_scale, controlnet_conditioning_scale, control_start, control_end, strength, seed, sampler],
|
284 |
outputs=[result_image, result_image, share_group, used_seed])
|
285 |
-
|
|
|
286 |
run_btn.click(
|
287 |
check_inputs,
|
288 |
inputs=[prompt, control_image],
|
@@ -291,9 +303,11 @@ with gr.Blocks() as app:
|
|
291 |
inference,
|
292 |
inputs=[control_image, prompt, negative_prompt, guidance_scale, controlnet_conditioning_scale, control_start, control_end, strength, seed, sampler],
|
293 |
outputs=[result_image, result_image, share_group, used_seed])
|
294 |
-
|
|
|
295 |
share_button.click(None, [], [], js=share_js)
|
296 |
|
|
|
297 |
with gr.Blocks(css=css) as app_with_history:
|
298 |
with gr.Tab("Demo"):
|
299 |
app.render()
|
|
|
88 |
"Euler": lambda config: EulerDiscreteScheduler.from_config(config),
|
89 |
}
|
90 |
|
91 |
+
# 入力画像を中央からクロップし、指定されたサイズにリサイズする
|
92 |
def center_crop_resize(img, output_size=(512, 512)):
|
93 |
width, height = img.size
|
94 |
|
|
|
105 |
|
106 |
return img
|
107 |
|
108 |
+
# 指定された方法で画像をアップスケールする
|
109 |
def common_upscale(samples, width, height, upscale_method, crop=False):
|
110 |
if crop == "center":
|
111 |
old_width = samples.shape[3]
|
|
|
124 |
|
125 |
return torch.nn.functional.interpolate(s, size=(height, width), mode=upscale_method)
|
126 |
|
127 |
+
# common_upscale を利用して画像を指定された倍率でアップスケールする
|
128 |
def upscale(samples, upscale_method, scale_by):
|
129 |
#s = samples.copy()
|
130 |
width = round(samples["images"].shape[3] * scale_by)
|
|
|
132 |
s = common_upscale(samples["images"], width, height, upscale_method, "disabled")
|
133 |
return (s)
|
134 |
|
135 |
+
# ユーザーの入力が適切かどうかをチェックする
|
136 |
def check_inputs(prompt: str, control_image: Image.Image):
|
137 |
if control_image is None:
|
138 |
raise gr.Error("Please select or upload an Input Illusion")
|
139 |
if prompt is None or prompt == "":
|
140 |
raise gr.Error("Prompt is required")
|
141 |
|
142 |
+
# Base64エンコードされた画像をPIL(Python Imaging Library)形式の画像に変換する
|
143 |
def convert_to_pil(base64_image):
|
144 |
pil_image = Image.open(base64_image)
|
145 |
return pil_image
|
146 |
|
147 |
+
# PIL形式の画像をBase64形式に変換する
|
148 |
def convert_to_base64(pil_image):
|
149 |
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp_file:
|
150 |
image.save(temp_file.name)
|
|
|
242 |
# Gradio UIの構築
|
243 |
|
244 |
with gr.Blocks() as app:
|
245 |
+
|
246 |
+
# アプリの紹介や説明。テキストやリンクを追加
|
247 |
gr.Markdown(
|
248 |
'''
|
249 |
<div style="text-align: center;">
|
|
|
256 |
'''
|
257 |
)
|
258 |
|
259 |
+
# 状態の管理
|
260 |
state_img_input = gr.State()
|
261 |
state_img_output = gr.State()
|
262 |
+
|
263 |
+
# アプリのレイアウトを設定
|
264 |
with gr.Row():
|
265 |
with gr.Column():
|
266 |
control_image = gr.Image(label="Input Illusion", type="pil", elem_id="control_image")
|
|
|
284 |
loading_icon = gr.HTML(loading_icon_html)
|
285 |
share_button = gr.Button("Share to community", elem_id="share-btn")
|
286 |
|
287 |
+
# テキストボックスに入力されたプロンプトが送信されたときに実行されるイベントを設定
|
288 |
prompt.submit(
|
289 |
check_inputs,
|
290 |
inputs=[prompt, control_image],
|
|
|
293 |
inference,
|
294 |
inputs=[control_image, prompt, negative_prompt, guidance_scale, controlnet_conditioning_scale, control_start, control_end, strength, seed, sampler],
|
295 |
outputs=[result_image, result_image, share_group, used_seed])
|
296 |
+
|
297 |
+
# 「Run」ボタンがクリックされたときに実行されるイベントを設定
|
298 |
run_btn.click(
|
299 |
check_inputs,
|
300 |
inputs=[prompt, control_image],
|
|
|
303 |
inference,
|
304 |
inputs=[control_image, prompt, negative_prompt, guidance_scale, controlnet_conditioning_scale, control_start, control_end, strength, seed, sampler],
|
305 |
outputs=[result_image, result_image, share_group, used_seed])
|
306 |
+
|
307 |
+
# 共有ボタンがクリックされたときに実行されるイベントを設定
|
308 |
share_button.click(None, [], [], js=share_js)
|
309 |
|
310 |
+
# アプリケーションの起動
|
311 |
with gr.Blocks(css=css) as app_with_history:
|
312 |
with gr.Tab("Demo"):
|
313 |
app.render()
|