Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
@@ -1,229 +0,0 @@
|
|
1 |
-
# -*- coding:utf-8 -*-
|
2 |
-
|
3 |
-
import sys
|
4 |
-
from bin_public.utils.utils import *
|
5 |
-
from bin_public.utils.utils_db import *
|
6 |
-
from bin_public.config.presets import *
|
7 |
-
|
8 |
-
my_api_key = ""
|
9 |
-
|
10 |
-
# if we are running in Docker
|
11 |
-
if os.environ.get('dockerrun') == 'yes':
|
12 |
-
dockerflag = True
|
13 |
-
else:
|
14 |
-
dockerflag = False
|
15 |
-
|
16 |
-
authflag = False
|
17 |
-
|
18 |
-
if dockerflag:
|
19 |
-
my_api_key = os.environ.get('my_api_key')
|
20 |
-
if my_api_key == "empty":
|
21 |
-
print("Please give a api key!")
|
22 |
-
sys.exit(1)
|
23 |
-
# auth
|
24 |
-
username = os.environ.get('USERNAME')
|
25 |
-
password = os.environ.get('PASSWORD')
|
26 |
-
if not (isinstance(username, type(None)) or isinstance(password, type(None))):
|
27 |
-
authflag = True
|
28 |
-
else:
|
29 |
-
'''if not my_api_key and os.path.exists("api_key.txt") and os.path.getsize("api_key.txt"): # API key 所在的文件
|
30 |
-
with open("api_key.txt", "r") as f:
|
31 |
-
my_api_key = f.read().strip()'''
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
if os.path.exists("auth.json"):
|
36 |
-
with open("auth.json", "r") as f:
|
37 |
-
auth = json.load(f)
|
38 |
-
username = auth["username"]
|
39 |
-
password = auth["password"]
|
40 |
-
if username != "" and password != "":
|
41 |
-
authflag = True
|
42 |
-
|
43 |
-
gr.Chatbot.postprocess = postprocess
|
44 |
-
|
45 |
-
with gr.Blocks(css=customCSS) as demo:
|
46 |
-
history = gr.State([])
|
47 |
-
token_count = gr.State([])
|
48 |
-
invite_code = gr.State()
|
49 |
-
promptTemplates = gr.State(load_template(get_template_names(plain=True)[0], mode=2))
|
50 |
-
TRUECOMSTANT = gr.State(True)
|
51 |
-
FALSECONSTANT = gr.State(False)
|
52 |
-
topic = gr.State("未命名对话历史记录")
|
53 |
-
|
54 |
-
# gr.HTML("""
|
55 |
-
# <div style="text-align: center; margin-top: 20px;">
|
56 |
-
# """)
|
57 |
-
gr.HTML(title)
|
58 |
-
|
59 |
-
with gr.Row(scale=1).style(equal_height=True):
|
60 |
-
with gr.Column(scale=5):
|
61 |
-
with gr.Row(scale=1):
|
62 |
-
chatbot = gr.Chatbot().style(height=600) # .style(color_map=("#1D51EE", "#585A5B"))
|
63 |
-
with gr.Row(scale=1):
|
64 |
-
with gr.Column(scale=12):
|
65 |
-
user_input = gr.Textbox(show_label=False, placeholder="在这里输入").style(
|
66 |
-
container=False)
|
67 |
-
with gr.Column(min_width=50, scale=1):
|
68 |
-
submitBtn = gr.Button("🚀", variant="primary")
|
69 |
-
with gr.Row(scale=1):
|
70 |
-
emptyBtn = gr.Button("🧹 新的对话", )
|
71 |
-
retryBtn = gr.Button("🔄 重新生成")
|
72 |
-
delLastBtn = gr.Button("🗑️ 删除一条对话")
|
73 |
-
reduceTokenBtn = gr.Button("♻️ 总结对话")
|
74 |
-
|
75 |
-
with gr.Column():
|
76 |
-
with gr.Column(min_width=50, scale=1):
|
77 |
-
status_display = gr.Markdown("status: ready")
|
78 |
-
with gr.Tab(label="ChatGPT"):
|
79 |
-
keyTXT = gr.Textbox(show_label=True, placeholder=f"OpenAI API-key...",
|
80 |
-
type="password", visible=not HIDE_MY_KEY, label="API-Key/Invite-Code")
|
81 |
-
|
82 |
-
keyTxt = gr.Textbox(visible=False)
|
83 |
-
|
84 |
-
key_button = gr.Button("Enter")
|
85 |
-
|
86 |
-
model_select_dropdown = gr.Dropdown(label="选择模型", choices=MODELS, multiselect=False,
|
87 |
-
value=MODELS[0])
|
88 |
-
with gr.Accordion("参数", open=False):
|
89 |
-
temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0,
|
90 |
-
step=0.1, interactive=True, label="Temperature", )
|
91 |
-
top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.05,
|
92 |
-
interactive=True, label="Top-p (nucleus sampling)", visible=False)
|
93 |
-
use_streaming_checkbox = gr.Checkbox(label="实时传输回答", value=True, visible=enable_streaming_option)
|
94 |
-
use_websearch_checkbox = gr.Checkbox(label="使用在线搜索", value=False)
|
95 |
-
|
96 |
-
with gr.Tab(label="Prompt"):
|
97 |
-
systemPromptTxt = gr.Textbox(show_label=True, placeholder=f"在这里输入System Prompt...",
|
98 |
-
label="System prompt", value=initial_prompt).style(container=True)
|
99 |
-
with gr.Accordion(label="加载Prompt模板", open=True):
|
100 |
-
with gr.Column():
|
101 |
-
with gr.Row():
|
102 |
-
with gr.Column(scale=6):
|
103 |
-
templateFileSelectDropdown = gr.Dropdown(label="选择Prompt模板集合文件",
|
104 |
-
choices=get_template_names(plain=True),
|
105 |
-
multiselect=False,
|
106 |
-
value=get_template_names(plain=True)[0])
|
107 |
-
with gr.Column(scale=1):
|
108 |
-
templateRefreshBtn = gr.Button("🔄 刷新")
|
109 |
-
with gr.Row():
|
110 |
-
with gr.Column():
|
111 |
-
templateSelectDropdown = gr.Dropdown(label="从Prompt模板中加载", choices=load_template(
|
112 |
-
get_template_names(plain=True)[0], mode=1), multiselect=False, value=
|
113 |
-
load_template(
|
114 |
-
get_template_names(plain=True)[0], mode=1)[
|
115 |
-
0])
|
116 |
-
|
117 |
-
with gr.Tab(label="保存/加载"):
|
118 |
-
with gr.Accordion(label="保存/加载对话历史记录", open=True):
|
119 |
-
with gr.Column():
|
120 |
-
with gr.Row():
|
121 |
-
with gr.Column(scale=6):
|
122 |
-
historyFileSelectDropdown = gr.Dropdown(
|
123 |
-
label="从列表中加载对话",
|
124 |
-
choices=get_history_names(plain=True),
|
125 |
-
multiselect=False,
|
126 |
-
value=get_history_names(plain=True)[0],
|
127 |
-
visible=False
|
128 |
-
)
|
129 |
-
|
130 |
-
with gr.Row():
|
131 |
-
with gr.Column(scale=6):
|
132 |
-
saveFileName = gr.Textbox(
|
133 |
-
show_label=True,
|
134 |
-
placeholder=f"设置文件名: 默认为.json,可选为.md",
|
135 |
-
label="设置保存文件名",
|
136 |
-
value="对话历史记录",
|
137 |
-
).style(container=True)
|
138 |
-
with gr.Column(scale=1):
|
139 |
-
saveHistoryBtn = gr.Button("💾 保存对话")
|
140 |
-
exportMarkdownBtn = gr.Button("📝 导出为Markdown")
|
141 |
-
#gr.Markdown("默认保存于history文件夹")
|
142 |
-
with gr.Row():
|
143 |
-
with gr.Column():
|
144 |
-
downloadFile = gr.File(interactive=True)
|
145 |
-
|
146 |
-
gr.HTML("""
|
147 |
-
<div style="text-align: center; margin-top: 20px; margin-bottom: 20px;">
|
148 |
-
""")
|
149 |
-
gr.Markdown(description)
|
150 |
-
|
151 |
-
# 输入为api key则保持不变,为邀请码则调用中心的api key
|
152 |
-
key_button.click(key_preprocessing, [keyTXT], [status_display, keyTxt, invite_code])
|
153 |
-
|
154 |
-
user_input.submit(predict, [keyTxt, invite_code, systemPromptTxt, history, user_input, chatbot, token_count, top_p,
|
155 |
-
temperature, use_streaming_checkbox, model_select_dropdown, use_websearch_checkbox],
|
156 |
-
[chatbot, history, status_display, token_count], show_progress=True)
|
157 |
-
user_input.submit(reset_textbox, [], [user_input])
|
158 |
-
|
159 |
-
submitBtn.click(predict, [keyTxt, invite_code, systemPromptTxt, history, user_input, chatbot, token_count, top_p,
|
160 |
-
temperature, use_streaming_checkbox, model_select_dropdown, use_websearch_checkbox],
|
161 |
-
[chatbot, history, status_display, token_count], show_progress=True)
|
162 |
-
submitBtn.click(reset_textbox, [], [user_input])
|
163 |
-
|
164 |
-
emptyBtn.click(reset_state, outputs=[chatbot, history, token_count, status_display], show_progress=True)
|
165 |
-
|
166 |
-
retryBtn.click(retry,
|
167 |
-
[keyTxt, invite_code, systemPromptTxt, history, chatbot, token_count, top_p, temperature, use_streaming_checkbox,
|
168 |
-
model_select_dropdown], [chatbot, history, status_display, token_count], show_progress=True)
|
169 |
-
|
170 |
-
delLastBtn.click(delete_last_conversation, [chatbot, history, token_count], [
|
171 |
-
chatbot, history, token_count, status_display], show_progress=True)
|
172 |
-
|
173 |
-
reduceTokenBtn.click(reduce_token_size, [keyTxt, invite_code, systemPromptTxt, history, chatbot, token_count, top_p,
|
174 |
-
temperature, use_streaming_checkbox, model_select_dropdown],
|
175 |
-
[chatbot, history, status_display, token_count], show_progress=True)
|
176 |
-
# History
|
177 |
-
saveHistoryBtn.click(
|
178 |
-
save_chat_history,
|
179 |
-
[saveFileName, systemPromptTxt, history, chatbot],
|
180 |
-
downloadFile,
|
181 |
-
show_progress=True,
|
182 |
-
)
|
183 |
-
saveHistoryBtn.click(get_history_names, None, [historyFileSelectDropdown])
|
184 |
-
exportMarkdownBtn.click(
|
185 |
-
export_markdown,
|
186 |
-
[saveFileName, systemPromptTxt, history, chatbot],
|
187 |
-
downloadFile,
|
188 |
-
show_progress=True,
|
189 |
-
)
|
190 |
-
#historyRefreshBtn.click(get_history_names, None, [historyFileSelectDropdown])
|
191 |
-
historyFileSelectDropdown.change(
|
192 |
-
load_chat_history,
|
193 |
-
[historyFileSelectDropdown, systemPromptTxt, history, chatbot],
|
194 |
-
[saveFileName, systemPromptTxt, history, chatbot],
|
195 |
-
show_progress=True,
|
196 |
-
)
|
197 |
-
downloadFile.change(
|
198 |
-
load_chat_history,
|
199 |
-
[downloadFile, systemPromptTxt, history, chatbot],
|
200 |
-
[saveFileName, systemPromptTxt, history, chatbot],
|
201 |
-
)
|
202 |
-
|
203 |
-
|
204 |
-
# Template
|
205 |
-
templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])
|
206 |
-
|
207 |
-
templateFileSelectDropdown.change(load_template, [templateFileSelectDropdown],
|
208 |
-
[promptTemplates, templateSelectDropdown], show_progress=True)
|
209 |
-
|
210 |
-
templateSelectDropdown.change(get_template_content, [promptTemplates, templateSelectDropdown, systemPromptTxt],
|
211 |
-
[systemPromptTxt], show_progress=True)
|
212 |
-
|
213 |
-
logging.info( "\n访问 http://localhost:7860 查看界面")
|
214 |
-
# 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
|
215 |
-
demo.title = "ChatGPT-长江商学院 🚀"
|
216 |
-
|
217 |
-
if __name__ == "__main__":
|
218 |
-
#if running in Docker
|
219 |
-
if dockerflag:
|
220 |
-
if authflag:
|
221 |
-
demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=(username, password))
|
222 |
-
else:
|
223 |
-
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
224 |
-
#if not running in Docker
|
225 |
-
else:
|
226 |
-
if authflag:
|
227 |
-
demo.queue().launch(share=False, auth=(username, password))
|
228 |
-
else:
|
229 |
-
demo.queue().launch(share=False) # 改为 share=True 可以创建公开分享链接
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|