File size: 6,929 Bytes
f2de1ca
 
af60005
f2de1ca
 
 
 
af60005
f2de1ca
af60005
f2de1ca
af60005
 
f2de1ca
 
 
af60005
 
f2de1ca
 
af60005
f2de1ca
af60005
f2de1ca
af60005
f2de1ca
 
af60005
f2de1ca
 
af60005
 
 
 
 
f2de1ca
af60005
 
 
 
 
 
 
 
f2de1ca
 
af60005
f2de1ca
af60005
 
 
 
 
 
f2de1ca
 
 
 
 
 
 
 
 
af60005
 
f2de1ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af60005
 
 
f2de1ca
af60005
 
 
 
 
f2de1ca
309b641
 
 
 
 
f86b482
f2de1ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af60005
 
 
 
 
 
 
 
 
f86b482
af60005
f2de1ca
 
 
 
 
 
af60005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2de1ca
af60005
f2de1ca
af60005
 
 
f2de1ca
af60005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2de1ca
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import os
import re
import random
import numpy as np

# !!! spaces must be imported before torch/CUDA
import spaces

from huggingface_hub import login
from diffusers import DiffusionPipeline
import gradio as gr
import torch

from utils import QPipeline


device = "cuda" if torch.cuda.is_available() else "cpu"

login(token=os.environ["HF_TOKEN"])
model_repo_id = os.environ["MODEL_ID"]

torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

pipe = QPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype).to(device)

MAX_SEED = 65535
MAX_IMAGE_SIZE = 128


@spaces.GPU  # Enable ZeroGPU if needed
def infer(
    prompt,
    negative_prompt,
    seed,
    randomize_seed,
    num_inference_steps=10,
    progress=gr.Progress(track_tqdm=True),
):
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)

    generator = torch.Generator().manual_seed(seed)

    image = pipe(
        [prompt],
        batch_size=1,
        generator=generator,
        num_inference_steps=num_inference_steps
    ).images[0]

    return image, seed


examples = [
    "Structure: (LR 文 英). Style: style001",
    "Structure: (TL 广 東). Style: style028",
    "Structure: (TB 艹 (LR 禾 魚)). Style: style015",
    "Structure: (TB 敬 音). Style: style013",
    "Structure: (LR 釒 馬). Style: style018",
    "Structure: (BL 走 羽). Style: style022",
    "Structure: (LR 羊 大). Style: style005",
    "Structure: (LR 鹿 孚). Style: style017",
    "Structure: (OI 口 也). Style: style002",
]

# Map style images to style names (use real image files later)
style_options = {
    "images/style001.png": "style001",
    "images/style002.png": "style002",
    "images/style003.png": "style003",
    "images/style004.png": "style004",
    "images/style005.png": "style005",
    "images/style006.png": "style006",
    "images/style007.png": "style007",
    "images/style008.png": "style008",
    "images/style009.png": "style009",
    "images/style010.png": "style010",
    "images/style011.png": "style011",
    "images/style012.png": "style012",
    "images/style013.png": "style013",
    "images/style014.png": "style014",
    "images/style015.png": "style015",
    # "images/style016.png": "style016", very similar to 002
    "images/style017.png": "style017",
    "images/style018.png": "style018",
    "images/style019.png": "style019",
    "images/style020.png": "style020",
    "images/style021.png": "style021",
    "images/style022.png": "style022",
    "images/style023.png": "style023",
    "images/style024.png": "style024",
    "images/style025.png": "style025",
    "images/style026.png": "style026",
    "images/style027.png": "style027",
    "images/style028.png": "style028",
    "images/style029.png": "style029",
}


def apply_style_on_click(evt: gr.SelectData, prompt_text):
    index = evt.index
    style_label = list(style_options.values())[index]

    if re.search(r"Style: [^\n]+", prompt_text):
        return re.sub(r"Style: [^\n]+", f"Style: {style_label}", prompt_text)
    else:
        return prompt_text.strip() + f" Style: {style_label}"


# CSS for fixing Gallery layout
css = """
#col-container {
    margin: 0 auto;
    max-width: 800px;
}
"""

with gr.Blocks(css=css) as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown(" # NeoChar ")
        gr.Markdown(""" - Generate New Chineses Characters (Hanzi/Kanji)
                        - Combine components in a creative way
                        - Write them in style
                        - A Gen-AI's implementation of [Lin Yutang's Ming-Kwai Typewriter](https://thereader.mitpress.mit.edu/the-uncanny-keyboard/)
                        - [README](https://huggingface.co/spaces/lqume/neochar/blob/main/README.md) for more""")
        gr.Markdown(" ## QuickStart: select an example, edit components, pick a style, then 'generate'")

        gr.HTML("""
        <style>
        .gallery-container .gallery-item {
            width: 60px !important;
            height: 60px !important;
            padding: 0 !important;
            margin: 4px !important;
            border-radius: 4px;
            overflow: hidden;
            background: none !important;
            box-shadow: none !important;
        }

        .gallery-container .gallery-item img {
            width: 64px !important;
            height: 64px !important;
            object-fit: cover;
            display: block;
            margin: auto;
        }

        .gallery-container button {
            all: unset !important;
            padding: 0 !important;
            margin: 0 !important;
            border: none !important;
            background: none !important;
            box-shadow: none !important;
        }

        .gallery__modal,
        .gallery-container .preview,
        .gallery-container .gallery-item:focus-visible {
            display: none !important;
            pointer-events: none !important;
        }
        </style>
        """)

        gallery = gr.Gallery(
            value=list(style_options.keys()),
            label="Click any image",
            columns=7,
            allow_preview=False,
            height=None,
            elem_classes=["gallery-container"]
        )

        with gr.Row():
            prompt = gr.Text(
                label="Prompt",
                show_label=False,
                max_lines=1,
                placeholder="Enter your prompt",
                container=False,
            )
            run_button = gr.Button("Generate", scale=0, variant="primary")

        gallery.select(
            fn=apply_style_on_click,
            inputs=[prompt],
            outputs=prompt
        )

        result = gr.Image(label="Result", show_label=False)

        with gr.Accordion("Advanced Settings", open=False):
            negative_prompt = gr.Text(
                label="Negative prompt",
                max_lines=1,
                placeholder="Enter a negative prompt",
                visible=False,
            )

            seed = gr.Slider(
                label="Seed",
                minimum=0,
                maximum=MAX_SEED,
                step=1,
                value=0,
            )

            randomize_seed = gr.Checkbox(label="Randomize seed", value=True)

            with gr.Row():
                num_inference_steps = gr.Slider(
                    label="Number of inference steps",
                    minimum=1,
                    maximum=20,
                    step=1,
                    value=10,
                )

        gr.Examples(examples=examples, inputs=[prompt])

    gr.on(
        triggers=[run_button.click, prompt.submit],
        fn=infer,
        inputs=[
            prompt,
            negative_prompt,
            seed,
            randomize_seed,
            num_inference_steps,
        ],
        outputs=[result, seed],
    )

if __name__ == "__main__":
    demo.launch()