Spaces:
Runtime error
Runtime error
XingHe0127
commited on
Commit
•
b4b041b
1
Parent(s):
bf7ef88
Delete Chatbot.py
Browse files- Chatbot.py +0 -166
Chatbot.py
DELETED
@@ -1,166 +0,0 @@
|
|
1 |
-
# -*- coding:utf-8 -*-
|
2 |
-
import gradio as gr
|
3 |
-
import os
|
4 |
-
import logging
|
5 |
-
import sys
|
6 |
-
import argparse
|
7 |
-
from utils import *
|
8 |
-
from presets import *
|
9 |
-
|
10 |
-
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s")
|
11 |
-
|
12 |
-
my_api_key = os.getenv("OpenAI-API") # 在这里输入你的 API 密钥
|
13 |
-
|
14 |
-
#if we are running in Docker
|
15 |
-
if os.environ.get('dockerrun') == 'yes':
|
16 |
-
dockerflag = True
|
17 |
-
else:
|
18 |
-
dockerflag = False
|
19 |
-
|
20 |
-
authflag = False
|
21 |
-
|
22 |
-
if dockerflag:
|
23 |
-
my_api_key = os.environ.get('my_api_key')
|
24 |
-
if my_api_key == "empty":
|
25 |
-
logging.error("Please give a api key!")
|
26 |
-
sys.exit(1)
|
27 |
-
#auth
|
28 |
-
username = os.environ.get('USERNAME')
|
29 |
-
password = os.environ.get('PASSWORD')
|
30 |
-
if not (isinstance(username, type(None)) or isinstance(password, type(None))):
|
31 |
-
authflag = True
|
32 |
-
else:
|
33 |
-
if not my_api_key and os.path.exists("api_key.txt") and os.path.getsize("api_key.txt"):
|
34 |
-
with open("api_key.txt", "r") as f:
|
35 |
-
my_api_key = f.read().strip()
|
36 |
-
if os.path.exists("auth.json"):
|
37 |
-
with open("auth.json", "r") as f:
|
38 |
-
auth = json.load(f)
|
39 |
-
username = auth["username"]
|
40 |
-
password = auth["password"]
|
41 |
-
if username != "" and password != "":
|
42 |
-
authflag = True
|
43 |
-
|
44 |
-
gr.Chatbot.postprocess = postprocess
|
45 |
-
|
46 |
-
with gr.Blocks(css=customCSS) as demo:
|
47 |
-
history = gr.State([])
|
48 |
-
token_count = 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 |
-
with gr.Row():
|
55 |
-
gr.HTML(title)
|
56 |
-
status_display = gr.Markdown("status: ready", elem_id="status_display")
|
57 |
-
|
58 |
-
with gr.Row(scale=1).style(equal_height=True):
|
59 |
-
with gr.Column(scale=5):
|
60 |
-
with gr.Row(scale=1):
|
61 |
-
chatbot = gr.Chatbot().style(height=600) # .style(color_map=("#1D51EE", "#585A5B"))
|
62 |
-
with gr.Row(scale=1):
|
63 |
-
with gr.Column(scale=12):
|
64 |
-
user_input = gr.Textbox(show_label=False, placeholder="在这里输入").style(
|
65 |
-
container=False)
|
66 |
-
with gr.Column(min_width=50, scale=1):
|
67 |
-
submitBtn = gr.Button("🚀", variant="primary")
|
68 |
-
with gr.Row(scale=1):
|
69 |
-
emptyBtn = gr.Button("🧹 新的对话",)
|
70 |
-
retryBtn = gr.Button("🔄 重新生成")
|
71 |
-
delLastBtn = gr.Button("🗑️ 删除一条对话")
|
72 |
-
reduceTokenBtn = gr.Button("♻️ 总结对话")
|
73 |
-
|
74 |
-
with gr.Column():
|
75 |
-
with gr.Column(min_width=50,scale=1):
|
76 |
-
with gr.Tab(label="Chatbot"):
|
77 |
-
keyTxt = gr.Textbox(show_label=True, placeholder=f"OpenAI API-key...",value=my_api_key, type="password", visible=not HIDE_MY_KEY, label="API-Key")
|
78 |
-
model_select_dropdown = gr.Dropdown(label="选择模型", choices=MODELS, multiselect=False, value=MODELS[0])
|
79 |
-
with gr.Accordion("参数", open=False):
|
80 |
-
top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.05,
|
81 |
-
interactive=True, label="Top-p (nucleus sampling)",)
|
82 |
-
temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0,
|
83 |
-
step=0.1, interactive=True, label="Temperature",)
|
84 |
-
use_streaming_checkbox = gr.Checkbox(label="实时传输回答", value=True, visible=enable_streaming_option)
|
85 |
-
use_websearch_checkbox = gr.Checkbox(label="使用在线搜索", value=False)
|
86 |
-
|
87 |
-
with gr.Tab(label="Prompt"):
|
88 |
-
systemPromptTxt = gr.Textbox(show_label=True, placeholder=f"在这里输入System Prompt...", label="System prompt", value=initial_prompt).style(container=True)
|
89 |
-
with gr.Accordion(label="加载Prompt模板", open=True):
|
90 |
-
with gr.Column():
|
91 |
-
with gr.Row():
|
92 |
-
with gr.Column(scale=6):
|
93 |
-
templateFileSelectDropdown = gr.Dropdown(label="选择Prompt模板集合文件", choices=get_template_names(plain=True), multiselect=False, value=get_template_names(plain=True)[0])
|
94 |
-
with gr.Column(scale=1):
|
95 |
-
templateRefreshBtn = gr.Button("🔄 刷新")
|
96 |
-
with gr.Row():
|
97 |
-
with gr.Column():
|
98 |
-
templateSelectDropdown = gr.Dropdown(label="从Prompt模板中加载", choices=load_template(get_template_names(plain=True)[0], mode=1), multiselect=False, value=load_template(get_template_names(plain=True)[0], mode=1)[0])
|
99 |
-
|
100 |
-
with gr.Tab(label="保存/加载"):
|
101 |
-
with gr.Accordion(label="保存/加载对话历史记录", open=True):
|
102 |
-
with gr.Column():
|
103 |
-
with gr.Row():
|
104 |
-
with gr.Column(scale=6):
|
105 |
-
saveFileName = gr.Textbox(
|
106 |
-
show_label=True, placeholder=f"在这里输入保存的文件名...", label="设置保存文件名", value="对话历史记录").style(container=True)
|
107 |
-
with gr.Column(scale=1):
|
108 |
-
saveHistoryBtn = gr.Button("💾 保存对话")
|
109 |
-
with gr.Row():
|
110 |
-
with gr.Column(scale=6):
|
111 |
-
historyFileSelectDropdown = gr.Dropdown(label="从列表中加载对话", choices=get_history_names(plain=True), multiselect=False, value=get_history_names(plain=True)[0])
|
112 |
-
with gr.Column(scale=1):
|
113 |
-
historyRefreshBtn = gr.Button("🔄 刷新")
|
114 |
-
|
115 |
-
gr.Markdown(description)
|
116 |
-
|
117 |
-
user_input.submit(predict, [keyTxt, systemPromptTxt, history, user_input, chatbot, token_count, top_p, temperature, use_streaming_checkbox, model_select_dropdown, use_websearch_checkbox], [chatbot, history, status_display, token_count], show_progress=True)
|
118 |
-
user_input.submit(reset_textbox, [], [user_input])
|
119 |
-
|
120 |
-
submitBtn.click(predict, [keyTxt, systemPromptTxt, history, user_input, chatbot, token_count, top_p, temperature, use_streaming_checkbox, model_select_dropdown, use_websearch_checkbox], [chatbot, history, status_display, token_count], show_progress=True)
|
121 |
-
submitBtn.click(reset_textbox, [], [user_input])
|
122 |
-
|
123 |
-
emptyBtn.click(reset_state, outputs=[chatbot, history, token_count, status_display], show_progress=True)
|
124 |
-
|
125 |
-
retryBtn.click(retry, [keyTxt, systemPromptTxt, history, chatbot, token_count, top_p, temperature, use_streaming_checkbox, model_select_dropdown], [chatbot, history, status_display, token_count], show_progress=True)
|
126 |
-
|
127 |
-
delLastBtn.click(delete_last_conversation, [chatbot, history, token_count], [
|
128 |
-
chatbot, history, token_count, status_display], show_progress=True)
|
129 |
-
|
130 |
-
reduceTokenBtn.click(reduce_token_size, [keyTxt, systemPromptTxt, history, chatbot, token_count, top_p, temperature, use_streaming_checkbox, model_select_dropdown], [chatbot, history, status_display, token_count], show_progress=True)
|
131 |
-
|
132 |
-
saveHistoryBtn.click(save_chat_history, [
|
133 |
-
saveFileName, systemPromptTxt, history, chatbot], None, show_progress=True)
|
134 |
-
|
135 |
-
saveHistoryBtn.click(get_history_names, None, [historyFileSelectDropdown])
|
136 |
-
|
137 |
-
historyRefreshBtn.click(get_history_names, None, [historyFileSelectDropdown])
|
138 |
-
|
139 |
-
historyFileSelectDropdown.change(load_chat_history, [historyFileSelectDropdown, systemPromptTxt, history, chatbot], [saveFileName, systemPromptTxt, history, chatbot], show_progress=True)
|
140 |
-
|
141 |
-
templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])
|
142 |
-
|
143 |
-
templateFileSelectDropdown.change(load_template, [templateFileSelectDropdown], [promptTemplates, templateSelectDropdown], show_progress=True)
|
144 |
-
|
145 |
-
templateSelectDropdown.change(get_template_content, [promptTemplates, templateSelectDropdown, systemPromptTxt], [systemPromptTxt], show_progress=True)
|
146 |
-
|
147 |
-
logging.info(colorama.Back.GREEN + "\nChatbot启动成功" + colorama.Style.RESET_ALL)
|
148 |
-
# 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
|
149 |
-
demo.title = "Chatbot"
|
150 |
-
|
151 |
-
if __name__ == "__main__":
|
152 |
-
#if running in Docker
|
153 |
-
if dockerflag:
|
154 |
-
if authflag:
|
155 |
-
demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=(username, password))
|
156 |
-
else:
|
157 |
-
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
158 |
-
#if not running in Docker
|
159 |
-
else:
|
160 |
-
if authflag:
|
161 |
-
demo.queue().launch(share=False, auth=(username, password))
|
162 |
-
else:
|
163 |
-
#demo.queue().launch(share=False) # 改为 share=True 可以创建公开分享链接
|
164 |
-
#demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False) # 可自定义端口
|
165 |
-
demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=(os.environ["username"], os.environ["password"])) # 可设置用户名与密码
|
166 |
-
#demo.queue().launch(auth=(os.environ["username"], os.environ["password"])) # 适合Nginx反向代理
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|