MLP89/MLPChatGPTPlus2.0
0
1# -*- coding:utf-8 -*-2import os3import logging4import sys5 6import gradio as gr7 8from modules import config9from modules.config import *10from modules.utils import *11from modules.presets import *12from modules.overwrites import *13from modules.models.models import get_model14 15 16gr.Chatbot._postprocess_chat_messages = postprocess_chat_messages17gr.Chatbot.postprocess = postprocess18PromptHelper.compact_text_chunks = compact_text_chunks19 20with open("assets/custom.css", "r", encoding="utf-8") as f:21 customCSS = f.read()22 23def create_new_model():24 return get_model(model_name = MODELS[DEFAULT_MODEL], access_key = my_api_key)[0]25 26with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:27 user_name = gr.State("")28 promptTemplates = gr.State(load_template(get_template_names(plain=True)[0], mode=2))29 user_question = gr.State("")30 assert type(my_api_key)==str31 user_api_key = gr.State(my_api_key)32 current_model = gr.State(create_new_model)33 34 topic = gr.State(i18n("未命名对话历史记录"))35 36 with gr.Row():37 gr.HTML(CHUANHU_TITLE, elem_id="app_title")38 status_display = gr.Markdown(get_geoip(), elem_id="status_display")39 with gr.Row(elem_id="float_display"):40 user_info = gr.Markdown(value="getting user info...", elem_id="user_info")41 42 with gr.Row().style(equal_height=True):43 with gr.Column(scale=5):44 with gr.Row():45 chatbot = gr.Chatbot(label="Chuanhu Chat", elem_id="chuanhu_chatbot").style(height="100%")46 with gr.Row():47 with gr.Column(min_width=225, scale=12):48 user_input = gr.Textbox(49 elem_id="user_input_tb",50 show_label=False, placeholder=i18n("在这里输入")51 ).style(container=False)52 with gr.Column(min_width=42, scale=1):53 submitBtn = gr.Button(value="", variant="primary", elem_id="submit_btn")54 cancelBtn = gr.Button(value="", variant="secondary", visible=False, elem_id="cancel_btn")55 with gr.Row():56 emptyBtn = gr.Button(57 i18n("🧹 新的对话"), elem_id="empty_btn"58 )59 retryBtn = gr.Button(i18n("🔄 重新生成"))60 delFirstBtn = gr.Button(i18n("🗑️ 删除最旧对话"))61 delLastBtn = gr.Button(i18n("🗑️ 删除最新对话"))62 with gr.Row(visible=False) as like_dislike_area:63 with gr.Column(min_width=20, scale=1):64 likeBtn = gr.Button(i18n("👍"))65 with gr.Column(min_width=20, scale=1):66 dislikeBtn = gr.Button(i18n("👎"))67 68 with gr.Column():69 with gr.Column(min_width=50, scale=1):70 with gr.Tab(label=i18n("模型")):71 keyTxt = gr.Textbox(72 show_label=True,73 placeholder=f"Your API-key...",74 value=hide_middle_chars(user_api_key.value),75 type="password",76 visible=not HIDE_MY_KEY,77 label="API-Key",78 )79 if multi_api_key:80 usageTxt = gr.Markdown(i18n("多账号模式已开启,无需输入key,可直接开始对话"), elem_id="usage_display", elem_classes="insert_block")81 else:82 usageTxt = gr.Markdown(i18n("**发送消息** 或 **提交key** 以显示额度"), elem_id="usage_display", elem_classes="insert_block")83 model_select_dropdown = gr.Dropdown(84 label=i18n("选择模型"), choices=MODELS, multiselect=False, value=MODELS[DEFAULT_MODEL], interactive=True85 )86 lora_select_dropdown = gr.Dropdown(87 label=i18n("选择LoRA模型"), choices=[], multiselect=False, interactive=True, visible=False88 )89 with gr.Row():90 single_turn_checkbox = gr.Checkbox(label=i18n("单轮对话"), value=False)91 use_websearch_checkbox = gr.Checkbox(label=i18n("使用在线搜索"), value=False)92 # render_latex_checkbox = gr.Checkbox(label=i18n("渲染LaTeX公式"), value=render_latex, interactive=True, elem_id="render_latex_checkbox")93 language_select_dropdown = gr.Dropdown(94 label=i18n("选择回复语言(针对搜索&索引功能)"),95 choices=REPLY_LANGUAGES,96 multiselect=False,97 value=REPLY_LANGUAGES[0],98 )99 index_files = gr.Files(label=i18n("上传"), type="file")100 two_column = gr.Checkbox(label=i18n("双栏pdf"), value=advance_docs["pdf"].get("two_column", False))101 # TODO: 公式ocr102 # formula_ocr = gr.Checkbox(label=i18n("识别公式"), value=advance_docs["pdf"].get("formula_ocr", False))103 104 with gr.Tab(label="Prompt"):105 systemPromptTxt = gr.Textbox(106 show_label=True,107 placeholder=i18n("在这里输入System Prompt..."),108 label="System prompt",109 value=INITIAL_SYSTEM_PROMPT,110 lines=10,111 ).style(container=False)112 with gr.Accordion(label=i18n("加载Prompt模板"), open=True):113 with gr.Column():114 with gr.Row():115 with gr.Column(scale=6):116 templateFileSelectDropdown = gr.Dropdown(117 label=i18n("选择Prompt模板集合文件"),118 choices=get_template_names(plain=True),119 multiselect=False,120 value=get_template_names(plain=True)[0],121 ).style(container=False)122 with gr.Column(scale=1):123 templateRefreshBtn = gr.Button(i18n("🔄 刷新"))124 with gr.Row():125 with gr.Column():126 templateSelectDropdown = gr.Dropdown(127 label=i18n("从Prompt模板中加载"),128 choices=load_template(129 get_template_names(plain=True)[0], mode=1130 ),131 multiselect=False,132 ).style(container=False)133 134 with gr.Tab(label=i18n("保存/加载")):135 with gr.Accordion(label=i18n("保存/加载对话历史记录"), open=True):136 with gr.Column():137 with gr.Row():138 with gr.Column(scale=6):139 historyFileSelectDropdown = gr.Dropdown(140 label=i18n("从列表中加载对话"),141 choices=get_history_names(plain=True),142 multiselect=False143 )144 with gr.Column(scale=1):145 historyRefreshBtn = gr.Button(i18n("🔄 刷新"))146 with gr.Row():147 with gr.Column(scale=6):148 saveFileName = gr.Textbox(149 show_label=True,150 placeholder=i18n("设置文件名: 默认为.json,可选为.md"),151 label=i18n("设置保存文件名"),152 value=i18n("对话历史记录"),153 ).style(container=True)154 with gr.Column(scale=1):155 saveHistoryBtn = gr.Button(i18n("💾 保存对话"))156 exportMarkdownBtn = gr.Button(i18n("📝 导出为Markdown"))157 gr.Markdown(i18n("默认保存于history文件夹"))158 with gr.Row():159 with gr.Column():160 downloadFile = gr.File(interactive=True)161 162 with gr.Tab(label=i18n("高级")):163 gr.Markdown(i18n("# ⚠️ 务必谨慎更改 ⚠️\n\n如果无法使用请恢复默认设置"))164 gr.HTML(APPEARANCE_SWITCHER, elem_classes="insert_block")165 use_streaming_checkbox = gr.Checkbox(166 label=i18n("实时传输回答"), value=True, visible=ENABLE_STREAMING_OPTION167 )168 with gr.Accordion(i18n("参数"), open=False):169 temperature_slider = gr.Slider(170 minimum=-0,171 maximum=2.0,172 value=1.0,173 step=0.1,174 interactive=True,175 label="temperature",176 )177 top_p_slider = gr.Slider(178 minimum=-0,179 maximum=1.0,180 value=1.0,181 step=0.05,182 interactive=True,183 label="top-p",184 )185 n_choices_slider = gr.Slider(186 minimum=1,187 maximum=10,188 value=1,189 step=1,190 interactive=True,191 label="n choices",192 )193 stop_sequence_txt = gr.Textbox(194 show_label=True,195 placeholder=i18n("在这里输入停止符,用英文逗号隔开..."),196 label="stop",197 value="",198 lines=1,199 )200 max_context_length_slider = gr.Slider(201 minimum=1,202 maximum=32768,203 value=2000,204 step=1,205 interactive=True,206 label="max context",207 )208 max_generation_slider = gr.Slider(209 minimum=1,210 maximum=32768,211 value=1000,212 step=1,213 interactive=True,214 label="max generations",215 )216 presence_penalty_slider = gr.Slider(217 minimum=-2.0,218 maximum=2.0,219 value=0.0,220 step=0.01,221 interactive=True,222 label="presence penalty",223 )224 frequency_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="frequency penalty",231 )232 logit_bias_txt = gr.Textbox(233 show_label=True,234 placeholder=f"word:likelihood",235 label="logit bias",236 value="",237 lines=1,238 )239 user_identifier_txt = gr.Textbox(240 show_label=True,241 placeholder=i18n("用于定位滥用行为"),242 label=i18n("用户名"),243 value=user_name.value,244 lines=1,245 )246 247 with gr.Accordion(i18n("网络设置"), open=False, visible=False):248 # 优先展示自定义的api_host249 apihostTxt = gr.Textbox(250 show_label=True,251 placeholder=i18n("在这里输入API-Host..."),252 label="API-Host",253 value=config.api_host or shared.API_HOST,254 lines=1,255 )256 changeAPIURLBtn = gr.Button(i18n("🔄 切换API地址"))257 proxyTxt = gr.Textbox(258 show_label=True,259 placeholder=i18n("在这里输入代理地址..."),260 label=i18n("代理地址(示例:http://127.0.0.1:10809)"),261 value="",262 lines=2,263 )264 changeProxyBtn = gr.Button(i18n("🔄 设置代理地址"))265 default_btn = gr.Button(i18n("🔙 恢复默认设置"))266 267 gr.Markdown(CHUANHU_DESCRIPTION, elem_id="description")268 gr.HTML(FOOTER.format(versions=versions_html()), elem_id="footer")269 270 # https://github.com/gradio-app/gradio/pull/3296271 def create_greeting(request: gr.Request):272 if hasattr(request, "username") and request.username: # is not None or is not ""273 logging.info(f"Get User Name: {request.username}")274 user_info, user_name = gr.Markdown.update(value=f"User: {request.username}"), request.username275 else:276 user_info, user_name = gr.Markdown.update(value=f"", visible=False), ""277 current_model = get_model(model_name = MODELS[DEFAULT_MODEL], access_key = my_api_key)[0]278 current_model.set_user_identifier(user_name)279 chatbot = gr.Chatbot.update(label=MODELS[DEFAULT_MODEL])280 return user_info, user_name, current_model, toggle_like_btn_visibility(DEFAULT_MODEL), *current_model.auto_load(), get_history_names(False, user_name), chatbot281 demo.load(create_greeting, inputs=None, outputs=[user_info, user_name, current_model, like_dislike_area, systemPromptTxt, chatbot, historyFileSelectDropdown, chatbot], api_name="load")282 chatgpt_predict_args = dict(283 fn=predict,284 inputs=[285 current_model,286 user_question,287 chatbot,288 use_streaming_checkbox,289 use_websearch_checkbox,290 index_files,291 language_select_dropdown,292 ],293 outputs=[chatbot, status_display],294 show_progress=True,295 )296 297 start_outputing_args = dict(298 fn=start_outputing,299 inputs=[],300 outputs=[submitBtn, cancelBtn],301 show_progress=True,302 )303 304 end_outputing_args = dict(305 fn=end_outputing, inputs=[], outputs=[submitBtn, cancelBtn]306 )307 308 reset_textbox_args = dict(309 fn=reset_textbox, inputs=[], outputs=[user_input]310 )311 312 transfer_input_args = dict(313 fn=transfer_input, inputs=[user_input], outputs=[user_question, user_input, submitBtn, cancelBtn], show_progress=True314 )315 316 get_usage_args = dict(317 fn=billing_info, inputs=[current_model], outputs=[usageTxt], show_progress=False318 )319 320 load_history_from_file_args = dict(321 fn=load_chat_history,322 inputs=[current_model, historyFileSelectDropdown, user_name],323 outputs=[saveFileName, systemPromptTxt, chatbot]324 )325 326 327 # Chatbot328 cancelBtn.click(interrupt, [current_model], [])329 330 user_input.submit(**transfer_input_args).then(**chatgpt_predict_args).then(**end_outputing_args)331 user_input.submit(**get_usage_args)332 333 submitBtn.click(**transfer_input_args).then(**chatgpt_predict_args, api_name="predict").then(**end_outputing_args)334 submitBtn.click(**get_usage_args)335 336 index_files.change(handle_file_upload, [current_model, index_files, chatbot], [index_files, chatbot, status_display])337 338 emptyBtn.click(339 reset,340 inputs=[current_model],341 outputs=[chatbot, status_display],342 show_progress=True,343 )344 345 retryBtn.click(**start_outputing_args).then(346 retry,347 [348 current_model,349 chatbot,350 use_streaming_checkbox,351 use_websearch_checkbox,352 index_files,353 language_select_dropdown,354 ],355 [chatbot, status_display],356 show_progress=True,357 ).then(**end_outputing_args)358 retryBtn.click(**get_usage_args)359 360 delFirstBtn.click(361 delete_first_conversation,362 [current_model],363 [status_display],364 )365 366 delLastBtn.click(367 delete_last_conversation,368 [current_model, chatbot],369 [chatbot, status_display],370 show_progress=False371 )372 373 likeBtn.click(374 like,375 [current_model],376 [status_display],377 show_progress=False378 )379 380 dislikeBtn.click(381 dislike,382 [current_model],383 [status_display],384 show_progress=False385 )386 387 two_column.change(update_doc_config, [two_column], None)388 389 # LLM Models390 keyTxt.change(set_key, [current_model, keyTxt], [user_api_key, status_display], api_name="set_key").then(**get_usage_args)391 keyTxt.submit(**get_usage_args)392 single_turn_checkbox.change(set_single_turn, [current_model, single_turn_checkbox], None)393 model_select_dropdown.change(get_model, [model_select_dropdown, lora_select_dropdown, user_api_key, temperature_slider, top_p_slider, systemPromptTxt, user_name], [current_model, status_display, chatbot, lora_select_dropdown], show_progress=True, api_name="get_model")394 model_select_dropdown.change(toggle_like_btn_visibility, [model_select_dropdown], [like_dislike_area], show_progress=False)395 lora_select_dropdown.change(get_model, [model_select_dropdown, lora_select_dropdown, user_api_key, temperature_slider, top_p_slider, systemPromptTxt, user_name], [current_model, status_display, chatbot], show_progress=True)396 397 # Template398 systemPromptTxt.change(set_system_prompt, [current_model, systemPromptTxt], None)399 templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])400 templateFileSelectDropdown.change(401 load_template,402 [templateFileSelectDropdown],403 [promptTemplates, templateSelectDropdown],404 show_progress=True,405 )406 templateSelectDropdown.change(407 get_template_content,408 [promptTemplates, templateSelectDropdown, systemPromptTxt],409 [systemPromptTxt],410 show_progress=True,411 )412 413 # S&L414 saveHistoryBtn.click(415 save_chat_history,416 [current_model, saveFileName, chatbot, user_name],417 downloadFile,418 show_progress=True,419 )420 saveHistoryBtn.click(get_history_names, [gr.State(False), user_name], [historyFileSelectDropdown])421 exportMarkdownBtn.click(422 export_markdown,423 [current_model, saveFileName, chatbot, user_name],424 downloadFile,425 show_progress=True,426 )427 historyRefreshBtn.click(get_history_names, [gr.State(False), user_name], [historyFileSelectDropdown])428 historyFileSelectDropdown.change(**load_history_from_file_args)429 downloadFile.change(upload_chat_history, [current_model, downloadFile, user_name], [saveFileName, systemPromptTxt, chatbot])430 431 # Advanced432 max_context_length_slider.change(set_token_upper_limit, [current_model, max_context_length_slider], None)433 temperature_slider.change(set_temperature, [current_model, temperature_slider], None)434 top_p_slider.change(set_top_p, [current_model, top_p_slider], None)435 n_choices_slider.change(set_n_choices, [current_model, n_choices_slider], None)436 stop_sequence_txt.change(set_stop_sequence, [current_model, stop_sequence_txt], None)437 max_generation_slider.change(set_max_tokens, [current_model, max_generation_slider], None)438 presence_penalty_slider.change(set_presence_penalty, [current_model, presence_penalty_slider], None)439 frequency_penalty_slider.change(set_frequency_penalty, [current_model, frequency_penalty_slider], None)440 logit_bias_txt.change(set_logit_bias, [current_model, logit_bias_txt], None)441 user_identifier_txt.change(set_user_identifier, [current_model, user_identifier_txt], None)442 443 default_btn.click(444 reset_default, [], [apihostTxt, proxyTxt, status_display], show_progress=True445 )446 changeAPIURLBtn.click(447 change_api_host,448 [apihostTxt],449 [status_display],450 show_progress=True,451 )452 changeProxyBtn.click(453 change_proxy,454 [proxyTxt],455 [status_display],456 show_progress=True,457 )458 459logging.info(460 colorama.Back.GREEN461 + "\n川虎的温馨提示:访问 http://localhost:7860 查看界面"462 + colorama.Style.RESET_ALL463)464# 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接465demo.title = i18n("川虎Chat 🚀")466 467if __name__ == "__main__":468 reload_javascript()469 demo.queue(concurrency_count=CONCURRENT_COUNT).launch(470 favicon_path="./assets/favicon.ico",471 )472 # demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=7860, share=False) # 可自定义端口473 # demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=7860,auth=("在这里填写用户名", "在这里填写密码")) # 可设置用户名与密码474 # demo.queue(concurrency_count=CONCURRENT_COUNT).launch(auth=("在这里填写用户名", "在这里填写密码")) # 适合Nginx反向代理475 