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