P01yH3dr0n commited on
Commit
a6d790c
1 Parent(s): 5893438

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -30
app.py CHANGED
@@ -1,11 +1,15 @@
1
  import os
2
  import datetime
3
 
 
4
  import gradio as gr
5
- from pnginfo import read_info_from_image, send_paras
6
 
 
 
7
  from utils import set_token, generate_novelai_image, image_from_bytes
8
 
 
9
  today_count = 0
10
  today = datetime.date.today().strftime('%Y-%m-%d')
11
 
@@ -15,17 +19,17 @@ def get_count():
15
  if now != today:
16
  today = now
17
  today_count = 0
18
- return "Total number of today's image generation: " + str(today_count)
19
 
20
  def control_ui():
21
- prompt = gr.TextArea(elem_id='txt2img_prompt', label="Prompt", lines=3, value="{breast expansion},{gigantic breasts},[artist:ningen_mame],artist:ciloranko,[artist:sho_(sho_lwlw)],[artist:foifoi (marfoyfoyfoy)],1girl,skinny,narrow waist,")
22
  quality_tags = gr.TextArea(
23
- elem_id='txt2img_qua_prompt', label="Quality Tags", lines=1,
24
- value="best quality, amazing quality, very aesthetic, absurdres",
25
  )
26
  neg_prompt = gr.TextArea(
27
- elem_id='txt2img_neg_prompt', label="Negative Prompt", lines=1,
28
- value="wide hips,thick thighs,plump,lowres, {bad}, error, fewer, extra, missing, worst quality, jpeg artifacts, bad quality, watermark, unfinished, displeasing, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract],pubic hair",
29
  )
30
  with gr.Row():
31
  sampler = gr.Dropdown(
@@ -34,27 +38,27 @@ def control_ui():
34
  "k_dpmpp_2m", "k_dpmpp_sde", "ddim_v3"
35
  ],
36
  value="k_euler",
37
- label="Sampler",
38
  interactive=True
39
  )
40
- scale = gr.Slider(label="Scale", value=5.0, minimum=1, maximum=10, step=0.1)
41
- steps = gr.Slider(label="Steps", value=28, minimum=1, maximum=28, step=1)
42
  with gr.Row():
43
- seed = gr.Number(label="Seed", value=-1, step=1, maximum=2**32-1, minimum=-1, scale=3)
44
  rand_seed = gr.Button('🎲️', scale=1)
45
  reuse_seed = gr.Button('♻️', scale=1)
46
  with gr.Row():
47
- width = gr.Slider(label="Width", value=1024, minimum=64, maximum=2048, step=64)
48
- height = gr.Slider(label="Height", value=1024, minimum=64, maximum=2048, step=64)
49
  with gr.Row():
50
  with gr.Column():
51
  with gr.Accordion('风格迁移', open=False):
52
- ref_image = gr.Image(label="上传图片", value=None, interactive=True, type="pil")
53
  info_extract = gr.Slider(label='参考信息提取', value=1, minimum=0, maximum=1, step=0.1)
54
  ref_str = gr.Slider(label='参考强度', value=0.6, minimum=0, maximum=1, step=0.1)
55
  reuse_img_vibe = gr.Button(value='使用上一次生成的图片')
56
  with gr.Accordion('图生图', open=False):
57
- i2i_image = gr.Image(label="上传图片", value=None, interactive=True, type="pil")
58
  i2i_str = gr.Slider(label='去噪强度', value=0.7, minimum=0, maximum=0.99, step=0.01)
59
  i2i_noise = gr.Slider(label='噪声', value=0, minimum=0, maximum=1, step=0.1)
60
  reuse_img_i2i = gr.Button(value='使用上一次生成的图片')
