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
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 5,
|
6 |
+
"id": "059cca54-7257-44be-991f-85d83572666e",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"# keys管理\n",
|
11 |
+
"\n",
|
12 |
+
"keys = {\"pst-HldliYUSKyTUF4lmAPnMXd4PuK6Ff6nBxnf5QnHz4w5rw96jAqwFqDsK68vpGjxH\"}\n",
|
13 |
+
"\n",
|
14 |
+
"def get(key): # 读取key\n",
|
15 |
+
" global keys\n",
|
16 |
+
" return keys[key]\n",
|
17 |
+
"\n",
|
18 |
+
"def set(key, status): # 存在key 则设置\n",
|
19 |
+
" global keys\n",
|
20 |
+
" if status in [True,False]:\n",
|
21 |
+
" if key in keys:\n",
|
22 |
+
" keys[key] = status\n",
|
23 |
+
"\n",
|
24 |
+
"def add(key): # 添加key\n",
|
25 |
+
" global keys\n",
|
26 |
+
" if key not in keys:\n",
|
27 |
+
" keys[key] = True\n",
|
28 |
+
" save()\n",
|
29 |
+
"\n",
|
30 |
+
"def delete(key): # 删除key\n",
|
31 |
+
" global keys\n",
|
32 |
+
" del keys[key]\n",
|
33 |
+
" save()\n",
|
34 |
+
"\n",
|
35 |
+
"def save():\n",
|
36 |
+
" global keys\n",
|
37 |
+
" with open('keys.txt','w') as f:\n",
|
38 |
+
" f.write('\\n'.join(list(keys.keys())))\n",
|
39 |
+
"\n",
|
40 |
+
"def load():\n",
|
41 |
+
" global keys\n",
|
42 |
+
" with open('keys.txt','r') as f:\n",
|
43 |
+
" keys = {i:True for i in f.read().split('\\n') if i}\n",
|
44 |
+
"\n",
|
45 |
+
"load()\n",
|
46 |
+
"\n",
|
47 |
+
"# nai3绘画\n",
|
48 |
+
"\n",
|
49 |
+
"import requests, zipfile, os, random, time, shutil, urllib3, json\n",
|
50 |
+
"urllib3.disable_warnings()\n",
|
51 |
+
"\n",
|
52 |
+
"IMAGE_URL = 'https://image.novelai.net/'#'https://image.goutou.cc/'#\n",
|
53 |
+
"\n",
|
54 |
+
"# 适配ai3参数\n",
|
55 |
+
"def check_data(dict): \n",
|
56 |
+
" inp = {\n",
|
57 |
+
" \"model\": \"nai-diffusion-3\",\n",
|
58 |
+
" \"prompt\": \"\",\n",
|
59 |
+
" 'img':'',\n",
|
60 |
+
" \"size\": \"832x1216\",\n",
|
61 |
+
" \"n_prompt\": \"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]\",\n",
|
62 |
+
" \"steps\": \"28\",\n",
|
63 |
+
" \"scale\": \"5\",\n",
|
64 |
+
" \"auto\": False,\n",
|
65 |
+
" \"cfg_scale\": \"0\",\n",
|
66 |
+
" \"seed\": \"-1\",\n",
|
67 |
+
" \"sampler\": \"k_euler\",\n",
|
68 |
+
" \"SMEA\": False,\n",
|
69 |
+
" \"DYN\": False,\n",
|
70 |
+
" \"noise\": \"native\"\n",
|
71 |
+
" }\n",
|
72 |
+
"\n",
|
73 |
+
" inp |= {k: v for k, v in dict.items() if k in ['model', 'prompt','img', 'size', 'n_prompt', 'steps', 'scale', 'auto', 'cfg_scale', 'seed', 'sampler', 'SMEA', 'DYN', 'noise']}\n",
|
74 |
+
"\n",
|
75 |
+
" if int(inp['seed']) == -1:\n",
|
76 |
+
" inp['seed'] = random.randint(0,21474836)\n",
|
77 |
+
"\n",
|
78 |
+
" data = {\n",
|
79 |
+
" \"input\":inp['prompt'], # 提示词\n",
|
80 |
+
" \"model\":inp['model'], # 模型\n",
|
81 |
+
" \"action\":\"generate\",\n",
|
82 |
+
" \"parameters\":{\n",
|
83 |
+
" \"params_version\":1,\n",
|
84 |
+
" \"width\":int(inp['size'].split('x')[0]), # 宽度\n",
|
85 |
+
" \"height\":int(inp['size'].split('x')[1]), # 高度\n",
|
86 |
+
" \"scale\":float(inp['scale']),# 提示词引导值\n",
|
87 |
+
" \"sampler\":inp['sampler'], # 采样器\n",
|
88 |
+
" \"steps\": int(inp['steps']), # 步数\n",
|
89 |
+
" \"seed\": int(inp['seed']), # 种子\n",
|
90 |
+
" \"sm\":bool(inp['SMEA']), # SMEA\n",
|
91 |
+
" \"sm_dyn\":bool(inp['DYN']), # DYN\n",
|
92 |
+
" \"cfg_rescale\":float(inp['cfg_scale']), # 缩放提示词引导式\n",
|
93 |
+
" \"noise_schedule\":inp['noise'], # 噪声计划表\n",
|
94 |
+
" \"negative_prompt\":inp['n_prompt'], # 反向提示词\n",
|
95 |
+
" }\n",
|
96 |
+
" }\n",
|
97 |
+
"\n",
|
98 |
+
" # 图生图\n",
|
99 |
+
" if inp['img'] != 'data:,' and inp['img'] != '':\n",
|
100 |
+
" data[\"action\"] = \"img2img\"\n",
|
101 |
+
" data[\"parameters\"][\"image\"] = inp['img'].split(',')[1] if ',' in inp['img'] else inp['img']\n",
|
102 |
+
" data[\"parameters\"][\"noise\"] = 0\n",
|
103 |
+
" data[\"parameters\"][\"strength\"] = 0.7\n",
|
104 |
+
" data[\"parameters\"][\"extra_noise_seed\"] = 3263049076\n",
|
105 |
+
" \n",
|
106 |
+
" return data\n",
|
107 |
+
"\n",
|
108 |
+
"# 提交任务\n",
|
109 |
+
"def draw(prompt,apikey,savepath):\n",
|
110 |
+
" global IMAGE_URL\n",
|
111 |
+
" \n",
|
112 |
+
" # 开始计时\n",
|
113 |
+
" start_time = time.time()\n",
|
114 |
+
"\n",
|
115 |
+
" print('API /nai3> 开始绘图')\n",
|
116 |
+
"\n",
|
117 |
+
" try:\n",
|
118 |
+
" draw_data = check_data(prompt)\n",
|
119 |
+
" except Exception as e:\n",
|
120 |
+
" print('API /nai3> 参数错误',e)\n",
|
121 |
+
" return {'status': 'error', 'msg':'参数错误'}\n",
|
122 |
+
" \n",
|
123 |
+
" try:\n",
|
124 |
+
" response = requests.post(IMAGE_URL+'ai/generate-image', json=draw_data, headers={\n",
|
125 |
+
" \"Authorization\": \"Bearer \" + apikey,\n",
|
126 |
+
" \"Content-type\": \"application/json\",\n",
|
127 |
+
" \"Referer\": \"https://novelai.net/\",\n",
|
128 |
+
" \"User-agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.6045.160 Safari/537.36\",\n",
|
129 |
+
" })\n",
|
130 |
+
" except:\n",
|
131 |
+
" print('API /nai3> 连接绘图服务器失败')\n",
|
132 |
+
" return {'status': 'error', 'msg':'连接绘图服务器失败'}\n",
|
133 |
+
"\n",
|
134 |
+
" # 判断是否成功\n",
|
135 |
+
" try:\n",
|
136 |
+
" data = response.json()\n",
|
137 |
+
"\n",
|
138 |
+
" # 绘图失败\n",
|
139 |
+
" if 'statusCode' in data and 'message' in data:\n",
|
140 |
+
" wrong = f'{data[\"statusCode\"]}: {data[\"message\"]}'\n",
|
141 |
+
"\n",
|
142 |
+
" print(f'API /nai3> 绘图失败 {wrong}')\n",
|
143 |
+
" return {'status': 'error', 'msg': wrong}\n",
|
144 |
+
" else:\n",
|
145 |
+
" # 无法获取错误信息\n",
|
146 |
+
" wrong = '无法得到正确响应,请检查地址'\n",
|
147 |
+
"\n",
|
148 |
+
" print(f'API /nai3> 绘图失败 {wrong}')\n",
|
149 |
+
" return {'status': 'error', 'msg': wrong}\n",
|
150 |
+
" \n",
|
151 |
+
" except:\n",
|
152 |
+
"\n",
|
153 |
+
" # 绘画成功\n",
|
154 |
+
" print('API /nai3> 绘图成功')\n",
|
155 |
+
" try:\n",
|
156 |
+
" down_img(response.content,savepath)\n",
|
157 |
+
" tools.save_img(savepath,draw_data)\n",
|
158 |
+
" return {\n",
|
159 |
+
" 'status' : 'ok',\n",
|
160 |
+
" 'time' : str(time.time() - start_time)\n",
|
161 |
+
" }\n",
|
162 |
+
" except:\n",
|
163 |
+
" print('API /nai3> 图片转码失败')\n",
|
164 |
+
" return {'status': 'error', 'msg':'图片转码失败'}\n",
|
165 |
+
"\n",
|
166 |
+
"# 处理图片\n",
|
167 |
+
"def down_img(content,savepath:str):\n",
|
168 |
+
"\n",
|
169 |
+
" # 清理文件\n",
|
170 |
+
" if os.path.exists(savepath):\n",
|
171 |
+
" os.remove(savepath)\n",
|
172 |
+
" if os.path.exists(f'{savepath}.zip'):\n",
|
173 |
+
" os.remove(f'{savepath}.zip')\n",
|
174 |
+
" if os.path.exists(f'{savepath}-f'):\n",
|
175 |
+
" shutil.rmtree(f'{savepath}-f')\n",
|
176 |
+
"\n",
|
177 |
+
" # 如果文件夹不存在,则创建\n",
|
178 |
+
" if not os.path.exists(savepath.replace(savepath.split('/')[-1],'')):\n",
|
179 |
+
" os.makedirs(savepath.replace(savepath.split('/')[-1],''))\n",
|
180 |
+
"\n",
|
181 |
+
" try:\n",
|
182 |
+
" # 转得图片文件夹\n",
|
183 |
+
" with open(f'{savepath}.zip', 'wb') as f:\n",
|
184 |
+
" f.write(content)\n",
|
185 |
+
"\n",
|
186 |
+
" with zipfile.ZipFile(f'{savepath}.zip', 'r') as zip_ref:\n",
|
187 |
+
" zip_ref.extractall(f'{savepath}-f')\n",
|
188 |
+
" os.remove(f'{savepath}.zip')\n",
|
189 |
+
" \n",
|
190 |
+
" print('API /nai3> 下载解压图片成功')\n",
|
191 |
+
" except:\n",
|
192 |
+
" print('API /nai3> 下载解压图片失败')\n",
|
193 |
+
" return {'status': 'error', 'msg':'下载解压图片失败'}\n",
|
194 |
+
"\n",
|
195 |
+
" # 提取图片\n",
|
196 |
+
" os.rename(f'{savepath}-f/image_0.png', savepath)\n",
|
197 |
+
"\n",
|
198 |
+
" shutil.rmtree(f'{savepath}-f')\n",
|
199 |
+
"\n",
|
200 |
+
" print('API /nai3> 提取图片成功')\n"
|
201 |
+
]
|
202 |
+
},
|
203 |
+
{
|
204 |
+
"cell_type": "code",
|
205 |
+
"execution_count": null,
|
206 |
+
"id": "ef4ee159-3ca1-4b21-abda-f586dff8943d",
|
207 |
+
"metadata": {},
|
208 |
+
"outputs": [],
|
209 |
+
"source": []
|
210 |
+
}
|
211 |
+
],
|
212 |
+
"metadata": {
|
213 |
+
"kernelspec": {
|
214 |
+
"display_name": "Python 3 (ipykernel)",
|
215 |
+
"language": "python",
|
216 |
+
"name": "python3"
|
217 |
+
},
|
218 |
+
"language_info": {
|
219 |
+
"codemirror_mode": {
|
220 |
+
"name": "ipython",
|
221 |
+
"version": 3
|
222 |
+
},
|
223 |
+
"file_extension": ".py",
|
224 |
+
"mimetype": "text/x-python",
|
225 |
+
"name": "python",
|
226 |
+
"nbconvert_exporter": "python",
|
227 |
+
"pygments_lexer": "ipython3",
|
228 |
+
"version": "3.10.12"
|
229 |
+
}
|
230 |
+
},
|
231 |
+
"nbformat": 4,
|
232 |
+
"nbformat_minor": 5
|
233 |
+
}
|