ZachNagengast commited on
Commit
1890401
·
0 Parent(s):

Add models and logic

Browse files
.gitattributes ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ NotoColorEmoji.ttf filter=lfs diff=lfs merge=lfs -text
37
+ TwitterColorEmoji.ttf filter=lfs diff=lfs merge=lfs -text
38
+ AppleColorEmoji.ttc filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ __pycache__
2
+ .DS_Store
AppleColorEmoji.ttc ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4be60f2801f40c651abd9c457b957114c88cc9385ac132356b588cc4a9ba2a00
3
+ size 189791856
NotoColorEmoji.ttf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5074922e76b4fab18cb8dada2448d09a26ec2df978843ebe34aca29348f313df
3
+ size 23709552
README.md ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Bigmojis Upscaler
3
+ emoji: ☝️
4
+ colorFrom: yellow
5
+ colorTo: gray
6
+ sdk: gradio
7
+ sdk_version: 3.50.2
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
TwitterColorEmoji.ttf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:aa0a9af2231807eebc35f9069ce9404019d1e10311cbf5d3c35277d6ece87463
3
+ size 14477348
app.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import random
7
+
8
+ import gradio as gr
9
+ import numpy as np
10
+ from PIL import Image, ImageDraw, ImageFont, ImageOps
11
+ import uuid
12
+
13
+ import argparse
14
+ import cv2
15
+ import glob
16
+ import os
17
+ from basicsr.archs.rrdbnet_arch import RRDBNet
18
+ from basicsr.utils.download_util import load_file_from_url
19
+
20
+ from realesrgan import RealESRGANer
21
+ from realesrgan.archs.srvgg_arch import SRVGGNetCompact
22
+
23
+ DESCRIPTION = '''<center><h1>☝️ Bigmojis ☝️</h1></span>
24
+ <span font-size:16px;">An emoji upscaler, for when you <i>really</i> mean it</span>
25
+ </center>
26
+
27
+ Space by [ZachNagengast](https://huggingface.co/ZachNagengast)
28
+
29
+ [Follow me on Twitter!](https://twitter.com/ZachNagengast)
30
+
31
+ Upscaler models provided by the [BasicSR](https://github.com/XPixelGroup/BasicSR) python package.
32
+
33
+ Emojis by [Apple](https://www.apple.com).
34
+ '''
35
+
36
+ upsampler = None
37
+ netscale = 4
38
+
39
+ # load model
40
+ def load_model(model_type='RealESRGAN_x4plus'):
41
+ if model_type == 'RealESRGAN_x4plus': # x4 RRDBNet model
42
+ model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
43
+ netscale = 4
44
+ file_url = f'models/{model_type}.pth'
45
+ elif model_type == 'RealESRNet_x4plus': # x4 RRDBNet model
46
+ model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
47
+ netscale = 4
48
+ file_url = f'models/{model_type}.pth'
49
+ elif model_type == 'RealESRGAN_x4plus_anime_6B': # x4 RRDBNet model with 6 blocks
50
+ model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=6, num_grow_ch=32, scale=4)
51
+ netscale = 4
52
+ file_url = f'models/{model_type}.pth'
53
+ elif model_type == 'RealESRGAN_x2plus': # x2 RRDBNet model
54
+ model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
55
+ netscale = 2
56
+ file_url = f'models/{model_type}.pth'
57
+ elif model_type == 'realesr-animevideov3': # x4 VGG-style model (XS size)
58
+ model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=16, upscale=4, act_type='prelu')
59
+ netscale = 4
60
+ file_url = f'models/{model_type}.pth'
61
+
62
+ ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
63
+ model_path = os.path.join(ROOT_DIR, file_url)
64
+
65
+ upsampler = RealESRGANer(
66
+ scale=netscale,
67
+ model_path=model_path,
68
+ model=model,
69
+
70
+ )
71
+
72
+ return upsampler
73
+
74
+ def switch_model(model_type):
75
+ global upsampler
76
+ upsampler = load_model(model_type)
77
+
78
+ def save_image(img):
79
+ unique_name = str(uuid.uuid4()) + '.png'
80
+ img.save(unique_name)
81
+ return unique_name
82
+
83
+ def generate_emoji_image(
84
+ prompt: str,
85
+ font_type: str = "Apple",
86
+ background: str = "white",
87
+ size: int = 160,
88
+ padding: int = 0,
89
+ ):
90
+ font_name = "AppleColorEmoji.ttc"
91
+ if font_type == "Google":
92
+ font_name = "NotoColorEmoji.ttf"
93
+ elif font_type == "Twitter":
94
+ font_name = "TwitterColorEmoji.ttf"
95
+
96
+ font = ImageFont.truetype(
97
+ font_name, size=160
98
+ )
99
+
100
+ if background == "transparent":
101
+ background = (0, 0, 0, 0)
102
+
103
+ im = Image.new("RGBA", (size, size), background)
104
+ d = ImageDraw.Draw(im)
105
+
106
+ d.text((padding, padding), prompt, fill='white', embedded_color=True, font=font)
107
+ return im
108
+
109
+ def generate_preview(
110
+ prompt: str,
111
+ font_type: str = "Apple",
112
+ background: str = "white",
113
+ ):
114
+ im = generate_emoji_image(prompt, font_type, background, 180, 10)
115
+ return im
116
+
117
+
118
+ def upscale_image(img):
119
+ print(f"Upscaling...")
120
+ cv2_im = np.array(img)
121
+ output, _ = upsampler.enhance(cv2_im, outscale=netscale)
122
+ return Image.fromarray(output)
123
+
124
+ def generate_upscaled_emoji(
125
+ prompt: str,
126
+ font_type: str = "Apple",
127
+ background: str = "white",
128
+ model: str = "RealESRGAN_x4plus",
129
+ ):
130
+ padding = 10 # prevents border artifacts
131
+ im = generate_emoji_image(prompt, font_type, background, 160+padding*2, padding)
132
+
133
+ result = upscale_image(im)
134
+
135
+ # crop padding
136
+ cropped = ImageOps.crop(result, border=padding)
137
+
138
+ return cropped
139
+
140
+
141
+
142
+ examples = ['🤗', '😂', '🦙', '👍', '💸', '✨', '🚀', '🫱🏼‍🫲🏾', '🎉', '😎', '🛸', '🍩', '🦜', '🗿', '🧌', '🦋', '🆙']
143
+
144
+ with gr.Blocks(css="style.css") as demo:
145
+ gr.Markdown(DESCRIPTION)
146
+ with gr.Row():
147
+ with gr.Column():
148
+ with gr.Group():
149
+ with gr.Row():
150
+ prompt = gr.Text(
151
+ label="Emoji",
152
+ elem_id="prompt-text",
153
+ show_label=True,
154
+ max_lines=1,
155
+ placeholder="Enter your emoji",
156
+ container=False,
157
+ )
158
+ run_button = gr.Button("Upscale", scale=0)
159
+ with gr.Row():
160
+ preview = gr.Image(label="Glyph Bitmap", show_label=True, scale=1)
161
+ background = gr.Radio(choices=["transparent", "white", "black", "red", "green", "blue"], value="transparent", label="Background Color")
162
+ font_type = gr.Radio(choices=["Apple", "Google", "Twitter"], value="Apple", label="Type", visible=True)
163
+ with gr.Row():
164
+ gr.Examples(
165
+ examples=examples,
166
+ inputs=prompt,
167
+ outputs=[preview],
168
+ fn=generate_emoji_image,
169
+ cache_examples=False,
170
+ )
171
+ with gr.Row():
172
+ model = gr.Radio(choices=['RealESRNet_x4plus','RealESRGAN_x4plus','RealESRGAN_x4plus_anime_6B','RealESRGAN_x2plus', 'realesr-animevideov3'], value="RealESRNet_x4plus", label="Model")
173
+ with gr.Column():
174
+ upscaled = gr.Image(label="Upscaled", value="huggingface-big.png")
175
+ gr.DuplicateButton(
176
+ value="Duplicate Space for private use",
177
+ elem_id="duplicate-button",
178
+ visible=True,
179
+ )
180
+
181
+ prompt.change(generate_preview, inputs=[prompt, font_type, background], outputs=[preview], api_name="run")
182
+ background.change(generate_preview, inputs=[prompt, font_type, background], outputs=[preview], api_name="run")
183
+ font_type.change(generate_preview, inputs=[prompt, font_type, background], outputs=[preview], api_name="run")
184
+ model.change(switch_model, inputs=[model], outputs=[])
185
+ prompt.submit(generate_upscaled_emoji, inputs=[prompt, font_type, background, model], outputs=[upscaled], api_name="run")
186
+ run_button.click(generate_upscaled_emoji, inputs=[prompt, font_type, background, model], outputs=[upscaled], api_name="run")
187
+
188
+ if __name__ == "__main__":
189
+ upsampler = load_model('RealESRNet_x4plus')
190
+
191
+ demo.queue(max_size=20).launch()
huggingface-big.png ADDED
models/RealESRGAN_x2plus.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:49fafd45f8fd7aa8d31ab2a22d14d91b536c34494a5cfe31eb5d89c2fa266abb
3
+ size 67061725
models/RealESRGAN_x4plus.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4fa0d38905f75ac06eb49a7951b426670021be3018265fd191d2125df9d682f1
3
+ size 67040989
models/RealESRGAN_x4plus_anime_6B.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f872d837d3c90ed2e05227bed711af5671a6fd1c9f7d7e91c911a61f155e99da
3
+ size 17938799
models/RealESRNet_x4plus.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a820b9bde89a874d7599d545567308ce6c128fc8754a53208eda016d40aa81df
3
+ size 67040989
models/realesr-animevideov3.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b8a8376811077954d82ca3fcf476f1ac3da3e8a68a4f4d71363008000a18b75d
3
+ size 2504012
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ basicsr>=1.4.2
2
+ facexlib>=0.2.5
3
+ gfpgan>=1.3.5
4
+ numpy
5
+ opencv-python
6
+ Pillow
7
+ torch>=1.7
8
+ torchvision
9
+ transformers
10
+ tqdm
11
+ facexlib
12
+ gfpgan
13
+ basicsr
style.css ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ }
4
+