@@ -69,7 +73,7 @@ def control_ui():
69
  '''
70
  with gr.Row():
71
  with gr.Column():
72
- with gr.Accordion('Advanced Gen Setting', open=False):
73
  scheduler = gr.Dropdown(
74
  choices=[
75
  "native", "karras", "exponential", "polyexponential"
@@ -85,15 +89,14 @@ def control_ui():
85
  dyn_threshold = gr.Checkbox(False, label="Dynamic Thresholding")
86
  cfg_rescale = gr.Slider(0, 1, 0, step=0.01, label="CFG rescale")
87
  with gr.Column():
88
- gr.Textbox(value=get_count, label='Usage count', every=10)
89
- save = gr.Checkbox(value=False, label='Always save all generated images', visible=False)
90
- gen_btn = gr.Button(value="Generate", variant="primary")
91
  rand_seed.click(fn=lambda: -1, inputs=None, outputs=seed)
92
  width.change(lambda w, h: h if w*h<=1024*1024 else (1024*1024//w//64)*64, [width, height], height)
93
  height.change(lambda w, h: w if w*h<=1024*1024 else (1024*1024//h//64)*64, [width, height], width)
94
  return gen_btn,[prompt, quality_tags, neg_prompt, seed, scale, width, height, steps, sampler, scheduler, smea, dyn, dyn_threshold, cfg_rescale, ref_image, info_extract, ref_str, i2i_image, i2i_str, i2i_noise], [save, rand_seed, reuse_seed, reuse_img_vibe, reuse_img_i2i]
95
 
96
-
97
  def generate(prompt, quality_tags, neg_prompt, seed, scale, width, height, steps, sampler, scheduler, smea, dyn, dyn_threshold, cfg_rescale, ref_image, info_extract, ref_str, i2i_image, i2i_str, i2i_noise, save):
98
  global today_count
99
  set_token(os.environ.get('token'))
@@ -104,19 +107,28 @@ def generate(prompt, quality_tags, neg_prompt, seed, scale, width, height, steps
104
  i2i_image, i2i_str, i2i_noise
105
  )
106
  if not isinstance(img_data, bytes):
107
- return None, payload
108
  today_count += 1
109
  img = image_from_bytes(img_data)
 
 
 
 
 
 
 
 
 
 
 
110
  return img, payload
111
 
112
-
113
  def preview_ui():
114
  with gr.Blocks(css='#preview_image { height: 100%;}'):
115
  image = gr.Image(elem_id='preview_image', interactive=False, type='pil')
116
- info = gr.JSON(value={}, label="Submitted Payload")
117
  return image, info
118
 
119
-
120
  def main_ui():
121
  with gr.Blocks() as page:
122
  with gr.Row(variant="panel"):
@@ -130,34 +142,41 @@ def main_ui():
130
  others[4].click(lambda i: i, inputs=image, outputs=paras[17])
131
  return page, paras[:14]
132
 
133
-
134
  def util_ui():
135
  with gr.Blocks(analytics_enabled=False) as page:
136
  with gr.Row(equal_height=False):
137
  with gr.Column(variant='panel'):
138
- image = gr.Image(label="Source", sources=["upload"], interactive=True, type="pil")
139
  with gr.Column(variant='panel'):
140
  info = gr.HTML()
141
  items = gr.JSON(value=None, visible=False)
142
- png2main = gr.Button('Send to txt2img')
143
  return page, png2main, items, info, image
144
 
145
  def ui():
146
  head = ''
147
  for f in sorted(os.listdir('./tagcomplete/javascript')):
148
  head += f'<script type="text/javascript" src="file=tagcomplete/javascript/{f}"></script>\n'
149
- with gr.Blocks(title="NAI Client", head=head, theme=gr.themes.Base()) as website:
150
  with gr.Tabs():
151
- with gr.TabItem("Main", elem_id="client_ui_main"):
152
  _, paras = main_ui()
153
- with gr.TabItem("PNG Info"):
154
  _, png2main, png_items, info, image = util_ui()
 
 
155
  png2main.click(fn=send_paras,
156
  inputs=[png_items] + paras,
157
  outputs=paras)
158
  png2main.click(fn=None,
159
  js="(x) => { if (x !== null) document.getElementById('client_ui_main-button').click(); return null; }",
160
  inputs=image)
 
 
 
 
 
 
161
  image.change(read_info_from_image, inputs=image, outputs=[info, png_items])
162
  return website
163
 
 
1
  import os
2
  import datetime
3
 
4
+ import toml
5
  import gradio as gr
6
+ from PIL import PngImagePlugin
7
 
8
+ from pnginfo import read_info_from_image, send_paras
9
+ from images_history import img_history_ui
10
  from utils import set_token, generate_novelai_image, image_from_bytes
11
 
12
+ client_config = toml.load("config.toml")['client']
13
  today_count = 0
14
  today = datetime.date.today().strftime('%Y-%m-%d')
15
 
 
19
  if now != today:
20
  today = now
21
  today_count = 0
22
+ return "今日已生成图片" + str(today_count) + "张"
23
 
24
  def control_ui():
25
+ prompt = gr.TextArea(elem_id='txt2img_prompt', label="提示词", lines=3)
26
  quality_tags = gr.TextArea(
27
+ elem_id='txt2img_qua_prompt', label="质量词", lines=1,
28
+ value=client_config['default_quality'],
29
  )
30
  neg_prompt = gr.TextArea(
31
+ elem_id='txt2img_neg_prompt', label="负面词", lines=1,
32
+ value=client_config['default_neg'],
33
  )
34
  with gr.Row():
35
  sampler = gr.Dropdown(
 
38
  "k_dpmpp_2m", "k_dpmpp_sde", "ddim_v3"
39
  ],
40
  value="k_euler",
41
+ label="采样器",
42
  interactive=True
43
  )
44
+ scale = gr.Slider(label="CFG Scale", value=5.0, minimum=1, maximum=10, step=0.1)
45
+ steps = gr.Slider(label="步数", value=28, minimum=1, maximum=28, step=1)
46
  with gr.Row():
47
+ seed = gr.Number(label="种子", value=-1, step=1, maximum=2**32-1, minimum=-1, scale=3)
48
  rand_seed = gr.Button('🎲️', scale=1)
49
  reuse_seed = gr.Button('♻️', scale=1)
50
  with gr.Row():
51
+ width = gr.Slider(label="宽度", value=1024, minimum=64, maximum=2048, step=64)
52
+ height = gr.Slider(label="高度", value=1024, minimum=64, maximum=2048, step=64)
53
  with gr.Row():
54
  with gr.Column():
55
  with gr.Accordion('风格迁移', open=False):
56
+ ref_image = gr.Image(label="上传图片", value=None, sources=["upload", "clipboard", "webcam"], interactive=True, type="pil")
57
  info_extract = gr.Slider(label='参考信息提取', value=1, minimum=0, maximum=1, step=0.1)
58
  ref_str = gr.Slider(label='参考强度', value=0.6, minimum=0, maximum=1, step=0.1)
59
  reuse_img_vibe = gr.Button(value='使用上一次生成的图片')
60
  with gr.Accordion('图生图', open=False):
61
+ i2i_image = gr.Image(label="上传图片", value=None, sources=["upload", "clipboard", "webcam"], interactive=True, type="pil")
62
  i2i_str = gr.Slider(label='去噪强度', value=0.7, minimum=0, maximum=0.99, step=0.01)
63
  i2i_noise = gr.Slider(label='噪声', value=0, minimum=0, maximum=1, step=0.1)
64
  reuse_img_i2i = gr.Button(value='使用上一次生成的图片')
 
73
  '''
