Spaces:
Paused
Paused
添加图片生成接口插件
Browse files- crazy_functional.py +10 -0
- crazy_functions/图片生成.py +64 -0
crazy_functional.py
CHANGED
@@ -236,5 +236,15 @@ def get_crazy_functions():
|
|
236 |
"Function": HotReload(同时问询_指定模型)
|
237 |
},
|
238 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
###################### 第n组插件 ###########################
|
240 |
return function_plugins
|
|
|
236 |
"Function": HotReload(同时问询_指定模型)
|
237 |
},
|
238 |
})
|
239 |
+
from crazy_functions.图片生成 import 图片生成
|
240 |
+
function_plugins.update({
|
241 |
+
"图片生成(先切换模型到openai或api2d)": {
|
242 |
+
"Color": "stop",
|
243 |
+
"AsButton": False,
|
244 |
+
"AdvancedArgs": True, # 调用时,唤起高级参数输入区(默认False)
|
245 |
+
"ArgsReminder": "在这里输入分辨率, 如256x256(默认)", # 高级参数输入区的显示提示
|
246 |
+
"Function": HotReload(图片生成)
|
247 |
+
},
|
248 |
+
})
|
249 |
###################### 第n组插件 ###########################
|
250 |
return function_plugins
|
crazy_functions/图片生成.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from toolbox import CatchException, update_ui, get_conf, select_api_key
|
2 |
+
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
|
3 |
+
import datetime
|
4 |
+
|
5 |
+
|
6 |
+
def gen_image(llm_kwargs, prompt, resolution="256x256"):
|
7 |
+
import requests, json, time, os
|
8 |
+
from request_llm.bridge_all import model_info
|
9 |
+
|
10 |
+
proxies, = get_conf('proxies')
|
11 |
+
# Set up OpenAI API key and model
|
12 |
+
api_key = select_api_key(llm_kwargs['api_key'], llm_kwargs['llm_model'])
|
13 |
+
chat_endpoint = model_info[llm_kwargs['llm_model']]['endpoint']
|
14 |
+
# 'https://api.openai.com/v1/chat/completions'
|
15 |
+
img_endpoint = chat_endpoint.replace('chat/completions','images/generations')
|
16 |
+
# # Generate the image
|
17 |
+
url = img_endpoint
|
18 |
+
headers = {
|
19 |
+
'Authorization': f"Bearer {api_key}",
|
20 |
+
'Content-Type': 'application/json'
|
21 |
+
}
|
22 |
+
data = {
|
23 |
+
'prompt': prompt,
|
24 |
+
'n': 1,
|
25 |
+
'size': '256x256',
|
26 |
+
'response_format': 'url'
|
27 |
+
}
|
28 |
+
response = requests.post(url, headers=headers, json=data, proxies=proxies)
|
29 |
+
print(response.content)
|
30 |
+
image_url = json.loads(response.content.decode('utf8'))['data'][0]['url']
|
31 |
+
|
32 |
+
# 文件保存到本地
|
33 |
+
r = requests.get(image_url, proxies=proxies)
|
34 |
+
file_path = 'gpt_log/image_gen/'
|
35 |
+
os.makedirs(file_path, exist_ok=True)
|
36 |
+
file_name = 'Image' + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + '.png'
|
37 |
+
with open(file_path+file_name, 'wb+') as f: f.write(r.content)
|
38 |
+
|
39 |
+
|
40 |
+
return image_url, file_path+file_name
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
@CatchException
|
45 |
+
def 图片生成(prompt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
46 |
+
"""
|
47 |
+
txt 输入栏用户输入的文本,例如需要翻译的一段话,再例如一个包含了待处理文件的路径
|
48 |
+
llm_kwargs gpt模型参数,如温度和top_p等,一般原样传递下去就行
|
49 |
+
plugin_kwargs 插件模型的参数,暂时没有用武之地
|
50 |
+
chatbot 聊天显示框的句柄,用于显示给用户
|
51 |
+
history 聊天历史,前情提要
|
52 |
+
system_prompt 给gpt的静默提醒
|
53 |
+
web_port 当前软件运行的端口号
|
54 |
+
"""
|
55 |
+
history = [] # 清空历史,以免输入溢出
|
56 |
+
chatbot.append(("这是什么功能?", "[Local Message] 生成图像, 请先把模型切换至gpt-xxxx或者api2d-xxxx。如果中文效果不理想, 尝试Prompt。正在处理中 ....."))
|
57 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 由于请求gpt需要一段时间,我们先及时地做一次界面更新
|
58 |
+
resolution = plugin_kwargs.get("advanced_arg", '256x256')
|
59 |
+
image_url, image_path = gen_image(llm_kwargs, prompt, resolution)
|
60 |
+
chatbot.append([prompt,
|
61 |
+
f'`{image_url}`\n\n'+
|
62 |
+
f'<div align="center"><img src="file={image_path}"></div>'
|
63 |
+
])
|
64 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
|