Spaces:
Paused
Paused
P01yH3dr0n
commited on
Commit
•
0f3c1d9
1
Parent(s):
ca93863
anlas generation
Browse files- app.py +26 -4
- pnginfo.py +1 -1
- utils.py +15 -0
app.py
CHANGED
@@ -8,13 +8,20 @@ from huggingface_hub import HfApi, snapshot_download
|
|
8 |
|
9 |
from pnginfo import read_info_from_image, send_paras
|
10 |
from images_history import img_history_ui
|
11 |
-
from utils import set_token, generate_novelai_image, image_from_bytes
|
12 |
|
13 |
client_config = toml.load("config.toml")['client']
|
14 |
today_count = 0
|
|
|
15 |
today = datetime.date.today().strftime('%Y-%m-%d')
|
16 |
api = HfApi()
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def get_count():
|
19 |
global today_count, today
|
20 |
now = datetime.date.today().strftime('%Y-%m-%d')
|
@@ -44,7 +51,7 @@ def control_ui():
|
|
44 |
interactive=True
|
45 |
)
|
46 |
scale = gr.Slider(label="CFG Scale", value=5.0, minimum=1, maximum=10, step=0.1)
|
47 |
-
steps = gr.Slider(label="步数", value=28, minimum=1, maximum=
|
48 |
with gr.Row():
|
49 |
seed = gr.Number(label="种子", value=-1, step=1, maximum=2**32-1, minimum=-1, scale=3)
|
50 |
rand_seed = gr.Button('🎲️', scale=1)
|
@@ -111,8 +118,6 @@ def control_ui():
|
|
111 |
gen_btn = gr.Button(value="生成", variant="primary")
|
112 |
stop_btn = gr.Button(value="取消", variant="stop", visible=False)
|
113 |
rand_seed.click(fn=lambda: -1, inputs=None, outputs=seed)
|
114 |
-
width.change(lambda w, h: h if w*h<=1024*1024 else (1024*1024//w//64)*64, [width, height], height)
|
115 |
-
height.change(lambda w, h: w if w*h<=1024*1024 else (1024*1024//h//64)*64, [width, height], width)
|
116 |
i2i.select(lambda: 'i2i', inputs=None, outputs=selection)
|
117 |
inp.select(lambda: 'inp', inputs=None, outputs=selection)
|
118 |
return gen_btn, stop_btn, [prompt, quality_tags, neg_prompt, seed, scale, width, height, steps, sampler, scheduler, smea, dyn, dyn_threshold, cfg_rescale, ref_images, info_extracts, ref_strs, i2i_image, i2i_str, i2i_noise, overlay, inp_img, selection], [save, rand_seed, reuse_seed, reuse_img_i2i, reuse_img_inp]
|
@@ -156,6 +161,15 @@ def rename_save_img(path, payload, save):
|
|
156 |
api.upload_folder(folder_path=today_path, path_in_repo=today_path, repo_id="P01yH3dr0n/naimages", repo_type="dataset", token=os.environ.get("hf_token"))
|
157 |
return renamed_path
|
158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
def main_ui():
|
160 |
with gr.Blocks() as page:
|
161 |
with gr.Row(variant="panel"):
|
@@ -163,6 +177,9 @@ def main_ui():
|
|
163 |
gen_btn, stop_btn, paras, others = control_ui()
|
164 |
with gr.Column():
|
165 |
image, info = preview_ui()
|
|
|
|
|
|
|
166 |
gen_btn.click(lambda: (gr.Button(visible=False), gr.Button(visible=True)), inputs=None, outputs=[gen_btn, stop_btn])
|
167 |
gen = gen_btn.click(generate, paras, [image, info], concurrency_limit=1)
|
168 |
others[2].click(lambda o, s: o if len(s) == 0 else s['parameters']['seed'], inputs=[paras[3], info], outputs=paras[3])
|
@@ -201,7 +218,12 @@ def load_javascript():
|
|
201 |
|
202 |
def ui():
|
203 |
load_javascript()
|
|
|
204 |
with gr.Blocks(title="NAI Client", theme=gr.themes.Base(), js="() => {document.body.classList.toggle('dark');}") as website:
|
|
|
|
|
|
|
|
|
205 |
with gr.Tabs():
|
206 |
with gr.TabItem("图片生成", elem_id="client_ui_main"):
|
207 |
_, paras = main_ui()
|
|
|
8 |
|
9 |
from pnginfo import read_info_from_image, send_paras
|
10 |
from images_history import img_history_ui
|
11 |
+
from utils import set_token, generate_novelai_image, image_from_bytes, get_remain_anlas, calculate_cost
|
12 |
|
13 |
client_config = toml.load("config.toml")['client']
|
14 |
today_count = 0
|
15 |
+
anlas = -1
|
16 |
today = datetime.date.today().strftime('%Y-%m-%d')
|
17 |
api = HfApi()
|
18 |
|
19 |
+
def refresh_anlas():
|
20 |
+
global anlas
|
21 |
+
set_token(client_config['token'])
|
22 |
+
anlas = get_remain_anlas()
|
23 |
+
return anlas
|
24 |
+
|
25 |
def get_count():
|
26 |
global today_count, today
|
27 |
now = datetime.date.today().strftime('%Y-%m-%d')
|
|
|
51 |
interactive=True
|
52 |
)
|
53 |
scale = gr.Slider(label="CFG Scale", value=5.0, minimum=1, maximum=10, step=0.1)
|
54 |
+
steps = gr.Slider(label="步数", value=28, minimum=1, maximum=50, step=1)
|
55 |
with gr.Row():
|
56 |
seed = gr.Number(label="种子", value=-1, step=1, maximum=2**32-1, minimum=-1, scale=3)
|
57 |
rand_seed = gr.Button('🎲️', scale=1)
|
|
|
118 |
gen_btn = gr.Button(value="生成", variant="primary")
|
119 |
stop_btn = gr.Button(value="取消", variant="stop", visible=False)
|
120 |
rand_seed.click(fn=lambda: -1, inputs=None, outputs=seed)
|
|
|
|
|
121 |
i2i.select(lambda: 'i2i', inputs=None, outputs=selection)
|
122 |
inp.select(lambda: 'inp', inputs=None, outputs=selection)
|
123 |
return gen_btn, stop_btn, [prompt, quality_tags, neg_prompt, seed, scale, width, height, steps, sampler, scheduler, smea, dyn, dyn_threshold, cfg_rescale, ref_images, info_extracts, ref_strs, i2i_image, i2i_str, i2i_noise, overlay, inp_img, selection], [save, rand_seed, reuse_seed, reuse_img_i2i, reuse_img_inp]
|
|
|
161 |
api.upload_folder(folder_path=today_path, path_in_repo=today_path, repo_id="P01yH3dr0n/naimages", repo_type="dataset", token=os.environ.get("hf_token"))
|
162 |
return renamed_path
|
163 |
|
164 |
+
def update_btn_cost(w, h, s, sm, dyn, i2i_img, i2i_str, inp_img, selection):
|
165 |
+
if selection == 'i2i' and i2i_img is not None:
|
166 |
+
cost = calculate_cost(w, h, s, False, False, i2i_str)
|
167 |
+
elif selection == 'inp' and inp_img is not None:
|
168 |
+
cost = calculate_cost(w, h, s, False, False)
|
169 |
+
else:
|
170 |
+
cost = calculate_cost(w, h, s, sm, dyn)
|
171 |
+
return gr.Button(value=f"生成(消耗{cost}点数)")
|
172 |
+
|
173 |
def main_ui():
|
174 |
with gr.Blocks() as page:
|
175 |
with gr.Row(variant="panel"):
|
|
|
177 |
gen_btn, stop_btn, paras, others = control_ui()
|
178 |
with gr.Column():
|
179 |
image, info = preview_ui()
|
180 |
+
cost_list = paras[5:8] + paras[10:12] + paras[17:19] + paras[21:23]
|
181 |
+
for component in cost_list:
|
182 |
+
component.change(update_btn_cost, inputs=cost_list, outputs=gen_btn)
|
183 |
gen_btn.click(lambda: (gr.Button(visible=False), gr.Button(visible=True)), inputs=None, outputs=[gen_btn, stop_btn])
|
184 |
gen = gen_btn.click(generate, paras, [image, info], concurrency_limit=1)
|
185 |
others[2].click(lambda o, s: o if len(s) == 0 else s['parameters']['seed'], inputs=[paras[3], info], outputs=paras[3])
|
|
|
218 |
|
219 |
def ui():
|
220 |
load_javascript()
|
221 |
+
refresh_anlas()
|
222 |
with gr.Blocks(title="NAI Client", theme=gr.themes.Base(), js="() => {document.body.classList.toggle('dark');}") as website:
|
223 |
+
with gr.Row():
|
224 |
+
remain_anlas = gr.Text(label="剩余点数", value=anlas)
|
225 |
+
refresh = gr.Button("🔄")
|
226 |
+
refresh.click(refresh_anlas, inputs=None, outputs=remain_anlas)
|
227 |
with gr.Tabs():
|
228 |
with gr.TabItem("图片生成", elem_id="client_ui_main"):
|
229 |
_, paras = main_ui()
|
pnginfo.py
CHANGED
@@ -23,7 +23,7 @@ def to_digit(v):
|
|
23 |
except:
|
24 |
return v
|
25 |
|
26 |
-
def read_info_from_image(image)
|
27 |
if image is None:
|
28 |
return '', {}
|
29 |
elif type(image) == str:
|
|
|
23 |
except:
|
24 |
return v
|
25 |
|
26 |
+
def read_info_from_image(image):
|
27 |
if image is None:
|
28 |
return '', {}
|
29 |
elif type(image) == str:
|
utils.py
CHANGED
@@ -4,6 +4,7 @@ import zipfile
|
|
4 |
import requests
|
5 |
import json
|
6 |
import base64
|
|
|
7 |
|
8 |
from PIL import Image
|
9 |
|
@@ -25,6 +26,20 @@ def set_token(token):
|
|
25 |
"Referer": "https://novelai.net/"
|
26 |
}
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
def generate_novelai_image(
|
29 |
input_text="",
|
30 |
negative_prompt="",
|
|
|
4 |
import requests
|
5 |
import json
|
6 |
import base64
|
7 |
+
import math
|
8 |
|
9 |
from PIL import Image
|
10 |
|
|
|
26 |
"Referer": "https://novelai.net/"
|
27 |
}
|
28 |
|
29 |
+
def get_remain_anlas():
|
30 |
+
data = requests.get("https://api.novelai.net/user/data", headers=headers).content
|
31 |
+
return data['fixedTrainingStepsLeft'] + data['purchasedTrainingSteps']
|
32 |
+
|
33 |
+
def calculate_cost(width, height, steps, sm, dyn, strength=1, rmbg=False):
|
34 |
+
pixels = width * height
|
35 |
+
if pixels <= 1048576 and steps <= 28:
|
36 |
+
return 0
|
37 |
+
dyn = sm and dyn
|
38 |
+
L = math.ceil(2951823174884865e-21 * pixels + 5.753298233447344e-7 * pixels * steps)
|
39 |
+
L *= 1.4 if dyn else (1.2 if sm else 1)
|
40 |
+
L = max(math.ceil(L * strength), 2)
|
41 |
+
return L * 3 + 5 if rmbg else L
|
42 |
+
|
43 |
def generate_novelai_image(
|
44 |
input_text="",
|
45 |
negative_prompt="",
|