File size: 1,761 Bytes
4947b46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from upcunet import *
import oneflow
import numpy as np
import gradio as gr
from time import time

class App:
    device = None
    models = {}

    def __init__(self):
        self.device = 'cuda' if oneflow.cuda.is_available() else 'cpu'
        print(f'Using device {self.device}')

        # read weights folder
        weights = os.listdir('weights')
        for weight in weights:
            scale = int(weight[2:3])
            self.models[weight] = RealWaifuUpScaler(scale, f'weights/{weight}', False, self.device)
            print(f'Loaded model {weight}')
    
    def get_models(self):
        return list(self.models.keys())
    
    def upscale(self, input, model, tile = 0):
        if model not in self.models:
            return None
        
        input = np.array(input)
        print(f'Upscaling image with model {model} and tile size {tile}')
        t0 = time()
        result = self.models[model](input, tile)
        t1 = time()
        print(f'Upscaling complete. Completion time: {t1 - t0}. Upscaled: {input.shape} -> {result.shape}.')
        return result
    
    def run(self):
        input = gr.Image(type='pil', label='Original Image')

        model = gr.Dropdown(
            self.get_models(),
            label='Model',
            value=self.get_models()[0]
        )

        #tile = gr.Slider(
        #    minimum=0,
        #    maximum=0,
        #    value=0,
        #    step=1,
        #    label='Tile Size'
        #)

        inputs = [input, model]
        outputs = 'image'

        interface = gr.Interface(
            self.upscale,
            inputs,
            outputs,
            allow_flagging='never'
        )

        interface.launch()

if __name__ == '__main__':
    app = App()
    app.run()