Spaces:
Runtime error
Runtime error
Commit
·
1bd67bd
1
Parent(s):
e1bbc1f
Delete ChuanhuChatbot.py
Browse files- ChuanhuChatbot.py +0 -147
ChuanhuChatbot.py
DELETED
|
@@ -1,147 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
# import openai
|
| 3 |
-
import os
|
| 4 |
-
import sys
|
| 5 |
-
from utils import *
|
| 6 |
-
from presets import *
|
| 7 |
-
|
| 8 |
-
my_api_key = "" # 在这里输入你的 API 密钥
|
| 9 |
-
HIDE_MY_KEY = False # 如果你想在UI中隐藏你的 API 密钥,将此值设置为 True
|
| 10 |
-
|
| 11 |
-
gr.Chatbot.postprocess = postprocess
|
| 12 |
-
|
| 13 |
-
#if we are running in Docker
|
| 14 |
-
if os.environ.get('dockerrun') == 'yes':
|
| 15 |
-
dockerflag = True
|
| 16 |
-
else:
|
| 17 |
-
dockerflag = False
|
| 18 |
-
|
| 19 |
-
authflag = False
|
| 20 |
-
|
| 21 |
-
if dockerflag:
|
| 22 |
-
my_api_key = os.environ.get('my_api_key')
|
| 23 |
-
if my_api_key == "empty":
|
| 24 |
-
print("Please give a api key!")
|
| 25 |
-
sys.exit(1)
|
| 26 |
-
#auth
|
| 27 |
-
username = os.environ.get('USERNAME')
|
| 28 |
-
password = os.environ.get('PASSWORD')
|
| 29 |
-
if not (isinstance(username, type(None)) or isinstance(password, type(None))):
|
| 30 |
-
authflag = True
|
| 31 |
-
else:
|
| 32 |
-
if os.path.exists("api_key.txt"):
|
| 33 |
-
with open("api_key.txt", "r") as f:
|
| 34 |
-
my_api_key = f.read().strip()
|
| 35 |
-
if os.path.exists("auth.json"):
|
| 36 |
-
with open("auth.json", "r") as f:
|
| 37 |
-
auth = json.load(f)
|
| 38 |
-
username = auth["username"]
|
| 39 |
-
password = auth["password"]
|
| 40 |
-
if username != "" and password != "":
|
| 41 |
-
authflag = True
|
| 42 |
-
|
| 43 |
-
with gr.Blocks(css=customCSS) as demo:
|
| 44 |
-
gr.HTML(title)
|
| 45 |
-
gr.HTML('''<center><a href="https://huggingface.co/spaces/JohnSmith9982/ChuanhuChatGPT?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="复制 Space"></a>强烈建议点击上面的按钮复制一份这个Space,在你自己的Space里运行,响应更迅速、也更安全👆</center>''')
|
| 46 |
-
keyTxt = gr.Textbox(show_label=True, placeholder=f"在这里输入你的OpenAI API-key...",
|
| 47 |
-
value=my_api_key, label="API Key", type="password", visible=not HIDE_MY_KEY).style(container=True)
|
| 48 |
-
chatbot = gr.Chatbot() # .style(color_map=("#1D51EE", "#585A5B"))
|
| 49 |
-
history = gr.State([])
|
| 50 |
-
promptTemplates = gr.State(load_template(get_template_names(plain=True)[0], mode=2))
|
| 51 |
-
TRUECOMSTANT = gr.State(True)
|
| 52 |
-
FALSECONSTANT = gr.State(False)
|
| 53 |
-
topic = gr.State("未命名对话历史记录")
|
| 54 |
-
|
| 55 |
-
with gr.Row():
|
| 56 |
-
with gr.Column(scale=12):
|
| 57 |
-
txt = gr.Textbox(show_label=False, placeholder="在这里输入").style(
|
| 58 |
-
container=False)
|
| 59 |
-
with gr.Column(min_width=50, scale=1):
|
| 60 |
-
submitBtn = gr.Button("🚀", variant="primary")
|
| 61 |
-
with gr.Row():
|
| 62 |
-
emptyBtn = gr.Button("🧹 新的对话")
|
| 63 |
-
retryBtn = gr.Button("🔄 重新生成")
|
| 64 |
-
delLastBtn = gr.Button("🗑️ 删除上条对话")
|
| 65 |
-
reduceTokenBtn = gr.Button("♻️ 总结对话")
|
| 66 |
-
statusDisplay = gr.Markdown("status: ready")
|
| 67 |
-
systemPromptTxt = gr.Textbox(show_label=True, placeholder=f"在这里输入System Prompt...",
|
| 68 |
-
label="System prompt", value=initial_prompt).style(container=True)
|
| 69 |
-
with gr.Accordion(label="加载Prompt模板", open=False):
|
| 70 |
-
with gr.Column():
|
| 71 |
-
with gr.Row():
|
| 72 |
-
with gr.Column(scale=6):
|
| 73 |
-
templateFileSelectDropdown = gr.Dropdown(label="选择Prompt模板集合文件(.csv)", choices=get_template_names(plain=True), multiselect=False, value=get_template_names(plain=True)[0])
|
| 74 |
-
with gr.Column(scale=1):
|
| 75 |
-
templateRefreshBtn = gr.Button("🔄 刷新")
|
| 76 |
-
templaeFileReadBtn = gr.Button("📂 读入模板")
|
| 77 |
-
with gr.Row():
|
| 78 |
-
with gr.Column(scale=6):
|
| 79 |
-
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])
|
| 80 |
-
with gr.Column(scale=1):
|
| 81 |
-
templateApplyBtn = gr.Button("⬇️ 应用")
|
| 82 |
-
with gr.Accordion(label="保存/加载对话历史记录", open=False):
|
| 83 |
-
with gr.Column():
|
| 84 |
-
with gr.Row():
|
| 85 |
-
with gr.Column(scale=6):
|
| 86 |
-
saveFileName = gr.Textbox(
|
| 87 |
-
show_label=True, placeholder=f"在这里输入保存的文件名...", label="设置保存文件名", value="对话历史记录").style(container=True)
|
| 88 |
-
with gr.Column(scale=1):
|
| 89 |
-
saveHistoryBtn = gr.Button("💾 保存对话")
|
| 90 |
-
with gr.Row():
|
| 91 |
-
with gr.Column(scale=6):
|
| 92 |
-
historyFileSelectDropdown = gr.Dropdown(label="从列表中加载对话", choices=get_history_names(plain=True), multiselect=False, value=get_history_names(plain=True)[0])
|
| 93 |
-
with gr.Column(scale=1):
|
| 94 |
-
historyRefreshBtn = gr.Button("🔄 刷新")
|
| 95 |
-
historyReadBtn = gr.Button("📂 读入对话")
|
| 96 |
-
#inputs, top_p, temperature, top_k, repetition_penalty
|
| 97 |
-
with gr.Accordion("参数", open=False):
|
| 98 |
-
top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.05,
|
| 99 |
-
interactive=True, label="Top-p (nucleus sampling)",)
|
| 100 |
-
temperature = gr.Slider(minimum=-0, maximum=5.0, value=1.0,
|
| 101 |
-
step=0.1, interactive=True, label="Temperature",)
|
| 102 |
-
#top_k = gr.Slider( minimum=1, maximum=50, value=4, step=1, interactive=True, label="Top-k",)
|
| 103 |
-
#repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.03, step=0.01, interactive=True, label="Repetition Penalty", )
|
| 104 |
-
gr.Markdown(description)
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
txt.submit(predict, [txt, top_p, temperature, keyTxt,
|
| 108 |
-
chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay])
|
| 109 |
-
txt.submit(reset_textbox, [], [txt])
|
| 110 |
-
submitBtn.click(predict, [txt, top_p, temperature, keyTxt, chatbot,
|
| 111 |
-
history, systemPromptTxt], [chatbot, history, statusDisplay], show_progress=True)
|
| 112 |
-
submitBtn.click(reset_textbox, [], [txt])
|
| 113 |
-
emptyBtn.click(reset_state, outputs=[chatbot, history])
|
| 114 |
-
retryBtn.click(predict, [txt, top_p, temperature, keyTxt, chatbot, history,
|
| 115 |
-
systemPromptTxt, TRUECOMSTANT], [chatbot, history, statusDisplay], show_progress=True)
|
| 116 |
-
delLastBtn.click(delete_last_conversation, [chatbot, history], [
|
| 117 |
-
chatbot, history], show_progress=True)
|
| 118 |
-
reduceTokenBtn.click(predict, [txt, top_p, temperature, keyTxt, chatbot, history,
|
| 119 |
-
systemPromptTxt, FALSECONSTANT, TRUECOMSTANT], [chatbot, history, statusDisplay], show_progress=True)
|
| 120 |
-
saveHistoryBtn.click(save_chat_history, [
|
| 121 |
-
saveFileName, systemPromptTxt, history, chatbot], None, show_progress=True)
|
| 122 |
-
saveHistoryBtn.click(get_history_names, None, [historyFileSelectDropdown])
|
| 123 |
-
historyRefreshBtn.click(get_history_names, None, [historyFileSelectDropdown])
|
| 124 |
-
historyReadBtn.click(load_chat_history, [historyFileSelectDropdown, systemPromptTxt, history, chatbot], [saveFileName, systemPromptTxt, history, chatbot], show_progress=True)
|
| 125 |
-
templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])
|
| 126 |
-
templaeFileReadBtn.click(load_template, [templateFileSelectDropdown], [promptTemplates, templateSelectDropdown], show_progress=True)
|
| 127 |
-
templateApplyBtn.click(get_template_content, [promptTemplates, templateSelectDropdown, systemPromptTxt], [systemPromptTxt], show_progress=True)
|
| 128 |
-
|
| 129 |
-
print("川虎的温馨提示:访问 http://localhost:7860 查看界面")
|
| 130 |
-
# 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
|
| 131 |
-
demo.title = "川虎ChatGPT 🚀"
|
| 132 |
-
|
| 133 |
-
#if running in Docker
|
| 134 |
-
if dockerflag:
|
| 135 |
-
if authflag:
|
| 136 |
-
demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=(username, password))
|
| 137 |
-
else:
|
| 138 |
-
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 139 |
-
#if not running in Docker
|
| 140 |
-
else:
|
| 141 |
-
if authflag:
|
| 142 |
-
demo.queue().launch(share=False, auth=(username, password))
|
| 143 |
-
else:
|
| 144 |
-
demo.queue().launch(share=False) # 改为 share=True 可以创建公开分享链接
|
| 145 |
-
#demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False) # 可自定义端口
|
| 146 |
-
#demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=("在这里填写用户名", "在这里填写密码")) # 可设置用户名与密码
|
| 147 |
-
#demo.queue().launch(auth=("在这里填写用户名", "在这里填写密码")) # 适合Nginx反向代理
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|