Spaces:
Running
Running
Update bin_public/app/app.py
Browse files- bin_public/app/app.py +62 -31
bin_public/app/app.py
CHANGED
@@ -1,7 +1,9 @@
|
|
1 |
# -*- coding:utf-8 -*-
|
|
|
2 |
import sys
|
3 |
-
from
|
4 |
-
from
|
|
|
5 |
|
6 |
my_api_key = ""
|
7 |
|
@@ -40,9 +42,10 @@ else:
|
|
40 |
|
41 |
gr.Chatbot.postprocess = postprocess
|
42 |
|
43 |
-
with gr.Blocks(css=customCSS
|
44 |
history = gr.State([])
|
45 |
token_count = gr.State([])
|
|
|
46 |
promptTemplates = gr.State(load_template(get_template_names(plain=True)[0], mode=2))
|
47 |
TRUECOMSTANT = gr.State(True)
|
48 |
FALSECONSTANT = gr.State(False)
|
@@ -114,21 +117,31 @@ with gr.Blocks(css=customCSS, ) as demo:
|
|
114 |
with gr.Tab(label="保存/加载"):
|
115 |
with gr.Accordion(label="保存/加载对话历史记录", open=True):
|
116 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
with gr.Row():
|
118 |
with gr.Column(scale=6):
|
119 |
saveFileName = gr.Textbox(
|
120 |
-
show_label=True,
|
121 |
-
|
|
|
|
|
|
|
122 |
with gr.Column(scale=1):
|
123 |
saveHistoryBtn = gr.Button("💾 保存对话")
|
|
|
|
|
124 |
with gr.Row():
|
125 |
-
with gr.Column(
|
126 |
-
|
127 |
-
choices=get_history_names(plain=True),
|
128 |
-
multiselect=False,
|
129 |
-
value=get_history_names(plain=True)[0])
|
130 |
-
with gr.Column(scale=1):
|
131 |
-
historyRefreshBtn = gr.Button("🔄 刷新")
|
132 |
|
133 |
gr.HTML("""
|
134 |
<div style="text-align: center; margin-top: 20px; margin-bottom: 20px;">
|
@@ -136,41 +149,59 @@ with gr.Blocks(css=customCSS, ) as demo:
|
|
136 |
gr.Markdown(description)
|
137 |
|
138 |
# 输入为api key则保持不变,为邀请码则调用中心的api key
|
139 |
-
key_button.click(key_preprocessing, [keyTXT], [
|
140 |
|
141 |
-
user_input.submit(predict, [keyTxt, systemPromptTxt, history, user_input, chatbot, token_count, top_p,
|
142 |
-
use_streaming_checkbox, model_select_dropdown, use_websearch_checkbox],
|
143 |
[chatbot, history, status_display, token_count], show_progress=True)
|
144 |
user_input.submit(reset_textbox, [], [user_input])
|
145 |
|
146 |
-
submitBtn.click(predict, [keyTxt, systemPromptTxt, history, user_input, chatbot, token_count, top_p,
|
147 |
-
use_streaming_checkbox, model_select_dropdown, use_websearch_checkbox],
|
148 |
[chatbot, history, status_display, token_count], show_progress=True)
|
149 |
submitBtn.click(reset_textbox, [], [user_input])
|
150 |
|
151 |
emptyBtn.click(reset_state, outputs=[chatbot, history, token_count, status_display], show_progress=True)
|
152 |
|
153 |
retryBtn.click(retry,
|
154 |
-
[keyTxt, systemPromptTxt, history, chatbot, token_count, top_p, temperature, use_streaming_checkbox,
|
155 |
model_select_dropdown], [chatbot, history, status_display, token_count], show_progress=True)
|
156 |
|
157 |
delLastBtn.click(delete_last_conversation, [chatbot, history, token_count], [
|
158 |
chatbot, history, token_count, status_display], show_progress=True)
|
159 |
|
160 |
-
reduceTokenBtn.click(reduce_token_size, [keyTxt, systemPromptTxt, history, chatbot, token_count, top_p,
|
161 |
-
use_streaming_checkbox, model_select_dropdown],
|
162 |
[chatbot, history, status_display, token_count], show_progress=True)
|
163 |
-
|
164 |
-
saveHistoryBtn.click(
|
165 |
-
|
166 |
-
|
|
|
|
|
|
|
167 |
saveHistoryBtn.click(get_history_names, None, [historyFileSelectDropdown])
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])
|
175 |
|
176 |
templateFileSelectDropdown.change(load_template, [templateFileSelectDropdown],
|
@@ -179,7 +210,7 @@ with gr.Blocks(css=customCSS, ) as demo:
|
|
179 |
templateSelectDropdown.change(get_template_content, [promptTemplates, templateSelectDropdown, systemPromptTxt],
|
180 |
[systemPromptTxt], show_progress=True)
|
181 |
|
182 |
-
logging.info(
|
183 |
# 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
|
184 |
demo.title = "ChatGPT-长江商学院 🚀"
|
185 |
|
|
|
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 |
|
|
|
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)
|
|
|
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;">
|
|
|
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],
|
|
|
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 |
|