74
  with gr.Row():
75
  with gr.Column():
76
+ with gr.Accordion('高级选项', open=False):
77
  scheduler = gr.Dropdown(
78
  choices=[
79
  "native", "karras", "exponential", "polyexponential"
 
89
  dyn_threshold = gr.Checkbox(False, label="Dynamic Thresholding")
90
  cfg_rescale = gr.Slider(0, 1, 0, step=0.01, label="CFG rescale")
91
  with gr.Column():
92
+ gr.Textbox(value=get_count, label='使用统计', every=10)
93
+ save = gr.Checkbox(value=True, label='云端保存图片')
94
+ gen_btn = gr.Button(value="生成", variant="primary")
95
  rand_seed.click(fn=lambda: -1, inputs=None, outputs=seed)
96
  width.change(lambda w, h: h if w*h<=1024*1024 else (1024*1024//w//64)*64, [width, height], height)
97
  height.change(lambda w, h: w if w*h<=1024*1024 else (1024*1024//h//64)*64, [width, height], width)
98
  return gen_btn,[prompt, quality_tags, neg_prompt, seed, scale, width, height, steps, sampler, scheduler, smea, dyn, dyn_threshold, cfg_rescale, ref_image, info_extract, ref_str, i2i_image, i2i_str, i2i_noise], [save, rand_seed, reuse_seed, reuse_img_vibe, reuse_img_i2i]
99
 
 
100
  def generate(prompt, quality_tags, neg_prompt, seed, scale, width, height, steps, sampler, scheduler, smea, dyn, dyn_threshold, cfg_rescale, ref_image, info_extract, ref_str, i2i_image, i2i_str, i2i_noise, save):
101
  global today_count
102
  set_token(os.environ.get('token'))
 
107
  i2i_image, i2i_str, i2i_noise
108
  )
109
  if not isinstance(img_data, bytes):
110
+ return gr.Image(value=None), payload
111
  today_count += 1
112
  img = image_from_bytes(img_data)
113
+ if save:
114
+ save_path = client_config['save_path']
115
+ today = datetime.date.today().strftime('%Y-%m-%d')
116
+ today_path = os.path.join(save_path, today)
117
+ if not os.path.exists(today_path):
118
+ os.makedirs(today_path, mode=777, exist_ok=True)
119
+ filename = str(today_count).rjust(5, '0') + '-' + str(payload['parameters']['seed']) + '.png'
120
+ pnginfo_data = PngImagePlugin.PngInfo()
121
+ for k, v in img.info.items():
122
+ pnginfo_data.add_text(k, str(v))
123
+ img.save(os.path.join(today_path, filename), pnginfo=pnginfo_data)
124
  return img, payload
125
 
 
126
  def preview_ui():
127
  with gr.Blocks(css='#preview_image { height: 100%;}'):
128
  image = gr.Image(elem_id='preview_image', interactive=False, type='pil')
129
+ info = gr.JSON(value={}, label="生成信息")
130
  return image, info
131
 
 
132
  def main_ui():
133
  with gr.Blocks() as page:
134
  with gr.Row(variant="panel"):
 
142
  others[4].click(lambda i: i, inputs=image, outputs=paras[17])
143
  return page, paras[:14]
144
 
 
145
  def util_ui():
146
  with gr.Blocks(analytics_enabled=False) as page:
147
  with gr.Row(equal_height=False):
148
  with gr.Column(variant='panel'):
149
+ image = gr.Image(label="上传图片", sources=["upload"], interactive=True, type="pil")
150
  with gr.Column(variant='panel'):
151
  info = gr.HTML()
152
  items = gr.JSON(value=None, visible=False)
153
+ png2main = gr.Button('参数发送到文生图')
154
  return page, png2main, items, info, image
155
 
156
  def ui():
157
  head = ''
158
  for f in sorted(os.listdir('./tagcomplete/javascript')):
159
  head += f'<script type="text/javascript" src="file=tagcomplete/javascript/{f}"></script>\n'
160
+ with gr.Blocks(title="NAI Client", head=head) as website:
161
  with gr.Tabs():
162
+ with gr.TabItem("文生图", elem_id="client_ui_main"):
163
  _, paras = main_ui()
164
+ with gr.TabItem("图片信息读取"):
165
  _, png2main, png_items, info, image = util_ui()
166
+ with gr.TabItem("云端图片浏览") as tab:
167
+ gal2main, gal_items = img_history_ui(tab)
168
  png2main.click(fn=send_paras,
169
  inputs=[png_items] + paras,
170
  outputs=paras)
171
  png2main.click(fn=None,
172
  js="(x) => { if (x !== null) document.getElementById('client_ui_main-button').click(); return null; }",
173
  inputs=image)
174
+ gal2main.click(fn=send_paras,
175
+ inputs=[gal_items] + paras,
176
+ outputs=paras)
177
+ gal2main.click(fn=None,
178
+ js="(x) => { if (x !== null) document.getElementById('client_ui_main-button').click(); return null; }",
179
+ inputs=gal_items)
180
  image.change(read_info_from_image, inputs=image, outputs=[info, png_items])
181
  return website
182