AllenYkl commited on
Commit
9bc5b0a
·
1 Parent(s): d128ed0

Update bin_public/app/Chatbot.py

Browse files
Files changed (1) hide show
  1. bin_public/app/Chatbot.py +82 -53
bin_public/app/Chatbot.py CHANGED
@@ -1,9 +1,10 @@
1
  # -*- coding:utf-8 -*-
2
 
3
- import sys
4
  from overwrites import *
5
  from chat_func import *
6
- from bin_public.utils.tools import *
 
 
7
  from bin_public.utils.utils_db import *
8
  from bin_public.config.presets import *
9
 
@@ -32,8 +33,6 @@ else:
32
  with open("api_key.txt", "r") as f:
33
  my_api_key = f.read().strip()'''
34
 
35
-
36
-
37
  if os.path.exists("auth.json"):
38
  with open("auth.json", "r") as f:
39
  auth = json.load(f)
@@ -45,13 +44,17 @@ else:
45
  gr.Chatbot.postprocess = postprocess
46
  PromptHelper.compact_text_chunks = compact_text_chunks
47
 
 
 
 
48
  with gr.Blocks(css=customCSS) as demo:
49
  history = gr.State([])
50
  token_count = gr.State([])
51
  invite_code = gr.State()
52
  promptTemplates = gr.State(load_template(get_template_names(plain=True)[0], mode=2))
53
- TRUECOMSTANT = gr.State(True)
54
- FALSECONSTANT = gr.State(False)
 
55
  topic = gr.State("未命名对话历史记录")
56
 
57
  # gr.HTML("""
@@ -69,10 +72,12 @@ with gr.Blocks(css=customCSS) as demo:
69
  container=False)
70
  with gr.Column(min_width=50, scale=1):
71
  submitBtn = gr.Button("🚀", variant="primary")
 
72
  with gr.Row(scale=1):
73
  emptyBtn = gr.Button("🧹 新的对话", )
74
  retryBtn = gr.Button("🔄 重新生成")
75
- delLastBtn = gr.Button("🗑️ 删除一条对话")
 
76
  reduceTokenBtn = gr.Button("♻️ 总结对话")
77
 
78
  with gr.Column():
@@ -80,11 +85,14 @@ with gr.Blocks(css=customCSS) as demo:
80
  status_display = gr.Markdown("status: ready")
81
  with gr.Tab(label="ChatGPT"):
82
  keyTXT = gr.Textbox(show_label=True, placeholder=f"OpenAI API-key...",
83
- type="password", visible=not HIDE_MY_KEY, label="API-Key/Invite-Code")
 
 
84
 
85
  keyTxt = gr.Textbox(visible=False)
86
 
87
  key_button = gr.Button("Enter")
 
88
 
89
  model_select_dropdown = gr.Dropdown(label="选择模型", choices=MODELS, multiselect=False,
90
  value=MODELS[0])
@@ -97,7 +105,6 @@ with gr.Blocks(css=customCSS) as demo:
97
  use_websearch_checkbox = gr.Checkbox(label="使用在线搜索", value=False)
98
  index_files = gr.Files(label="上传索引文件", type="file", multiple=True)
99
 
100
-
101
  with gr.Tab(label="Prompt"):
102
  systemPromptTxt = gr.Textbox(show_label=True, placeholder=f"在这里输入System Prompt...",
103
  label="System prompt", value=initial_prompt).style(container=True)
@@ -143,7 +150,6 @@ with gr.Blocks(css=customCSS) as demo:
143
  with gr.Column(scale=1):
144
  saveHistoryBtn = gr.Button("💾 保存对话")
145
  exportMarkdownBtn = gr.Button("📝 导出为Markdown")
146
- #gr.Markdown("默认保存于history文件夹")
147
  with gr.Row():
148
  with gr.Column():
149
  downloadFile = gr.File(interactive=True)
@@ -153,40 +159,49 @@ with gr.Blocks(css=customCSS) as demo:
153
  """)
154
  gr.Markdown(description)
155
 
 
 
 
 
156
  # 输入为api key则保持不变,为邀请码则调用中心的api key
157
- key_button.click(key_preprocessing, [keyTXT], [status_display, keyTxt, invite_code])
158
-
159
- user_input.submit(predict, [
160
- keyTxt,
161
- invite_code,
162
- systemPromptTxt,
163
- history,
164
- user_input,
165
- chatbot,
166
- token_count,
167
- top_p,
168
- temperature,
169
- use_streaming_checkbox,
170
- model_select_dropdown,
171
- use_websearch_checkbox,
172
- index_files],
 
 
 
 
173
  [chatbot, history, status_display, token_count], show_progress=True)
 
174
  user_input.submit(reset_textbox, [], [user_input])
175
 
176
  submitBtn.click(predict, [
177
- keyTxt,
178
- invite_code,
179
- systemPromptTxt,
180
- history,
181
- user_input,
182
- chatbot,
183
- token_count,
184
- top_p,
185
- temperature,
186
- use_streaming_checkbox,
187
- model_select_dropdown,
188
- use_websearch_checkbox,
189
- index_files],
190
  [chatbot, history, status_display, token_count], show_progress=True)
191
 
192
  submitBtn.click(reset_textbox, [], [user_input])
@@ -194,11 +209,20 @@ with gr.Blocks(css=customCSS) as demo:
194
  emptyBtn.click(reset_state, outputs=[chatbot, history, token_count, status_display], show_progress=True)
195
 
196
  retryBtn.click(retry,
197
- [keyTxt, invite_code, systemPromptTxt, history, chatbot, token_count, top_p, temperature, use_streaming_checkbox,
 
198
  model_select_dropdown], [chatbot, history, status_display, token_count], show_progress=True)
199
 
200
- delLastBtn.click(delete_last_conversation, [chatbot, history, token_count], [
201
- chatbot, history, token_count, status_display], show_progress=True)
 
 
 
 
 
 
 
 
202
 
203
  reduceTokenBtn.click(reduce_token_size, [keyTxt, invite_code, systemPromptTxt, history, chatbot, token_count, top_p,
204
  temperature, use_streaming_checkbox, model_select_dropdown],
@@ -217,7 +241,7 @@ with gr.Blocks(css=customCSS) as demo:
217
  downloadFile,
218
  show_progress=True,
219
  )
220
- #historyRefreshBtn.click(get_history_names, None, [historyFileSelectDropdown])
221
  historyFileSelectDropdown.change(
222
  load_chat_history,
223
  [historyFileSelectDropdown, systemPromptTxt, history, chatbot],
@@ -230,30 +254,35 @@ with gr.Blocks(css=customCSS) as demo:
230
  [saveFileName, systemPromptTxt, history, chatbot],
231
  )
232
 
233
-
234
  # Template
235
  templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])
236
 
237
- templateFileSelectDropdown.change(load_template, [templateFileSelectDropdown],
238
- [promptTemplates, templateSelectDropdown], show_progress=True)
 
 
239
 
240
- templateSelectDropdown.change(get_template_content, [promptTemplates, templateSelectDropdown, systemPromptTxt],
241
- [systemPromptTxt], show_progress=True)
 
 
242
 
243
- logging.info( "\n访问 http://localhost:7860 查看界面")
244
  # 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
245
  demo.title = "ChatGPT-长江商学院 🚀"
246
 
247
  if __name__ == "__main__":
248
- #if running in Docker
 
 
249
  if dockerflag:
250
  if authflag:
251
- demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=(username, password))
252
  else:
253
  demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
254
- #if not running in Docker
255
  else:
256
  if authflag:
257
  demo.queue().launch(share=False, auth=(username, password))
258
  else:
259
- demo.queue().launch(share=False) # 改为 share=True 可以创建公开分享链接
 
1
  # -*- coding:utf-8 -*-
2
 
 
3
  from overwrites import *
4
  from chat_func import *
5
+ from openai_func import *
6
+
7
+ from bin_public.utils.utils import *
8
  from bin_public.utils.utils_db import *
9
  from bin_public.config.presets import *
10
 
 
33
  with open("api_key.txt", "r") as f:
34
  my_api_key = f.read().strip()'''
