Rabbit482023/ChatImprovement
0
1import os; os.environ['no_proxy'] = '*' 2import gradio as gr 3from predict import predict4from toolbox import format_io, find_free_port5 6try: from config_private import proxies, WEB_PORT # 放自己的秘密如API和代理网址 os.path.exists('config_private.py')7except: from config import proxies, WEB_PORT8 9PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT10 11initial_prompt = "Serve me as a writing and programming assistant."12title_html = """<h1 align="center">ChatGPT 学术优化</h1>"""13 14description = """<div align=center>15 16本项目参考自[chatgpt_academic](https://github.com/binary-husky/chatgpt_academic)17 18</div>19"""20 21import logging22os.makedirs('gpt_log', exist_ok=True)23logging.basicConfig(filename='gpt_log/chat_secrets.log', level=logging.INFO)24#print('所有问询记录将自动保存在本地目录./gpt_log/chat_secrets.log,请注意自我隐私保护哦!')25 26# 一些普通功能27from functional import get_functionals28functional = get_functionals()29 30# 对一些丧心病狂的实验性功能进行测试31from functional_crazy import get_crazy_functionals32crazy_functional = get_crazy_functionals()33 34gr.Chatbot.postprocess = format_io35 36with gr.Blocks() as demo:37 gr.HTML(title_html)38 with gr.Row():39 with gr.Column(scale=2):40 chatbot = gr.Chatbot()41 chatbot.style(height=1000)42 chatbot.style()43 history = gr.State([])44 TRUE = gr.State(True)45 FALSE = gr.State(False)46 with gr.Column(scale=1):47 with gr.Row():48 with gr.Column(scale=12):49 api = gr.Textbox(show_label=False, placeholder="Input OpenAI Key.").style(container=False)50 with gr.Column(scale=12):51 txt = gr.Textbox(show_label=False, placeholder="Input question here.").style(container=False)52 with gr.Column(scale=1):53 submitBtn = gr.Button("Ask", variant="primary")54 with gr.Row():55 for k in functional:56 variant = functional[k]["Color"] if "Color" in functional[k] else "secondary"57 functional[k]["Button"] = gr.Button(k, variant=variant)58 #for k in crazy_functional:59 # variant = crazy_functional[k]["Color"] if "Color" in crazy_functional[k] else "secondary"60 # crazy_functional[k]["Button"] = gr.Button(k, variant=variant)61 from check_proxy import check_proxy62 statusDisplay = gr.Markdown(f"{check_proxy(proxies)}")63 systemPromptTxt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt).style(container=True)64 #inputs, top_p, temperature, top_k, repetition_penalty65 with gr.Accordion("arguments", open=False):66 top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)67 temperature = gr.Slider(minimum=-0, maximum=5.0, value=1.0, step=0.01, interactive=True, label="Temperature",)68 69 gr.Markdown(description)70 71 txt.submit(predict, [api, txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay])72 submitBtn.click(predict, [api, txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay], show_progress=True)73 for k in functional:74 functional[k]["Button"].click(predict, 75 [api, txt, top_p, temperature, chatbot, history, systemPromptTxt, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True)76 #for k in crazy_functional:77 # crazy_functional[k]["Button"].click(crazy_functional[k]["Function"], 78 # [txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay])79 80 81def auto_opentab_delay():82 import threading, webbrowser, time83 print(f"URL http://localhost:{PORT}")84 def open(): time.sleep(2)85 webbrowser.open_new_tab(f'http://localhost:{PORT}')86 t = threading.Thread(target=open)87 t.daemon = True; t.start()88 89auto_opentab_delay()90demo.title = "ChatGPT 学术优化"91demo.queue().launch(share=False)92 