File size: 2,327 Bytes
f74caab
 
 
 
7f5aa58
23553ba
f74caab
 
54005c9
 
bdb95dc
54005c9
f74caab
 
 
23553ba
 
 
7b74cf4
a06d38b
23553ba
 
a06d38b
23553ba
a06d38b
23553ba
 
f74caab
 
3bbaf85
f74caab
 
 
 
 
 
 
 
 
 
 
052031d
f74caab
 
 
 
 
 
 
 
 
 
54005c9
f74caab
 
 
 
 
3bbaf85
f74caab
 
3bbaf85
f74caab
 
 
 
 
 
 
 
 
 
 
 
 
7f5aa58
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import gradio as gr
import spaces
from image_gen_aux import UpscaleWithModel
from image_gen_aux.utils import load_image
from fastapi.middleware.cors import CORSMiddleware
import math

MODELS = {
    "4xNomosWebPhotoRealPLKSR": "Phips/4xNomosWebPhoto_RealPLKSR",
    "4xRealESRGAN": "luca115/4xRealESRGAN",
    "4xRealHATGANSharper": "luca115/Real_HAT_GAN_SHARPER",
    "4xSwinIRLarge": "luca115/4xSwinIRLarge",
}


def get_duration(
    image, model_selection
):
    width, height = image.size
    pixel = width * height

    if model_selection in ["4xNomosWebPhotoRealPLKSR", "4xRealESRGAN"]:
        return math.ceil((pixel * 10) / 1_000_000) + 3
    else:
        return math.ceil((pixel * 30) / 1_000_000) + 3

@spaces.GPU(duration = get_duration)
def upscale_image(image, model_selection):
    original = load_image(image)
    
    upscaler = UpscaleWithModel.from_pretrained(MODELS[model_selection]).to("cuda")
    image = upscaler(original, tiling=True, tile_width=1024, tile_height=1024)

    return original, image


def clear_result():
    return gr.update(value=None)


title = """<h1 align="center">Best Upscaling Models</h1>
<div align="center">A collection of my favorite non-diffusion-based upscaling models. For diffusion-based methods, check out these <a href="https://upsampler.com">creative image upscalers and enhancers</a>.</div>
"""

with gr.Blocks() as demo:
    gr.HTML(title)
    with gr.Row():
        with gr.Column():
            input_image = gr.Image(type="pil", label="Input Image")

            model_selection = gr.Dropdown(
                choices=list(MODELS.keys()),
                value="4xSwinIRLarge",
                label="Model",
            )

            run_button = gr.Button("Upscale")
        with gr.Column():
            result = gr.ImageSlider(
                interactive=False,
                label="Generated Image",
                format="png"
            )

    run_button.click(
        fn=clear_result,
        inputs=None,
        outputs=result,
    ).then(
        fn=upscale_image,
        inputs=[input_image, model_selection],
        outputs=result,
    )


app, local_url, share_url = demo.launch(share=True)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], 
    allow_credentials=True,
    allow_methods=["*"], 
    allow_headers=["*"], 
)