35
 
 
 
36
  if os.path.exists("auth.json"):
37
  with open("auth.json", "r") as f:
38
  auth = json.load(f)
 
44
  gr.Chatbot.postprocess = postprocess
45
  PromptHelper.compact_text_chunks = compact_text_chunks
46
 
47
+ with open(CSS_path, "r", encoding="utf-8") as f:
48
+ customCSS = f.read()
49
+
50
  with gr.Blocks(css=customCSS) as demo:
51
  history = gr.State([])
52
  token_count = gr.State([])
53
  invite_code = gr.State()
54
  promptTemplates = gr.State(load_template(get_template_names(plain=True)[0], mode=2))
55
+ user_api_key = gr.State(my_api_key)
56
+ user_question = gr.State("")
57
+ outputing = gr.State(False)
58
  topic = gr.State("未命名对话历史记录")
59
 
60
  # gr.HTML("""
 
72
  container=False)
73
  with gr.Column(min_width=50, scale=1):
74
  submitBtn = gr.Button("🚀", variant="primary")
75
+ cancelBtn = gr.Button("取消", variant="secondary", visible=False)
76
  with gr.Row(scale=1):
77
  emptyBtn = gr.Button("🧹 新的对话", )
78
  retryBtn = gr.Button("🔄 重新生成")
