forked from heshengtao/comfyui_LLM_party
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
321 lines (284 loc) · 12 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import io
import json
import os
import urllib.parse
import urllib.request
import uuid
import pandas as pd
import pygments
import streamlit as st
import websocket # NOTE: websocket-client (https://github.com/websocket-client/websocket-client)
from PIL import Image
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name
server_address = "127.0.0.1:8188"
client_id = str(uuid.uuid4())
def queue_prompt(prompt):
p = {"prompt": prompt, "client_id": client_id}
data = json.dumps(p).encode("utf-8")
req = urllib.request.Request("http://{}/prompt".format(server_address), data=data)
return json.loads(urllib.request.urlopen(req).read())
def get_image(filename, subfolder, folder_type):
data = {"filename": filename, "subfolder": subfolder, "type": folder_type}
url_values = urllib.parse.urlencode(data)
with urllib.request.urlopen("http://{}/view?{}".format(server_address, url_values)) as response:
return response.read()
def get_history(prompt_id):
with urllib.request.urlopen("http://{}/history/{}".format(server_address, prompt_id)) as response:
return json.loads(response.read())
def get_all(ws, prompt):
prompt_id = queue_prompt(prompt)["prompt_id"]
output_images = {}
output_text = ""
while True:
out = ws.recv()
if isinstance(out, str):
message = json.loads(out)
if message["type"] == "executing":
data = message["data"]
if data["node"] is None and data["prompt_id"] == prompt_id:
break # Execution is done
else:
continue # previews are binary data
history = get_history(prompt_id)[prompt_id]
for o in history["outputs"]:
for node_id in history["outputs"]:
node_output = history["outputs"][node_id]
if "images" in node_output:
images_output = []
for image in node_output["images"]:
image_data = get_image(image["filename"], image["subfolder"], image["type"])
images_output.append(image_data)
output_images[node_id] = images_output
if "response" in node_output:
output_text = node_output["response"][0]["content"]
return output_images, output_text
def api(
file_content="",
image_input=None,
file_path="",
img_path="",
system_prompt="你是一个强大的智能助手",
user_prompt="",
positive_prompt="",
negative_prompt="",
model_name="",
workflow_path="测试画画api.json",
):
global current_dir_path
workflow_path = workflow_path
WF_path = os.path.join(current_dir_path, "workflow_api", workflow_path)
with open(WF_path, "r", encoding="utf-8") as f:
prompt_text = f.read()
prompt = json.loads(prompt_text)
for p in prompt:
# 如果p的class_type是start_workflow
if prompt[p]["class_type"] == "start_workflow":
if file_content != "":
prompt[p]["inputs"]["file_content"] = file_content
if image_input is not None and image_input != []:
prompt[p]["inputs"]["image_input"] = image_input
prompt[p]["inputs"]["file_path"] = file_path
prompt[p]["inputs"]["img_path"] = img_path
prompt[p]["inputs"]["system_prompt"] = system_prompt
prompt[p]["inputs"]["user_prompt"] = user_prompt
prompt[p]["inputs"]["positive_prompt"] = positive_prompt
prompt[p]["inputs"]["negative_prompt"] = negative_prompt
prompt[p]["inputs"]["model_name"] = model_name
ws = websocket.WebSocket()
ws.connect("ws://{}/ws?clientId={}".format(server_address, client_id))
images, res = get_all(ws, prompt)
return images, res
#####以下是UI界面部分代码,上面是API接口代码
button_style = """
<style>
div.stButton > button:first-child {
background-color: white; /* White background */
color: black; /* Black text */
width: 100%; /* Full width */
border: 1px solid #ccc; /* Light grey border */
padding: 10px 24px; /* Padding */
border-radius: 4px; /* Rounded corners */
transition: background-color 0.3s, color 0.3s; /* Smooth transition */
}
div.stButton > button:first-child:hover {
background-color: #d2d1d1; /* Very light background on hover */
color: black; /* Black text on hover */
}
div.stButton > button:first-child:active {
background-color: #bababa; /* Light background on click */
}
div[data-testid="stForm"] {
border: none !important; /* 隐藏边框 */
padding: 0 !important; /* 移除内边距 */
margin: 0 !important; /* 移除外边距 */
}
/* 隐藏 text_area 的标签 */
div.stTextArea > label {
display: none;
}
/* 当 text_area 获得焦点时,设置边框颜色为橙色 */
div.stTextArea textarea:focus {
border-color: #FFA500;
box-shadow: 0 0 0.25rem rgba(255, 165, 0, 0.5); /* 可选:添加发光效果 */
}
</style>
"""
# Add the custom button style to the Streamlit app
st.markdown(button_style, unsafe_allow_html=True)
language_mapping = {
".py": "python",
".js": "javascript",
".java": "java",
".c": "c",
".cpp": "cpp",
".html": "html",
".css": "css",
".sql": "sql",
".r": "r",
".swift": "swift",
# Add more mappings for other languages as needed
}
# 如果没有'wf_path'就创造
if "wf_path" not in st.session_state:
st.session_state["wf_path"] = "测试画画app.json"
# 如果没有'system_prompt'就创造
if "system_prompt" not in st.session_state:
st.session_state["system_prompt"] = "你是一个强大的智能助手"
def get_current_page():
# 安全地获取当前页面
return st.session_state.get("current_page", "chat") # 默认返回 'chat'
def set_current_page(page_name):
# 安全地设置当前页面
st.session_state["current_page"] = page_name
# 初始化当前页面的会话状态(如果尚未设置)
set_current_page(get_current_page())
current_dir_path = os.path.dirname(os.path.realpath(__file__))
# 创建侧边栏按钮
if st.sidebar.button("聊天"):
set_current_page("chat")
if st.sidebar.button("设置"):
set_current_page("settings")
if "chat_history" not in st.session_state:
st.session_state["chat_history"] = []
# 使用函数来访问 'current_page'
if get_current_page() == "chat":
response = "你好哇~"
ai_name = st.session_state["wf_path"]
ai_name = ai_name.replace(".json", "")
st.markdown(f"{ai_name}: 你好哇~")
chat_history_container = st.container() # Use a container to hold the chat history
# 更新对话记录容器
chat_history_container.empty()
def display_chat_history():
# 更新对话记录容器
chat_history_container.empty()
with chat_history_container:
for message in st.session_state["chat_history"]:
if message["role"] == "assistant":
if message["content"] is not None and message["content"] != "" and message["content"] != "empty":
st.markdown(f"{ai_name}: {message['content']}")
elif message["role"] == "user":
st.markdown(f"你: {message['content']}")
elif message["role"] == "image":
if message["content"] is not None:
st.markdown(f"{ai_name}:")
st.image(message["content"])
with st.form("Question", clear_on_submit=True):
# 用户输入
user_input = st.text_area("", height=100, placeholder="让我们开始聊天吧...")
# 文件上传
uploaded_file = st.file_uploader(
"上传文件或图片",
type=[
"png",
"jpg",
"jpeg",
"txt",
"docx",
"doc",
"pdf",
"xlsx",
"xls",
"csv",
"py",
"html",
"css",
"sql",
"r",
"swift",
"javascript",
"java",
"c",
"cpp",
],
)
col3, col4 = st.columns(2)
with col3:
if st.form_submit_button("发送"):
st.session_state["chat_history"].append({"role": "user", "content": user_input})
file_path = ""
img_path = ""
if uploaded_file is not None:
# 如果上传的是照片,将文件路径传入image_path变量,否则传入file_path变量
if uploaded_file.type.startswith("image"):
# 保存上传的文件
with open(os.path.join(current_dir_path, "img", uploaded_file.name), "wb") as f:
f.write(uploaded_file.getbuffer())
img_path = os.path.join(current_dir_path, "img", uploaded_file.name)
file_path = ""
st.session_state["chat_history"].append({"role": "image", "content": uploaded_file})
else:
# 保存上传的文件
with open(os.path.join(current_dir_path, "file", uploaded_file.name), "wb") as f:
f.write(uploaded_file.getbuffer())
file_path = os.path.join(current_dir_path, "file", uploaded_file.name)
img_path = ""
# 调用API函数
images, response = api(
"",
None,
file_path,
img_path,
st.session_state["system_prompt"],
user_input,
positive_prompt="",
negative_prompt="",
model_name="",
workflow_path=str(st.session_state["wf_path"]),
)
# 更新对话记录
if response is not None and response != "" and response != "empty":
st.session_state["chat_history"].append({"role": "assistant", "content": response})
if images is not None:
# 更新对话记录
for node_id in images:
for image_data in images[node_id]:
image = Image.open(io.BytesIO(image_data))
st.session_state["chat_history"].append({"role": "image", "content": image})
display_chat_history()
with col4:
if st.form_submit_button("清空"):
st.session_state["chat_history"] = []
# 更新对话记录容器
chat_history_container.empty()
if get_current_page() == "settings":
st.title("设置")
st.markdown("在这里可以设置你的设置。")
st.markdown("当前系统提示词(system_prompt):" + st.session_state["system_prompt"])
# 设置系统提示词system_prompt
system_prompt = st.text_area("系统提示词", height=100, placeholder="请输入你的系统提示词")
path1 = st.session_state["wf_path"]
# 添加下拉菜单,选项为WF_path=os.path.join(current_dir_path,"workflow")文件夹下的json文件
st.markdown(f"当前工作流文件(workflow):{path1}")
_path = st.selectbox(
"选择一个包含start_workflow & end_workflow的工作流文件",
[f for f in os.listdir(os.path.join(current_dir_path, "workflow_api")) if f.endswith(".json")],
)
# 保存按钮
if st.button("保存"):
# 保存_path到session_state
st.session_state["wf_path"] = _path
print(st.session_state["wf_path"])
# 保存system_prompt到session_state
st.session_state["system_prompt"] = system_prompt