XingHe0127 commited on
Commit
501ebc0
·
1 Parent(s): bc3aee6

Delete Chatbot.py

Browse files
Files changed (1) hide show
  1. Chatbot.py +0 -165
Chatbot.py DELETED
@@ -1,165 +0,0 @@
1
- import gradio as gr
2
- # import openai
3
- import os
4
- import sys
5
- from utils import *
6
-
7
- my_api_key = os.getenv("OpenAI-API") # 在这里输入你的 API 密钥
8
- HIDE_MY_KEY = True # 如果你想在UI中隐藏你的 API 密钥,将此值设置为 True
9
-
10
- gr.Chatbot.postprocess = postprocess
11
-
12
- #if we are running in Docker
13
- if os.environ.get('dockerrun') == 'yes':
14
- dockerflag = True
15
- else:
16
- dockerflag = 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 isinstance(username, type(None)) or isinstance(password, type(None)):
27
- authflag = False
28
- else:
29
- authflag = True
30
-
31
- title = """<h1 align="center"></h1>"""
32
- description = """<div align=center>
33
-
34
- 此网页基于 `gpt-3.5-turbo` 大型自然语言处理模型
35
- Build by 星河
36
-
37
- </div>
38
- """
39
- customCSS = """
40
- code {
41
- display: inline;
42
- white-space: break-spaces;
43
- border-radius: 6px;
44
- margin: 0 2px 0 2px;
45
- padding: .2em .4em .1em .4em;
46
- background-color: rgba(175,184,193,0.2);
47
- }
48
- pre {
49
- display: block;
50
- white-space: pre;
51
- background-color: hsla(0, 0%, 0%, 72%);
52
- border: solid 5px var(--color-border-primary) !important;
53
- border-radius: 8px;
54
- padding: 0 1.2rem 1.2rem;
55
- margin-top: 1em !important;
56
- color: #FFF;
57
- box-shadow: inset 0px 8px 16px hsla(0, 0%, 0%, .2)
58
- }
59
- pre code, pre code code {
60
- background-color: transparent !important;
61
- margin: 0;
62
- padding: 0;
63
- }
64
- """
65
-
66
- with gr.Blocks(css=customCSS) as demo:
67
- gr.HTML(title)
68
- keyTxt = gr.Textbox(show_label=True, placeholder=f"在这里输入你的OpenAI API-key...",
69
- value=my_api_key, label="API Key", type="password", visible=not HIDE_MY_KEY).style(container=True)
70
- chatbot = gr.Chatbot() # .style(color_map=("#1D51EE", "#585A5B"))
71
- history = gr.State([])
72
- promptTemplates = gr.State(load_template(get_template_names(plain=True)[0], mode=2))
73
- TRUECOMSTANT = gr.State(True)
74
- FALSECONSTANT = gr.State(False)
75
- topic = gr.State("未命名对话历史记录")
76
-
77
- with gr.Row():
78
- with gr.Column(scale=12):
79
- txt = gr.Textbox(show_label=False, placeholder="在这里输入").style(
80
- container=False)
81
- with gr.Column(min_width=50, scale=1):
82
- submitBtn = gr.Button("🚀", variant="primary")
83
- with gr.Row():
84
- emptyBtn = gr.Button("🧹 新的对话")
85
- retryBtn = gr.Button("🔄 重新生成")
86
- delLastBtn = gr.Button("🗑️ 删除上条对话")
87
- reduceTokenBtn = gr.Button("♻️ 总结对话")
88
- statusDisplay = gr.Markdown("status: ready")
89
- systemPromptTxt = gr.Textbox(show_label=True, placeholder=f"在这里输入System Prompt...",
90
- label="System prompt", value=initial_prompt).style(container=True)
91
- with gr.Accordion(label="加载Prompt模板", open=False):
92
- with gr.Column():
93
- with gr.Row():
94
- with gr.Column(scale=6):
95
- templateFileSelectDropdown = gr.Dropdown(label="选择Prompt模板集合文件(.csv)", choices=get_template_names(plain=True), multiselect=False)
96
- with gr.Column(scale=1):
97
- templateRefreshBtn = gr.Button("🔄 刷新")
98
- templaeFileReadBtn = gr.Button("📂 读入模板")
99
- with gr.Row():
100
- with gr.Column(scale=6):
101
- templateSelectDropdown = gr.Dropdown(label="从Prompt模板中加载", choices=load_template(get_template_names(plain=True)[0], mode=1), multiselect=False)
102
- with gr.Column(scale=1):
103
- templateApplyBtn = gr.Button("⬇️ 应用")
104
- with gr.Accordion(label="保存/加载对话历史记录(在文本框中输入文件名,点击“保存对话”按钮)", open=False):
105
- with gr.Column():
106
- with gr.Row():
107
- with gr.Column(scale=6):
108
- saveFileName = gr.Textbox(
109
- show_label=True, placeholder=f"在这里输入保存的文件名...", label="设置保存文件名", value="对话历史记录").style(container=True)
110
- with gr.Column(scale=1):
111
- saveBtn = gr.Button("💾 保存对话")
112
- with gr.Row():
113
- with gr.Column(scale=6):
114
- historyFileSelectDropdown = gr.Dropdown(label="从列表中加载对话", choices=get_history_names(plain=True), multiselect=False)
115
- with gr.Column(scale=1):
116
- historyRefreshBtn = gr.Button("🔄 刷新")
117
- historyReadBtn = gr.Button("📂 读入对话")
118
- #inputs, top_p, temperature, top_k, repetition_penalty
119
- with gr.Accordion("参数", open=False):
120
- top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.05,
121
- interactive=True, label="Top-p (nucleus sampling)",)
122
- temperature = gr.Slider(minimum=-0, maximum=5.0, value=1.0,
123
- step=0.1, interactive=True, label="Temperature",)
124
- #top_k = gr.Slider( minimum=1, maximum=50, value=4, step=1, interactive=True, label="Top-k",)
125
- #repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.03, step=0.01, interactive=True, label="Repetition Penalty", )
126
- gr.Markdown(description)
127
-
128
-
129
- txt.submit(predict, [txt, top_p, temperature, keyTxt,
130
- chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay])
131
- txt.submit(reset_textbox, [], [txt])
132
- submitBtn.click(predict, [txt, top_p, temperature, keyTxt, chatbot,
133
- history, systemPromptTxt], [chatbot, history, statusDisplay], show_progress=True)
134
- submitBtn.click(reset_textbox, [], [txt])
135
- emptyBtn.click(reset_state, outputs=[chatbot, history])
136
- retryBtn.click(predict, [txt, top_p, temperature, keyTxt, chatbot, history,
137
- systemPromptTxt, TRUECOMSTANT], [chatbot, history, statusDisplay], show_progress=True)
138
- delLastBtn.click(delete_last_conversation, [chatbot, history], [
139
- chatbot, history], show_progress=True)
140
- reduceTokenBtn.click(predict, [txt, top_p, temperature, keyTxt, chatbot, history,
141
- systemPromptTxt, FALSECONSTANT, TRUECOMSTANT], [chatbot, history, statusDisplay], show_progress=True)
142
- saveBtn.click(save_chat_history, [
143
- saveFileName, systemPromptTxt, history, chatbot], None, show_progress=True)
144
- saveBtn.click(get_history_names, None, [historyFileSelectDropdown])
145
- historyRefreshBtn.click(get_history_names, None, [historyFileSelectDropdown])
146
- historyReadBtn.click(load_chat_history, [historyFileSelectDropdown], [saveFileName, systemPromptTxt, history, chatbot], show_progress=True)
147
- templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])
148
- templaeFileReadBtn.click(load_template, [templateFileSelectDropdown], [promptTemplates, templateSelectDropdown], show_progress=True)
149
- templateApplyBtn.click(lambda x, y: x[y], [promptTemplates, templateSelectDropdown], [systemPromptTxt], show_progress=True)
150
-
151
- # 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
152
- demo.title = "Chatbot"
153
-
154
- #if running in Docker
155
- if dockerflag:
156
- if authflag:
157
- demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=(username, password))
158
- else:
159
- demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
160
- #if not running in Docker
161
- else:
162
- #demo.queue().launch(share=False) # 改为 share=True 可以创建公开分享链接
163
- #demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False) # 可自定义端口
164
- demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=(os.environ["username"], os.environ["password"])) # 可设置用户名与密码
165
- #demo.queue().launch(auth=("在这里填写用户名", "在这里填写密码")) # 适合Nginx反向代理