79
+ delFirstBtn = gr.Button("🗑️ 删除最旧对话")
80
+ delLastBtn = gr.Button("🗑️ 删除最新对话")
81
  reduceTokenBtn = gr.Button("♻️ 总结对话")
82
 
83
  with gr.Column():
 
85
  status_display = gr.Markdown("status: ready")
86
  with gr.Tab(label="ChatGPT"):
87
  keyTXT = gr.Textbox(show_label=True, placeholder=f"OpenAI API-key...",
88
+ type="password", visible=not HIDE_MY_KEY, label="API-Key/Invite-Code",
89
+ value="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")
90
+ usageTxt = gr.Markdown("**发送消息** 或 **提交key** 以显示额度", elem_id="usage_display")
91
 
92
  keyTxt = gr.Textbox(visible=False)
93
 
94
  key_button = gr.Button("Enter")
95
+ #usage_button = gr.Button("显示用量")
96
 
97
  model_select_dropdown = gr.Dropdown(label="选择模型", choices=MODELS, multiselect=False,
98
  value=MODELS[0])
 
105
  use_websearch_checkbox = gr.Checkbox(label="使用在线搜索", value=False)
106
  index_files = gr.Files(label="上传索引文件", type="file", multiple=True)
107
 
 
108
  with gr.Tab(label="Prompt"):
109
  systemPromptTxt = gr.Textbox(show_label=True, placeholder=f"在这里输入System Prompt...",
110
  label="System prompt", value=initial_prompt).style(container=True)
 
150
  with gr.Column(scale=1):
151
  saveHistoryBtn = gr.Button("💾 保存对话")
152
  exportMarkdownBtn = gr.Button("📝 导出为Markdown")
 
153
  with gr.Row():
154
  with gr.Column():
155
  downloadFile = gr.File(interactive=True)
 
