Spaces:
Runtime error
Runtime error
JohnSmith9982
commited on
Commit
•
b0d15be
1
Parent(s):
890e483
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,13 @@
|
|
1 |
-
import json
|
2 |
import gradio as gr
|
3 |
# import openai
|
4 |
import os
|
5 |
import sys
|
6 |
-
import
|
7 |
-
import requests
|
8 |
-
# import markdown
|
9 |
-
import csv
|
10 |
|
11 |
my_api_key = "" # 在这里输入你的 API 密钥
|
12 |
HIDE_MY_KEY = False # 如果你想在UI中隐藏你的 API 密钥,将此值设置为 True
|
13 |
|
14 |
-
|
15 |
-
API_URL = "https://api.openai.com/v1/chat/completions"
|
16 |
-
HISTORY_DIR = "history"
|
17 |
-
TEMPLATES_DIR = "templates"
|
18 |
-
|
19 |
-
|
20 |
|
21 |
#if we are running in Docker
|
22 |
if os.environ.get('dockerrun') == 'yes':
|
@@ -24,6 +15,8 @@ if os.environ.get('dockerrun') == 'yes':
|
|
24 |
else:
|
25 |
dockerflag = False
|
26 |
|
|
|
|
|
27 |
if dockerflag:
|
28 |
my_api_key = os.environ.get('my_api_key')
|
29 |
if my_api_key == "empty":
|
@@ -32,237 +25,19 @@ if dockerflag:
|
|
32 |
#auth
|
33 |
username = os.environ.get('USERNAME')
|
34 |
password = os.environ.get('PASSWORD')
|
35 |
-
if isinstance(username, type(None)) or isinstance(password, type(None)):
|
36 |
-
authflag = False
|
37 |
-
else:
|
38 |
authflag = True
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
if count % 2 == 1:
|
51 |
-
lines[i] = f'<pre><code class="{items[-1]}">'
|
52 |
-
firstline = True
|
53 |
-
else:
|
54 |
-
lines[i] = f'</code></pre>'
|
55 |
-
else:
|
56 |
-
if i > 0:
|
57 |
-
if count % 2 == 1:
|
58 |
-
line = line.replace("`", "\`")
|
59 |
-
line = line.replace("\"", "`\"`")
|
60 |
-
line = line.replace("\'", "`\'`")
|
61 |
-
# line = line.replace("&", "&")
|
62 |
-
line = line.replace("<", "<")
|
63 |
-
line = line.replace(">", ">")
|
64 |
-
line = line.replace(" ", " ")
|
65 |
-
line = line.replace("*", "*")
|
66 |
-
line = line.replace("_", "_")
|
67 |
-
line = line.replace("-", "-")
|
68 |
-
line = line.replace(".", ".")
|
69 |
-
line = line.replace("!", "!")
|
70 |
-
line = line.replace("(", "(")
|
71 |
-
line = line.replace(")", ")")
|
72 |
-
line = line.replace("$", "$")
|
73 |
-
lines[i] = "<br>"+line
|
74 |
-
text = "".join(lines)
|
75 |
-
return text
|
76 |
-
|
77 |
-
def predict(inputs, top_p, temperature, openai_api_key, chatbot=[], history=[], system_prompt=initial_prompt, retry=False, summary=False, summary_on_crash = False, stream = True): # repetition_penalty, top_k
|
78 |
-
|
79 |
-
if summary:
|
80 |
-
stream = False
|
81 |
-
|
82 |
-
headers = {
|
83 |
-
"Content-Type": "application/json",
|
84 |
-
"Authorization": f"Bearer {openai_api_key}"
|
85 |
-
}
|
86 |
-
|
87 |
-
chat_counter = len(history) // 2
|
88 |
-
|
89 |
-
print(f"chat_counter - {chat_counter}")
|
90 |
-
|
91 |
-
messages = [compose_system(system_prompt)]
|
92 |
-
if chat_counter:
|
93 |
-
for index in range(0, 2*chat_counter, 2):
|
94 |
-
temp1 = {}
|
95 |
-
temp1["role"] = "user"
|
96 |
-
temp1["content"] = history[index]
|
97 |
-
temp2 = {}
|
98 |
-
temp2["role"] = "assistant"
|
99 |
-
temp2["content"] = history[index+1]
|
100 |
-
if temp1["content"] != "":
|
101 |
-
if temp2["content"] != "" or retry:
|
102 |
-
messages.append(temp1)
|
103 |
-
messages.append(temp2)
|
104 |
-
else:
|
105 |
-
messages[-1]['content'] = temp2['content']
|
106 |
-
if retry and chat_counter:
|
107 |
-
messages.pop()
|
108 |
-
elif summary:
|
109 |
-
history = [*[i["content"] for i in messages[-2:]], "我们刚刚聊了什么?"]
|
110 |
-
messages.append(compose_user(
|
111 |
-
"请帮我总结一下上述对话的内容,实现减少字数的同时,保证对话的质量。在总结中不要加入这一句话。"))
|
112 |
-
else:
|
113 |
-
temp3 = {}
|
114 |
-
temp3["role"] = "user"
|
115 |
-
temp3["content"] = inputs
|
116 |
-
messages.append(temp3)
|
117 |
-
chat_counter += 1
|
118 |
-
# messages
|
119 |
-
payload = {
|
120 |
-
"model": "gpt-3.5-turbo",
|
121 |
-
"messages": messages, # [{"role": "user", "content": f"{inputs}"}],
|
122 |
-
"temperature": temperature, # 1.0,
|
123 |
-
"top_p": top_p, # 1.0,
|
124 |
-
"n": 1,
|
125 |
-
"stream": stream,
|
126 |
-
"presence_penalty": 0,
|
127 |
-
"frequency_penalty": 0,
|
128 |
-
}
|
129 |
-
|
130 |
-
if not summary:
|
131 |
-
history.append(inputs)
|
132 |
-
else:
|
133 |
-
print("精简中...")
|
134 |
-
# make a POST request to the API endpoint using the requests.post method, passing in stream=True
|
135 |
-
response = requests.post(API_URL, headers=headers,
|
136 |
-
json=payload, stream=True)
|
137 |
-
|
138 |
-
token_counter = 0
|
139 |
-
partial_words = ""
|
140 |
-
|
141 |
-
counter = 0
|
142 |
-
if stream:
|
143 |
-
chatbot.append((parse_text(history[-1]), ""))
|
144 |
-
for chunk in response.iter_lines():
|
145 |
-
if counter == 0:
|
146 |
-
counter += 1
|
147 |
-
continue
|
148 |
-
counter += 1
|
149 |
-
# check whether each line is non-empty
|
150 |
-
if chunk:
|
151 |
-
# decode each line as response data is in bytes
|
152 |
-
try:
|
153 |
-
if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
|
154 |
-
break
|
155 |
-
except Exception as e:
|
156 |
-
traceback.print_exc()
|
157 |
-
print("Context 过长,正在尝试精简……")
|
158 |
-
chatbot.pop()
|
159 |
-
chatbot, history, status_text = next(predict(inputs, top_p, temperature, openai_api_key, chatbot, history, system_prompt, retry, summary=True, summary_on_crash=True, stream=False))
|
160 |
-
yield chatbot, history, status_text
|
161 |
-
if not "ERROR" in status_text:
|
162 |
-
print("精简完成,正在尝试重新生成……")
|
163 |
-
yield next(predict(inputs, top_p, temperature, openai_api_key, chatbot, history, system_prompt, retry, summary=False, summary_on_crash=True, stream=False))
|
164 |
-
else:
|
165 |
-
print("精简出错了,可能是网络原因。")
|
166 |
-
break
|
167 |
-
chunkjson = json.loads(chunk.decode()[6:])
|
168 |
-
status_text = f"id: {chunkjson['id']}, finish_reason: {chunkjson['choices'][0]['finish_reason']}"
|
169 |
-
partial_words = partial_words + \
|
170 |
-
json.loads(chunk.decode()[6:])[
|
171 |
-
'choices'][0]["delta"]["content"]
|
172 |
-
if token_counter == 0:
|
173 |
-
history.append(" " + partial_words)
|
174 |
-
else:
|
175 |
-
history[-1] = partial_words
|
176 |
-
chatbot[-1] = (parse_text(history[-2]), parse_text(history[-1]))
|
177 |
-
token_counter += 1
|
178 |
-
yield chatbot, history, status_text
|
179 |
-
else:
|
180 |
-
try:
|
181 |
-
responsejson = json.loads(response.text)
|
182 |
-
content = responsejson["choices"][0]["message"]["content"]
|
183 |
-
history.append(content)
|
184 |
-
chatbot.append((parse_text(history[-2]), parse_text(content)))
|
185 |
-
status_text = "精简完成"
|
186 |
-
except:
|
187 |
-
chatbot.append((parse_text(history[-1]), "☹️发生了错误,请检查网络连接或者稍后再试。"))
|
188 |
-
status_text = "status: ERROR"
|
189 |
-
yield chatbot, history, status_text
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
def delete_last_conversation(chatbot, history):
|
194 |
-
if "☹️发生了错误" in chatbot[-1][1]:
|
195 |
-
chatbot.pop()
|
196 |
-
print(history)
|
197 |
-
return chatbot, history
|
198 |
-
history.pop()
|
199 |
-
history.pop()
|
200 |
-
print(history)
|
201 |
-
return chatbot, history
|
202 |
-
|
203 |
-
def save_chat_history(filename, system, history, chatbot):
|
204 |
-
if filename == "":
|
205 |
-
return
|
206 |
-
if not filename.endswith(".json"):
|
207 |
-
filename += ".json"
|
208 |
-
os.makedirs(HISTORY_DIR, exist_ok=True)
|
209 |
-
json_s = {"system": system, "history": history, "chatbot": chatbot}
|
210 |
-
print(json_s)
|
211 |
-
with open(os.path.join(HISTORY_DIR, filename), "w") as f:
|
212 |
-
json.dump(json_s, f)
|
213 |
-
|
214 |
-
|
215 |
-
def load_chat_history(filename):
|
216 |
-
with open(os.path.join(HISTORY_DIR, filename), "r") as f:
|
217 |
-
json_s = json.load(f)
|
218 |
-
print(json_s)
|
219 |
-
return filename, json_s["system"], json_s["history"], json_s["chatbot"]
|
220 |
-
|
221 |
-
|
222 |
-
def get_file_names(dir, plain=False, filetype=".json"):
|
223 |
-
# find all json files in the current directory and return their names
|
224 |
-
try:
|
225 |
-
files = sorted([f for f in os.listdir(dir) if f.endswith(filetype)])
|
226 |
-
except FileNotFoundError:
|
227 |
-
files = []
|
228 |
-
if plain:
|
229 |
-
return files
|
230 |
-
else:
|
231 |
-
return gr.Dropdown.update(choices=files)
|
232 |
-
|
233 |
-
def get_history_names(plain=False):
|
234 |
-
return get_file_names(HISTORY_DIR, plain)
|
235 |
-
|
236 |
-
def load_template(filename, mode=0):
|
237 |
-
lines = []
|
238 |
-
with open(os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8") as csvfile:
|
239 |
-
reader = csv.reader(csvfile)
|
240 |
-
lines = list(reader)
|
241 |
-
lines = lines[1:]
|
242 |
-
if mode == 1:
|
243 |
-
return sorted([row[0] for row in lines])
|
244 |
-
elif mode == 2:
|
245 |
-
return {row[0]:row[1] for row in lines}
|
246 |
-
else:
|
247 |
-
return {row[0]:row[1] for row in lines}, gr.Dropdown.update(choices=sorted([row[0] for row in lines]))
|
248 |
-
|
249 |
-
def get_template_names(plain=False):
|
250 |
-
return get_file_names(TEMPLATES_DIR, plain, filetype=".csv")
|
251 |
-
|
252 |
-
def reset_state():
|
253 |
-
return [], []
|
254 |
-
|
255 |
-
|
256 |
-
def compose_system(system_prompt):
|
257 |
-
return {"role": "system", "content": system_prompt}
|
258 |
-
|
259 |
-
|
260 |
-
def compose_user(user_input):
|
261 |
-
return {"role": "user", "content": user_input}
|
262 |
-
|
263 |
-
|
264 |
-
def reset_textbox():
|
265 |
-
return gr.update(value='')
|
266 |
|
267 |
title = """<h1 align="center">川虎ChatGPT 🚀</h1>"""
|
268 |
description = """<div align=center>
|
@@ -399,7 +174,10 @@ if dockerflag:
|
|
399 |
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
400 |
#if not running in Docker
|
401 |
else:
|
402 |
-
|
|
|
|
|
|
|
403 |
#demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False) # 可自定义端口
|
404 |
#demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=("在这里填写用户名", "在这里填写密码")) # 可设置用户名与密码
|
405 |
#demo.queue().launch(auth=("在这里填写用户名", "在这里填写密码")) # 适合Nginx反向代理
|
|
|
|
|
1 |
import gradio as gr
|
2 |
# import openai
|
3 |
import os
|
4 |
import sys
|
5 |
+
from utils import *
|
|
|
|
|
|
|
6 |
|
7 |
my_api_key = "" # 在这里输入你的 API 密钥
|
8 |
HIDE_MY_KEY = False # 如果你想在UI中隐藏你的 API 密钥,将此值设置为 True
|
9 |
|
10 |
+
gr.Chatbot.postprocess = postprocess
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
#if we are running in Docker
|
13 |
if os.environ.get('dockerrun') == 'yes':
|
|
|
15 |
else:
|
16 |
dockerflag = False
|
17 |
|
18 |
+
authflag = False
|
19 |
+
|
20 |
if dockerflag:
|
21 |
my_api_key = os.environ.get('my_api_key')
|
22 |
if my_api_key == "empty":
|
|
|
25 |
#auth
|
26 |
username = os.environ.get('USERNAME')
|
27 |
password = os.environ.get('PASSWORD')
|
28 |
+
if not (isinstance(username, type(None)) or isinstance(password, type(None))):
|
|
|
|
|
29 |
authflag = True
|
30 |
+
else:
|
31 |
+
if os.path.exists("api_key.txt"):
|
32 |
+
with open("api_key.txt", "r") as f:
|
33 |
+
my_api_key = f.read().strip()
|
34 |
+
if os.path.exists("auth.json"):
|
35 |
+
with open("auth.json", "r") as f:
|
36 |
+
auth = json.load(f)
|
37 |
+
username = auth["username"]
|
38 |
+
password = auth["password"]
|
39 |
+
if username != "" and password != "":
|
40 |
+
authflag = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
title = """<h1 align="center">川虎ChatGPT 🚀</h1>"""
|
43 |
description = """<div align=center>
|
|
|
174 |
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
175 |
#if not running in Docker
|
176 |
else:
|
177 |
+
if authflag:
|
178 |
+
demo.queue().launch(share=False, auth=(username, password))
|
179 |
+
else:
|
180 |
+
demo.queue().launch(share=False) # 改为 share=True 可以创建公开分享链接
|
181 |
#demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False) # 可自定义端口
|
182 |
#demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=("在这里填写用户名", "在这里填写密码")) # 可设置用户名与密码
|
183 |
#demo.queue().launch(auth=("在这里填写用户名", "在这里填写密码")) # 适合Nginx反向代理
|