fiyen commited on
Commit
edab19c
1 Parent(s): 417286a

Update app1.py

Browse files
Files changed (1) hide show
  1. app1.py +434 -0
app1.py CHANGED
@@ -0,0 +1,434 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding:utf-8 -*-
2
+ import os
3
+ import logging
4
+ import sys
5
+
6
+ import gradio as gr
7
+
8
+ from modules.utils import *
9
+ from modules.presets import *
10
+ from modules.overwrites import *
11
+ from modules.chat_func import *
12
+ from modules.encrypt_it import *
13
+
14
+ logging.basicConfig(
15
+ level=logging.DEBUG,
16
+ format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s",
17
+ )
18
+
19
+ my_api_key = "" # 在这里输入你的 API 密钥
20
+ # check username and password, get api key
21
+ def check_access_right(username, password):
22
+ try:
23
+ with open('encrypted.bin', 'rb') as f:
24
+ ciphertext = f.read()
25
+ key = generate_key(username, password)
26
+ my_api_key[:] = decrypt(ciphertext, key)[:]
27
+ return True
28
+ except:
29
+ return False
30
+
31
+ # if we are running in Docker
32
+ if os.environ.get("dockerrun") == "yes":
33
+ dockerflag = True
34
+ else:
35
+ dockerflag = False
36
+
37
+ authflag = False
38
+
39
+ if dockerflag:
40
+ my_api_key = os.environ.get("my_api_key")
41
+ if my_api_key == "empty":
42
+ logging.error("Please give a api key!")
43
+ sys.exit(1)
44
+ # auth
45
+ username = os.environ.get("USERNAME")
46
+ password = os.environ.get("PASSWORD")
47
+ if not (isinstance(username, type(None)) or isinstance(password, type(None))):
48
+ authflag = True
49
+ else:
50
+ # if (
51
+ # not my_api_key
52
+ # and os.path.exists("api_key.txt")
53
+ # and os.path.getsize("api_key.txt")
54
+ # ):
55
+ # with open("api_key.txt", "r") as f:
56
+ # my_api_key = f.read().strip()
57
+ # if os.path.exists("auth.json"):
58
+ # with open("auth.json", "r", encoding='utf-8') as f:
59
+ # auth = json.load(f)
60
+ # username = auth["username"]
61
+ # password = auth["password"]
62
+ # if username != "" and password != "":
63
+ # authflag = True
64
+ authflag = True
65
+
66
+ gr.Chatbot.postprocess = postprocess
67
+ PromptHelper.compact_text_chunks = compact_text_chunks
68
+
69
+ with open("assets/custom.css", "r", encoding="utf-8") as f:
70
+ customCSS = f.read()
71
+
72
+ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
73
+ history = gr.State([])
74
+ token_count = gr.State([])
75
+ promptTemplates = gr.State(load_template(get_template_names(plain=True)[0], mode=2))
76
+ user_api_key = gr.State(my_api_key)
77
+ user_question = gr.State("")
78
+ outputing = gr.State(False)
79
+ topic = gr.State("未命名对话历史记录")
80
+
81
+ with gr.Row():
82
+ gr.HTML(title)
83
+ status_display = gr.Markdown(get_geoip(), elem_id="status_display")
84
+
85
+ with gr.Row(scale=1).style(equal_height=True):
86
+ with gr.Column(scale=5):
87
+ with gr.Row(scale=1):
88
+ chatbot = gr.Chatbot(elem_id="chuanhu_chatbot").style(height="100%")
89
+ with gr.Row(scale=1):
90
+ with gr.Column(scale=12):
91
+ user_input = gr.Textbox(
92
+ show_label=False, placeholder="在这里输入"
93
+ ).style(container=False)
94
+ with gr.Column(min_width=70, scale=1):
95
+ submitBtn = gr.Button("发送", variant="primary")
96
+ cancelBtn = gr.Button("取消", variant="secondary", visible=False)
97
+ with gr.Row(scale=1):
98
+ emptyBtn = gr.Button(
99
+ "🧹 新的对话",
100
+ )
101
+ retryBtn = gr.Button("🔄 重新生成")
102
+ delLastBtn = gr.Button("🗑️ 删除一条对话")
103
+ reduceTokenBtn = gr.Button("♻️ 总结对话")
104
+
105
+ with gr.Column():
106
+ with gr.Column(min_width=50, scale=1):
107
+ with gr.Tab(label="ChatGPT"):
108
+ keyTxt = gr.Textbox(
109
+ show_label=True,
110
+ placeholder=f"OpenAI API-key...",
111
+ value=hide_middle_chars(my_api_key),
112
+ type="password",
113
+ visible=not HIDE_MY_KEY,
114
+ label="API-Key",
115
+ )
116
+ model_select_dropdown = gr.Dropdown(
117
+ label="选择模型", choices=MODELS, multiselect=False, value=MODELS[0]
118
+ )
119
+ use_streaming_checkbox = gr.Checkbox(
120
+ label="实时传输回答", value=True, visible=enable_streaming_option
121
+ )
122
+ use_websearch_checkbox = gr.Checkbox(label="使用在线搜索", value=False)
123
+ language_select_dropdown = gr.Dropdown(
124
+ label="选择回复语言(针对搜索&索引功能)",
125
+ choices=REPLY_LANGUAGES,
126
+ multiselect=False,
127
+ value=REPLY_LANGUAGES[0],
128
+ )
129
+ index_files = gr.Files(label="上传索引文件", type="file", multiple=True)
130
+
131
+ with gr.Tab(label="Prompt"):
132
+ systemPromptTxt = gr.Textbox(
133
+ show_label=True,
134
+ placeholder=f"在这里输入System Prompt...",
135
+ label="System prompt",
136
+ value=initial_prompt,
137
+ lines=10,
138
+ ).style(container=False)
139
+ with gr.Accordion(label="加载Prompt模板", open=True):
140
+ with gr.Column():
141
+ with gr.Row():
142
+ with gr.Column(scale=6):
143
+ templateFileSelectDropdown = gr.Dropdown(
144
+ label="选择Prompt模板集合文件",
145
+ choices=get_template_names(plain=True),
146
+ multiselect=False,
147
+ value=get_template_names(plain=True)[0],
148
+ ).style(container=False)
149
+ with gr.Column(scale=1):
150
+ templateRefreshBtn = gr.Button("🔄 刷新")
151
+ with gr.Row():
152
+ with gr.Column():
153
+ templateSelectDropdown = gr.Dropdown(
154
+ label="从Prompt模板中加载",
155
+ choices=load_template(
156
+ get_template_names(plain=True)[0], mode=1
157
+ ),
158
+ multiselect=False,
159
+ value=load_template(
160
+ get_template_names(plain=True)[0], mode=1
161
+ )[0],
162
+ ).style(container=False)
163
+
164
+ with gr.Tab(label="保存/加载"):
165
+ with gr.Accordion(label="保存/加载对话历史记录", open=True):
166
+ with gr.Column():
167
+ with gr.Row():
168
+ with gr.Column(scale=6):
169
+ historyFileSelectDropdown = gr.Dropdown(
170
+ label="从列表中加载对话",
171
+ choices=get_history_names(plain=True),
172
+ multiselect=False,
173
+ value=get_history_names(plain=True)[0],
174
+ )
175
+ with gr.Column(scale=1):
176
+ historyRefreshBtn = gr.Button("🔄 刷新")
177
+ with gr.Row():
178
+ with gr.Column(scale=6):
179
+ saveFileName = gr.Textbox(
180
+ show_label=True,
181
+ placeholder=f"设置文件名: 默认为.json,可选为.md",
182
+ label="设置保存文件名",
183
+ value="对话历史记录",
184
+ ).style(container=True)
185
+ with gr.Column(scale=1):
186
+ saveHistoryBtn = gr.Button("💾 保存对话")
187
+ exportMarkdownBtn = gr.Button("📝 导出为Markdown")
188
+ gr.Markdown("默认保存于history文件夹")
189
+ with gr.Row():
190
+ with gr.Column():
191
+ downloadFile = gr.File(interactive=True)
192
+
193
+ with gr.Tab(label="高级"):
194
+ default_btn = gr.Button("🔙 恢复默认设置")
195
+ gr.Markdown("# ⚠️ 务必谨慎更改 ⚠️\n\n如果无法使用请恢复默认设置")
196
+
197
+ with gr.Accordion("参数", open=False):
198
+ top_p = gr.Slider(
199
+ minimum=-0,
200
+ maximum=1.0,
201
+ value=1.0,
202
+ step=0.05,
203
+ interactive=True,
204
+ label="Top-p",
205
+ )
206
+ temperature = gr.Slider(
207
+ minimum=-0,
208
+ maximum=2.0,
209
+ value=1.0,
210
+ step=0.1,
211
+ interactive=True,
212
+ label="Temperature",
213
+ )
214
+
215
+ apiurlTxt = gr.Textbox(
216
+ show_label=True,
217
+ placeholder=f"在这里输入API地址...",
218
+ label="API地址",
219
+ value="https://api.openai.com/v1/chat/completions",
220
+ lines=2,
221
+ )
222
+ changeAPIURLBtn = gr.Button("🔄 切换API地址")
223
+ proxyTxt = gr.Textbox(
224
+ show_label=True,
225
+ placeholder=f"在这里输入代理地址...",
226
+ label="代理地址(示例:http://127.0.0.1:10809)",
227
+ value="",
228
+ lines=2,
229
+ )
230
+ changeProxyBtn = gr.Button("🔄 设置代理地址")
231
+
232
+ gr.Markdown(description)
233
+
234
+ chatgpt_predict_args = dict(
235
+ fn=predict,
236
+ inputs=[
237
+ user_api_key,
238
+ systemPromptTxt,
239
+ history,
240
+ user_question,
241
+ chatbot,
242
+ token_count,
243
+ top_p,
244
+ temperature,
245
+ use_streaming_checkbox,
246
+ model_select_dropdown,
247
+ use_websearch_checkbox,
248
+ index_files,
249
+ language_select_dropdown,
250
+ ],
251
+ outputs=[chatbot, history, status_display, token_count],
252
+ show_progress=True,
253
+ )
254
+
255
+ start_outputing_args = dict(
256
+ fn=start_outputing,
257
+ inputs=[],
258
+ outputs=[submitBtn, cancelBtn],
259
+ show_progress=True,
260
+ )
261
+
262
+ end_outputing_args = dict(
263
+ fn=end_outputing, inputs=[], outputs=[submitBtn, cancelBtn]
264
+ )
265
+
266
+ reset_textbox_args = dict(
267
+ fn=reset_textbox, inputs=[], outputs=[user_input]
268
+ )
269
+
270
+ transfer_input_args = dict(
271
+ fn=transfer_input, inputs=[user_input], outputs=[user_question, user_input, submitBtn, cancelBtn], show_progress=True
272
+ )
273
+
274
+ keyTxt.submit(submit_key, keyTxt, [user_api_key, status_display])
275
+ keyTxt.change(submit_key, keyTxt, [user_api_key, status_display])
276
+ # Chatbot
277
+ cancelBtn.click(cancel_outputing, [], [])
278
+
279
+ user_input.submit(**transfer_input_args).then(**chatgpt_predict_args).then(**end_outputing_args)
280
+
281
+ submitBtn.click(**transfer_input_args).then(**chatgpt_predict_args).then(**end_outputing_args)
282
+
283
+ emptyBtn.click(
284
+ reset_state,
285
+ outputs=[chatbot, history, token_count, status_display],
286
+ show_progress=True,
287
+ )
288
+ emptyBtn.click(**reset_textbox_args)
289
+
290
+ retryBtn.click(**start_outputing_args).then(
291
+ retry,
292
+ [
293
+ user_api_key,
294
+ systemPromptTxt,
295
+ history,
296
+ chatbot,
297
+ token_count,
298
+ top_p,
299
+ temperature,
300
+ use_streaming_checkbox,
301
+ model_select_dropdown,
302
+ language_select_dropdown,
303
+ ],
304
+ [chatbot, history, status_display, token_count],
305
+ show_progress=True,
306
+ ).then(**end_outputing_args)
307
+
308
+ delLastBtn.click(
309
+ delete_last_conversation,
310
+ [chatbot, history, token_count],
311
+ [chatbot, history, token_count, status_display],
312
+ show_progress=True,
313
+ )
314
+
315
+ reduceTokenBtn.click(
316
+ reduce_token_size,
317
+ [
318
+ user_api_key,
319
+ systemPromptTxt,
320
+ history,
321
+ chatbot,
322
+ token_count,
323
+ top_p,
324
+ temperature,
325
+ gr.State(0),
326
+ model_select_dropdown,
327
+ language_select_dropdown,
328
+ ],
329
+ [chatbot, history, status_display, token_count],
330
+ show_progress=True,
331
+ )
332
+
333
+ # Template
334
+ templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])
335
+ templateFileSelectDropdown.change(
336
+ load_template,
337
+ [templateFileSelectDropdown],
338
+ [promptTemplates, templateSelectDropdown],
339
+ show_progress=True,
340
+ )
341
+ templateSelectDropdown.change(
342
+ get_template_content,
343
+ [promptTemplates, templateSelectDropdown, systemPromptTxt],
344
+ [systemPromptTxt],
345
+ show_progress=True,
346
+ )
347
+
348
+ # S&L
349
+ saveHistoryBtn.click(
350
+ save_chat_history,
351
+ [saveFileName, systemPromptTxt, history, chatbot],
352
+ downloadFile,
353
+ show_progress=True,
354
+ )
355
+ saveHistoryBtn.click(get_history_names, None, [historyFileSelectDropdown])
356
+ exportMarkdownBtn.click(
357
+ export_markdown,
358
+ [saveFileName, systemPromptTxt, history, chatbot],
359
+ downloadFile,
360
+ show_progress=True,
361
+ )
362
+ historyRefreshBtn.click(get_history_names, None, [historyFileSelectDropdown])
363
+ historyFileSelectDropdown.change(
364
+ load_chat_history,
365
+ [historyFileSelectDropdown, systemPromptTxt, history, chatbot],
366
+ [saveFileName, systemPromptTxt, history, chatbot],
367
+ show_progress=True,
368
+ )
369
+ downloadFile.change(
370
+ load_chat_history,
371
+ [downloadFile, systemPromptTxt, history, chatbot],
372
+ [saveFileName, systemPromptTxt, history, chatbot],
373
+ )
374
+
375
+ # Advanced
376
+ default_btn.click(
377
+ reset_default, [], [apiurlTxt, proxyTxt, status_display], show_progress=True
378
+ )
379
+ changeAPIURLBtn.click(
380
+ change_api_url,
381
+ [apiurlTxt],
382
+ [status_display],
383
+ show_progress=True,
384
+ )
385
+ changeProxyBtn.click(
386
+ change_proxy,
387
+ [proxyTxt],
388
+ [status_display],
389
+ show_progress=True,
390
+ )
391
+
392
+ logging.info(
393
+ colorama.Back.GREEN
394
+ + "\n温馨提示:访问 http://localhost:7860 查看界面"
395
+ + colorama.Style.RESET_ALL
396
+ )
397
+
398
+ # 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
399
+ demo.title = "YangyangChat"
400
+
401
+ if __name__ == "__main__":
402
+ reload_javascript()
403
+ # if running in Docker
404
+ if dockerflag:
405
+ if authflag:
406
+ demo.queue(concurrency_count=CONCURRENT_COUNT).launch(
407
+ server_name="0.0.0.0",
408
+ server_port=7860,
409
+ auth=(username, password),
410
+ favicon_path="./assets/favicon.ico",
411
+ )
412
+ else:
413
+ demo.queue(concurrency_count=CONCURRENT_COUNT).launch(
414
+ server_name="0.0.0.0",
415
+ server_port=7860,
416
+ share=False,
417
+ favicon_path="./assets/favicon.ico",
418
+ )
419
+ # if not running in Docker
420
+ else:
421
+ if authflag:
422
+ demo.queue(concurrency_count=CONCURRENT_COUNT).launch(
423
+ share=False,
424
+ auth=check_access_right,
425
+ favicon_path="./assets/favicon.ico",
426
+ inbrowser=True,
427
+ )
428
+ else:
429
+ demo.queue(concurrency_count=CONCURRENT_COUNT).launch(
430
+ share=False, favicon_path="./assets/favicon.ico", inbrowser=True
431
+ ) # 改为 share=True 可以创建公开分享链接
432
+ # demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=7860, share=False) # 可自定义端口
433
+ # demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=7860,auth=("在这里填写用户名", "在这里填写密码")) # 可设置用户名与密码
434
+ # demo.queue(concurrency_count=CONCURRENT_COUNT).launch(auth=("在这里填写用户名", "在这里填写密码")) # 适合Nginx反向代理