159
  """)
160
  gr.Markdown(description)
161
 
162
+ get_usage_args = dict(
163
+ fn=get_usage, inputs=[keyTxt], outputs=[usageTxt], show_progress=False
164
+ )
165
+
166
  # 输入为api key则保持不变,为邀请码则调用中心的api key
167
+ key_button.click(key_preprocessing, [keyTXT], [status_display, keyTxt, invite_code]).then(**get_usage_args)
168
+
169
+ #usage_button.click(**get_usage_args)
170
+
171
+ user_input.submit(predict,
172
+ [
173
+ keyTxt,
174
+ invite_code,
175
+ systemPromptTxt,
176
+ history,
177
+ user_input,
178
+ chatbot,
179
+ token_count,
180
+ top_p,
181
+ temperature,
182
+ use_streaming_checkbox,
183
+ model_select_dropdown,
184
+ use_websearch_checkbox,
185
+ index_files
186
+ ],
187
  [chatbot, history, status_display, token_count], show_progress=True)
188
+
189
  user_input.submit(reset_textbox, [], [user_input])
190
 
191
  submitBtn.click(predict, [
192
+ keyTxt,
193
+ invite_code,
194
+ systemPromptTxt,
195
+ history,
196
+ user_input,
197
+ chatbot,
198
+ token_count,
199
+ top_p,
200
+ temperature,
201
+ use_streaming_checkbox,
202
+ model_select_dropdown,
203
+ use_websearch_checkbox,
204
+ index_files],
205
  [chatbot, history, status_display, token_count], show_progress=True)
206
 
207
  submitBtn.click(reset_textbox, [], [user_input])
 
209
  emptyBtn.click(reset_state, outputs=[chatbot, history, token_count, status_display], show_progress=True)
210
 
211
  retryBtn.click(retry,
212
+ [keyTxt, invite_code, systemPromptTxt, history, chatbot, token_count, top_p, temperature,
213
+ use_streaming_checkbox,
214
  model_select_dropdown], [chatbot, history, status_display, token_count], show_progress=True)
215
 
216
+ delFirstBtn.click(
217
+ delete_first_conversation,
218
+ [history, token_count],
219
+ [history, token_count, status_display],
220
+ )
221
+
222
+ delLastBtn.click(
223
+ delete_last_conversation,
224
+ [chatbot, history, token_count],
225
+ [chatbot, history, token_count, status_display], show_progress=True)
226
 
227
  reduceTokenBtn.click(reduce_token_size, [keyTxt, invite_code, systemPromptTxt, history, chatbot, token_count, top_p,
228
  temperature, use_streaming_checkbox, model_select_dropdown],
 
241
  downloadFile,
242
  show_progress=True,
243
  )
244
+ # historyRefreshBtn.click(get_history_names, None, [historyFileSelectDropdown])
245
  historyFileSelectDropdown.change(
246
  load_chat_history,
247
  [historyFileSelectDropdown, systemPromptTxt, history, chatbot],
 
254
  [saveFileName, systemPromptTxt, history, chatbot],
255
  )
256
 
 
257
  # Template
258
  templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])
259
 
260
+ templateFileSelectDropdown.change(load_template,
261
+ [templateFileSelectDropdown],
262
+ [promptTemplates, templateSelectDropdown],
263
+ show_progress=True)
264
 
265
+ templateSelectDropdown.change(get_template_content,
266
+ [promptTemplates, templateSelectDropdown, systemPromptTxt],
267
+ [systemPromptTxt],
268
+ show_progress=True)
269
 
270
+ logging.info("\n访问 http://localhost:7860 查看界面")
271
  # 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
272
  demo.title = "ChatGPT-长江商学院 🚀"
273
 
274
  if __name__ == "__main__":
275
+ reload_javascript()
276
+
277
+ # if running in Docker
278
  if dockerflag:
279
  if authflag:
280
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860, auth=(username, password))
281
  else:
282
  demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
283
+ # if not running in Docker
284
  else:
285
  if authflag:
286
  demo.queue().launch(share=False, auth=(username, password))
287
  else:
288
+ demo.queue().launch(share=False) # 改为 share=True 可以创建公开分享链接