JohnSmith9982 commited on
Commit
d340a9d
1 Parent(s): ee1a637

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -37
app.py CHANGED
@@ -2,13 +2,12 @@ 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':
@@ -29,7 +28,7 @@ if dockerflag:
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"):
@@ -40,13 +39,18 @@ else:
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)
@@ -54,23 +58,23 @@ with gr.Blocks(css=customCSS) as demo:
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("📂 读入模板")
@@ -104,44 +108,53 @@ with gr.Blocks(css=customCSS) as demo:
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反向代理
 
 
 
 
2
  # import openai
3
  import os
4
  import sys
5
+ import argparse
6
  from utils import *
7
  from presets import *
8
 
 
 
9
 
10
+ my_api_key = "" # 在这里输入你的 API 密钥
11
 
12
  #if we are running in Docker
13
  if os.environ.get('dockerrun') == 'yes':
 
28
  if not (isinstance(username, type(None)) or isinstance(password, type(None))):
29
  authflag = True
30
  else:
31
+ if not my_api_key and os.path.exists("api_key.txt") and os.path.getsize("api_key.txt"):
32
  with open("api_key.txt", "r") as f:
33
  my_api_key = f.read().strip()
34
  if os.path.exists("auth.json"):
 
39
  if username != "" and password != "":
40
  authflag = True
41
 
42
+ gr.Chatbot.postprocess = postprocess
43
+
44
  with gr.Blocks(css=customCSS) as demo:
45
  gr.HTML(title)
46
  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>''')
47
+ with gr.Row():
48
+ keyTxt = gr.Textbox(show_label=False, placeholder=f"在这里输入你的OpenAI API-key...",
49
+ value=my_api_key, type="password", visible=not HIDE_MY_KEY).style(container=True)
50
+ use_streaming_checkbox = gr.Checkbox(label="实时传输回答", value=True, visible=enable_streaming_option)
51
  chatbot = gr.Chatbot() # .style(color_map=("#1D51EE", "#585A5B"))
52
  history = gr.State([])
53
+ token_count = gr.State([])
54
  promptTemplates = gr.State(load_template(get_template_names(plain=True)[0], mode=2))
55
  TRUECOMSTANT = gr.State(True)
56
  FALSECONSTANT = gr.State(False)
 
58
 
59
  with gr.Row():
60
  with gr.Column(scale=12):
61
+ user_input = gr.Textbox(show_label=False, placeholder="在这里输入").style(
62
  container=False)
63
  with gr.Column(min_width=50, scale=1):
64
  submitBtn = gr.Button("🚀", variant="primary")
65
  with gr.Row():
66
  emptyBtn = gr.Button("🧹 新的对话")
67
  retryBtn = gr.Button("🔄 重新生成")
68
+ delLastBtn = gr.Button("🗑️ 删除最近一条对话")
69
  reduceTokenBtn = gr.Button("♻️ 总结对话")
70
+ status_display = gr.Markdown("status: ready")
71
  systemPromptTxt = gr.Textbox(show_label=True, placeholder=f"在这里输入System Prompt...",
72
  label="System prompt", value=initial_prompt).style(container=True)
73
  with gr.Accordion(label="加载Prompt模板", open=False):
74
  with gr.Column():
75
  with gr.Row():
76
  with gr.Column(scale=6):
77
+ templateFileSelectDropdown = gr.Dropdown(label="选择Prompt模板集合文件", choices=get_template_names(plain=True), multiselect=False, value=get_template_names(plain=True)[0])
78
  with gr.Column(scale=1):
79
  templateRefreshBtn = gr.Button("🔄 刷新")
80
  templaeFileReadBtn = gr.Button("📂 读入模板")
 
108
  gr.Markdown(description)
109
 
110
 
111
+ user_input.submit(predict, [keyTxt, systemPromptTxt, history, user_input, chatbot, token_count, top_p, temperature, use_streaming_checkbox], [chatbot, history, status_display, token_count], show_progress=True)
112
+ user_input.submit(reset_textbox, [], [user_input])
113
+
114
+ submitBtn.click(predict, [keyTxt, systemPromptTxt, history, user_input, chatbot, token_count, top_p, temperature, use_streaming_checkbox], [chatbot, history, status_display, token_count], show_progress=True)
115
+ submitBtn.click(reset_textbox, [], [user_input])
116
+
117
+ emptyBtn.click(reset_state, outputs=[chatbot, history, token_count, status_display], show_progress=True)
118
+
119
+ retryBtn.click(retry, [keyTxt, systemPromptTxt, history, chatbot, token_count, top_p, temperature, use_streaming_checkbox], [chatbot, history, status_display, token_count], show_progress=True)
120
+
121
+ delLastBtn.click(delete_last_conversation, [chatbot, history, token_count, use_streaming_checkbox], [
122
+ chatbot, history, token_count, status_display], show_progress=True)
123
+
124
+ reduceTokenBtn.click(reduce_token_size, [keyTxt, systemPromptTxt, history, chatbot, token_count, top_p, temperature, use_streaming_checkbox], [chatbot, history, status_display, token_count], show_progress=True)
125
+
126
  saveHistoryBtn.click(save_chat_history, [
127
  saveFileName, systemPromptTxt, history, chatbot], None, show_progress=True)
128
+
129
  saveHistoryBtn.click(get_history_names, None, [historyFileSelectDropdown])
130
+
131
  historyRefreshBtn.click(get_history_names, None, [historyFileSelectDropdown])
132
+
133
  historyReadBtn.click(load_chat_history, [historyFileSelectDropdown, systemPromptTxt, history, chatbot], [saveFileName, systemPromptTxt, history, chatbot], show_progress=True)
134
+
135
  templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])
136
+
137
  templaeFileReadBtn.click(load_template, [templateFileSelectDropdown], [promptTemplates, templateSelectDropdown], show_progress=True)
138
+
139
  templateApplyBtn.click(get_template_content, [promptTemplates, templateSelectDropdown, systemPromptTxt], [systemPromptTxt], show_progress=True)
140
 
141
  print("川虎的温馨提示:访问 http://localhost:7860 查看界面")
142
  # 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
143
  demo.title = "川虎ChatGPT 🚀"
144
 
145
+ if __name__ == "__main__":
146
+ #if running in Docker
147
+ if dockerflag:
148
+ if authflag:
149
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=(username, password))
150
+ else:
151
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
152
+ #if not running in Docker
 
 
153
  else:
154
+ if authflag:
155
+ demo.queue().launch(share=False, auth=(username, password))
156
+ else:
157
+ demo.queue().launch(share=False) # 改为 share=True 可以创建公开分享链接
158
+ #demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False) # 可自定义端口
159
+ #demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=("在这里填写用户名", "在这里填写密码")) # 可设置用户名与密码
160
+ #demo.queue().launch(auth=("在这里填写用户名", "在这里填写密码")) # 适合Nginx反向代理