ESRGAN-MANGA / app.py
0x90e's picture
UI improvements and easier photo download
d570bef
raw
history blame
3.08 kB
import os
import gradio as gr
import util
from run_cmd import run_cmd
from random import randint
from PIL import Image
import tempfile
temp_path = tempfile.gettempdir()
is_colab = util.is_google_colab()
run_cmd("pip install pngquant")
def inference(img, size, type):
_id = randint(1, 10000)
INPUT_DIR = os.path.join(temp_path, f"input_image{str(_id)}")
OUTPUT_DIR = os.path.join(temp_path, f"output_image{str(_id)}")
img_in_path = os.path.join(INPUT_DIR, "1.jpg")
img_out_path = os.path.join(OUTPUT_DIR, f"1_{size}.png")
run_cmd(f"rm -rf {INPUT_DIR}")
run_cmd(f"rm -rf {OUTPUT_DIR}")
run_cmd(f"mkdir {INPUT_DIR}")
run_cmd(f"mkdir {OUTPUT_DIR}")
img.save(img_in_path, "PNG")
if type == "Manga":
run_cmd(f"python inference_manga_v2.py {img_in_path} {img_out_path}")
else:
run_cmd(f"python inference.py {img_in_path} {img_out_path} {type}")
img_out = Image.open(img_out_path)
if size == "x2":
img_out = img_out.resize((img_out.width // 2, img_out.height // 2), resample=Image.BICUBIC)
#img_out.save(img_out_path, optimize=True) # Add more optimizations
#img_out = Image.open(img_out_path)
# Remove input and output image
run_cmd(f"rm -f {img_in_path}")
#run_cmd(f"rm -f {img_out_path}")
out_file.update(value=img_out_path, visible=True)
return img_out, gr.File.update(value=img_out_path, visible=True)
css = '''
.file-preview {
overflow: hidden !important;
margin: 5px 0 !important;
padding: 0 10px !important;
}
.file-preview div div:nth-child(2) {
flex-grow: 1 !important;
}
.file-preview div div:nth-child(3) {
text-align: right !important;
}
'''
title = "ESRGAN Upscaling With Custom Models"
description = "This space uses old ESRGAN architecture to upscale images, using models made by the community."
article = "<p><a href='https://upscale.wiki/wiki/Model_Database'>Model Database</a></p>"
with gr.Blocks(title=title, css=css) as demo:
gr.Markdown(
f"""
# {title}
{description}
""")
with gr.Box():
with gr.Row():
with gr.Column():
input_image = gr.Image(type="pil", label="Input")
upscale_size = gr.Radio(["x4", "x2"], label="Upscale by:", value="x4")
upscale_type = gr.Radio(["Manga", "Anime", "General"], label="Select the type of picture you want to upscale:", value="Manga")
with gr.Row():
upscale_btn = gr.Button(value="Upscale", variant="primary")
with gr.Column():
output_image = gr.Image(type="filepath", interactive=False, label="Upscaled image", )
with gr.Row():
out_file = gr.File(interactive=False, show_label=False, visible=False)
gr.HTML(value=article)
upscale_btn.click(inference, inputs=[input_image, upscale_size, upscale_type], outputs=[output_image, out_file])
demo.queue()
demo.launch(debug=is_colab, share=is_colab, inline=is_colab)