Spaces:
Runtime error
Runtime error
JunchuanYu
commited on
Commit
·
82a00d8
1
Parent(s):
19edaa0
Upload 5 files
Browse files- app.py +92 -0
- chat_func.py +300 -0
- presets.py +46 -0
- requirements.txt +7 -0
- utils.py +301 -0
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import sys
|
4 |
+
import argparse
|
5 |
+
from utils import *
|
6 |
+
from presets import *
|
7 |
+
from chat_func import *
|
8 |
+
my_api_key = "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" # 在这里输入你的 API 密钥
|
9 |
+
|
10 |
+
if my_api_key == "":
|
11 |
+
my_api_key = os.environ.get('my_api_key')
|
12 |
+
|
13 |
+
if my_api_key == "empty":
|
14 |
+
print("Please give a api key!")
|
15 |
+
sys.exit(1)
|
16 |
+
|
17 |
+
gr.Chatbot.postprocess = postprocess
|
18 |
+
|
19 |
+
# css = """
|
20 |
+
# #col-container {max-width: 80%; margin-left: auto; margin-right: auto;}
|
21 |
+
# #chatbox {min-height: 150px}
|
22 |
+
# #header {text-align: center;font-size: 2.8em}
|
23 |
+
# #prompt_template_preview {padding: 1em; border-width: 1px; border-style: solid; border-color: #e0e0e0; border-radius: 4px;}
|
24 |
+
# #submit {text-align: center; background-color: #e0e0e0;}
|
25 |
+
# #label {font-size: 0.8em; padding: 0.5em; margin: 0;}
|
26 |
+
# """
|
27 |
+
with gr.Blocks() as demo:
|
28 |
+
history = gr.State([])
|
29 |
+
token_count = gr.State([])
|
30 |
+
promptTemplates = gr.State(load_template(get_template_names(plain=True)[0], mode=2))
|
31 |
+
user_api_key = gr.State(my_api_key)
|
32 |
+
TRUECOMSTANT = gr.State(True)
|
33 |
+
FALSECONSTANT = gr.State(False)
|
34 |
+
gr.Markdown(title)
|
35 |
+
|
36 |
+
with gr.Accordion("Build by [45度科研人](WeChat Public Accounts)", open=False):
|
37 |
+
gr.Markdown(description)
|
38 |
+
|
39 |
+
with gr.Row(scale=1).style(equal_height=True):
|
40 |
+
with gr.Column(scale=5):
|
41 |
+
with gr.Column():
|
42 |
+
chatbot = gr.Chatbot().style(color_map=("blue", "green"))
|
43 |
+
user_input = gr.Textbox(show_label=False, placeholder="Enter text and press submit", visible=True).style(container=False)
|
44 |
+
submitBtn = gr.Button("Submit", variant="primary").style(container=False)
|
45 |
+
emptyBtn = gr.Button("Restart Conversation")
|
46 |
+
status_display = gr.Markdown("")
|
47 |
+
|
48 |
+
with gr.Column():
|
49 |
+
with gr.Column(min_width=50):
|
50 |
+
with gr.Tab(label="ChatGPT"):
|
51 |
+
with gr.Column():
|
52 |
+
with gr.Row():
|
53 |
+
keyTxt = gr.Textbox(show_label=False, placeholder=f"You can input your own openAI API-key",value=hide_middle_chars(my_api_key),visible=True, type="password", label="API-Key")
|
54 |
+
# keyTxt = gr.Textbox(show_label=False, placeholder=f"You can input your own openAI API-key",value=my_api_key,visible=True, type="password", label="API-Key")
|
55 |
+
systemPromptTxt = gr.Textbox(show_label=True,placeholder=f"Set a custom insruction for the chatbot: You are a helpful assistant.",label="Custom prompt",value=initial_prompt,lines=10,).style(container=False)
|
56 |
+
|
57 |
+
with gr.Row():
|
58 |
+
templateSelectDropdown = gr.Dropdown(label="load from template",choices=load_template(get_template_names(plain=True)[0], mode=1),
|
59 |
+
multiselect=False,value=load_template(get_template_names(plain=True)[0], mode=1)[0],).style(container=False)
|
60 |
+
|
61 |
+
with gr.Tab(label="Settings"):
|
62 |
+
with gr.Column():
|
63 |
+
with gr.Row():
|
64 |
+
with gr.Column(scale=3):
|
65 |
+
saveFileName = gr.Textbox(show_label=True, placeholder=f"output file name...",label='Save conversation history', value="").style(container=False)
|
66 |
+
with gr.Column(scale=1):
|
67 |
+
exportMarkdownBtn = gr.Button("Save")
|
68 |
+
with gr.Row():
|
69 |
+
with gr.Column(scale=1):
|
70 |
+
downloadFile = gr.File(interactive=False)
|
71 |
+
gr.Markdown("""
|
72 |
+
<div align=center>you can follow the WeChat public account [45度科研人] and leave me a message!
|
73 |
+
<div align=center><img width = '200' height ='200' src ="https://dunazo.oss-cn-beijing.aliyuncs.com/blog/wechat-simple.png"/></div>""")
|
74 |
+
keyTxt.submit(submit_key, keyTxt, [user_api_key, status_display])
|
75 |
+
keyTxt.change(submit_key, keyTxt, [user_api_key, status_display])
|
76 |
+
# Chatbot
|
77 |
+
user_input.submit(predict,[user_api_key,systemPromptTxt,history,user_input,chatbot,token_count,],[chatbot, history, status_display, token_count],show_progress=True)
|
78 |
+
user_input.submit(reset_textbox, [], [user_input])
|
79 |
+
|
80 |
+
submitBtn.click(predict,[user_api_key,systemPromptTxt,history,user_input,chatbot,token_count,],[chatbot, history, status_display, token_count],show_progress=True)
|
81 |
+
submitBtn.click(reset_textbox, [], [user_input])
|
82 |
+
|
83 |
+
emptyBtn.click(reset_state,outputs=[chatbot, history, token_count, status_display],show_progress=True,)
|
84 |
+
|
85 |
+
templateSelectDropdown.change(get_template_content,[promptTemplates, templateSelectDropdown, systemPromptTxt],[systemPromptTxt],show_progress=True,)
|
86 |
+
exportMarkdownBtn.click(export_markdown,[saveFileName, systemPromptTxt, history, chatbot],downloadFile,show_progress=True,)
|
87 |
+
downloadFile.change(load_chat_history,[downloadFile, systemPromptTxt, history, chatbot],[saveFileName, systemPromptTxt, history, chatbot],)
|
88 |
+
|
89 |
+
# demo.title = "Sydney-AI 2.0"
|
90 |
+
|
91 |
+
if __name__ == "__main__":
|
92 |
+
demo.queue().launch(debug=False,show_api=False,share=True)
|
chat_func.py
ADDED
@@ -0,0 +1,300 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding:utf-8 -*-
|
2 |
+
from __future__ import annotations
|
3 |
+
from typing import TYPE_CHECKING, List
|
4 |
+
|
5 |
+
import logging
|
6 |
+
import json
|
7 |
+
import os
|
8 |
+
import requests
|
9 |
+
|
10 |
+
from tqdm import tqdm
|
11 |
+
|
12 |
+
from presets import *
|
13 |
+
# from llama_func import *
|
14 |
+
from utils import *
|
15 |
+
|
16 |
+
# logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s")
|
17 |
+
|
18 |
+
if TYPE_CHECKING:
|
19 |
+
from typing import TypedDict
|
20 |
+
|
21 |
+
class DataframeData(TypedDict):
|
22 |
+
headers: List[str]
|
23 |
+
data: List[List[str | int | bool]]
|
24 |
+
|
25 |
+
|
26 |
+
initial_prompt = "You are a helpful assistant."
|
27 |
+
API_URL = "https://api.openai.com/v1/chat/completions"
|
28 |
+
TEMPLATES_DIR = "templates"
|
29 |
+
|
30 |
+
def get_response(
|
31 |
+
openai_api_key, system_prompt, history, stream, selected_model
|
32 |
+
):
|
33 |
+
headers = {
|
34 |
+
"Content-Type": "application/json",
|
35 |
+
"Authorization": f"Bearer {openai_api_key}",
|
36 |
+
}
|
37 |
+
|
38 |
+
history = [construct_system(system_prompt), *history]
|
39 |
+
|
40 |
+
payload = {
|
41 |
+
"model": selected_model,
|
42 |
+
"messages": history, # [{"role": "user", "content": f"{inputs}"}],
|
43 |
+
"temperature": 1.0, # 1.0,
|
44 |
+
"top_p": 1.0, # 1.0,
|
45 |
+
"n": 1,
|
46 |
+
"stream": stream,
|
47 |
+
"presence_penalty": 0,
|
48 |
+
"frequency_penalty": 0,
|
49 |
+
}
|
50 |
+
if stream:
|
51 |
+
timeout = timeout_streaming
|
52 |
+
else:
|
53 |
+
timeout = timeout_all
|
54 |
+
|
55 |
+
# 获取环境变量中的代理设置
|
56 |
+
http_proxy = os.environ.get("HTTP_PROXY") or os.environ.get("http_proxy")
|
57 |
+
https_proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy")
|
58 |
+
|
59 |
+
# 如果存在代理设置,使用它们
|
60 |
+
proxies = {}
|
61 |
+
if http_proxy:
|
62 |
+
logging.info(f"Using HTTP proxy: {http_proxy}")
|
63 |
+
proxies["http"] = http_proxy
|
64 |
+
if https_proxy:
|
65 |
+
logging.info(f"Using HTTPS proxy: {https_proxy}")
|
66 |
+
proxies["https"] = https_proxy
|
67 |
+
|
68 |
+
# 如果有代理,使用代理发送请求,否则使用默认设置发送请求
|
69 |
+
if proxies:
|
70 |
+
response = requests.post(
|
71 |
+
API_URL,
|
72 |
+
headers=headers,
|
73 |
+
json=payload,
|
74 |
+
stream=True,
|
75 |
+
timeout=timeout,
|
76 |
+
proxies=proxies,
|
77 |
+
)
|
78 |
+
else:
|
79 |
+
response = requests.post(
|
80 |
+
API_URL,
|
81 |
+
headers=headers,
|
82 |
+
json=payload,
|
83 |
+
stream=True,
|
84 |
+
timeout=timeout,
|
85 |
+
)
|
86 |
+
return response
|
87 |
+
|
88 |
+
|
89 |
+
def stream_predict(
|
90 |
+
openai_api_key,
|
91 |
+
system_prompt,
|
92 |
+
history,
|
93 |
+
inputs,
|
94 |
+
chatbot,
|
95 |
+
all_token_counts,
|
96 |
+
selected_model,
|
97 |
+
fake_input=None,
|
98 |
+
display_append=""
|
99 |
+
):
|
100 |
+
def get_return_value():
|
101 |
+
return chatbot, history, status_text, all_token_counts
|
102 |
+
# logging.info("实时回答模式")
|
103 |
+
partial_words = ""
|
104 |
+
counter = 0
|
105 |
+
status_text = "answering……"
|
106 |
+
history.append(construct_user(inputs))
|
107 |
+
history.append(construct_assistant(""))
|
108 |
+
if fake_input:
|
109 |
+
chatbot.append((fake_input, ""))
|
110 |
+
else:
|
111 |
+
chatbot.append((inputs, ""))
|
112 |
+
user_token_count = 0
|
113 |
+
if len(all_token_counts) == 0:
|
114 |
+
system_prompt_token_count = count_token(construct_system(system_prompt))
|
115 |
+
user_token_count = (
|
116 |
+
count_token(construct_user(inputs)) + system_prompt_token_count
|
117 |
+
)
|
118 |
+
else:
|
119 |
+
user_token_count = count_token(construct_user(inputs))
|
120 |
+
all_token_counts.append(user_token_count)
|
121 |
+
logging.info(f"input token count: {user_token_count}")
|
122 |
+
yield get_return_value()
|
123 |
+
try:
|
124 |
+
response = get_response(
|
125 |
+
openai_api_key,
|
126 |
+
system_prompt,
|
127 |
+
history,
|
128 |
+
True,
|
129 |
+
selected_model,
|
130 |
+
)
|
131 |
+
except requests.exceptions.ConnectTimeout:
|
132 |
+
status_text = (
|
133 |
+
standard_error_msg + connection_timeout_prompt + error_retrieve_prompt
|
134 |
+
)
|
135 |
+
yield get_return_value()
|
136 |
+
return
|
137 |
+
except requests.exceptions.ReadTimeout:
|
138 |
+
status_text = standard_error_msg + read_timeout_prompt + error_retrieve_prompt
|
139 |
+
yield get_return_value()
|
140 |
+
return
|
141 |
+
|
142 |
+
yield get_return_value()
|
143 |
+
error_json_str = ""
|
144 |
+
|
145 |
+
for chunk in tqdm(response.iter_lines()):
|
146 |
+
if counter == 0:
|
147 |
+
counter += 1
|
148 |
+
continue
|
149 |
+
counter += 1
|
150 |
+
# check whether each line is non-empty
|
151 |
+
if chunk:
|
152 |
+
chunk = chunk.decode()
|
153 |
+
chunklength = len(chunk)
|
154 |
+
try:
|
155 |
+
chunk = json.loads(chunk[6:])
|
156 |
+
except json.JSONDecodeError:
|
157 |
+
logging.info(chunk)
|
158 |
+
error_json_str += chunk
|
159 |
+
status_text = f"JSON file parsing error. Please reset the conversation. received content: {error_json_str}"
|
160 |
+
yield get_return_value()
|
161 |
+
continue
|
162 |
+
# decode each line as response data is in bytes
|
163 |
+
if chunklength > 6 and "delta" in chunk["choices"][0]:
|
164 |
+
finish_reason = chunk["choices"][0]["finish_reason"]
|
165 |
+
status_text = construct_token_message(
|
166 |
+
sum(all_token_counts), stream=True
|
167 |
+
)
|
168 |
+
if finish_reason == "stop":
|
169 |
+
yield get_return_value()
|
170 |
+
break
|
171 |
+
try:
|
172 |
+
partial_words = (
|
173 |
+
partial_words + chunk["choices"][0]["delta"]["content"]
|
174 |
+
)
|
175 |
+
except KeyError:
|
176 |
+
status_text = (
|
177 |
+
standard_error_msg
|
178 |
+
+ "Token count has reached the maxtoken limit. Please reset the conversation. Current Token Count: "
|
179 |
+
+ str(sum(all_token_counts))
|
180 |
+
)
|
181 |
+
yield get_return_value()
|
182 |
+
break
|
183 |
+
history[-1] = construct_assistant(partial_words)
|
184 |
+
chatbot[-1] = (chatbot[-1][0], partial_words+display_append)
|
185 |
+
all_token_counts[-1] += 1
|
186 |
+
yield get_return_value()
|
187 |
+
|
188 |
+
|
189 |
+
def predict_all(
|
190 |
+
openai_api_key,
|
191 |
+
system_prompt,
|
192 |
+
history,
|
193 |
+
inputs,
|
194 |
+
chatbot,
|
195 |
+
all_token_counts,
|
196 |
+
selected_model,
|
197 |
+
fake_input=None,
|
198 |
+
display_append=""
|
199 |
+
):
|
200 |
+
# logging.info("一次性回答模式")
|
201 |
+
history.append(construct_user(inputs))
|
202 |
+
history.append(construct_assistant(""))
|
203 |
+
if fake_input:
|
204 |
+
chatbot.append((fake_input, ""))
|
205 |
+
else:
|
206 |
+
chatbot.append((inputs, ""))
|
207 |
+
all_token_counts.append(count_token(construct_user(inputs)))
|
208 |
+
try:
|
209 |
+
response = get_response(
|
210 |
+
openai_api_key,
|
211 |
+
system_prompt,
|
212 |
+
history,
|
213 |
+
False,
|
214 |
+
selected_model,
|
215 |
+
)
|
216 |
+
except requests.exceptions.ConnectTimeout:
|
217 |
+
status_text = (
|
218 |
+
standard_error_msg + connection_timeout_prompt + error_retrieve_prompt
|
219 |
+
)
|
220 |
+
return chatbot, history, status_text, all_token_counts
|
221 |
+
except requests.exceptions.ProxyError:
|
222 |
+
status_text = standard_error_msg + proxy_error_prompt + error_retrieve_prompt
|
223 |
+
return chatbot, history, status_text, all_token_counts
|
224 |
+
except requests.exceptions.SSLError:
|
225 |
+
status_text = standard_error_msg + ssl_error_prompt + error_retrieve_prompt
|
226 |
+
return chatbot, history, status_text, all_token_counts
|
227 |
+
response = json.loads(response.text)
|
228 |
+
content = response["choices"][0]["message"]["content"]
|
229 |
+
history[-1] = construct_assistant(content)
|
230 |
+
chatbot[-1] = (chatbot[-1][0], content+display_append)
|
231 |
+
total_token_count = response["usage"]["total_tokens"]
|
232 |
+
all_token_counts[-1] = total_token_count - sum(all_token_counts)
|
233 |
+
status_text = construct_token_message(total_token_count)
|
234 |
+
return chatbot, history, status_text, all_token_counts
|
235 |
+
|
236 |
+
|
237 |
+
def predict(
|
238 |
+
openai_api_key,
|
239 |
+
system_prompt,
|
240 |
+
history,
|
241 |
+
inputs,
|
242 |
+
chatbot,
|
243 |
+
all_token_counts,
|
244 |
+
stream=True,
|
245 |
+
selected_model=MODELS[0],
|
246 |
+
use_websearch=False,
|
247 |
+
files = None,
|
248 |
+
should_check_token_count=True,
|
249 |
+
): # repetition_penalty, top_k
|
250 |
+
|
251 |
+
old_inputs = ""
|
252 |
+
link_references = ""
|
253 |
+
|
254 |
+
if len(openai_api_key) != 51:
|
255 |
+
status_text = standard_error_msg + no_apikey_msg
|
256 |
+
logging.info(status_text)
|
257 |
+
chatbot.append((inputs, ""))
|
258 |
+
if len(history) == 0:
|
259 |
+
history.append(construct_user(inputs))
|
260 |
+
history.append("")
|
261 |
+
all_token_counts.append(0)
|
262 |
+
else:
|
263 |
+
history[-2] = construct_user(inputs)
|
264 |
+
yield chatbot, history, status_text, all_token_counts
|
265 |
+
return
|
266 |
+
|
267 |
+
yield chatbot, history, "answering……", all_token_counts
|
268 |
+
|
269 |
+
if stream:
|
270 |
+
# logging.info("使用流式传输")
|
271 |
+
iter = stream_predict(
|
272 |
+
openai_api_key,
|
273 |
+
system_prompt,
|
274 |
+
history,
|
275 |
+
inputs,
|
276 |
+
chatbot,
|
277 |
+
all_token_counts,
|
278 |
+
selected_model,
|
279 |
+
fake_input=old_inputs,
|
280 |
+
display_append=link_references
|
281 |
+
)
|
282 |
+
for chatbot, history, status_text, all_token_counts in iter:
|
283 |
+
yield chatbot, history, status_text, all_token_counts
|
284 |
+
else:
|
285 |
+
# logging.info("不使用流式传输")
|
286 |
+
chatbot, history, status_text, all_token_counts = predict_all(
|
287 |
+
openai_api_key,
|
288 |
+
system_prompt,
|
289 |
+
history,
|
290 |
+
inputs,
|
291 |
+
chatbot,
|
292 |
+
all_token_counts,
|
293 |
+
selected_model,
|
294 |
+
fake_input=old_inputs,
|
295 |
+
display_append=link_references
|
296 |
+
)
|
297 |
+
yield chatbot, history, status_text, all_token_counts
|
298 |
+
|
299 |
+
logging.info(f"The current token count is{all_token_counts}")
|
300 |
+
|
presets.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding:utf-8 -*-
|
2 |
+
use_websearch_checkbox=False
|
3 |
+
use_streaming_checkbox=True
|
4 |
+
model_select_dropdown="gpt-3.5-turbo"
|
5 |
+
# top_p=1
|
6 |
+
dockerflag = True
|
7 |
+
authflag = False
|
8 |
+
|
9 |
+
# ChatGPT 设置
|
10 |
+
initial_prompt = "You are a helpful assistant."
|
11 |
+
API_URL = "https://api.openai.com/v1/chat/completions"
|
12 |
+
HISTORY_DIR = "history"
|
13 |
+
TEMPLATES_DIR = "templates"
|
14 |
+
|
15 |
+
# 错误信息
|
16 |
+
standard_error_msg = "Error:" # 错误信息的标准前缀
|
17 |
+
error_retrieve_prompt = "Please check the network connection and the API-Key" # 获取对话时发生错误
|
18 |
+
connection_timeout_prompt = "Time out" # 连接超时
|
19 |
+
read_timeout_prompt = "Time out" # 读取超时
|
20 |
+
proxy_error_prompt = "Proxy error" # 代理错误
|
21 |
+
ssl_error_prompt = "SSL error" # SSL 错误
|
22 |
+
no_apikey_msg = "please check whether the input is correct" # API key 长度不足 51 位
|
23 |
+
|
24 |
+
max_token_streaming = 3500 # 流式对话时的最大 token 数
|
25 |
+
timeout_streaming = 5 # 流式对话时的超时时间
|
26 |
+
max_token_all = 3500 # 非流式对话时的最大 token 数
|
27 |
+
timeout_all = 200 # 非流式对话时的超时时间
|
28 |
+
enable_streaming_option = True # 是否启用选择选择是否实时显示回答的勾选框
|
29 |
+
HIDE_MY_KEY = True # 如果你想在UI中隐藏你的 API 密钥,将此值设置为 True
|
30 |
+
|
31 |
+
SIM_K = 5
|
32 |
+
INDEX_QUERY_TEMPRATURE = 1.0
|
33 |
+
title= """\
|
34 |
+
# <p align="center">Sydney-AI 2.0<b>"""
|
35 |
+
|
36 |
+
description = """\
|
37 |
+
<p>
|
38 |
+
<p>
|
39 |
+
|
40 |
+
本应用是一款基于最新OpenAI API“gpt-3.5-turbo”开发的智能在线聊天应用。 该应用程序的运营成本由“45 Degrees Research Fellows”赞助。 目前,token 限制为 3500。如果你想取消这个限制,你可以输入你自己的 OpenAI API 密钥。 <p>
|
41 |
+
App默认角色为ChatGPT原版助手,但您也可以从模板提供的角色中进行选择。 如果您对自定义Prompt有好的建议,请联系我们!<p>
|
42 |
+
This app is an intelligent online chat app developed based on the newly released OpenAI API "gpt-3.5-turbo". The app's operating costs are sponsored by "45度科研人". Currently, the tokens is limited to 3500. If you want to remove this restriction, you can input your own OpenAI API key.<p>
|
43 |
+
The default model role of the app is the original assistant of ChatGPT, but you can also choose from the provided roles. If you have good suggestions for customizing Prompt, please contact us!<p>
|
44 |
+
"""
|
45 |
+
|
46 |
+
MODELS = ["gpt-3.5-turbo", "gpt-3.5-turbo-0301",]
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.19.1
|
2 |
+
mdtex2html
|
3 |
+
pypinyin
|
4 |
+
tiktoken
|
5 |
+
tqdm
|
6 |
+
Pygments
|
7 |
+
markdown
|
utils.py
ADDED
@@ -0,0 +1,301 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding:utf-8 -*-
|
2 |
+
from __future__ import annotations
|
3 |
+
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Tuple, Type
|
4 |
+
import logging
|
5 |
+
import json
|
6 |
+
import os
|
7 |
+
|
8 |
+
import csv
|
9 |
+
import requests
|
10 |
+
import re
|
11 |
+
|
12 |
+
import gradio as gr
|
13 |
+
from pypinyin import lazy_pinyin
|
14 |
+
import tiktoken
|
15 |
+
import mdtex2html
|
16 |
+
from markdown import markdown
|
17 |
+
from pygments import highlight
|
18 |
+
from pygments.lexers import get_lexer_by_name
|
19 |
+
from pygments.formatters import HtmlFormatter
|
20 |
+
|
21 |
+
from presets import *
|
22 |
+
|
23 |
+
# logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s")
|
24 |
+
|
25 |
+
if TYPE_CHECKING:
|
26 |
+
from typing import TypedDict
|
27 |
+
|
28 |
+
class DataframeData(TypedDict):
|
29 |
+
headers: List[str]
|
30 |
+
data: List[List[str | int | bool]]
|
31 |
+
|
32 |
+
|
33 |
+
def count_token(message):
|
34 |
+
encoding = tiktoken.get_encoding("cl100k_base")
|
35 |
+
input_str = f"role: {message['role']}, content: {message['content']}"
|
36 |
+
length = len(encoding.encode(input_str))
|
37 |
+
return length
|
38 |
+
|
39 |
+
|
40 |
+
def markdown_to_html_with_syntax_highlight(md_str):
|
41 |
+
def replacer(match):
|
42 |
+
lang = match.group(1) or "text"
|
43 |
+
code = match.group(2)
|
44 |
+
|
45 |
+
try:
|
46 |
+
lexer = get_lexer_by_name(lang, stripall=True)
|
47 |
+
except ValueError:
|
48 |
+
lexer = get_lexer_by_name("text", stripall=True)
|
49 |
+
|
50 |
+
formatter = HtmlFormatter()
|
51 |
+
highlighted_code = highlight(code, lexer, formatter)
|
52 |
+
|
53 |
+
return f'<pre><code class="{lang}">{highlighted_code}</code></pre>'
|
54 |
+
|
55 |
+
code_block_pattern = r"```(\w+)?\n([\s\S]+?)\n```"
|
56 |
+
md_str = re.sub(code_block_pattern, replacer, md_str, flags=re.MULTILINE)
|
57 |
+
|
58 |
+
html_str = markdown(md_str)
|
59 |
+
return html_str
|
60 |
+
|
61 |
+
|
62 |
+
def normalize_markdown(md_text: str) -> str:
|
63 |
+
lines = md_text.split("\n")
|
64 |
+
normalized_lines = []
|
65 |
+
inside_list = False
|
66 |
+
|
67 |
+
for i, line in enumerate(lines):
|
68 |
+
if re.match(r"^(\d+\.|-|\*|\+)\s", line.strip()):
|
69 |
+
if not inside_list and i > 0 and lines[i - 1].strip() != "":
|
70 |
+
normalized_lines.append("")
|
71 |
+
inside_list = True
|
72 |
+
normalized_lines.append(line)
|
73 |
+
elif inside_list and line.strip() == "":
|
74 |
+
if i < len(lines) - 1 and not re.match(
|
75 |
+
r"^(\d+\.|-|\*|\+)\s", lines[i + 1].strip()
|
76 |
+
):
|
77 |
+
normalized_lines.append(line)
|
78 |
+
continue
|
79 |
+
else:
|
80 |
+
inside_list = False
|
81 |
+
normalized_lines.append(line)
|
82 |
+
|
83 |
+
return "\n".join(normalized_lines)
|
84 |
+
|
85 |
+
def postprocess(
|
86 |
+
self, y: List[Tuple[str | None, str | None]]
|
87 |
+
) -> List[Tuple[str | None, str | None]]:
|
88 |
+
"""
|
89 |
+
Parameters:
|
90 |
+
y: List of tuples representing the message and response pairs. Each message and response should be a string, which may be in Markdown format.
|
91 |
+
Returns:
|
92 |
+
List of tuples representing the message and response. Each message and response will be a string of HTML.
|
93 |
+
"""
|
94 |
+
if y is None or y == []:
|
95 |
+
return []
|
96 |
+
tag_regex = re.compile(r"^<\w+>[^<]+</\w+>")
|
97 |
+
if tag_regex.search(y[-1][1]):
|
98 |
+
y[-1] = (convert_user(y[-1][0]), y[-1][1])
|
99 |
+
else:
|
100 |
+
y[-1] = (convert_user(y[-1][0]), convert_mdtext(y[-1][1]))
|
101 |
+
return y
|
102 |
+
|
103 |
+
def convert_mdtext(md_text):
|
104 |
+
code_block_pattern = re.compile(r"```(.*?)(?:```|$)", re.DOTALL)
|
105 |
+
inline_code_pattern = re.compile(r"`(.*?)`", re.DOTALL)
|
106 |
+
code_blocks = code_block_pattern.findall(md_text)
|
107 |
+
non_code_parts = code_block_pattern.split(md_text)[::2]
|
108 |
+
|
109 |
+
result = []
|
110 |
+
for non_code, code in zip(non_code_parts, code_blocks + [""]):
|
111 |
+
if non_code.strip():
|
112 |
+
non_code = normalize_markdown(non_code)
|
113 |
+
if inline_code_pattern.search(non_code):
|
114 |
+
result.append(markdown(non_code, extensions=["tables"]))
|
115 |
+
else:
|
116 |
+
result.append(mdtex2html.convert(non_code, extensions=["tables"]))
|
117 |
+
if code.strip():
|
118 |
+
# _, code = detect_language(code) # 暂时去除代码高亮功能,因为在大段代码的情况下会出现问题
|
119 |
+
# code = code.replace("\n\n", "\n") # 暂时去除代码中的空行,因为在大段代码的情况下会出现问题
|
120 |
+
code = f"```{code}\n\n```"
|
121 |
+
code = markdown_to_html_with_syntax_highlight(code)
|
122 |
+
result.append(code)
|
123 |
+
result = "".join(result)
|
124 |
+
return result
|
125 |
+
|
126 |
+
def convert_user(userinput):
|
127 |
+
userinput = userinput.replace("\n", "<br>")
|
128 |
+
return f"<pre>{userinput}</pre>"
|
129 |
+
|
130 |
+
def construct_text(role, text):
|
131 |
+
return {"role": role, "content": text}
|
132 |
+
|
133 |
+
|
134 |
+
def construct_user(text):
|
135 |
+
return construct_text("user", text)
|
136 |
+
|
137 |
+
|
138 |
+
def construct_system(text):
|
139 |
+
return construct_text("system", text)
|
140 |
+
|
141 |
+
|
142 |
+
def construct_assistant(text):
|
143 |
+
return construct_text("assistant", text)
|
144 |
+
|
145 |
+
|
146 |
+
def construct_token_message(token, stream=False):
|
147 |
+
return f"Token count: {token}"
|
148 |
+
|
149 |
+
|
150 |
+
def save_file(filename, system, history, chatbot):
|
151 |
+
logging.info("saving......")
|
152 |
+
os.makedirs(HISTORY_DIR, exist_ok=True)
|
153 |
+
if filename.endswith(".json"):
|
154 |
+
json_s = {"system": system, "history": history, "chatbot": chatbot}
|
155 |
+
print(json_s)
|
156 |
+
with open(os.path.join(HISTORY_DIR, filename), "w") as f:
|
157 |
+
json.dump(json_s, f)
|
158 |
+
elif filename.endswith(".md"):
|
159 |
+
md_s = f"system: \n- {system} \n"
|
160 |
+
for data in history:
|
161 |
+
md_s += f"\n{data['role']}: \n- {data['content']} \n"
|
162 |
+
with open(os.path.join(HISTORY_DIR, filename), "w", encoding="utf8") as f:
|
163 |
+
f.write(md_s)
|
164 |
+
# logging.info("保存对话历史完毕")
|
165 |
+
return os.path.join(HISTORY_DIR, filename)
|
166 |
+
|
167 |
+
|
168 |
+
def save_chat_history(filename, system, history, chatbot):
|
169 |
+
if filename == "":
|
170 |
+
return
|
171 |
+
if not filename.endswith(".json"):
|
172 |
+
filename += ".json"
|
173 |
+
return save_file(filename, system, history, chatbot)
|
174 |
+
|
175 |
+
|
176 |
+
def export_markdown(filename, system, history, chatbot):
|
177 |
+
if filename == "":
|
178 |
+
return
|
179 |
+
if not filename.endswith(".md"):
|
180 |
+
filename += ".md"
|
181 |
+
return save_file(filename, system, history, chatbot)
|
182 |
+
|
183 |
+
|
184 |
+
def load_chat_history(filename, system, history, chatbot):
|
185 |
+
# logging.info("加载对话历史中……")
|
186 |
+
if type(filename) != str:
|
187 |
+
filename = filename.name
|
188 |
+
try:
|
189 |
+
with open(os.path.join(HISTORY_DIR, filename), "r") as f:
|
190 |
+
json_s = json.load(f)
|
191 |
+
try:
|
192 |
+
if type(json_s["history"][0]) == str:
|
193 |
+
# logging.info("历史记录格式为旧版,正在转换……")
|
194 |
+
new_history = []
|
195 |
+
for index, item in enumerate(json_s["history"]):
|
196 |
+
if index % 2 == 0:
|
197 |
+
new_history.append(construct_user(item))
|
198 |
+
else:
|
199 |
+
new_history.append(construct_assistant(item))
|
200 |
+
json_s["history"] = new_history
|
201 |
+
logging.info(new_history)
|
202 |
+
except:
|
203 |
+
# 没有对话历史
|
204 |
+
pass
|
205 |
+
# logging.info("加载对话历史完毕")
|
206 |
+
return filename, json_s["system"], json_s["history"], json_s["chatbot"]
|
207 |
+
except FileNotFoundError:
|
208 |
+
# logging.info("没有找到对话历史文件,不执行任何操作")
|
209 |
+
return filename, system, history, chatbot
|
210 |
+
|
211 |
+
|
212 |
+
def sorted_by_pinyin(list):
|
213 |
+
return sorted(list, key=lambda char: lazy_pinyin(char)[0][0])
|
214 |
+
|
215 |
+
|
216 |
+
def get_file_names(dir, plain=False, filetypes=[".json"]):
|
217 |
+
# logging.info(f"获取文件名列表,目录为{dir},文件类型为{filetypes},是否为纯文本列表{plain}")
|
218 |
+
files = []
|
219 |
+
try:
|
220 |
+
for type in filetypes:
|
221 |
+
files += [f for f in os.listdir(dir) if f.endswith(type)]
|
222 |
+
except FileNotFoundError:
|
223 |
+
files = []
|
224 |
+
files = sorted_by_pinyin(files)
|
225 |
+
if files == []:
|
226 |
+
files = [""]
|
227 |
+
if plain:
|
228 |
+
return files
|
229 |
+
else:
|
230 |
+
return gr.Dropdown.update(choices=files)
|
231 |
+
|
232 |
+
|
233 |
+
def get_history_names(plain=False):
|
234 |
+
# logging.info("获取历史记录文件名列表")
|
235 |
+
return get_file_names(HISTORY_DIR, plain)
|
236 |
+
|
237 |
+
|
238 |
+
def load_template(filename, mode=0):
|
239 |
+
# logging.info(f"加载模板文件{filename},模式为{mode}(0为返回字典和下拉菜单,1为返回下拉菜单,2为返回字典)")
|
240 |
+
lines = []
|
241 |
+
logging.info("Loading template...")
|
242 |
+
# filename='中文Prompts.json'
|
243 |
+
if filename.endswith(".json"):
|
244 |
+
with open(os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8") as f:
|
245 |
+
lines = json.load(f)
|
246 |
+
lines = [[i["act"], i["prompt"]] for i in lines]
|
247 |
+
else:
|
248 |
+
with open(
|
249 |
+
os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8") as csvfile:
|
250 |
+
reader = csv.reader(csvfile)
|
251 |
+
lines = list(reader)
|
252 |
+
lines = lines[1:]
|
253 |
+
if mode == 1:
|
254 |
+
return sorted_by_pinyin([row[0] for row in lines])
|
255 |
+
elif mode == 2:
|
256 |
+
return {row[0]: row[1] for row in lines}
|
257 |
+
else:
|
258 |
+
choices = sorted_by_pinyin([row[0] for row in lines])
|
259 |
+
return {row[0]: row[1] for row in lines}, gr.Dropdown.update(
|
260 |
+
choices=choices, value=choices[0])
|
261 |
+
|
262 |
+
|
263 |
+
def get_template_names(plain=False):
|
264 |
+
# logging.info("获取模板文件名列表")
|
265 |
+
return get_file_names(TEMPLATES_DIR, plain, filetypes=[".csv", "json"])
|
266 |
+
|
267 |
+
|
268 |
+
def get_template_content(templates, selection, original_system_prompt):
|
269 |
+
logging.info(f"Prompt: {selection}")
|
270 |
+
try:
|
271 |
+
return templates[selection]
|
272 |
+
except:
|
273 |
+
return original_system_prompt
|
274 |
+
|
275 |
+
|
276 |
+
def reset_state():
|
277 |
+
logging.info("Reset")
|
278 |
+
return [], [], [], construct_token_message(0)
|
279 |
+
|
280 |
+
|
281 |
+
def reset_textbox():
|
282 |
+
return gr.update(value="")
|
283 |
+
|
284 |
+
|
285 |
+
def hide_middle_chars(s):
|
286 |
+
if len(s) <= 8:
|
287 |
+
return s
|
288 |
+
else:
|
289 |
+
head = s[:4]
|
290 |
+
tail = s[-4:]
|
291 |
+
hidden = "*" * (len(s) - 8)
|
292 |
+
return head + hidden + tail
|
293 |
+
|
294 |
+
def submit_key(key):
|
295 |
+
key = key.strip()
|
296 |
+
msg = f"API-Key: {hide_middle_chars(key)}"
|
297 |
+
logging.info(msg)
|
298 |
+
return key, msg
|
299 |
+
|
300 |
+
|
301 |
+
|