liu.yuqiang
commited on
Commit
•
becb383
1
Parent(s):
d4e8083
Edit application file2
Browse files
README.md
CHANGED
@@ -5,7 +5,7 @@ colorFrom: blue
|
|
5 |
colorTo: pink
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.21.0
|
8 |
-
app_file:
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
---
|
|
|
5 |
colorTo: pink
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.21.0
|
8 |
+
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
---
|
app.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = "" # 在这里输入你的 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="ChatGPT"):
|
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 |
+
gr.Markdown("对话��史默认保存在history文件夹中。")
|
103 |
+
with gr.Column():
|
104 |
+
with gr.Row():
|
105 |
+
with gr.Column(scale=6):
|
106 |
+
saveFileName = gr.Textbox(
|
107 |
+
show_label=True, placeholder=f"设置文件名: 默认为.json,可选为.md", label="设置保存文件名", value="对话历史记录").style(container=True)
|
108 |
+
with gr.Column(scale=1):
|
109 |
+
saveHistoryBtn = gr.Button("💾 保存对话")
|
110 |
+
exportMarkdownBtn = gr.Button("📝 导出为Markdown")
|
111 |
+
with gr.Row():
|
112 |
+
with gr.Column(scale=6):
|
113 |
+
historyFileSelectDropdown = gr.Dropdown(label="从列表中加载对话", choices=get_history_names(plain=True), multiselect=False, value=get_history_names(plain=True)[0])
|
114 |
+
with gr.Column(scale=1):
|
115 |
+
historyRefreshBtn = gr.Button("🔄 刷新")
|
116 |
+
with gr.Row():
|
117 |
+
with gr.Column():
|
118 |
+
downloadFile = gr.File(interactive=True)
|
119 |
+
|
120 |
+
gr.Markdown(description)
|
121 |
+
|
122 |
+
# Chatbot
|
123 |
+
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)
|
124 |
+
user_input.submit(reset_textbox, [], [user_input])
|
125 |
+
|
126 |
+
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)
|
127 |
+
submitBtn.click(reset_textbox, [], [user_input])
|
128 |
+
|
129 |
+
emptyBtn.click(reset_state, outputs=[chatbot, history, token_count, status_display], show_progress=True)
|
130 |
+
|
131 |
+
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)
|
132 |
+
|
133 |
+
delLastBtn.click(delete_last_conversation, [chatbot, history, token_count], [
|
134 |
+
chatbot, history, token_count, status_display], show_progress=True)
|
135 |
+
|
136 |
+
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)
|
137 |
+
|
138 |
+
# Template
|
139 |
+
templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])
|
140 |
+
templateFileSelectDropdown.change(load_template, [templateFileSelectDropdown], [promptTemplates, templateSelectDropdown], show_progress=True)
|
141 |
+
templateSelectDropdown.change(get_template_content, [promptTemplates, templateSelectDropdown, systemPromptTxt], [systemPromptTxt], show_progress=True)
|
142 |
+
|
143 |
+
# S&L
|
144 |
+
saveHistoryBtn.click(save_chat_history, [saveFileName, systemPromptTxt, history, chatbot], downloadFile, show_progress=True)
|
145 |
+
saveHistoryBtn.click(get_history_names, None, [historyFileSelectDropdown])
|
146 |
+
exportMarkdownBtn.click(export_markdown, [saveFileName, systemPromptTxt, history, chatbot], downloadFile, show_progress=True)
|
147 |
+
historyRefreshBtn.click(get_history_names, None, [historyFileSelectDropdown])
|
148 |
+
historyFileSelectDropdown.change(load_chat_history, [historyFileSelectDropdown, systemPromptTxt, history, chatbot], [saveFileName, systemPromptTxt, history, chatbot], show_progress=True)
|
149 |
+
downloadFile.change(load_chat_history, [downloadFile, systemPromptTxt, history, chatbot], [saveFileName, systemPromptTxt, history, chatbot])
|
150 |
+
|
151 |
+
|
152 |
+
logging.info(colorama.Back.GREEN + "\n川虎的温馨提示:访问 http://localhost:7860 查看界面" + colorama.Style.RESET_ALL)
|
153 |
+
# 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
|
154 |
+
demo.title = "川虎ChatGPT 🚀"
|
155 |
+
|
156 |
+
if __name__ == "__main__":
|
157 |
+
#if running in Docker
|
158 |
+
if dockerflag:
|
159 |
+
if authflag:
|
160 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=(username, password))
|
161 |
+
else:
|
162 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
163 |
+
#if not running in Docker
|
164 |
+
else:
|
165 |
+
if authflag:
|
166 |
+
demo.queue().launch(share=False, auth=(username, password))
|
167 |
+
else:
|
168 |
+
demo.queue().launch(share=True) # 改为 share=True 可以创建公开分享链接
|
169 |
+
#demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False) # 可自定义端口
|
170 |
+
#demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=("在这里填写用户名", "在这里填写密码")) # 可设置用户名与密码
|
171 |
+
#demo.queue().launch(auth=("在这里填写用户名", "在这里填写密码")) # 适合Nginx反向代理
|