Upload ChuanhuChatbot.py
Browse files- ChuanhuChatbot.py +195 -235
ChuanhuChatbot.py
CHANGED
|
@@ -5,288 +5,247 @@ import sys
|
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
-
from modules import config
|
| 9 |
-
from modules.config import *
|
| 10 |
from modules.utils import *
|
| 11 |
from modules.presets import *
|
| 12 |
from modules.overwrites import *
|
| 13 |
-
from modules.
|
|
|
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
gr.Chatbot._postprocess_chat_messages = postprocess_chat_messages
|
| 17 |
gr.Chatbot.postprocess = postprocess
|
| 18 |
PromptHelper.compact_text_chunks = compact_text_chunks
|
| 19 |
|
| 20 |
with open("assets/custom.css", "r", encoding="utf-8") as f:
|
| 21 |
customCSS = f.read()
|
| 22 |
|
| 23 |
-
def create_new_model():
|
| 24 |
-
return get_model(model_name = MODELS[DEFAULT_MODEL], access_key = my_api_key)[0]
|
| 25 |
-
|
| 26 |
with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
| 27 |
-
|
|
|
|
| 28 |
promptTemplates = gr.State(load_template(get_template_names(plain=True)[0], mode=2))
|
| 29 |
-
user_question = gr.State("")
|
| 30 |
user_api_key = gr.State(my_api_key)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
topic = gr.State(
|
| 34 |
|
| 35 |
with gr.Row():
|
| 36 |
-
gr.HTML(
|
| 37 |
status_display = gr.Markdown(get_geoip(), elem_id="status_display")
|
| 38 |
-
with gr.Row(elem_id="float_display"):
|
| 39 |
-
user_info = gr.Markdown(value="getting user info...", elem_id="user_info")
|
| 40 |
-
|
| 41 |
-
# https://github.com/gradio-app/gradio/pull/3296
|
| 42 |
-
def create_greeting(request: gr.Request):
|
| 43 |
-
if hasattr(request, "username") and request.username: # is not None or is not ""
|
| 44 |
-
logging.info(f"Get User Name: {request.username}")
|
| 45 |
-
return gr.Markdown.update(value=f"User: {request.username}"), request.username
|
| 46 |
-
else:
|
| 47 |
-
return gr.Markdown.update(value=f"User: default", visible=False), ""
|
| 48 |
-
demo.load(create_greeting, inputs=None, outputs=[user_info, user_name])
|
| 49 |
|
| 50 |
with gr.Row().style(equal_height=True):
|
| 51 |
with gr.Column(scale=5):
|
| 52 |
with gr.Row():
|
| 53 |
chatbot = gr.Chatbot(elem_id="chuanhu_chatbot").style(height="100%")
|
| 54 |
with gr.Row():
|
| 55 |
-
with gr.Column(
|
| 56 |
user_input = gr.Textbox(
|
| 57 |
-
|
| 58 |
-
show_label=False, placeholder=i18n("在这里输入")
|
| 59 |
).style(container=False)
|
| 60 |
-
with gr.Column(min_width=
|
| 61 |
-
submitBtn = gr.Button(
|
| 62 |
-
cancelBtn = gr.Button(
|
| 63 |
with gr.Row():
|
| 64 |
emptyBtn = gr.Button(
|
| 65 |
-
|
| 66 |
)
|
| 67 |
-
retryBtn = gr.Button(
|
| 68 |
-
delFirstBtn = gr.Button(
|
| 69 |
-
delLastBtn = gr.Button(
|
| 70 |
-
|
| 71 |
-
with gr.Column(min_width=20, scale=1):
|
| 72 |
-
likeBtn = gr.Button(i18n("👍"))
|
| 73 |
-
with gr.Column(min_width=20, scale=1):
|
| 74 |
-
dislikeBtn = gr.Button(i18n("👎"))
|
| 75 |
|
| 76 |
with gr.Column():
|
| 77 |
with gr.Column(min_width=50, scale=1):
|
| 78 |
-
with gr.Tab(label=
|
| 79 |
keyTxt = gr.Textbox(
|
| 80 |
show_label=True,
|
| 81 |
-
placeholder=f"
|
| 82 |
-
value=hide_middle_chars(
|
| 83 |
type="password",
|
| 84 |
visible=not HIDE_MY_KEY,
|
| 85 |
label="API-Key",
|
| 86 |
)
|
| 87 |
-
|
| 88 |
-
usageTxt = gr.Markdown(i18n("多账号模式已开启,无需输入key,可直接开始对话"), elem_id="usage_display", elem_classes="insert_block")
|
| 89 |
-
else:
|
| 90 |
-
usageTxt = gr.Markdown(i18n("**发送消息** 或 **提交key** 以显示额度"), elem_id="usage_display", elem_classes="insert_block")
|
| 91 |
model_select_dropdown = gr.Dropdown(
|
| 92 |
-
label=
|
| 93 |
)
|
| 94 |
-
|
| 95 |
-
label=
|
| 96 |
)
|
| 97 |
-
|
| 98 |
-
use_streaming_checkbox = gr.Checkbox(
|
| 99 |
-
label=i18n("实时传输回答"), value=True, visible=ENABLE_STREAMING_OPTION
|
| 100 |
-
)
|
| 101 |
-
single_turn_checkbox = gr.Checkbox(label=i18n("单轮对话"), value=False)
|
| 102 |
-
use_websearch_checkbox = gr.Checkbox(label=i18n("使用在线搜索"), value=False)
|
| 103 |
language_select_dropdown = gr.Dropdown(
|
| 104 |
-
label=
|
| 105 |
choices=REPLY_LANGUAGES,
|
| 106 |
multiselect=False,
|
| 107 |
value=REPLY_LANGUAGES[0],
|
| 108 |
)
|
| 109 |
-
index_files = gr.Files(label=
|
| 110 |
-
two_column = gr.Checkbox(label=i18n("双栏pdf"), value=advance_docs["pdf"].get("two_column", False))
|
| 111 |
-
# TODO: 公式ocr
|
| 112 |
-
# formula_ocr = gr.Checkbox(label=i18n("识别公式"), value=advance_docs["pdf"].get("formula_ocr", False))
|
| 113 |
|
| 114 |
with gr.Tab(label="Prompt"):
|
| 115 |
systemPromptTxt = gr.Textbox(
|
| 116 |
show_label=True,
|
| 117 |
-
placeholder=
|
| 118 |
label="System prompt",
|
| 119 |
-
value=
|
| 120 |
lines=10,
|
| 121 |
).style(container=False)
|
| 122 |
-
with gr.Accordion(label=
|
| 123 |
with gr.Column():
|
| 124 |
with gr.Row():
|
| 125 |
with gr.Column(scale=6):
|
| 126 |
templateFileSelectDropdown = gr.Dropdown(
|
| 127 |
-
label=
|
| 128 |
choices=get_template_names(plain=True),
|
| 129 |
multiselect=False,
|
| 130 |
value=get_template_names(plain=True)[0],
|
| 131 |
).style(container=False)
|
| 132 |
with gr.Column(scale=1):
|
| 133 |
-
templateRefreshBtn = gr.Button(
|
| 134 |
with gr.Row():
|
| 135 |
with gr.Column():
|
| 136 |
templateSelectDropdown = gr.Dropdown(
|
| 137 |
-
label=
|
| 138 |
choices=load_template(
|
| 139 |
get_template_names(plain=True)[0], mode=1
|
| 140 |
),
|
| 141 |
multiselect=False,
|
|
|
|
|
|
|
|
|
|
| 142 |
).style(container=False)
|
| 143 |
|
| 144 |
-
with gr.Tab(label=
|
| 145 |
-
with gr.Accordion(label=
|
| 146 |
with gr.Column():
|
| 147 |
with gr.Row():
|
| 148 |
with gr.Column(scale=6):
|
| 149 |
historyFileSelectDropdown = gr.Dropdown(
|
| 150 |
-
label=
|
| 151 |
choices=get_history_names(plain=True),
|
| 152 |
multiselect=False,
|
| 153 |
value=get_history_names(plain=True)[0],
|
| 154 |
)
|
| 155 |
with gr.Column(scale=1):
|
| 156 |
-
historyRefreshBtn = gr.Button(
|
| 157 |
with gr.Row():
|
| 158 |
with gr.Column(scale=6):
|
| 159 |
saveFileName = gr.Textbox(
|
| 160 |
show_label=True,
|
| 161 |
-
placeholder=
|
| 162 |
-
label=
|
| 163 |
-
value=
|
| 164 |
).style(container=True)
|
| 165 |
with gr.Column(scale=1):
|
| 166 |
-
saveHistoryBtn = gr.Button(
|
| 167 |
-
exportMarkdownBtn = gr.Button(
|
| 168 |
-
gr.Markdown(
|
| 169 |
with gr.Row():
|
| 170 |
with gr.Column():
|
| 171 |
downloadFile = gr.File(interactive=True)
|
| 172 |
|
| 173 |
-
with gr.Tab(label=
|
| 174 |
-
gr.Markdown(
|
| 175 |
-
gr.
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
maximum=2.0,
|
| 180 |
-
value=1.0,
|
| 181 |
-
step=0.1,
|
| 182 |
-
interactive=True,
|
| 183 |
-
label="temperature",
|
| 184 |
-
)
|
| 185 |
-
top_p_slider = gr.Slider(
|
| 186 |
minimum=-0,
|
| 187 |
maximum=1.0,
|
| 188 |
value=1.0,
|
| 189 |
step=0.05,
|
| 190 |
interactive=True,
|
| 191 |
-
label="
|
| 192 |
)
|
| 193 |
-
|
| 194 |
-
minimum=
|
| 195 |
-
maximum=10,
|
| 196 |
-
value=1,
|
| 197 |
-
step=1,
|
| 198 |
-
interactive=True,
|
| 199 |
-
label="n choices",
|
| 200 |
-
)
|
| 201 |
-
stop_sequence_txt = gr.Textbox(
|
| 202 |
-
show_label=True,
|
| 203 |
-
placeholder=i18n("在这里输入停止符,用英文逗号隔开..."),
|
| 204 |
-
label="stop",
|
| 205 |
-
value="",
|
| 206 |
-
lines=1,
|
| 207 |
-
)
|
| 208 |
-
max_context_length_slider = gr.Slider(
|
| 209 |
-
minimum=1,
|
| 210 |
-
maximum=32768,
|
| 211 |
-
value=2000,
|
| 212 |
-
step=1,
|
| 213 |
-
interactive=True,
|
| 214 |
-
label="max context",
|
| 215 |
-
)
|
| 216 |
-
max_generation_slider = gr.Slider(
|
| 217 |
-
minimum=1,
|
| 218 |
-
maximum=32768,
|
| 219 |
-
value=1000,
|
| 220 |
-
step=1,
|
| 221 |
-
interactive=True,
|
| 222 |
-
label="max generations",
|
| 223 |
-
)
|
| 224 |
-
presence_penalty_slider = gr.Slider(
|
| 225 |
-
minimum=-2.0,
|
| 226 |
-
maximum=2.0,
|
| 227 |
-
value=0.0,
|
| 228 |
-
step=0.01,
|
| 229 |
-
interactive=True,
|
| 230 |
-
label="presence penalty",
|
| 231 |
-
)
|
| 232 |
-
frequency_penalty_slider = gr.Slider(
|
| 233 |
-
minimum=-2.0,
|
| 234 |
maximum=2.0,
|
| 235 |
-
value=
|
| 236 |
-
step=0.
|
| 237 |
interactive=True,
|
| 238 |
-
label="
|
| 239 |
-
)
|
| 240 |
-
logit_bias_txt = gr.Textbox(
|
| 241 |
-
show_label=True,
|
| 242 |
-
placeholder=f"word:likelihood",
|
| 243 |
-
label="logit bias",
|
| 244 |
-
value="",
|
| 245 |
-
lines=1,
|
| 246 |
-
)
|
| 247 |
-
user_identifier_txt = gr.Textbox(
|
| 248 |
-
show_label=True,
|
| 249 |
-
placeholder=i18n("用于定位滥用行为"),
|
| 250 |
-
label=i18n("用户名"),
|
| 251 |
-
value=user_name.value,
|
| 252 |
-
lines=1,
|
| 253 |
)
|
| 254 |
|
| 255 |
-
with gr.Accordion(
|
| 256 |
-
|
| 257 |
-
apihostTxt = gr.Textbox(
|
| 258 |
show_label=True,
|
| 259 |
-
placeholder=
|
| 260 |
-
label="API
|
| 261 |
-
value=
|
| 262 |
-
lines=
|
| 263 |
)
|
| 264 |
-
changeAPIURLBtn = gr.Button(
|
| 265 |
proxyTxt = gr.Textbox(
|
| 266 |
show_label=True,
|
| 267 |
-
placeholder=
|
| 268 |
-
label=
|
| 269 |
value="",
|
| 270 |
lines=2,
|
| 271 |
)
|
| 272 |
-
changeProxyBtn = gr.Button(
|
| 273 |
-
default_btn = gr.Button(i18n("🔙 恢复默认设置"))
|
| 274 |
|
| 275 |
-
gr.Markdown(
|
| 276 |
-
gr.HTML(
|
| 277 |
-
demo.load(refresh_ui_elements_on_load, [current_model, model_select_dropdown], [like_dislike_area], show_progress=False)
|
| 278 |
chatgpt_predict_args = dict(
|
| 279 |
fn=predict,
|
| 280 |
inputs=[
|
| 281 |
-
|
|
|
|
|
|
|
| 282 |
user_question,
|
| 283 |
chatbot,
|
|
|
|
|
|
|
|
|
|
| 284 |
use_streaming_checkbox,
|
|
|
|
| 285 |
use_websearch_checkbox,
|
| 286 |
index_files,
|
| 287 |
language_select_dropdown,
|
| 288 |
],
|
| 289 |
-
outputs=[chatbot, status_display],
|
| 290 |
show_progress=True,
|
| 291 |
)
|
| 292 |
|
|
@@ -310,18 +269,12 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
|
| 310 |
)
|
| 311 |
|
| 312 |
get_usage_args = dict(
|
| 313 |
-
fn=
|
| 314 |
-
)
|
| 315 |
-
|
| 316 |
-
load_history_from_file_args = dict(
|
| 317 |
-
fn=load_chat_history,
|
| 318 |
-
inputs=[current_model, historyFileSelectDropdown, chatbot, user_name],
|
| 319 |
-
outputs=[saveFileName, systemPromptTxt, chatbot]
|
| 320 |
)
|
| 321 |
|
| 322 |
|
| 323 |
# Chatbot
|
| 324 |
-
cancelBtn.click(
|
| 325 |
|
| 326 |
user_input.submit(**transfer_input_args).then(**chatgpt_predict_args).then(**end_outputing_args)
|
| 327 |
user_input.submit(**get_usage_args)
|
|
@@ -329,69 +282,69 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
|
| 329 |
submitBtn.click(**transfer_input_args).then(**chatgpt_predict_args).then(**end_outputing_args)
|
| 330 |
submitBtn.click(**get_usage_args)
|
| 331 |
|
| 332 |
-
index_files.change(handle_file_upload, [current_model, index_files, chatbot], [index_files, chatbot, status_display])
|
| 333 |
-
|
| 334 |
emptyBtn.click(
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
outputs=[chatbot, status_display],
|
| 338 |
show_progress=True,
|
| 339 |
)
|
|
|
|
| 340 |
|
| 341 |
retryBtn.click(**start_outputing_args).then(
|
| 342 |
retry,
|
| 343 |
[
|
| 344 |
-
|
|
|
|
|
|
|
| 345 |
chatbot,
|
|
|
|
|
|
|
|
|
|
| 346 |
use_streaming_checkbox,
|
| 347 |
-
|
| 348 |
-
index_files,
|
| 349 |
language_select_dropdown,
|
| 350 |
],
|
| 351 |
-
[chatbot, status_display],
|
| 352 |
show_progress=True,
|
| 353 |
).then(**end_outputing_args)
|
| 354 |
retryBtn.click(**get_usage_args)
|
| 355 |
|
| 356 |
delFirstBtn.click(
|
| 357 |
delete_first_conversation,
|
| 358 |
-
[
|
| 359 |
-
[status_display],
|
| 360 |
)
|
| 361 |
|
| 362 |
delLastBtn.click(
|
| 363 |
delete_last_conversation,
|
| 364 |
-
[
|
| 365 |
-
[chatbot, status_display],
|
| 366 |
-
show_progress=
|
| 367 |
-
)
|
| 368 |
-
|
| 369 |
-
likeBtn.click(
|
| 370 |
-
like,
|
| 371 |
-
[current_model],
|
| 372 |
-
[status_display],
|
| 373 |
-
show_progress=False
|
| 374 |
)
|
| 375 |
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
[
|
| 379 |
-
|
| 380 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
)
|
|
|
|
| 382 |
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
# LLM Models
|
| 386 |
-
keyTxt.change(set_key, [current_model, keyTxt], [user_api_key, status_display]).then(**get_usage_args)
|
| 387 |
keyTxt.submit(**get_usage_args)
|
| 388 |
-
single_turn_checkbox.change(set_single_turn, [current_model, single_turn_checkbox], None)
|
| 389 |
-
model_select_dropdown.change(get_model, [model_select_dropdown, lora_select_dropdown, user_api_key, temperature_slider, top_p_slider, systemPromptTxt], [current_model, status_display, lora_select_dropdown], show_progress=True)
|
| 390 |
-
model_select_dropdown.change(toggle_like_btn_visibility, [model_select_dropdown], [like_dislike_area], show_progress=False)
|
| 391 |
-
lora_select_dropdown.change(get_model, [model_select_dropdown, lora_select_dropdown, user_api_key, temperature_slider, top_p_slider, systemPromptTxt], [current_model, status_display], show_progress=True)
|
| 392 |
|
| 393 |
# Template
|
| 394 |
-
systemPromptTxt.change(set_system_prompt, [current_model, systemPromptTxt], None)
|
| 395 |
templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])
|
| 396 |
templateFileSelectDropdown.change(
|
| 397 |
load_template,
|
|
@@ -409,39 +362,37 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
|
| 409 |
# S&L
|
| 410 |
saveHistoryBtn.click(
|
| 411 |
save_chat_history,
|
| 412 |
-
[
|
| 413 |
downloadFile,
|
| 414 |
show_progress=True,
|
| 415 |
)
|
| 416 |
-
saveHistoryBtn.click(get_history_names,
|
| 417 |
exportMarkdownBtn.click(
|
| 418 |
export_markdown,
|
| 419 |
-
[
|
| 420 |
downloadFile,
|
| 421 |
show_progress=True,
|
| 422 |
)
|
| 423 |
-
historyRefreshBtn.click(get_history_names,
|
| 424 |
-
historyFileSelectDropdown.change(
|
| 425 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
|
| 427 |
# Advanced
|
| 428 |
-
max_context_length_slider.change(set_token_upper_limit, [current_model, max_context_length_slider], None)
|
| 429 |
-
temperature_slider.change(set_temperature, [current_model, temperature_slider], None)
|
| 430 |
-
top_p_slider.change(set_top_p, [current_model, top_p_slider], None)
|
| 431 |
-
n_choices_slider.change(set_n_choices, [current_model, n_choices_slider], None)
|
| 432 |
-
stop_sequence_txt.change(set_stop_sequence, [current_model, stop_sequence_txt], None)
|
| 433 |
-
max_generation_slider.change(set_max_tokens, [current_model, max_generation_slider], None)
|
| 434 |
-
presence_penalty_slider.change(set_presence_penalty, [current_model, presence_penalty_slider], None)
|
| 435 |
-
frequency_penalty_slider.change(set_frequency_penalty, [current_model, frequency_penalty_slider], None)
|
| 436 |
-
logit_bias_txt.change(set_logit_bias, [current_model, logit_bias_txt], None)
|
| 437 |
-
user_identifier_txt.change(set_user_identifier, [current_model, user_identifier_txt], None)
|
| 438 |
-
|
| 439 |
default_btn.click(
|
| 440 |
-
reset_default, [], [
|
| 441 |
)
|
| 442 |
changeAPIURLBtn.click(
|
| 443 |
-
|
| 444 |
-
[
|
| 445 |
[status_display],
|
| 446 |
show_progress=True,
|
| 447 |
)
|
|
@@ -458,13 +409,22 @@ logging.info(
|
|
| 458 |
+ colorama.Style.RESET_ALL
|
| 459 |
)
|
| 460 |
# 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
|
| 461 |
-
demo.title =
|
| 462 |
|
| 463 |
if __name__ == "__main__":
|
| 464 |
reload_javascript()
|
| 465 |
-
demo.queue(
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
|
|
|
|
|
|
|
| 8 |
from modules.utils import *
|
| 9 |
from modules.presets import *
|
| 10 |
from modules.overwrites import *
|
| 11 |
+
from modules.chat_func import *
|
| 12 |
+
from modules.openai_func import get_usage
|
| 13 |
|
| 14 |
+
logging.basicConfig(
|
| 15 |
+
level=logging.DEBUG,
|
| 16 |
+
format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s",
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
my_api_key = "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" # 在这里输入你的 API 密钥
|
| 20 |
+
|
| 21 |
+
# if we are running in Docker
|
| 22 |
+
if os.environ.get("dockerrun") == "yes":
|
| 23 |
+
dockerflag = True
|
| 24 |
+
else:
|
| 25 |
+
dockerflag = False
|
| 26 |
+
|
| 27 |
+
authflag = False
|
| 28 |
+
auth_list = []
|
| 29 |
+
|
| 30 |
+
if dockerflag:
|
| 31 |
+
my_api_key = os.environ.get("my_api_key")
|
| 32 |
+
if my_api_key == "empty":
|
| 33 |
+
logging.error("Please give a api key!")
|
| 34 |
+
sys.exit(1)
|
| 35 |
+
# auth
|
| 36 |
+
username = os.environ.get("USERNAME")
|
| 37 |
+
password = os.environ.get("PASSWORD")
|
| 38 |
+
if not (isinstance(username, type(None)) or isinstance(password, type(None))):
|
| 39 |
+
auth_list.append((os.environ.get("USERNAME"), os.environ.get("PASSWORD")))
|
| 40 |
+
authflag = True
|
| 41 |
+
else:
|
| 42 |
+
if (
|
| 43 |
+
not my_api_key
|
| 44 |
+
and os.path.exists("api_key.txt")
|
| 45 |
+
and os.path.getsize("api_key.txt")
|
| 46 |
+
):
|
| 47 |
+
with open("api_key.txt", "r") as f:
|
| 48 |
+
my_api_key = f.read().strip()
|
| 49 |
+
if os.path.exists("auth.json"):
|
| 50 |
+
authflag = True
|
| 51 |
+
with open("auth.json", "r", encoding='utf-8') as f:
|
| 52 |
+
auth = json.load(f)
|
| 53 |
+
for _ in auth:
|
| 54 |
+
if auth[_]["username"] and auth[_]["password"]:
|
| 55 |
+
auth_list.append((auth[_]["username"], auth[_]["password"]))
|
| 56 |
+
else:
|
| 57 |
+
logging.error("请检查auth.json文件中的用户名和密码!")
|
| 58 |
+
sys.exit(1)
|
| 59 |
|
|
|
|
| 60 |
gr.Chatbot.postprocess = postprocess
|
| 61 |
PromptHelper.compact_text_chunks = compact_text_chunks
|
| 62 |
|
| 63 |
with open("assets/custom.css", "r", encoding="utf-8") as f:
|
| 64 |
customCSS = f.read()
|
| 65 |
|
|
|
|
|
|
|
|
|
|
| 66 |
with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
|
| 67 |
+
history = gr.State([])
|
| 68 |
+
token_count = gr.State([])
|
| 69 |
promptTemplates = gr.State(load_template(get_template_names(plain=True)[0], mode=2))
|
|
|
|
| 70 |
user_api_key = gr.State(my_api_key)
|
| 71 |
+
user_question = gr.State("")
|
| 72 |
+
outputing = gr.State(False)
|
| 73 |
+
topic = gr.State("未命名对话历史记录")
|
| 74 |
|
| 75 |
with gr.Row():
|
| 76 |
+
gr.HTML(title)
|
| 77 |
status_display = gr.Markdown(get_geoip(), elem_id="status_display")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
with gr.Row().style(equal_height=True):
|
| 80 |
with gr.Column(scale=5):
|
| 81 |
with gr.Row():
|
| 82 |
chatbot = gr.Chatbot(elem_id="chuanhu_chatbot").style(height="100%")
|
| 83 |
with gr.Row():
|
| 84 |
+
with gr.Column(scale=12):
|
| 85 |
user_input = gr.Textbox(
|
| 86 |
+
show_label=False, placeholder="在这里输入"
|
|
|
|
| 87 |
).style(container=False)
|
| 88 |
+
with gr.Column(min_width=70, scale=1):
|
| 89 |
+
submitBtn = gr.Button("发送", variant="primary")
|
| 90 |
+
cancelBtn = gr.Button("取消", variant="secondary", visible=False)
|
| 91 |
with gr.Row():
|
| 92 |
emptyBtn = gr.Button(
|
| 93 |
+
"🧹 新的对话",
|
| 94 |
)
|
| 95 |
+
retryBtn = gr.Button("🔄 重新生成")
|
| 96 |
+
delFirstBtn = gr.Button("🗑️ 删除最旧对话")
|
| 97 |
+
delLastBtn = gr.Button("🗑️ 删除最新对话")
|
| 98 |
+
reduceTokenBtn = gr.Button("♻️ 总结对话")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
with gr.Column():
|
| 101 |
with gr.Column(min_width=50, scale=1):
|
| 102 |
+
with gr.Tab(label="ChatGPT"):
|
| 103 |
keyTxt = gr.Textbox(
|
| 104 |
show_label=True,
|
| 105 |
+
placeholder=f"OpenAI API-key...",
|
| 106 |
+
value=hide_middle_chars(my_api_key),
|
| 107 |
type="password",
|
| 108 |
visible=not HIDE_MY_KEY,
|
| 109 |
label="API-Key",
|
| 110 |
)
|
| 111 |
+
usageTxt = gr.Markdown("**发送消息** 或 **提交key** 以显示额度", elem_id="usage_display")
|
|
|
|
|
|
|
|
|
|
| 112 |
model_select_dropdown = gr.Dropdown(
|
| 113 |
+
label="选择模型", choices=MODELS, multiselect=False, value=MODELS[0]
|
| 114 |
)
|
| 115 |
+
use_streaming_checkbox = gr.Checkbox(
|
| 116 |
+
label="实时传输回答", value=True, visible=enable_streaming_option
|
| 117 |
)
|
| 118 |
+
use_websearch_checkbox = gr.Checkbox(label="使用在线搜索", value=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
language_select_dropdown = gr.Dropdown(
|
| 120 |
+
label="选择回复语言(针对搜索&索引功能)",
|
| 121 |
choices=REPLY_LANGUAGES,
|
| 122 |
multiselect=False,
|
| 123 |
value=REPLY_LANGUAGES[0],
|
| 124 |
)
|
| 125 |
+
index_files = gr.Files(label="上传索引文件", type="file", multiple=True)
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
with gr.Tab(label="Prompt"):
|
| 128 |
systemPromptTxt = gr.Textbox(
|
| 129 |
show_label=True,
|
| 130 |
+
placeholder=f"在这里输入System Prompt...",
|
| 131 |
label="System prompt",
|
| 132 |
+
value=initial_prompt,
|
| 133 |
lines=10,
|
| 134 |
).style(container=False)
|
| 135 |
+
with gr.Accordion(label="加载Prompt模板", open=True):
|
| 136 |
with gr.Column():
|
| 137 |
with gr.Row():
|
| 138 |
with gr.Column(scale=6):
|
| 139 |
templateFileSelectDropdown = gr.Dropdown(
|
| 140 |
+
label="选择Prompt模板集合文件",
|
| 141 |
choices=get_template_names(plain=True),
|
| 142 |
multiselect=False,
|
| 143 |
value=get_template_names(plain=True)[0],
|
| 144 |
).style(container=False)
|
| 145 |
with gr.Column(scale=1):
|
| 146 |
+
templateRefreshBtn = gr.Button("🔄 刷新")
|
| 147 |
with gr.Row():
|
| 148 |
with gr.Column():
|
| 149 |
templateSelectDropdown = gr.Dropdown(
|
| 150 |
+
label="从Prompt模板中加载",
|
| 151 |
choices=load_template(
|
| 152 |
get_template_names(plain=True)[0], mode=1
|
| 153 |
),
|
| 154 |
multiselect=False,
|
| 155 |
+
value=load_template(
|
| 156 |
+
get_template_names(plain=True)[0], mode=1
|
| 157 |
+
)[0],
|
| 158 |
).style(container=False)
|
| 159 |
|
| 160 |
+
with gr.Tab(label="保存/加载"):
|
| 161 |
+
with gr.Accordion(label="保存/加载对话历史记录", open=True):
|
| 162 |
with gr.Column():
|
| 163 |
with gr.Row():
|
| 164 |
with gr.Column(scale=6):
|
| 165 |
historyFileSelectDropdown = gr.Dropdown(
|
| 166 |
+
label="从列表中加载对话",
|
| 167 |
choices=get_history_names(plain=True),
|
| 168 |
multiselect=False,
|
| 169 |
value=get_history_names(plain=True)[0],
|
| 170 |
)
|
| 171 |
with gr.Column(scale=1):
|
| 172 |
+
historyRefreshBtn = gr.Button("🔄 刷新")
|
| 173 |
with gr.Row():
|
| 174 |
with gr.Column(scale=6):
|
| 175 |
saveFileName = gr.Textbox(
|
| 176 |
show_label=True,
|
| 177 |
+
placeholder=f"设置文件名: 默认为.json,可选为.md",
|
| 178 |
+
label="设置保存文件名",
|
| 179 |
+
value="对话历史记录",
|
| 180 |
).style(container=True)
|
| 181 |
with gr.Column(scale=1):
|
| 182 |
+
saveHistoryBtn = gr.Button("💾 保存对话")
|
| 183 |
+
exportMarkdownBtn = gr.Button("📝 导出为Markdown")
|
| 184 |
+
gr.Markdown("默认保存于history文件夹")
|
| 185 |
with gr.Row():
|
| 186 |
with gr.Column():
|
| 187 |
downloadFile = gr.File(interactive=True)
|
| 188 |
|
| 189 |
+
with gr.Tab(label="高级"):
|
| 190 |
+
gr.Markdown("# ⚠️ 务必谨慎更改 ⚠️\n\n如果无法使用请恢复默认设置")
|
| 191 |
+
default_btn = gr.Button("🔙 恢复默认设置")
|
| 192 |
+
|
| 193 |
+
with gr.Accordion("参数", open=False):
|
| 194 |
+
top_p = gr.Slider(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
minimum=-0,
|
| 196 |
maximum=1.0,
|
| 197 |
value=1.0,
|
| 198 |
step=0.05,
|
| 199 |
interactive=True,
|
| 200 |
+
label="Top-p",
|
| 201 |
)
|
| 202 |
+
temperature = gr.Slider(
|
| 203 |
+
minimum=-0,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
maximum=2.0,
|
| 205 |
+
value=1.0,
|
| 206 |
+
step=0.1,
|
| 207 |
interactive=True,
|
| 208 |
+
label="Temperature",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
)
|
| 210 |
|
| 211 |
+
with gr.Accordion("网络设置", open=False):
|
| 212 |
+
apiurlTxt = gr.Textbox(
|
|
|
|
| 213 |
show_label=True,
|
| 214 |
+
placeholder=f"在这里输入API地址...",
|
| 215 |
+
label="API地址",
|
| 216 |
+
value="https://api.openai.com/v1/chat/completions",
|
| 217 |
+
lines=2,
|
| 218 |
)
|
| 219 |
+
changeAPIURLBtn = gr.Button("🔄 切换API地址")
|
| 220 |
proxyTxt = gr.Textbox(
|
| 221 |
show_label=True,
|
| 222 |
+
placeholder=f"在这里输入代理地址...",
|
| 223 |
+
label="代理地址(示例:http://127.0.0.1:10809)",
|
| 224 |
value="",
|
| 225 |
lines=2,
|
| 226 |
)
|
| 227 |
+
changeProxyBtn = gr.Button("🔄 设置代理地址")
|
|
|
|
| 228 |
|
| 229 |
+
gr.Markdown(description)
|
| 230 |
+
gr.HTML(footer.format(versions=versions_html()), elem_id="footer")
|
|
|
|
| 231 |
chatgpt_predict_args = dict(
|
| 232 |
fn=predict,
|
| 233 |
inputs=[
|
| 234 |
+
user_api_key,
|
| 235 |
+
systemPromptTxt,
|
| 236 |
+
history,
|
| 237 |
user_question,
|
| 238 |
chatbot,
|
| 239 |
+
token_count,
|
| 240 |
+
top_p,
|
| 241 |
+
temperature,
|
| 242 |
use_streaming_checkbox,
|
| 243 |
+
model_select_dropdown,
|
| 244 |
use_websearch_checkbox,
|
| 245 |
index_files,
|
| 246 |
language_select_dropdown,
|
| 247 |
],
|
| 248 |
+
outputs=[chatbot, history, status_display, token_count],
|
| 249 |
show_progress=True,
|
| 250 |
)
|
| 251 |
|
|
|
|
| 269 |
)
|
| 270 |
|
| 271 |
get_usage_args = dict(
|
| 272 |
+
fn=get_usage, inputs=[user_api_key], outputs=[usageTxt], show_progress=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
)
|
| 274 |
|
| 275 |
|
| 276 |
# Chatbot
|
| 277 |
+
cancelBtn.click(cancel_outputing, [], [])
|
| 278 |
|
| 279 |
user_input.submit(**transfer_input_args).then(**chatgpt_predict_args).then(**end_outputing_args)
|
| 280 |
user_input.submit(**get_usage_args)
|
|
|
|
| 282 |
submitBtn.click(**transfer_input_args).then(**chatgpt_predict_args).then(**end_outputing_args)
|
| 283 |
submitBtn.click(**get_usage_args)
|
| 284 |
|
|
|
|
|
|
|
| 285 |
emptyBtn.click(
|
| 286 |
+
reset_state,
|
| 287 |
+
outputs=[chatbot, history, token_count, status_display],
|
|
|
|
| 288 |
show_progress=True,
|
| 289 |
)
|
| 290 |
+
emptyBtn.click(**reset_textbox_args)
|
| 291 |
|
| 292 |
retryBtn.click(**start_outputing_args).then(
|
| 293 |
retry,
|
| 294 |
[
|
| 295 |
+
user_api_key,
|
| 296 |
+
systemPromptTxt,
|
| 297 |
+
history,
|
| 298 |
chatbot,
|
| 299 |
+
token_count,
|
| 300 |
+
top_p,
|
| 301 |
+
temperature,
|
| 302 |
use_streaming_checkbox,
|
| 303 |
+
model_select_dropdown,
|
|
|
|
| 304 |
language_select_dropdown,
|
| 305 |
],
|
| 306 |
+
[chatbot, history, status_display, token_count],
|
| 307 |
show_progress=True,
|
| 308 |
).then(**end_outputing_args)
|
| 309 |
retryBtn.click(**get_usage_args)
|
| 310 |
|
| 311 |
delFirstBtn.click(
|
| 312 |
delete_first_conversation,
|
| 313 |
+
[history, token_count],
|
| 314 |
+
[history, token_count, status_display],
|
| 315 |
)
|
| 316 |
|
| 317 |
delLastBtn.click(
|
| 318 |
delete_last_conversation,
|
| 319 |
+
[chatbot, history, token_count],
|
| 320 |
+
[chatbot, history, token_count, status_display],
|
| 321 |
+
show_progress=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
)
|
| 323 |
|
| 324 |
+
reduceTokenBtn.click(
|
| 325 |
+
reduce_token_size,
|
| 326 |
+
[
|
| 327 |
+
user_api_key,
|
| 328 |
+
systemPromptTxt,
|
| 329 |
+
history,
|
| 330 |
+
chatbot,
|
| 331 |
+
token_count,
|
| 332 |
+
top_p,
|
| 333 |
+
temperature,
|
| 334 |
+
gr.State(sum(token_count.value[-4:])),
|
| 335 |
+
model_select_dropdown,
|
| 336 |
+
language_select_dropdown,
|
| 337 |
+
],
|
| 338 |
+
[chatbot, history, status_display, token_count],
|
| 339 |
+
show_progress=True,
|
| 340 |
)
|
| 341 |
+
reduceTokenBtn.click(**get_usage_args)
|
| 342 |
|
| 343 |
+
# ChatGPT
|
| 344 |
+
keyTxt.change(submit_key, keyTxt, [user_api_key, status_display]).then(**get_usage_args)
|
|
|
|
|
|
|
| 345 |
keyTxt.submit(**get_usage_args)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
|
| 347 |
# Template
|
|
|
|
| 348 |
templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])
|
| 349 |
templateFileSelectDropdown.change(
|
| 350 |
load_template,
|
|
|
|
| 362 |
# S&L
|
| 363 |
saveHistoryBtn.click(
|
| 364 |
save_chat_history,
|
| 365 |
+
[saveFileName, systemPromptTxt, history, chatbot],
|
| 366 |
downloadFile,
|
| 367 |
show_progress=True,
|
| 368 |
)
|
| 369 |
+
saveHistoryBtn.click(get_history_names, None, [historyFileSelectDropdown])
|
| 370 |
exportMarkdownBtn.click(
|
| 371 |
export_markdown,
|
| 372 |
+
[saveFileName, systemPromptTxt, history, chatbot],
|
| 373 |
downloadFile,
|
| 374 |
show_progress=True,
|
| 375 |
)
|
| 376 |
+
historyRefreshBtn.click(get_history_names, None, [historyFileSelectDropdown])
|
| 377 |
+
historyFileSelectDropdown.change(
|
| 378 |
+
load_chat_history,
|
| 379 |
+
[historyFileSelectDropdown, systemPromptTxt, history, chatbot],
|
| 380 |
+
[saveFileName, systemPromptTxt, history, chatbot],
|
| 381 |
+
show_progress=True,
|
| 382 |
+
)
|
| 383 |
+
downloadFile.change(
|
| 384 |
+
load_chat_history,
|
| 385 |
+
[downloadFile, systemPromptTxt, history, chatbot],
|
| 386 |
+
[saveFileName, systemPromptTxt, history, chatbot],
|
| 387 |
+
)
|
| 388 |
|
| 389 |
# Advanced
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
default_btn.click(
|
| 391 |
+
reset_default, [], [apiurlTxt, proxyTxt, status_display], show_progress=True
|
| 392 |
)
|
| 393 |
changeAPIURLBtn.click(
|
| 394 |
+
change_api_url,
|
| 395 |
+
[apiurlTxt],
|
| 396 |
[status_display],
|
| 397 |
show_progress=True,
|
| 398 |
)
|
|
|
|
| 409 |
+ colorama.Style.RESET_ALL
|
| 410 |
)
|
| 411 |
# 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
|
| 412 |
+
demo.title = "川虎ChatGPT 🚀"
|
| 413 |
|
| 414 |
if __name__ == "__main__":
|
| 415 |
reload_javascript()
|
| 416 |
+
# if running in Docker demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 417 |
+
if dockerflag:
|
| 418 |
+
if authflag:
|
| 419 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 420 |
+
else:
|
| 421 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 422 |
+
# if not running in Docker
|
| 423 |
+
else:
|
| 424 |
+
if authflag:
|
| 425 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 426 |
+
else:
|
| 427 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 428 |
+
# demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=7860, share=False) # 可自定义端口
|
| 429 |
+
# demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=7860,auth=("在这里填写用户名", "在这里填写密码")) # 可设置用户名与密码
|
| 430 |
+
# demo.queue(concurrency_count=CONCURRENT_COUNT).launch(auth=("在这里填写用户名", "在这��填写密码")) # 适合Nginx反向代理
|