P01yH3dr0n commited on
Commit
4debf12
1 Parent(s): e63761a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +148 -0
app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import datetime
3
+
4
+ import toml
5
+ import gradio as gr
6
+ from pnginfo import read_info_from_image, send_paras
7
+
8
+ from utils import set_token, generate_novelai_image, image_from_bytes
9
+ from PIL import PngImagePlugin
10
+
11
+ today_count = 0
12
+ today = datetime.date.today().strftime('%Y-%m-%d')
13
+
14
+ def get_count():
15
+ global today_count, today
16
+ now = datetime.date.today().strftime('%Y-%m-%d')
17
+ if now != today:
18
+ today = now
19
+ today_count = 0
20
+ return "Total number of today's image generation: " + str(today_count)
21
+
22
+ def control_ui():
23
+ prompt = gr.TextArea(label="Prompt", lines=3)
24
+ quality_tags = gr.TextArea(
25
+ label="Quality Tags", lines=1,
26
+ value=client_config['default_quality'],
27
+ )
28
+ neg_prompt = gr.TextArea(
29
+ label="Negative Prompt", lines=1,
30
+ value=client_config['default_neg'],
31
+ )
32
+ with gr.Row():
33
+ sampler = gr.Dropdown(
34
+ choices=[
35
+ "k_euler", "k_euler_ancestral", "k_dpmpp_2s_ancestral",
36
+ "k_dpmpp_2m", "k_dpmpp_sde", "ddim_v3"
37
+ ],
38
+ value="k_euler",
39
+ label="Sampler",
40
+ interactive=True
41
+ )
42
+ scale = gr.Slider(label="Scale", value=5.0, minimum=1, maximum=10, step=0.1)
43
+ steps = gr.Slider(label="Steps", value=28, minimum=1, maximum=28, step=1)
44
+ with gr.Row():
45
+ seed = gr.Number(label="Seed", value=-1, step=1, maximum=2**32-1, minimum=-1, scale=3)
46
+ rand_seed = gr.Button('🎲️', scale=1)
47
+ reuse_seed = gr.Button('♻️', scale=1)
48
+ with gr.Row():
49
+ width = gr.Slider(label="Width", value=1024, minimum=64, maximum=2048, step=64)
50
+ height = gr.Slider(label="Height", value=1024, minimum=64, maximum=2048, step=64)
51
+ with gr.Row():
52
+ with gr.Column():
53
+ with gr.Accordion('Advanced Gen Setting', open=False):
54
+ scheduler = gr.Dropdown(
55
+ choices=[
56
+ "native", "karras", "exponential", "polyexponential"
57
+ ],
58
+ value="native",
59
+ label="Scheduler",
60
+ interactive=True
61
+ )
62
+ with gr.Row():
63
+ smea = gr.Checkbox(False, label="SMEA")
64
+ dyn = gr.Checkbox(False, label="SMEA DYN")
65
+ with gr.Row():
66
+ dyn_threshold = gr.Checkbox(False, label="Dynamic Thresholding")
67
+ cfg_rescale = gr.Slider(0, 1, 0, step=0.01, label="CFG rescale")
68
+
69
+ with gr.Column():
70
+ gr.Textbox(value=get_count, label='Usage count', every=10)
71
+ save = gr.Checkbox(value=True, label='Always save all generated images')
72
+
73
+ gen_btn = gr.Button(value="Generate", variant="primary")
74
+ rand_seed.click(fn=lambda: -1, inputs=None, outputs=seed)
75
+ width.change(lambda w, h: h if w*h<=1024*1024 else (1024*1024//w//64)*64, [width, height], height)
76
+ height.change(lambda w, h: w if w*h<=1024*1024 else (1024*1024//h//64)*64, [width, height], width)
77
+ return gen_btn,[prompt, quality_tags, neg_prompt, seed, scale, width, height, steps, sampler, scheduler, smea, dyn, dyn_threshold, cfg_rescale], [save, rand_seed, reuse_seed]
78
+
79
+
80
+ async def generate(prompt, quality_tags, neg_prompt, seed, scale, width, height, steps, sampler, scheduler, smea, dyn, dyn_threshold, cfg_rescale, save):
81
+ global today_count
82
+ img_data, payload = await generate_novelai_image(
83
+ f"{prompt}, {quality_tags}", neg_prompt, seed, scale,
84
+ width, height, steps, sampler, scheduler,
85
+ smea, dyn, dyn_threshold, cfg_rescale
86
+ )
87
+ if not isinstance(img_data, bytes):
88
+ return None
89
+ today_count += 1
90
+ img = image_from_bytes(img_data)
91
+
92
+ return img, payload
93
+
94
+
95
+ def preview_ui():
96
+ with gr.Blocks(css='#preview_image { height: 100%;}') as page:
97
+ h_slider = gr.Slider(label="Height", value=500, minimum=100, maximum=1200, step=10)
98
+ image = gr.Image(elem_id='preview_image', interactive=False, height=500)
99
+ info = gr.JSON(value={}, label="Submitted Payload")
100
+ h_slider.change(lambda h: gr.Image(height=h), h_slider, image)
101
+ return image, info
102
+
103
+
104
+ def main_ui():
105
+ with gr.Blocks() as page:
106
+ with gr.Row(variant="panel"):
107
+ with gr.Column():
108
+ gen_btn, paras, others = control_ui()
109
+ with gr.Column():
110
+ image, info = preview_ui()
111
+ gen_btn.click(generate, paras + [others[0]], [image, info])
112
+ others[2].click(lambda o, s: o if len(s) == 0 else s['parameters']['seed'], inputs=[paras[3], info], outputs=paras[3])
113
+ return page, paras
114
+
115
+
116
+ def util_ui():
117
+ with gr.Blocks(analytics_enabled=False) as page:
118
+ with gr.Row(equal_height=False):
119
+ with gr.Column(variant='panel'):
120
+ image = gr.Image(label="Source", sources=["upload"], interactive=True, type="pil")
121
+ with gr.Column(variant='panel'):
122
+ info = gr.HTML()
123
+ items = gr.JSON(value=None, visible=False)
124
+ png2main = gr.Button('Send to txt2img')
125
+ return page, png2main, items, info, image
126
+
127
+ def ui():
128
+ with gr.Blocks(title="NAI Client") as website:
129
+ with gr.Tabs():
130
+ with gr.TabItem("Main", elem_id="client_ui_main"):
131
+ _, paras = main_ui()
132
+ with gr.TabItem("PNG Info"):
133
+ _, png2main, png_items, info, image = util_ui()
134
+ png2main.click(fn=send_paras,
135
+ inputs=[png_items] + paras,
136
+ outputs=paras)
137
+ png2main.click(fn=None,
138
+ js="(x) => { if (x !== null) document.getElementById('client_ui_main-button').click(); "
139
+ "return null; }",
140
+ inputs=image)
141
+ image.change(read_info_from_image, inputs=image, outputs=[info, png_items])
142
+ return website
143
+
144
+
145
+ if __name__ == '__main__':
146
+ website = ui()
147
+ website.queue()
148
+ website.launch(auth=(os.environ.get('account'), os.environ.get('password')))