Dhruv Diddi
commited on
Commit
·
af48b88
1
Parent(s):
a2a4738
update docs
Browse files
app.py
CHANGED
|
@@ -6,6 +6,9 @@ import json
|
|
| 6 |
import os
|
| 7 |
import random
|
| 8 |
import tempfile
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
import gradio as gr
|
| 11 |
import torch
|
|
@@ -105,15 +108,18 @@ def upload_stage1_result(stage1_param_path: str, stage1_result_path: str,
|
|
| 105 |
if not UPLOAD_REPO_ID:
|
| 106 |
return
|
| 107 |
try:
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
| 117 |
except Exception as e:
|
| 118 |
print(e)
|
| 119 |
|
|
@@ -140,18 +146,20 @@ def upload_stage2_info(stage1_param_file_hash_name: str,
|
|
| 140 |
param_file.write(json.dumps(stage2_params))
|
| 141 |
stage2_param_file_hash_name = get_param_file_hash_name(param_file.name)
|
| 142 |
save_name = f'{stage1_param_file_hash_name}_{stage2_param_file_hash_name}'
|
| 143 |
-
|
| 144 |
try:
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
| 150 |
if UPLOAD_RESULT_IMAGE:
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
| 155 |
except Exception as e:
|
| 156 |
print(e)
|
| 157 |
|
|
@@ -188,17 +196,16 @@ def upload_stage2_3_info(stage1_param_file_hash_name: str,
|
|
| 188 |
save_name = f'{stage1_param_file_hash_name}_{stage2_3_param_file_hash_name}'
|
| 189 |
|
| 190 |
try:
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
if UPLOAD_RESULT_IMAGE:
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
repo_type='dataset')
|
| 202 |
except Exception as e:
|
| 203 |
print(e)
|
| 204 |
|
|
@@ -221,6 +228,22 @@ def show_gallery_view() -> tuple[dict, dict]:
|
|
| 221 |
def show_upscaled_view() -> tuple[dict, dict]:
|
| 222 |
return _update_result_view(False)
|
| 223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
examples = [
|
| 226 |
'high quality dslr photo, a photo product of a lemon inspired by natural and organic materials, wooden accents, intricately decorated with glowing vines of led lights, inspired by baroque luxury',
|
|
@@ -239,6 +262,8 @@ examples = [
|
|
| 239 |
]
|
| 240 |
|
| 241 |
with gr.Blocks(css='style.css') as demo:
|
|
|
|
|
|
|
| 242 |
with gr.Box():
|
| 243 |
with gr.Row(elem_id='prompt-container').style(equal_height=True):
|
| 244 |
with gr.Column():
|
|
@@ -249,6 +274,13 @@ with gr.Blocks(css='style.css') as demo:
|
|
| 249 |
placeholder='Enter your prompt',
|
| 250 |
elem_id='prompt-text-input',
|
| 251 |
).style(container=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
generate_button = gr.Button('Generate').style(full_width=False)
|
| 253 |
|
| 254 |
with gr.Column() as gallery_view:
|
|
@@ -274,7 +306,12 @@ with gr.Blocks(css='style.css') as demo:
|
|
| 274 |
interactive=False,
|
| 275 |
elem_id='upscaled-image').style(height=640)
|
| 276 |
back_to_selection_button = gr.Button('Back to selection')
|
| 277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
with gr.Accordion('Advanced options',
|
| 279 |
open=False,
|
| 280 |
visible=SHOW_ADVANCED_OPTIONS):
|
|
@@ -385,12 +422,12 @@ with gr.Blocks(css='style.css') as demo:
|
|
| 385 |
gr.HTML(FOOTER)
|
| 386 |
stage1_inputs = [
|
| 387 |
prompt,
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
]
|
| 395 |
stage1_outputs = [
|
| 396 |
gallery,
|
|
@@ -441,6 +478,49 @@ with gr.Blocks(css='style.css') as demo:
|
|
| 441 |
queue=False,
|
| 442 |
)
|
| 443 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
generate_button.click(
|
| 445 |
fn=randomize_seed_fn,
|
| 446 |
inputs=[seed_1, randomize_seed_1],
|
|
@@ -553,7 +633,7 @@ with gr.Blocks(css='style.css') as demo:
|
|
| 553 |
custom_timesteps_2,
|
| 554 |
num_inference_steps_2,
|
| 555 |
prompt,
|
| 556 |
-
|
| 557 |
seed_3,
|
| 558 |
guidance_scale_3,
|
| 559 |
num_inference_steps_3,
|
|
@@ -596,7 +676,7 @@ with gr.Blocks(css='style.css') as demo:
|
|
| 596 |
custom_timesteps_2,
|
| 597 |
num_inference_steps_2,
|
| 598 |
prompt,
|
| 599 |
-
|
| 600 |
seed_3,
|
| 601 |
guidance_scale_3,
|
| 602 |
num_inference_steps_3,
|
|
@@ -612,5 +692,10 @@ with gr.Blocks(css='style.css') as demo:
|
|
| 612 |
],
|
| 613 |
queue=False,
|
| 614 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
|
| 616 |
-
demo.queue(api_open=
|
|
|
|
| 6 |
import os
|
| 7 |
import random
|
| 8 |
import tempfile
|
| 9 |
+
import shortuuid
|
| 10 |
+
from apscheduler.schedulers.background import BackgroundScheduler
|
| 11 |
+
import shutil
|
| 12 |
|
| 13 |
import gradio as gr
|
| 14 |
import torch
|
|
|
|
| 108 |
if not UPLOAD_REPO_ID:
|
| 109 |
return
|
| 110 |
try:
|
| 111 |
+
folder_params = "tmp/results/stage1_params"
|
| 112 |
+
folder_results = "tmp/results/stage1_results"
|
| 113 |
+
|
| 114 |
+
path_params = f"{folder_params}/{save_name}.json"
|
| 115 |
+
path_results = f"{folder_results}/{save_name}.pth"
|
| 116 |
+
|
| 117 |
+
os.makedirs(folder_params, exist_ok=True)
|
| 118 |
+
os.makedirs(folder_results, exist_ok=True)
|
| 119 |
+
|
| 120 |
+
shutil.copy(stage1_param_path, path_params)
|
| 121 |
+
shutil.copy(stage1_result_path, path_results)
|
| 122 |
+
|
| 123 |
except Exception as e:
|
| 124 |
print(e)
|
| 125 |
|
|
|
|
| 146 |
param_file.write(json.dumps(stage2_params))
|
| 147 |
stage2_param_file_hash_name = get_param_file_hash_name(param_file.name)
|
| 148 |
save_name = f'{stage1_param_file_hash_name}_{stage2_param_file_hash_name}'
|
| 149 |
+
|
| 150 |
try:
|
| 151 |
+
folder_params = "tmp/results/stage2_params"
|
| 152 |
+
|
| 153 |
+
os.makedirs(folder_params, exist_ok=True)
|
| 154 |
+
path_params = f"{folder_params}/{save_name}.json"
|
| 155 |
+
shutil.copy(param_file.name, path_params)
|
| 156 |
+
|
| 157 |
if UPLOAD_RESULT_IMAGE:
|
| 158 |
+
folder_results = "tmp/results/stage2_results"
|
| 159 |
+
os.makedirs(folder_results, exist_ok=True)
|
| 160 |
+
path_results = f"{folder_results}/{save_name}.png"
|
| 161 |
+
shutil.copy(stage2_output_path, path_results)
|
| 162 |
+
|
| 163 |
except Exception as e:
|
| 164 |
print(e)
|
| 165 |
|
|
|
|
| 196 |
save_name = f'{stage1_param_file_hash_name}_{stage2_3_param_file_hash_name}'
|
| 197 |
|
| 198 |
try:
|
| 199 |
+
folder_params = "tmp/results/stage2_3_params"
|
| 200 |
+
os.makedirs(folder_params, exist_ok=True)
|
| 201 |
+
path_params = f"{folder_params}/{save_name}.json"
|
| 202 |
+
shutil.copy(param_file.name, path_params)
|
| 203 |
+
|
| 204 |
if UPLOAD_RESULT_IMAGE:
|
| 205 |
+
folder_results = "tmp/results/stage2_3_results"
|
| 206 |
+
os.makedirs(folder_results, exist_ok=True)
|
| 207 |
+
path_results = f"{folder_results}/{save_name}.png"
|
| 208 |
+
shutil.copy(stage2_3_output_path, path_results)
|
|
|
|
| 209 |
except Exception as e:
|
| 210 |
print(e)
|
| 211 |
|
|
|
|
| 228 |
def show_upscaled_view() -> tuple[dict, dict]:
|
| 229 |
return _update_result_view(False)
|
| 230 |
|
| 231 |
+
def upload_files():
|
| 232 |
+
"""Zips files and uploads to dataset. Local data is deleted
|
| 233 |
+
"""
|
| 234 |
+
if os.path.exists("tmp/results") and os.path.isdir("tmp/results"):
|
| 235 |
+
try:
|
| 236 |
+
random_folder = random.randint(0,1000)
|
| 237 |
+
shutil.make_archive("tmp/results", 'zip', "tmp/results")
|
| 238 |
+
hf_api.upload_file(
|
| 239 |
+
path_or_fileobj="tmp/results.zip",
|
| 240 |
+
path_in_repo=f"{random_folder}/results_{shortuuid.uuid()}.zip",
|
| 241 |
+
repo_id=UPLOAD_REPO_ID,
|
| 242 |
+
repo_type="dataset",
|
| 243 |
+
)
|
| 244 |
+
shutil.rmtree("tmp/results")
|
| 245 |
+
except Exception as e:
|
| 246 |
+
print(e)
|
| 247 |
|
| 248 |
examples = [
|
| 249 |
'high quality dslr photo, a photo product of a lemon inspired by natural and organic materials, wooden accents, intricately decorated with glowing vines of led lights, inspired by baroque luxury',
|
|
|
|
| 262 |
]
|
| 263 |
|
| 264 |
with gr.Blocks(css='style.css') as demo:
|
| 265 |
+
gr.Markdown(TITLE)
|
| 266 |
+
gr.Markdown(DESCRIPTION)
|
| 267 |
with gr.Box():
|
| 268 |
with gr.Row(elem_id='prompt-container').style(equal_height=True):
|
| 269 |
with gr.Column():
|
|
|
|
| 274 |
placeholder='Enter your prompt',
|
| 275 |
elem_id='prompt-text-input',
|
| 276 |
).style(container=False)
|
| 277 |
+
negative_prompt = gr.Text(
|
| 278 |
+
label='Negative prompt',
|
| 279 |
+
show_label=False,
|
| 280 |
+
max_lines=1,
|
| 281 |
+
placeholder='Enter a negative prompt',
|
| 282 |
+
elem_id='negative-prompt-text-input',
|
| 283 |
+
).style(container=False)
|
| 284 |
generate_button = gr.Button('Generate').style(full_width=False)
|
| 285 |
|
| 286 |
with gr.Column() as gallery_view:
|
|
|
|
| 306 |
interactive=False,
|
| 307 |
elem_id='upscaled-image').style(height=640)
|
| 308 |
back_to_selection_button = gr.Button('Back to selection')
|
| 309 |
+
with gr.Group(elem_id="share-btn-container"):
|
| 310 |
+
community_icon = gr.HTML(community_icon_html)
|
| 311 |
+
loading_icon = gr.HTML(loading_icon_html)
|
| 312 |
+
share_button = gr.Button(
|
| 313 |
+
"Share to community", elem_id="share-btn")
|
| 314 |
+
share_button.click(None, [], [], _js=share_js)
|
| 315 |
with gr.Accordion('Advanced options',
|
| 316 |
open=False,
|
| 317 |
visible=SHOW_ADVANCED_OPTIONS):
|
|
|
|
| 422 |
gr.HTML(FOOTER)
|
| 423 |
stage1_inputs = [
|
| 424 |
prompt,
|
| 425 |
+
negative_prompt,
|
| 426 |
+
seed_1,
|
| 427 |
+
num_images,
|
| 428 |
+
guidance_scale_1,
|
| 429 |
+
custom_timesteps_1,
|
| 430 |
+
num_inference_steps_1,
|
| 431 |
]
|
| 432 |
stage1_outputs = [
|
| 433 |
gallery,
|
|
|
|
| 478 |
queue=False,
|
| 479 |
)
|
| 480 |
|
| 481 |
+
negative_prompt.submit(
|
| 482 |
+
fn=randomize_seed_fn,
|
| 483 |
+
inputs=[seed_1, randomize_seed_1],
|
| 484 |
+
outputs=seed_1,
|
| 485 |
+
queue=False,
|
| 486 |
+
).then(
|
| 487 |
+
fn=lambda: -1,
|
| 488 |
+
outputs=selected_index_for_stage2,
|
| 489 |
+
queue=False,
|
| 490 |
+
).then(
|
| 491 |
+
fn=show_gallery_view,
|
| 492 |
+
outputs=[
|
| 493 |
+
gallery_view,
|
| 494 |
+
upscale_view,
|
| 495 |
+
],
|
| 496 |
+
queue=False,
|
| 497 |
+
).then(
|
| 498 |
+
fn=update_upscale_button,
|
| 499 |
+
inputs=selected_index_for_stage2,
|
| 500 |
+
outputs=[
|
| 501 |
+
upscale_button,
|
| 502 |
+
upscale_to_256_button,
|
| 503 |
+
],
|
| 504 |
+
queue=False,
|
| 505 |
+
).then(
|
| 506 |
+
fn=model.run_stage1,
|
| 507 |
+
inputs=stage1_inputs,
|
| 508 |
+
outputs=stage1_outputs,
|
| 509 |
+
).success(
|
| 510 |
+
fn=get_param_file_hash_name,
|
| 511 |
+
inputs=stage1_param_path,
|
| 512 |
+
outputs=stage1_param_file_hash_name,
|
| 513 |
+
queue=False,
|
| 514 |
+
).then(
|
| 515 |
+
fn=upload_stage1_result,
|
| 516 |
+
inputs=[
|
| 517 |
+
stage1_param_path,
|
| 518 |
+
stage1_result_path,
|
| 519 |
+
stage1_param_file_hash_name,
|
| 520 |
+
],
|
| 521 |
+
queue=False,
|
| 522 |
+
)
|
| 523 |
+
|
| 524 |
generate_button.click(
|
| 525 |
fn=randomize_seed_fn,
|
| 526 |
inputs=[seed_1, randomize_seed_1],
|
|
|
|
| 633 |
custom_timesteps_2,
|
| 634 |
num_inference_steps_2,
|
| 635 |
prompt,
|
| 636 |
+
negative_prompt,
|
| 637 |
seed_3,
|
| 638 |
guidance_scale_3,
|
| 639 |
num_inference_steps_3,
|
|
|
|
| 676 |
custom_timesteps_2,
|
| 677 |
num_inference_steps_2,
|
| 678 |
prompt,
|
| 679 |
+
negative_prompt,
|
| 680 |
seed_3,
|
| 681 |
guidance_scale_3,
|
| 682 |
num_inference_steps_3,
|
|
|
|
| 692 |
],
|
| 693 |
queue=False,
|
| 694 |
)
|
| 695 |
+
|
| 696 |
+
if UPLOAD_REPO_ID:
|
| 697 |
+
scheduler = BackgroundScheduler()
|
| 698 |
+
scheduler.add_job(func=upload_files, trigger="interval", seconds=60*20)
|
| 699 |
+
scheduler.start()
|
| 700 |
|
| 701 |
+
demo.queue(api_open=False, max_size=MAX_QUEUE_SIZE).launch(debug=DEBUG)
|