Danielzero/GPT3.5
1
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 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 user_api_key = gr.State(my_api_key)31 current_model = gr.State(create_new_model)32 33 topic = gr.State(i18n("未命名对话历史记录"))34 35 with gr.Row():36 gr.HTML(CHUANHU_TITLE, elem_id="app_title")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/329642 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.username46 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(min_width=225, scale=12):56 user_input = gr.Textbox(57 elem_id="user_input_tb",58 show_label=False, placeholder=i18n("在这里输入")59 ).style(container=False)60 with gr.Column(min_width=42, scale=1):61 submitBtn = gr.Button(value="", variant="primary", elem_id="submit_btn")62 cancelBtn = gr.Button(value="", variant="secondary", visible=False, elem_id="cancel_btn")63 with gr.Row():64 emptyBtn = gr.Button(65 i18n("🧹 新的对话"),66 )67 retryBtn = gr.Button(i18n("🔄 重新生成"))68 delFirstBtn = gr.Button(i18n("🗑️ 删除最旧对话"))69 delLastBtn = gr.Button(i18n("🗑️ 删除最新对话"))70 with gr.Row(visible=False) as like_dislike_area: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=i18n("模型")):79 keyTxt = gr.Textbox(80 show_label=True,81 placeholder=f"Your API-key...",82 value=hide_middle_chars(user_api_key.value),83 type="password",84 visible=not HIDE_MY_KEY,85 label="API-Key",86 )87 if multi_api_key: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=i18n("选择模型"), choices=MODELS, multiselect=False, value=MODELS[DEFAULT_MODEL], interactive=True93 )94 lora_select_dropdown = gr.Dropdown(95 label=i18n("选择LoRA模型"), choices=[], multiselect=False, interactive=True, visible=False96 )97 with gr.Row():98 use_streaming_checkbox = gr.Checkbox(99 label=i18n("实时传输回答"), value=True, visible=ENABLE_STREAMING_OPTION100 )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=i18n("选择回复语言(针对搜索&索引功能)"),105 choices=REPLY_LANGUAGES,106 multiselect=False,107 value=REPLY_LANGUAGES[0],108 )109 index_files = gr.Files(label=i18n("上传"), type="file")110 two_column = gr.Checkbox(label=i18n("双栏pdf"), value=advance_docs["pdf"].get("two_column", False))111 # TODO: 公式ocr112 # 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=i18n("在这里输入System Prompt..."),118 label="System prompt",119 value=INITIAL_SYSTEM_PROMPT,120 lines=10,121 ).style(container=False)122 with gr.Accordion(label=i18n("加载Prompt模板"), open=True):123 with gr.Column():124 with gr.Row():125 with gr.Column(scale=6):126 templateFileSelectDropdown = gr.Dropdown(127 label=i18n("选择Prompt模板集合文件"),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(i18n("🔄 刷新"))134 with gr.Row():135 with gr.Column():136 templateSelectDropdown = gr.Dropdown(137 label=i18n("从Prompt模板中加载"),138 choices=load_template(139 get_template_names(plain=True)[0], mode=1140 ),141 multiselect=False,142 ).style(container=False)143 144 with gr.Tab(label=i18n("保存/加载")):145 with gr.Accordion(label=i18n("保存/加载对话历史记录"), open=True):146 with gr.Column():147 with gr.Row():148 with gr.Column(scale=6):149 historyFileSelectDropdown = gr.Dropdown(150 label=i18n("从列表中加载对话"),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(i18n("🔄 刷新"))157 with gr.Row():158 with gr.Column(scale=6):159 saveFileName = gr.Textbox(160 show_label=True,161 placeholder=i18n("设置文件名: 默认为.json,可选为.md"),162 label=i18n("设置保存文件名"),163 value=i18n("对话历史记录"),164 ).style(container=True)165 with gr.Column(scale=1):166 saveHistoryBtn = gr.Button(i18n("💾 保存对话"))167 exportMarkdownBtn = gr.Button(i18n("📝 导出为Markdown"))168 gr.Markdown(i18n("默认保存于history文件夹"))169 with gr.Row():170 with gr.Column():171 downloadFile = gr.File(interactive=True)172 173 with gr.Tab(label=i18n("高级")):174 gr.Markdown(i18n("# ⚠️ 务必谨慎更改 ⚠️\n\n如果无法使用请恢复默认设置"))175 gr.HTML(APPEARANCE_SWITCHER, elem_classes="insert_block")176 with gr.Accordion(i18n("参数"), open=False):177 temperature_slider = gr.Slider(178 minimum=-0,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="top-p",192 )193 n_choices_slider = gr.Slider(194 minimum=1,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=0.0,236 step=0.01,237 interactive=True,238 label="frequency penalty",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(i18n("网络设置"), open=False):256 # 优先展示自定义的api_host257 apihostTxt = gr.Textbox(258 show_label=True,259 placeholder=i18n("在这里输入API-Host..."),260 label="API-Host",261 value=config.api_host or shared.API_HOST,262 lines=1,263 )264 changeAPIURLBtn = gr.Button(i18n("🔄 切换API地址"))265 proxyTxt = gr.Textbox(266 show_label=True,267 placeholder=i18n("在这里输入代理地址..."),268 label=i18n("代理地址(示例:http://127.0.0.1:10809)"),269 value="",270 lines=2,271 )272 changeProxyBtn = gr.Button(i18n("🔄 设置代理地址"))273 default_btn = gr.Button(i18n("🔙 恢复默认设置"))274 275 gr.Markdown(CHUANHU_DESCRIPTION, elem_id="description")276 gr.HTML(FOOTER.format(versions=versions_html()), elem_id="footer")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 current_model,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 293 start_outputing_args = dict(294 fn=start_outputing,295 inputs=[],296 outputs=[submitBtn, cancelBtn],297 show_progress=True,298 )299 300 end_outputing_args = dict(301 fn=end_outputing, inputs=[], outputs=[submitBtn, cancelBtn]302 )303 304 reset_textbox_args = dict(305 fn=reset_textbox, inputs=[], outputs=[user_input]306 )307 308 transfer_input_args = dict(309 fn=transfer_input, inputs=[user_input], outputs=[user_question, user_input, submitBtn, cancelBtn], show_progress=True310 )311 312 get_usage_args = dict(313 fn=billing_info, inputs=[current_model], outputs=[usageTxt], show_progress=False314 )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 # Chatbot324 cancelBtn.click(interrupt, [current_model], [])325 326 user_input.submit(**transfer_input_args).then(**chatgpt_predict_args).then(**end_outputing_args)327 user_input.submit(**get_usage_args)328 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 reset,336 inputs=[current_model],337 outputs=[chatbot, status_display],338 show_progress=True,339 )340 341 retryBtn.click(**start_outputing_args).then(342 retry,343 [344 current_model,345 chatbot,346 use_streaming_checkbox,347 use_websearch_checkbox,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 [current_model],359 [status_display],360 )361 362 delLastBtn.click(363 delete_last_conversation,364 [current_model, chatbot],365 [chatbot, status_display],366 show_progress=False367 )368 369 likeBtn.click(370 like,371 [current_model],372 [status_display],373 show_progress=False374 )375 376 dislikeBtn.click(377 dislike,378 [current_model],379 [status_display],380 show_progress=False381 )382 383 two_column.change(update_doc_config, [two_column], None)384 385 # LLM Models386 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 # Template394 systemPromptTxt.change(set_system_prompt, [current_model, systemPromptTxt], None)395 templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])396 templateFileSelectDropdown.change(397 load_template,398 [templateFileSelectDropdown],399 [promptTemplates, templateSelectDropdown],400 show_progress=True,401 )402 templateSelectDropdown.change(403 get_template_content,404 [promptTemplates, templateSelectDropdown, systemPromptTxt],405 [systemPromptTxt],406 show_progress=True,407 )408 409 # S&L410 saveHistoryBtn.click(411 save_chat_history,412 [current_model, saveFileName, chatbot, user_name],413 downloadFile,414 show_progress=True,415 )416 saveHistoryBtn.click(get_history_names, [gr.State(False), user_name], [historyFileSelectDropdown])417 exportMarkdownBtn.click(418 export_markdown,419 [current_model, saveFileName, chatbot, user_name],420 downloadFile,421 show_progress=True,422 )423 historyRefreshBtn.click(get_history_names, [gr.State(False), user_name], [historyFileSelectDropdown])424 historyFileSelectDropdown.change(**load_history_from_file_args)425 downloadFile.change(**load_history_from_file_args)426 427 # Advanced428 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, [], [apihostTxt, proxyTxt, status_display], show_progress=True441 )442 changeAPIURLBtn.click(443 change_api_host,444 [apihostTxt],445 [status_display],446 show_progress=True,447 )448 changeProxyBtn.click(449 change_proxy,450 [proxyTxt],451 [status_display],452 show_progress=True,453 )454 455logging.info(456 colorama.Back.GREEN457 + "\n川虎的温馨提示:访问 http://localhost:7860 查看界面"458 + colorama.Style.RESET_ALL459)460# 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接461demo.title = i18n("川虎Chat 🚀")462 463if __name__ == "__main__":464 reload_javascript()465 demo.queue(concurrency_count=CONCURRENT_COUNT).launch(466 favicon_path="./assets/favicon.ico",467 )468 # demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=7860, share=False) # 可自定义端口469 # demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", server_port=7860,auth=("在这里填写用户名", "在这里填写密码")) # 可设置用户名与密码470 # demo.queue(concurrency_count=CONCURRENT_COUNT).launch(auth=("在这里填写用户名", "在这里填写密码")) # 适合Nginx反向代理471 