From 954bc36d76c028a325def14492b7c98e368f51ab Mon Sep 17 00:00:00 2001 From: Chuan Hu <51039745+GaiZhenbiao@users.noreply.github.com> Date: Tue, 28 Mar 2023 22:47:30 +0800 Subject: [PATCH 01/30] Improve the way to open webbrowser --- main.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/main.py b/main.py index c9af1a1..eecd126 100644 --- a/main.py +++ b/main.py @@ -89,15 +89,5 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: except: pass -# 延迟函数, 做一些准备工作, 最后尝试打开浏览器 -def auto_opentab_delay(): - import threading, webbrowser, time - print(f"URL http://localhost:{PORT}") - def open(): time.sleep(2) - webbrowser.open_new_tab(f'http://localhost:{PORT}') - t = threading.Thread(target=open) - t.daemon = True; t.start() - -auto_opentab_delay() demo.title = "ChatGPT 学术优化" -demo.queue().launch(server_name="0.0.0.0", share=True, server_port=PORT) +demo.queue().launch(server_name="0.0.0.0", share=True, server_port=PORT, inbrowser=True) From 51bde973a1e96f3ca72a6cd685a14788ffc6eede Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 28 Mar 2023 23:09:25 +0800 Subject: [PATCH 02/30] simplify codes --- functional_crazy.py | 26 --------- main.py | 25 ++++---- project_self_analysis.md | 122 +++++++++++++++++++++++++++++++++++++++ toolbox.py | 32 +++++++++- 4 files changed, 164 insertions(+), 41 deletions(-) create mode 100644 project_self_analysis.md diff --git a/functional_crazy.py b/functional_crazy.py index d537573..15fcf79 100644 --- a/functional_crazy.py +++ b/functional_crazy.py @@ -38,31 +38,5 @@ def get_crazy_functionals(): }, } -def on_file_uploaded(files, chatbot, txt): - if len(files) == 0: return chatbot, txt - import shutil, os, time, glob - from toolbox import extract_archive - try: shutil.rmtree('./private_upload/') - except: pass - time_tag = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) - os.makedirs(f'private_upload/{time_tag}', exist_ok=True) - for file in files: - file_origin_name = os.path.basename(file.orig_name) - shutil.copy(file.name, f'private_upload/{time_tag}/{file_origin_name}') - extract_archive(f'private_upload/{time_tag}/{file_origin_name}', - dest_dir=f'private_upload/{time_tag}/{file_origin_name}.extract') - moved_files = [fp for fp in glob.glob('private_upload/**/*', recursive=True)] - txt = f'private_upload/{time_tag}' - moved_files_str = '\t\n\n'.join(moved_files) - chatbot.append(['我上传了文件,请查收', - f'[Local Message] 收到以下文件: \n\n{moved_files_str}\n\n调用路径参数已自动修正到: \n\n{txt}\n\n现在您可以直接选择任意实现性功能']) - return chatbot, txt -def on_report_generated(files, chatbot): - from toolbox import find_recent_files - report_files = find_recent_files('gpt_log') - if len(report_files) == 0: return report_files, chatbot - # files.extend(report_files) - chatbot.append(['汇总报告如何远程获取?', '汇总报告已经添加到右侧文件上传区,请查收。']) - return report_files, chatbot diff --git a/main.py b/main.py index c9af1a1..c0ec736 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染 import gradio as gr from predict import predict -from toolbox import format_io, find_free_port +from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到 try: from config_private import proxies, WEB_PORT, LLM_MODEL @@ -25,7 +25,7 @@ from functional import get_functionals functional = get_functionals() # 对一些丧心病狂的实验性功能模块进行测试 -from functional_crazy import get_crazy_functionals, on_file_uploaded, on_report_generated +from functional_crazy import get_crazy_functionals crazy_functional = get_crazy_functionals() # 处理markdown文本格式的转变 @@ -43,8 +43,6 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: chatbot.style(height=1000) chatbot.style() history = gr.State([]) - TRUE = gr.State(True) - FALSE = gr.State(False) with gr.Column(scale=1): with gr.Row(): with gr.Column(scale=12): @@ -68,36 +66,35 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: gr.Markdown("上传本地文件供上面的实验性功能调用.") with gr.Row(): file_upload = gr.Files(label='任何文件,但推荐上传压缩文件(zip, tar)', file_count="multiple") - - systemPromptTxt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt).style(container=True) - #inputs, top_p, temperature, top_k, repetition_penalty + system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt).style(container=True) with gr.Accordion("arguments", open=False): top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",) temperature = gr.Slider(minimum=-0, maximum=5.0, value=1.0, step=0.01, interactive=True, label="Temperature",) - txt.submit(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay]) - submitBtn.click(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay], show_progress=True) + txt.submit(predict, [txt, top_p, temperature, chatbot, history, system_prompt], [chatbot, history, statusDisplay]) + submitBtn.click(predict, [txt, top_p, temperature, chatbot, history, system_prompt], [chatbot, history, statusDisplay], show_progress=True) for k in functional: functional[k]["Button"].click(predict, - [txt, top_p, temperature, chatbot, history, systemPromptTxt, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True) + [txt, top_p, temperature, chatbot, history, system_prompt, gr.State(True), gr.State(k)], [chatbot, history, statusDisplay], show_progress=True) file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt]) for k in crazy_functional: click_handle = crazy_functional[k]["Button"].click(crazy_functional[k]["Function"], - [txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay] + [txt, top_p, temperature, chatbot, history, system_prompt, gr.State(PORT)], [chatbot, history, statusDisplay] ) try: click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot]) except: pass - # 延迟函数, 做一些准备工作, 最后尝试打开浏览器 def auto_opentab_delay(): import threading, webbrowser, time print(f"URL http://localhost:{PORT}") - def open(): time.sleep(2) + def open_webbrowser(): time.sleep(2) webbrowser.open_new_tab(f'http://localhost:{PORT}') - t = threading.Thread(target=open) + t = threading.Thread(target=open_webbrowser) t.daemon = True; t.start() auto_opentab_delay() demo.title = "ChatGPT 学术优化" demo.queue().launch(server_name="0.0.0.0", share=True, server_port=PORT) + +# -> 代码控制在100行以内 \ No newline at end of file diff --git a/project_self_analysis.md b/project_self_analysis.md new file mode 100644 index 0000000..c817421 --- /dev/null +++ b/project_self_analysis.md @@ -0,0 +1,122 @@ +# chatgpt-academic项目分析报告 +(Author补充:以下分析均由本项目调用ChatGPT一键生成,如果有不准确的地方全怪GPT) + +## [0/10] 程序摘要: check_proxy.py + +这个程序是一个用来检查代理服务器是否有效的 Python 程序代码。程序文件名为 check_proxy.py。其中定义了一个函数 check_proxy,该函数接收一个代理配置信息 proxies,使用 requests 库向一个代理服务器发送请求,获取该代理的所在地信息并返回。如果请求超时或者异常,该函数将返回一个代理无效的结果。 + +程序代码分为两个部分,首先是 check_proxy 函数的定义部分,其次是程序文件的入口部分,在该部分代码中,程序从 config_private.py 文件或者 config.py 文件中加载代理配置信息,然后调用 check_proxy 函数来检测代理服务器是否有效。如果配置文件 config_private.py 存在,则会加载其中的代理配置信息,否则会从 config.py 文件中读取。 + +## [1/10] 程序摘要: config.py + +本程序文件名为config.py,主要功能是存储应用所需的常量和配置信息。 + +其中,包含了应用所需的OpenAI API密钥、API接口地址、网络代理设置、超时设置、网络端口和OpenAI模型选择等信息,在运行应用前需要进行相应的配置。在未配置网络代理时,程序给出了相应的警告提示。 + +此外,还包含了一个检查函数,用于检查是否忘记修改API密钥。 + +总之,config.py文件是应用中的一个重要配置文件,用来存储应用所需的常量和配置信息,需要在应用运行前进行相应的配置。 + +## [2/10] 程序摘要: config_private.py + +该文件是一个配置文件,命名为config_private.py。它是一个Python脚本,用于配置OpenAI的API密钥、模型和其它相关设置。该配置文件还可以设置是否使用代理。如果使用代理,需要设置代理协议、地址和端口。在设置代理之后,该文件还包括一些用于测试代理是否正常工作的代码。该文件还包括超时时间、随机端口、重试次数等设置。在文件末尾,还有一个检查代码,如果没有更改API密钥,则抛出异常。 + +## [3/10] 程序摘要: functional.py + +该程序文件名为 functional.py,其中包含一个名为 get_functionals 的函数,该函数返回一个字典,该字典包含了各种翻译、校对等功能的名称、前缀、后缀以及默认按钮颜色等信息。具体功能包括:英语学术润色、中文学术润色、查找语法错误、中英互译、中译英、学术中译英、英译中、解释代码等。该程序的作用为提供各种翻译、校对等功能的模板,以便后续程序可以直接调用。 + +(Author补充:这个文件汇总了模块化的Prompt调用,如果发现了新的好用Prompt,别藏着哦^_^速速PR) + + +## [4/10] 程序摘要: functional_crazy.py + +这个程序文件 functional_crazy.py 导入了一些 python 模块,并提供了一个函数 get_crazy_functionals(),该函数返回不同实验功能的描述和函数。其中,使用的的模块包括: + +- crazy_functions.读文章写摘要 中的 读文章写摘要 +- crazy_functions.生成函数注释 中的 批量生成函数注释 +- crazy_functions.解析项目源代码 中的 解析项目本身、解析一个Python项目、解析一个C项目的头文件、解析一个C项目 +- crazy_functions.高级功能函数模板 中的 高阶功能模板函数 + +返回的实验功能函数包括: + +- "[实验] 请解析并解构此项目本身",包含函数:解析项目本身 +- "[实验] 解析整个py项目(配合input输入框)",包含函数:解析一个Python项目 +- "[实验] 解析整个C++项目头文件(配合input输入框)",包含函数:解析一个C项目的头文件 +- "[实验] 解析整个C++项目(配合input输入框)",包含函数:解析一个C项目 +- "[实验] 读tex论文写摘要(配合input输入框)",包含函数:读文章写摘要 +- "[实验] 批量生成函数注释(配合input输入框)",包含函数:批量生成函数注释 +- "[实验] 实验功能函数模板",包含函数:高阶功能模板函数 + +这些函数用于系统开发和测试,方便开发者进行特定程序语言后台功能开发的测试和实验,增加系统可靠稳定性和用户友好性。 + +(Author补充:这个文件汇总了模块化的函数,如此设计以方便任何新功能的加入) + +## [5/10] 程序摘要: main.py + +该程序是一个基于Gradio框架的聊天机器人应用程序。用户可以通过输入问题来获取答案,并与聊天机器人进行对话。该应用程序还集成了一些实验性功能模块,用户可以通过上传本地文件或点击相关按钮来使用这些模块。程序还可以生成对话日志,并且具有一些外观上的调整。在运行时,它会自动打开一个网页并在本地启动服务器。 + + +## [6/10] 程序摘要: predict.py + +该程序文件名为predict.py,主要是针对一个基于ChatGPT的聊天机器人进行交互和预测。 + +第一部分是导入所需的库和配置文件。 + +第二部分是一个用于获取Openai返回的完整错误信息的函数。 + +第三部分是用于一次性完成向ChatGPT发送请求和等待回复的函数。 + +第四部分是用于基础的对话功能的函数,通过stream参数可以选择是否显示中间的过程。 + +第五部分是用于整合所需信息和选择LLM模型生成的HTTP请求。 + +(Author补充:主要是predict_no_ui和predict两个函数。前者不用stream,方便、高效、易用。后者用stream,展现效果好。) + +## [7/10] 程序摘要: show_math.py + +这是一个名为show_math.py的Python程序文件,主要用于将Markdown-LaTeX混合文本转换为HTML格式,并包括MathML数学公式。程序使用latex2mathml.converter库将LaTeX公式转换为MathML格式,并使用正则表达式递归地翻译输入的Markdown-LaTeX混合文本。程序包括转换成双美元符号($$)形式、转换成单美元符号($)形式、转换成\[\]形式以及转换成\(\)形式的LaTeX数学公式。如果转换中出现错误,程序将返回相应的错误消息。 + +## [8/10] 程序摘要: theme.py + +这是一个名为theme.py的程序文件,用于设置Gradio界面的颜色和字体主题。该文件中定义了一个名为adjust_theme()的函数,其作用是返回一个Gradio theme对象,设置了Gradio界面的颜色和字体主题。在该函数里面,使用了Graido可用的颜色列表,主要参数包括primary_hue、neutral_hue、font和font_mono等,用于设置Gradio界面的主题色调、字体等。另外,该函数还实现了一些参数的自定义,如input_background_fill_dark、button_transition、button_shadow_hover等,用于设置Gradio界面的渐变、阴影等特效。如果Gradio版本过于陈旧,该函数会抛出异常并返回None。 + +## [9/10] 程序摘要: toolbox.py + +该文件为Python程序文件,文件名为toolbox.py。主要功能包括: + +1. 导入markdown、mdtex2html、threading、functools等模块。 +2. 定义函数predict_no_ui_but_counting_down,用于生成对话。 +3. 定义函数write_results_to_file,用于将对话记录生成Markdown文件。 +4. 定义函数regular_txt_to_markdown,将普通文本转换为Markdown格式的文本。 +5. 定义装饰器函数CatchException,用于捕获函数执行异常并返回生成器。 +6. 定义函数report_execption,用于向chatbot中添加错误信息。 +7. 定义函数text_divide_paragraph,用于将文本按照段落分隔符分割开,生成带有段落标签的HTML代码。 +8. 定义函数markdown_convertion,用于将Markdown格式的文本转换为HTML格式。 +9. 定义函数format_io,用于将输入和输出解析为HTML格式。 +10. 定义函数find_free_port,用于返回当前系统中可用的未使用端口。 +11. 定义函数extract_archive,用于解压归档文件。 +12. 定义函数find_recent_files,用于查找最近创建的文件。 +13. 定义函数on_file_uploaded,用于处理上传文件的操作。 +14. 定义函数on_report_generated,用于处理生成报告文件的操作。 + +## 程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能。 + +这是一个基于Gradio框架的聊天机器人应用,支持通过文本聊天来获取答案,并可以使用一系列实验性功能模块,例如生成函数注释、解析项目源代码、读取Latex论文写摘要等。 程序架构分为前端和后端两个部分。前端使用Gradio实现,包括用户输入区域、应答区域、按钮、调用方式等。后端使用Python实现,包括聊天机器人模型、实验性功能模块、模板模块、管理模块、主程序模块等。 + +每个程序文件的功能如下: + +| 文件名 | 功能描述 | +|:----:|:----:| +| check_proxy.py | 检查代理服务器是否有效 | +| config.py | 存储应用所需的常量和配置信息 | +| config_private.py | 存储Openai的API密钥、模型和其他相关设置 | +| functional.py | 提供各种翻译、校对等实用模板 | +| functional_crazy.py | 提供一些实验性质的高级功能 | +| main.py | 基于Gradio框架的聊天机器人应用程序的主程序 | +| predict.py | 用于chatbot预测方案创建,向ChatGPT发送请求和获取回复 | +| show_math.py | 将Markdown-LaTeX混合文本转换为HTML格式,并包括MathML数学公式 | +| theme.py | 设置Gradio界面的颜色和字体主题 | +| toolbox.py | 定义一系列工具函数,用于对输入输出进行格式转换、文件操作、异常捕捉和处理等 | + +这些程序文件共同组成了一个聊天机器人应用程序的前端和后端实现,使用户可以方便地进行聊天,并可以使用相应的实验功能模块。 + diff --git a/toolbox.py b/toolbox.py index 284067c..12e6524 100644 --- a/toolbox.py +++ b/toolbox.py @@ -184,4 +184,34 @@ def find_recent_files(directory): if created_time >= one_minute_ago: recent_files.append(file_path) - return recent_files \ No newline at end of file + return recent_files + + +def on_file_uploaded(files, chatbot, txt): + if len(files) == 0: return chatbot, txt + import shutil, os, time, glob + from toolbox import extract_archive + try: shutil.rmtree('./private_upload/') + except: pass + time_tag = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + os.makedirs(f'private_upload/{time_tag}', exist_ok=True) + for file in files: + file_origin_name = os.path.basename(file.orig_name) + shutil.copy(file.name, f'private_upload/{time_tag}/{file_origin_name}') + extract_archive(f'private_upload/{time_tag}/{file_origin_name}', + dest_dir=f'private_upload/{time_tag}/{file_origin_name}.extract') + moved_files = [fp for fp in glob.glob('private_upload/**/*', recursive=True)] + txt = f'private_upload/{time_tag}' + moved_files_str = '\t\n\n'.join(moved_files) + chatbot.append(['我上传了文件,请查收', + f'[Local Message] 收到以下文件: \n\n{moved_files_str}\n\n调用路径参数已自动修正到: \n\n{txt}\n\n现在您可以直接选择任意实现性功能']) + return chatbot, txt + + +def on_report_generated(files, chatbot): + from toolbox import find_recent_files + report_files = find_recent_files('gpt_log') + if len(report_files) == 0: return report_files, chatbot + # files.extend(report_files) + chatbot.append(['汇总报告如何远程获取?', '汇总报告已经添加到右侧文件上传区,请查收。']) + return report_files, chatbot From c7e1b86b520f37cc6255061198dfcfdf345b5f33 Mon Sep 17 00:00:00 2001 From: Tuchuanhuhuhu Date: Tue, 28 Mar 2023 23:17:12 +0800 Subject: [PATCH 03/30] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B9=B6=E8=A1=8C?= =?UTF-8?q?=E5=A4=84=E7=90=86=E4=B8=8E=E6=9D=83=E9=99=90=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 12 +++++++++--- main.py | 15 ++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/config.py b/config.py index aa85bc0..bff413b 100644 --- a/config.py +++ b/config.py @@ -6,7 +6,7 @@ API_URL = "https://api.openai.com/v1/chat/completions" USE_PROXY = False if USE_PROXY: # 代理网络的地址,打开你的科学上网软件查看代理的协议(socks5/http)、地址(localhost)和端口(11284) - proxies = { "http": "socks5h://localhost:11284", "https": "socks5h://localhost:11284", } + proxies = { "http": "socks5h://localhost:11284", "https": "socks5h://localhost:11284", } print('网络代理状态:运行。') else: proxies = None @@ -25,5 +25,11 @@ MAX_RETRY = 2 LLM_MODEL = "gpt-3.5-turbo" # 检查一下是不是忘了改config -if API_KEY == "sk-此处填API秘钥": - assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行" \ No newline at end of file +if len(API_KEY) != 51: + assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行" + +# 设置并行使用的线程数 +CONCURRENT_COUNT = 100 + +# 设置用户名和密码 +AUTHENTICATION = [] # [("username", "password"), ("username2", "password2"), ...] \ No newline at end of file diff --git a/main.py b/main.py index eecd126..61de4b7 100644 --- a/main.py +++ b/main.py @@ -1,14 +1,15 @@ import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染 -import gradio as gr +import gradio as gr from predict import predict from toolbox import format_io, find_free_port # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到 -try: from config_private import proxies, WEB_PORT, LLM_MODEL -except: from config import proxies, WEB_PORT, LLM_MODEL +try: from config_private import proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION +except: from config import proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION # 如果WEB_PORT是-1, 则随机选取WEB端口 PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT +AUTHENTICATION = None if AUTHENTICATION == [] else AUTHENTICATION initial_prompt = "Serve me as a writing and programming assistant." title_html = """

ChatGPT 学术优化

""" @@ -16,7 +17,7 @@ title_html = """

ChatGPT 学术优化

""" # 问询记录, python 版本建议3.9+(越新越好) import logging os.makedirs('gpt_log', exist_ok=True) -try:logging.basicConfig(filename='gpt_log/chat_secrets.log', level=logging.INFO, encoding='utf-8') +try:logging.basicConfig(filename='gpt_log/chat_secrets.log', level=logging.INFO, encoding='utf-8') except:logging.basicConfig(filename='gpt_log/chat_secrets.log', level=logging.INFO) print('所有问询记录将自动保存在本地目录./gpt_log/chat_secrets.log, 请注意自我隐私保护哦!') @@ -78,11 +79,11 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: txt.submit(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay]) submitBtn.click(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay], show_progress=True) for k in functional: - functional[k]["Button"].click(predict, + functional[k]["Button"].click(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True) file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt]) for k in crazy_functional: - click_handle = crazy_functional[k]["Button"].click(crazy_functional[k]["Function"], + click_handle = crazy_functional[k]["Button"].click(crazy_functional[k]["Function"], [txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay] ) try: click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot]) @@ -90,4 +91,4 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: demo.title = "ChatGPT 学术优化" -demo.queue().launch(server_name="0.0.0.0", share=True, server_port=PORT, inbrowser=True) +demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", share=True, server_port=PORT, inbrowser=True, auth=AUTHENTICATION) From 17c6524b8d5f0b26dded5a78a23f45b38b2eb34b Mon Sep 17 00:00:00 2001 From: Tuchuanhuhuhu Date: Tue, 28 Mar 2023 23:20:54 +0800 Subject: [PATCH 04/30] =?UTF-8?q?temprature=E7=9A=84=E5=8F=96=E5=80=BC?= =?UTF-8?q?=E8=8C=83=E5=9B=B4=E4=B8=BA[0,=202]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 61de4b7..c1c18a1 100644 --- a/main.py +++ b/main.py @@ -74,7 +74,7 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: #inputs, top_p, temperature, top_k, repetition_penalty with gr.Accordion("arguments", open=False): top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",) - temperature = gr.Slider(minimum=-0, maximum=5.0, value=1.0, step=0.01, interactive=True, label="Temperature",) + temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",) txt.submit(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay]) submitBtn.click(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay], show_progress=True) From 1805f081d3a4749f10cd1a14f881862940eddf24 Mon Sep 17 00:00:00 2001 From: Tuchuanhuhuhu Date: Tue, 28 Mar 2023 23:33:19 +0800 Subject: [PATCH 05/30] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E2=80=9C=E9=87=8D?= =?UTF-8?q?=E7=BD=AE=E2=80=9D=E6=8C=89=E9=92=AE=EF=BC=8C=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E4=B9=8B=E5=90=8E=E8=87=AA=E5=8A=A8=E6=B8=85=E7=A9=BA=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E6=A1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 14 +++++++++++--- toolbox.py | 8 ++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index c1c18a1..c2b4b94 100644 --- a/main.py +++ b/main.py @@ -51,7 +51,9 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: with gr.Column(scale=12): txt = gr.Textbox(show_label=False, placeholder="Input question here.").style(container=False) with gr.Column(scale=1): - submitBtn = gr.Button("提交", variant="primary") + with gr.Row(): + resetBtn = gr.Button("重置", variant="secondary") + submitBtn = gr.Button("提交", variant="primary") with gr.Row(): from check_proxy import check_proxy statusDisplay = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行. \nNetwork: {check_proxy(proxies)}\nModel: {LLM_MODEL}") @@ -76,8 +78,14 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",) temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",) - txt.submit(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay]) - submitBtn.click(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay], show_progress=True) + predict_args = dict(fn=predict, inputs=[txt, top_p, temperature, chatbot, history, systemPromptTxt], outputs=[chatbot, history, statusDisplay], show_progress=True) + empty_txt_args = dict(fn=lambda: "", inputs=[], outputs=[txt]) + + txt.submit(**predict_args) + txt.submit(**empty_txt_args) + submitBtn.click(**predict_args) + submitBtn.click(**empty_txt_args) + resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, statusDisplay]) for k in functional: functional[k]["Button"].click(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True) diff --git a/toolbox.py b/toolbox.py index 284067c..5491b5e 100644 --- a/toolbox.py +++ b/toolbox.py @@ -14,7 +14,7 @@ def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temp # list就是最简单的mutable结构,我们第一个位置放gpt输出,第二个位置传递报错信息 mutable = [None, ''] # multi-threading worker - def mt(i_say, history): + def mt(i_say, history): while True: try: mutable[0] = predict_no_ui(inputs=i_say, top_p=top_p, temperature=temperature, history=history) @@ -124,7 +124,7 @@ def format_io(self, y): """ 将输入和输出解析为HTML格式。将y中最后一项的输入部分段落化,并将输出部分的Markdown和数学公式转换为HTML格式。 """ - if y is None: return [] + if y is None or y == []: return [] i_ask, gpt_reply = y[-1] i_ask = text_divide_paragraph(i_ask) # 输入部分太自由,预处理一波 y[-1] = ( @@ -144,7 +144,7 @@ def find_free_port(): s.bind(('', 0)) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) return s.getsockname()[1] - + def extract_archive(file_path, dest_dir): import zipfile @@ -165,7 +165,7 @@ def extract_archive(file_path, dest_dir): print("Successfully extracted tar archive to {}".format(dest_dir)) else: return - + def find_recent_files(directory): """ me: find files that is created with in one minutes under a directory with python, write a function From 61b4ea6d1bd20429c4b761132d8947ee0818a4a8 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 01:11:53 +0800 Subject: [PATCH 06/30] introduce project self-translation --- crazy_functions/全项目切换英文.py | 59 +++++++++++++++++++++++++++++++ functional_crazy.py | 5 +++ predict.py | 40 +++++++++++++++++++-- toolbox.py | 8 +++-- 4 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 crazy_functions/全项目切换英文.py diff --git a/crazy_functions/全项目切换英文.py b/crazy_functions/全项目切换英文.py new file mode 100644 index 0000000..8015f55 --- /dev/null +++ b/crazy_functions/全项目切换英文.py @@ -0,0 +1,59 @@ +import threading +from predict import predict_no_ui_long_connection +from toolbox import CatchException, write_results_to_file + + + +@CatchException +def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt, WEB_PORT): + history = [] # 清空历史,以免输入溢出 + # 集合文件 + import time, glob, os + file_manifest = [f for f in glob.glob('./**/*.py', recursive=True) if ('test_project' not in f) and ('gpt_log' not in f)] + i_say_show_user_buffer = [] + + # 随便显示点什么防止卡顿 + for index, fp in enumerate(file_manifest): + # if 'test_project' in fp: continue + with open(fp, 'r', encoding='utf-8') as f: + file_content = f.read() + i_say_show_user =f'[{index}/{len(file_manifest)}] 接下来请将以下代码中包含的所有中文转化为英文,只输出代码: {os.path.abspath(fp)}' + i_say_show_user_buffer.append(i_say_show_user) + chatbot.append((i_say_show_user, "[Local Message] 等待多线程操作,中间过程不予显示.")) + yield chatbot, history, '正常' + + # 任务函数 + mutable_return = [None for _ in file_manifest] + def thread_worker(fp,index): + with open(fp, 'r', encoding='utf-8') as f: + file_content = f.read() + i_say = f'接下来请将以下代码中包含的所有中文转化为英文,只输出代码,文件名是{fp},文件代码是 ```{file_content}```' + # ** gpt request ** + gpt_say = predict_no_ui_long_connection(inputs=i_say, top_p=top_p, temperature=temperature, history=history, sys_prompt=sys_prompt) + mutable_return[index] = gpt_say + + # 所有线程同时开始执行任务函数 + handles = [threading.Thread(target=thread_worker, args=(fp,index)) for index, fp in enumerate(file_manifest)] + for h in handles: + h.daemon = True + h.start() + + # 等待各个线程逐一完成 + for index, h in enumerate(handles): + h.join() + fp = file_manifest[index] + gpt_say = mutable_return[index] + i_say_show_user = i_say_show_user_buffer[index] + os.makedirs('gpt_log/generated_english_version', exist_ok=True) + os.makedirs('gpt_log/generated_english_version/crazy_functions', exist_ok=True) + where_to_relocate = f'gpt_log/generated_english_version/{fp}' + with open(where_to_relocate, 'w+', encoding='utf-8') as f: f.write(gpt_say.lstrip('```').rstrip('```')) + chatbot.append((i_say_show_user, f'[Local Message] 已完成{os.path.abspath(fp)}的转化,\n\n存入{os.path.abspath(where_to_relocate)}')) + history.append(i_say_show_user); history.append(gpt_say) + yield chatbot, history, '正常' + time.sleep(2) + + # 结束 + res = write_results_to_file(history) + chatbot.append(("完成了吗?", res)) + yield chatbot, history, '正常' diff --git a/functional_crazy.py b/functional_crazy.py index 15fcf79..0c3480d 100644 --- a/functional_crazy.py +++ b/functional_crazy.py @@ -7,11 +7,16 @@ def get_crazy_functionals(): from crazy_functions.解析项目源代码 import 解析一个C项目的头文件 from crazy_functions.解析项目源代码 import 解析一个C项目 from crazy_functions.高级功能函数模板 import 高阶功能模板函数 + from crazy_functions.全项目切换英文 import 全项目切换英文 return { "[实验] 请解析并解构此项目本身": { "Function": 解析项目本身 }, + "[实验] 把此项目代码切换成全英文": { + "Color": "stop", # 按钮颜色 + "Function": 全项目切换英文 + }, "[实验] 解析整个py项目(配合input输入框)": { "Color": "stop", # 按钮颜色 "Function": 解析一个Python项目 diff --git a/predict.py b/predict.py index cda511e..fb1eb26 100644 --- a/predict.py +++ b/predict.py @@ -25,7 +25,7 @@ def get_full_error(chunk, stream_response): break return chunk -def predict_no_ui(inputs, top_p, temperature, history=[]): +def predict_no_ui(inputs, top_p, temperature, history=[], sys_prompt=""): """ 发送至chatGPT,等待回复,一次性完成,不显示中间过程。 predict函数的简化版。 @@ -36,7 +36,7 @@ def predict_no_ui(inputs, top_p, temperature, history=[]): history 是之前的对话列表 (注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误,然后raise ConnectionAbortedError) """ - headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt="", stream=False) + headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt=sys_prompt, stream=False) retry = 0 while True: @@ -47,8 +47,8 @@ def predict_no_ui(inputs, top_p, temperature, history=[]): except requests.exceptions.ReadTimeout as e: retry += 1 traceback.print_exc() - if MAX_RETRY!=0: print(f'请求超时,正在重试 ({retry}/{MAX_RETRY}) ……') if retry > MAX_RETRY: raise TimeoutError + if MAX_RETRY!=0: print(f'请求超时,正在重试 ({retry}/{MAX_RETRY}) ……') try: result = json.loads(response.text)["choices"][0]["message"]["content"] @@ -58,6 +58,40 @@ def predict_no_ui(inputs, top_p, temperature, history=[]): raise ConnectionAbortedError("Json解析不合常规,可能是文本过长" + response.text) +def predict_no_ui_long_connection(inputs, top_p, temperature, history=[], sys_prompt=""): + """ + 发送至chatGPT,等待回复,一次性完成,不显示中间过程。但内部用stream的方法避免有人中途掐网线。 + """ + headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt=sys_prompt, stream=True) + + retry = 0 + while True: + try: + # make a POST request to the API endpoint, stream=False + response = requests.post(API_URL, headers=headers, proxies=proxies, + json=payload, stream=True, timeout=TIMEOUT_SECONDS); break + except requests.exceptions.ReadTimeout as e: + retry += 1 + traceback.print_exc() + if retry > MAX_RETRY: raise TimeoutError + if MAX_RETRY!=0: print(f'请求超时,正在重试 ({retry}/{MAX_RETRY}) ……') + + stream_response = response.iter_lines() + result = '' + while True: + try: chunk = next(stream_response).decode() + except StopIteration: break + if len(chunk)==0: continue + if not chunk.startswith('data:'): + raise ConnectionAbortedError("OpenAI返回了错误:" + chunk) + delta = json.loads(chunk.lstrip('data:'))['choices'][0]["delta"] + if len(delta) == 0: break + if "role" in delta: continue + if "content" in delta: result += delta["content"]; print(delta["content"], end='') + else: raise RuntimeError("意外Json结构:"+delta) + return result + + def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt='', stream = True, additional_fn=None): """ diff --git a/toolbox.py b/toolbox.py index 12e6524..9c420d1 100644 --- a/toolbox.py +++ b/toolbox.py @@ -2,7 +2,7 @@ import markdown, mdtex2html, threading from show_math import convert as convert_math from functools import wraps -def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temperature, history=[]): +def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temperature, history=[], sys_prompt=''): """ 调用简单的predict_no_ui接口,但是依然保留了些许界面心跳功能,当对话太长时,会自动采用二分法截断 """ @@ -17,7 +17,7 @@ def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temp def mt(i_say, history): while True: try: - mutable[0] = predict_no_ui(inputs=i_say, top_p=top_p, temperature=temperature, history=history) + mutable[0] = predict_no_ui(inputs=i_say, top_p=top_p, temperature=temperature, history=history, sys_prompt=sys_prompt) break except ConnectionAbortedError as e: if len(history) > 0: @@ -27,7 +27,8 @@ def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temp i_say = i_say[:len(i_say)//2] mutable[1] = 'Warning! Input file is too long, cut into half. ' except TimeoutError as e: - mutable[0] = '[Local Message] Failed with timeout' + mutable[0] = '[Local Message] Failed with timeout.' + raise TimeoutError # 创建新线程发出http请求 thread_name = threading.Thread(target=mt, args=(i_say, history)); thread_name.start() # 原来的线程则负责持续更新UI,实现一个超时倒计时,并等待新线程的任务完成 @@ -39,6 +40,7 @@ def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temp time.sleep(1) # 把gpt的输出从mutable中取出来 gpt_say = mutable[0] + if gpt_say=='[Local Message] Failed with timeout.': raise TimeoutError return gpt_say def write_results_to_file(history, file_name=None): From d0e3ca76718fdc44b0871bf28fbd7d09e7005041 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 01:32:28 +0800 Subject: [PATCH 07/30] =?UTF-8?q?=E6=9B=B4=E5=A5=BD=E7=9A=84=E5=A4=9A?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E4=BA=A4=E4=BA=92=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...目切换英文.py => 代码重写为全英文_多线程.py} | 24 +++++++++++++++---- functional_crazy.py | 4 ++-- 2 files changed, 21 insertions(+), 7 deletions(-) rename crazy_functions/{全项目切换英文.py => 代码重写为全英文_多线程.py} (77%) diff --git a/crazy_functions/全项目切换英文.py b/crazy_functions/代码重写为全英文_多线程.py similarity index 77% rename from crazy_functions/全项目切换英文.py rename to crazy_functions/代码重写为全英文_多线程.py index 8015f55..13500cf 100644 --- a/crazy_functions/全项目切换英文.py +++ b/crazy_functions/代码重写为全英文_多线程.py @@ -37,10 +37,24 @@ def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt, for h in handles: h.daemon = True h.start() + chatbot.append(('开始了吗?', f'多线程操作已经开始')) + yield chatbot, history, '正常' - # 等待各个线程逐一完成 + # 循环轮询各个线程是否执行完毕 + cnt = 0 + while True: + time.sleep(1) + th_alive = [h.is_alive() for h in handles] + if not any(th_alive): break + stat = ['执行中' if alive else '已完成' for alive in th_alive] + stat_str = '|'.join(stat) + cnt += 1 + chatbot[-1] = (chatbot[-1][0], f'多线程操作已经开始,完成情况: {stat_str}' + ''.join(['.']*(cnt%4))) + yield chatbot, history, '正常' + + # 把结果写入文件 for index, h in enumerate(handles): - h.join() + h.join() # 这里其实不需要join了,肯定已经都结束了 fp = file_manifest[index] gpt_say = mutable_return[index] i_say_show_user = i_say_show_user_buffer[index] @@ -51,9 +65,9 @@ def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt, chatbot.append((i_say_show_user, f'[Local Message] 已完成{os.path.abspath(fp)}的转化,\n\n存入{os.path.abspath(where_to_relocate)}')) history.append(i_say_show_user); history.append(gpt_say) yield chatbot, history, '正常' - time.sleep(2) + time.sleep(1) - # 结束 + # 备份一个文件 res = write_results_to_file(history) - chatbot.append(("完成了吗?", res)) + chatbot.append(("给爷一份任务执行报告", res)) yield chatbot, history, '正常' diff --git a/functional_crazy.py b/functional_crazy.py index 0c3480d..a319449 100644 --- a/functional_crazy.py +++ b/functional_crazy.py @@ -7,13 +7,13 @@ def get_crazy_functionals(): from crazy_functions.解析项目源代码 import 解析一个C项目的头文件 from crazy_functions.解析项目源代码 import 解析一个C项目 from crazy_functions.高级功能函数模板 import 高阶功能模板函数 - from crazy_functions.全项目切换英文 import 全项目切换英文 + from crazy_functions.代码重写为全英文_多线程 import 全项目切换英文 return { "[实验] 请解析并解构此项目本身": { "Function": 解析项目本身 }, - "[实验] 把此项目代码切换成全英文": { + "[实验] 把本项目代码切换成全英文": { "Color": "stop", # 按钮颜色 "Function": 全项目切换英文 }, From 6f1e9b63c2ee7e78056b0330578dc7c530993cee Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 01:39:15 +0800 Subject: [PATCH 08/30] change description --- functional_crazy.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/functional_crazy.py b/functional_crazy.py index a319449..000e65d 100644 --- a/functional_crazy.py +++ b/functional_crazy.py @@ -13,10 +13,6 @@ def get_crazy_functionals(): "[实验] 请解析并解构此项目本身": { "Function": 解析项目本身 }, - "[实验] 把本项目代码切换成全英文": { - "Color": "stop", # 按钮颜色 - "Function": 全项目切换英文 - }, "[实验] 解析整个py项目(配合input输入框)": { "Color": "stop", # 按钮颜色 "Function": 解析一个Python项目 @@ -37,6 +33,10 @@ def get_crazy_functionals(): "Color": "stop", # 按钮颜色 "Function": 批量生成函数注释 }, + "[实验] 把本项目源代码切换成全英文(多线程)": { + "Color": "stop", # 按钮颜色 + "Function": 全项目切换英文 + }, "[实验] 实验功能函数模板": { "Color": "stop", # 按钮颜色 "Function": 高阶功能模板函数 From 71d2f0168568a306d75f7fb0470d77daf5583346 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 01:42:11 +0800 Subject: [PATCH 09/30] bug fix --- crazy_functions/代码重写为全英文_多线程.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crazy_functions/代码重写为全英文_多线程.py b/crazy_functions/代码重写为全英文_多线程.py index 13500cf..6f176ce 100644 --- a/crazy_functions/代码重写为全英文_多线程.py +++ b/crazy_functions/代码重写为全英文_多线程.py @@ -9,10 +9,13 @@ def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt, history = [] # 清空历史,以免输入溢出 # 集合文件 import time, glob, os - file_manifest = [f for f in glob.glob('./**/*.py', recursive=True) if ('test_project' not in f) and ('gpt_log' not in f)] + os.makedirs('gpt_log/generated_english_version', exist_ok=True) + os.makedirs('gpt_log/generated_english_version/crazy_functions', exist_ok=True) + file_manifest = [f for f in glob.glob('./*.py') if ('test_project' not in f) and ('gpt_log' not in f)] + \ + [f for f in glob.glob('./crazy_functions/*.py') if ('test_project' not in f) and ('gpt_log' not in f)] i_say_show_user_buffer = [] - # 随便显示点什么防止卡顿 + # 随便显示点什么防止卡顿的感觉 for index, fp in enumerate(file_manifest): # if 'test_project' in fp: continue with open(fp, 'r', encoding='utf-8') as f: @@ -58,8 +61,7 @@ def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt, fp = file_manifest[index] gpt_say = mutable_return[index] i_say_show_user = i_say_show_user_buffer[index] - os.makedirs('gpt_log/generated_english_version', exist_ok=True) - os.makedirs('gpt_log/generated_english_version/crazy_functions', exist_ok=True) + where_to_relocate = f'gpt_log/generated_english_version/{fp}' with open(where_to_relocate, 'w+', encoding='utf-8') as f: f.write(gpt_say.lstrip('```').rstrip('```')) chatbot.append((i_say_show_user, f'[Local Message] 已完成{os.path.abspath(fp)}的转化,\n\n存入{os.path.abspath(where_to_relocate)}')) From e76d8cfbc2b23a4e2ce9cc360efe53ccab061055 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 11:34:03 +0800 Subject: [PATCH 10/30] =?UTF-8?q?[=E5=AE=9E=E9=AA=8C]=20=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E4=B8=8A=E7=9A=84=E4=BB=8A=E5=A4=A9=EF=BC=88=E9=AB=98=E7=BA=A7?= =?UTF-8?q?=E5=87=BD=E6=95=B0demo=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/高级功能函数模板.py | 15 +++++++++------ functional_crazy.py | 6 ++---- other_llms/text-generation-webui | 1 + 3 files changed, 12 insertions(+), 10 deletions(-) create mode 160000 other_llms/text-generation-webui diff --git a/crazy_functions/高级功能函数模板.py b/crazy_functions/高级功能函数模板.py index bbaa545..f29e387 100644 --- a/crazy_functions/高级功能函数模板.py +++ b/crazy_functions/高级功能函数模板.py @@ -1,16 +1,19 @@ -from predict import predict_no_ui -from toolbox import CatchException, report_execption, write_results_to_file, predict_no_ui_but_counting_down -fast_debug = False +from predict import predict_no_ui_long_connection +from toolbox import CatchException, report_execption, write_results_to_file +import datetime @CatchException def 高阶功能模板函数(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT): history = [] # 清空历史,以免输入溢出 - for i in range(5): - i_say = f'我给出一个数字,你给出该数字的平方。我给出数字:{i}' + for i in range(3): + currentMonth = (datetime.date.today() + datetime.timedelta(days=i)).month + currentDay = (datetime.date.today() + datetime.timedelta(days=i)).day + i_say = f'今天是{currentMonth}月{currentDay}日,历史中的{currentMonth}月{currentDay}日发生了哪些大事?' chatbot.append((i_say, "[Local Message] waiting gpt response.")) yield chatbot, history, '正常' # 由于请求gpt需要一段时间,我们先及时地做一次状态显示 - gpt_say = predict_no_ui(inputs=i_say, top_p=top_p, temperature=temperature) # 请求gpt,需要一段时间 + # history = [] 每次询问不携带之前的询问历史 + gpt_say = predict_no_ui_long_connection(inputs=i_say, top_p=top_p, temperature=temperature, history=[], sys_prompt="You are a history teacher.") # 请求gpt,需要一段时间 chatbot[-1] = (i_say, gpt_say) history.append(i_say);history.append(gpt_say) diff --git a/functional_crazy.py b/functional_crazy.py index 000e65d..50405a3 100644 --- a/functional_crazy.py +++ b/functional_crazy.py @@ -33,12 +33,10 @@ def get_crazy_functionals(): "Color": "stop", # 按钮颜色 "Function": 批量生成函数注释 }, - "[实验] 把本项目源代码切换成全英文(多线程)": { - "Color": "stop", # 按钮颜色 + "[实验] 把本项目源代码切换成全英文(多线程demo)": { "Function": 全项目切换英文 }, - "[实验] 实验功能函数模板": { - "Color": "stop", # 按钮颜色 + "[实验] 历史上的今天(高级函数demo)": { "Function": 高阶功能模板函数 }, } diff --git a/other_llms/text-generation-webui b/other_llms/text-generation-webui new file mode 160000 index 0000000..46f6536 --- /dev/null +++ b/other_llms/text-generation-webui @@ -0,0 +1 @@ +Subproject commit 46f6536fae8650b4c9f8564c45f4b9f0a46bb8e4 From ebf365841e0ede768ac5a6878bef55c05a5aa07c Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 11:36:55 +0800 Subject: [PATCH 11/30] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=9B=B4=E6=9C=89=E6=84=8F=E6=80=9D=E7=9A=84=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- functional_crazy.py | 2 +- other_llms/text-generation-webui | 1 - 3 files changed, 3 insertions(+), 3 deletions(-) delete mode 160000 other_llms/text-generation-webui diff --git a/.gitignore b/.gitignore index 02ec352..36f3534 100644 --- a/.gitignore +++ b/.gitignore @@ -138,4 +138,5 @@ ssr_conf config_private.py gpt_log private.md -private_upload \ No newline at end of file +private_upload +other_llms \ No newline at end of file diff --git a/functional_crazy.py b/functional_crazy.py index 50405a3..6faed94 100644 --- a/functional_crazy.py +++ b/functional_crazy.py @@ -36,7 +36,7 @@ def get_crazy_functionals(): "[实验] 把本项目源代码切换成全英文(多线程demo)": { "Function": 全项目切换英文 }, - "[实验] 历史上的今天(高级函数demo)": { + "[实验] 历史上的今天(高阶功能模板函数demo)": { "Function": 高阶功能模板函数 }, } diff --git a/other_llms/text-generation-webui b/other_llms/text-generation-webui deleted file mode 160000 index 46f6536..0000000 --- a/other_llms/text-generation-webui +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 46f6536fae8650b4c9f8564c45f4b9f0a46bb8e4 From 2bedba2e17a8f501a8677ea9e476dd7e144c5147 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 12:21:47 +0800 Subject: [PATCH 12/30] =?UTF-8?q?=E5=8E=86=E5=8F=B2=E4=B8=8A=E7=9A=84?= =?UTF-8?q?=E4=BB=8A=E5=A4=A9=EF=BC=8C=E5=B8=A6=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/高级功能函数模板.py | 6 +++--- predict.py | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/crazy_functions/高级功能函数模板.py b/crazy_functions/高级功能函数模板.py index f29e387..e9fe6bc 100644 --- a/crazy_functions/高级功能函数模板.py +++ b/crazy_functions/高级功能函数模板.py @@ -5,15 +5,15 @@ import datetime @CatchException def 高阶功能模板函数(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT): history = [] # 清空历史,以免输入溢出 - for i in range(3): + for i in range(5): currentMonth = (datetime.date.today() + datetime.timedelta(days=i)).month currentDay = (datetime.date.today() + datetime.timedelta(days=i)).day - i_say = f'今天是{currentMonth}月{currentDay}日,历史中的{currentMonth}月{currentDay}日发生了哪些大事?' + i_say = f'历史中哪些事件发生在{currentMonth}月{currentDay}日?列举两条并发送相关图片。' chatbot.append((i_say, "[Local Message] waiting gpt response.")) yield chatbot, history, '正常' # 由于请求gpt需要一段时间,我们先及时地做一次状态显示 # history = [] 每次询问不携带之前的询问历史 - gpt_say = predict_no_ui_long_connection(inputs=i_say, top_p=top_p, temperature=temperature, history=[], sys_prompt="You are a history teacher.") # 请求gpt,需要一段时间 + gpt_say = predict_no_ui_long_connection(inputs=i_say, top_p=top_p, temperature=temperature, history=[], sys_prompt="当你想发送一张照片时,请使用 Markdown ,并且 不要有反斜线, 不要用代码块。使用 Unsplash API (https://source.unsplash.com/1280x720/? < PUT YOUR QUERY HERE >)。") # 请求gpt,需要一段时间 chatbot[-1] = (i_say, gpt_say) history.append(i_say);history.append(gpt_say) diff --git a/predict.py b/predict.py index fb1eb26..c2294a0 100644 --- a/predict.py +++ b/predict.py @@ -83,7 +83,8 @@ def predict_no_ui_long_connection(inputs, top_p, temperature, history=[], sys_pr except StopIteration: break if len(chunk)==0: continue if not chunk.startswith('data:'): - raise ConnectionAbortedError("OpenAI返回了错误:" + chunk) + chunk = get_full_error(chunk.encode('utf8'), stream_response) + raise ConnectionAbortedError("OpenAI拒绝了请求:" + chunk.decode()) delta = json.loads(chunk.lstrip('data:'))['choices'][0]["delta"] if len(delta) == 0: break if "role" in delta: continue From 6ed2b259db4e64b87353bd9c46941a1127eded61 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 12:27:47 +0800 Subject: [PATCH 13/30] =?UTF-8?q?=E4=BC=98=E5=8C=96Unsplash=20API=E7=9A=84?= =?UTF-8?q?=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/高级功能函数模板.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crazy_functions/高级功能函数模板.py b/crazy_functions/高级功能函数模板.py index e9fe6bc..b74f191 100644 --- a/crazy_functions/高级功能函数模板.py +++ b/crazy_functions/高级功能函数模板.py @@ -8,12 +8,12 @@ def 高阶功能模板函数(txt, top_p, temperature, chatbot, history, systemPr for i in range(5): currentMonth = (datetime.date.today() + datetime.timedelta(days=i)).month currentDay = (datetime.date.today() + datetime.timedelta(days=i)).day - i_say = f'历史中哪些事件发生在{currentMonth}月{currentDay}日?列举两条并发送相关图片。' + i_say = f'历史中哪些事件发生在{currentMonth}月{currentDay}日?列举两条并发送相关图片。发送图片时,请使用Markdown,将Unsplash API中的PUT_YOUR_QUERY_HERE替换成描述改事件的两个最重要的单词。' chatbot.append((i_say, "[Local Message] waiting gpt response.")) yield chatbot, history, '正常' # 由于请求gpt需要一段时间,我们先及时地做一次状态显示 # history = [] 每次询问不携带之前的询问历史 - gpt_say = predict_no_ui_long_connection(inputs=i_say, top_p=top_p, temperature=temperature, history=[], sys_prompt="当你想发送一张照片时,请使用 Markdown ,并且 不要有反斜线, 不要用代码块。使用 Unsplash API (https://source.unsplash.com/1280x720/? < PUT YOUR QUERY HERE >)。") # 请求gpt,需要一段时间 + gpt_say = predict_no_ui_long_connection(inputs=i_say, top_p=top_p, temperature=temperature, history=[], sys_prompt="当你想发送一张照片时,请使用Markdown, 并且不要有反斜线, 不要用代码块。使用 Unsplash API (https://source.unsplash.com/1280x720/? < PUT_YOUR_QUERY_HERE >)。") # 请求gpt,需要一段时间 chatbot[-1] = (i_say, gpt_say) history.append(i_say);history.append(gpt_say) From 403d66c3c262be360d48d0b3f6b64a3b91249890 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 12:28:45 +0800 Subject: [PATCH 14/30] =?UTF-8?q?=E4=BC=98=E5=8C=96Unsplash=20API=E7=9A=84?= =?UTF-8?q?=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/高级功能函数模板.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crazy_functions/高级功能函数模板.py b/crazy_functions/高级功能函数模板.py index e9fe6bc..aa1d2aa 100644 --- a/crazy_functions/高级功能函数模板.py +++ b/crazy_functions/高级功能函数模板.py @@ -8,12 +8,14 @@ def 高阶功能模板函数(txt, top_p, temperature, chatbot, history, systemPr for i in range(5): currentMonth = (datetime.date.today() + datetime.timedelta(days=i)).month currentDay = (datetime.date.today() + datetime.timedelta(days=i)).day - i_say = f'历史中哪些事件发生在{currentMonth}月{currentDay}日?列举两条并发送相关图片。' + i_say = f'历史中哪些事件发生在{currentMonth}月{currentDay}日?列举两条并发送相关图片。发送图片时,请使用Markdown,将Unsplash API中的PUT_YOUR_QUERY_HERE替换成描述改事件的两个最重要的单词。' chatbot.append((i_say, "[Local Message] waiting gpt response.")) yield chatbot, history, '正常' # 由于请求gpt需要一段时间,我们先及时地做一次状态显示 # history = [] 每次询问不携带之前的询问历史 - gpt_say = predict_no_ui_long_connection(inputs=i_say, top_p=top_p, temperature=temperature, history=[], sys_prompt="当你想发送一张照片时,请使用 Markdown ,并且 不要有反斜线, 不要用代码块。使用 Unsplash API (https://source.unsplash.com/1280x720/? < PUT YOUR QUERY HERE >)。") # 请求gpt,需要一段时间 + gpt_say = predict_no_ui_long_connection( + inputs=i_say, top_p=top_p, temperature=temperature, history=[], + sys_prompt="当你想发送一张照片时,请使用Markdown, 并且不要有反斜线, 不要用代码块。使用 Unsplash API (https://source.unsplash.com/1280x720/? < PUT_YOUR_QUERY_HERE >)。") # 请求gpt,需要一段时间 chatbot[-1] = (i_say, gpt_say) history.append(i_say);history.append(gpt_say) From c90391a902b0bbf7cd65b529659cb1c8d8e3fa68 Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Wed, 29 Mar 2023 13:38:43 +0800 Subject: [PATCH 15/30] Update README.md --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1f4d564..d894348 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,13 @@ If you like this project, please give it a Star. If you've come up with more use ``` 代码中参考了很多其他优秀项目中的设计,主要包括: -# 借鉴项目1:借鉴了mdtex2html中公式处理的方法 -https://github.com/polarwinkel/mdtex2html -# 借鉴项目2:借鉴了ChuanhuChatGPT中读取OpenAI json的方法、记录历史问询记录的方法以及gradio queue的使用技巧 +# 借鉴项目1:借鉴了ChuanhuChatGPT中读取OpenAI json的方法、记录历史问询记录的方法以及gradio queue的使用技巧 https://github.com/GaiZhenbiao/ChuanhuChatGPT +# 借鉴项目2:借鉴了mdtex2html中公式处理的方法 +https://github.com/polarwinkel/mdtex2html + 项目使用OpenAI的gpt-3.5-turbo模型,期待gpt-4早点放宽门槛😂 ``` From 4d70bc0288b7531ee4e5c8b666262b9ab36a4935 Mon Sep 17 00:00:00 2001 From: 505030475 <505030475@qq.com> Date: Wed, 29 Mar 2023 13:43:07 +0800 Subject: [PATCH 16/30] config comments --- config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.py b/config.py index bff413b..ec04a63 100644 --- a/config.py +++ b/config.py @@ -25,7 +25,7 @@ MAX_RETRY = 2 LLM_MODEL = "gpt-3.5-turbo" # 检查一下是不是忘了改config -if len(API_KEY) != 51: +if len(API_KEY) != 51: # 正确的密钥是51位 assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行" # 设置并行使用的线程数 From 67215bcec52594315693203bde813ccb32768e20 Mon Sep 17 00:00:00 2001 From: 505030475 <505030475@qq.com> Date: Wed, 29 Mar 2023 13:58:30 +0800 Subject: [PATCH 17/30] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crazy_functions/高级功能函数模板.py | 5 ++++- main.py | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/crazy_functions/高级功能函数模板.py b/crazy_functions/高级功能函数模板.py index aa1d2aa..4cf1cb9 100644 --- a/crazy_functions/高级功能函数模板.py +++ b/crazy_functions/高级功能函数模板.py @@ -5,10 +5,13 @@ import datetime @CatchException def 高阶功能模板函数(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT): history = [] # 清空历史,以免输入溢出 + chatbot.append(("这是什么功能?", "[Local Message] 请注意,您正在调用一个函数模板,该函数面向希望实现更多有趣功能的开发者,它可以作为创建新功能函数的模板。为了做到简单易读,该函数只有25行代码,不会实时反馈文字流或心跳,请耐心等待程序输出完成。另外您若希望分享新的功能模组,请不吝PR!")) + yield chatbot, history, '正常' # 由于请求gpt需要一段时间,我们先及时地做一次状态显示 + for i in range(5): currentMonth = (datetime.date.today() + datetime.timedelta(days=i)).month currentDay = (datetime.date.today() + datetime.timedelta(days=i)).day - i_say = f'历史中哪些事件发生在{currentMonth}月{currentDay}日?列举两条并发送相关图片。发送图片时,请使用Markdown,将Unsplash API中的PUT_YOUR_QUERY_HERE替换成描述改事件的两个最重要的单词。' + i_say = f'历史中哪些事件发生在{currentMonth}月{currentDay}日?列举两条并发送相关图片。发送图片时,请使用Markdown,将Unsplash API中的PUT_YOUR_QUERY_HERE替换成描述改事件的三个最重要的单词。' chatbot.append((i_say, "[Local Message] waiting gpt response.")) yield chatbot, history, '正常' # 由于请求gpt需要一段时间,我们先及时地做一次状态显示 diff --git a/main.py b/main.py index 2b6b807..6359d36 100644 --- a/main.py +++ b/main.py @@ -74,7 +74,7 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",) temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",) - predict_args = dict(fn=predict, inputs=[txt, top_p, temperature, chatbot, history, systemPromptTxt], outputs=[chatbot, history, statusDisplay], show_progress=True) + predict_args = dict(fn=predict, inputs=[txt, top_p, temperature, chatbot, history, system_prompt], outputs=[chatbot, history, statusDisplay], show_progress=True) empty_txt_args = dict(fn=lambda: "", inputs=[], outputs=[txt]) txt.submit(**predict_args) @@ -84,11 +84,11 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, statusDisplay]) for k in functional: functional[k]["Button"].click(predict, - [txt, top_p, temperature, chatbot, history, systemPromptTxt, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True) + [txt, top_p, temperature, chatbot, history, system_prompt, gr.State(True), gr.State(k)], [chatbot, history, statusDisplay], show_progress=True) file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt]) for k in crazy_functional: click_handle = crazy_functional[k]["Button"].click(crazy_functional[k]["Function"], - [txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay] + [txt, top_p, temperature, chatbot, history, system_prompt, gr.State(PORT)], [chatbot, history, statusDisplay] ) try: click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot]) except: pass From 7d5aaa5aee9822339cb2384a4c5fc6bbb1d4d886 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 14:16:59 +0800 Subject: [PATCH 18/30] update comments --- predict.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/predict.py b/predict.py index c2294a0..90d360b 100644 --- a/predict.py +++ b/predict.py @@ -1,5 +1,16 @@ # 借鉴了 https://github.com/GaiZhenbiao/ChuanhuChatGPT 项目 +""" + 该文件中主要包含三个函数 + + 不具备多线程能力的函数: + 1. predict: 正常对话时使用,具备完备的交互功能,不可多线程 + + 具备多线程调用能力的函数 + 2. predict_no_ui:高级实验性功能模块调用,不会实时显示在界面上,参数简单,可以多线程并行,方便实现复杂的功能逻辑 + 3. predict_no_ui_long_connection:在实验过程中发现调用predict_no_ui处理长文档时,和openai的连接容易断掉,这个函数用stream的方式解决这个问题,同样支持多线程 +""" + import json import gradio as gr import logging From 17d9a060d8fb44c8b08228deb1459d0176ee9dc0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 14:28:57 +0800 Subject: [PATCH 19/30] fix directory return bug --- crazy_functions/代码重写为全英文_多线程.py | 2 +- toolbox.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/crazy_functions/代码重写为全英文_多线程.py b/crazy_functions/代码重写为全英文_多线程.py index 6f176ce..6c6b1c7 100644 --- a/crazy_functions/代码重写为全英文_多线程.py +++ b/crazy_functions/代码重写为全英文_多线程.py @@ -71,5 +71,5 @@ def 全项目切换英文(txt, top_p, temperature, chatbot, history, sys_prompt, # 备份一个文件 res = write_results_to_file(history) - chatbot.append(("给爷一份任务执行报告", res)) + chatbot.append(("生成一份任务执行报告", res)) yield chatbot, history, '正常' diff --git a/toolbox.py b/toolbox.py index 56d2b90..2aaa82e 100644 --- a/toolbox.py +++ b/toolbox.py @@ -184,6 +184,7 @@ def find_recent_files(directory): if file_path.endswith('.log'): continue created_time = os.path.getctime(file_path) if created_time >= one_minute_ago: + if os.path.isdir(file_path): continue recent_files.append(file_path) return recent_files From 4b21ebdba6ee6f7cf92f5dc1442d0bec507152e2 Mon Sep 17 00:00:00 2001 From: okabe <923048992@qq.com> Date: Wed, 29 Mar 2023 14:53:53 +0800 Subject: [PATCH 20/30] feat: support stop generate button (#53) --- main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 6359d36..5c25d96 100644 --- a/main.py +++ b/main.py @@ -52,6 +52,7 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: with gr.Row(): resetBtn = gr.Button("重置", variant="secondary") submitBtn = gr.Button("提交", variant="primary") + stopBtn = gr.Button("停止", variant="stop") with gr.Row(): from check_proxy import check_proxy statusDisplay = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行. \nNetwork: {check_proxy(proxies)}\nModel: {LLM_MODEL}") @@ -79,9 +80,10 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: txt.submit(**predict_args) txt.submit(**empty_txt_args) - submitBtn.click(**predict_args) + submit_event = submitBtn.click(**predict_args) submitBtn.click(**empty_txt_args) resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, statusDisplay]) + stopBtn.click(fn=None, inputs=None, outputs=None, cancels=[submit_event]) for k in functional: functional[k]["Button"].click(predict, [txt, top_p, temperature, chatbot, history, system_prompt, gr.State(True), gr.State(k)], [chatbot, history, statusDisplay], show_progress=True) From 33adfc35dfca288a26661c022eb8e0ac656c87e0 Mon Sep 17 00:00:00 2001 From: okabe <923048992@qq.com> Date: Wed, 29 Mar 2023 15:29:40 +0800 Subject: [PATCH 21/30] fix: markdown display bug #84 --- toolbox.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/toolbox.py b/toolbox.py index 2aaa82e..eeac69d 100644 --- a/toolbox.py +++ b/toolbox.py @@ -107,8 +107,8 @@ def text_divide_paragraph(text): # wtf input lines = text.split("\n") for i, line in enumerate(lines): - if i!=0: lines[i] = "

"+lines[i].replace(" ", " ")+"

" - text = "".join(lines) + lines[i] = "

"+lines[i].replace(" ", " ")+"

" + text = "\n".join(lines) return text def markdown_convertion(txt): From 4699395425522bd5c9b13e14370fd866118c65e1 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 16:47:15 +0800 Subject: [PATCH 22/30] dev --- main.py | 12 +++++++++++- toolbox.py | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 6359d36..50ccf30 100644 --- a/main.py +++ b/main.py @@ -93,6 +93,16 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: try: click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot]) except: pass +# gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数 +def auto_opentab_delay(): + import threading, webbrowser, time + print(f"URL http://localhost:{PORT}") + def open(): + time.sleep(2) + webbrowser.open_new_tab(f'http://localhost:{PORT}') + t = threading.Thread(target=open) + t.daemon = True; t.start() +auto_opentab_delay() demo.title = "ChatGPT 学术优化" -demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", share=True, server_port=PORT, inbrowser=True, auth=AUTHENTICATION) +demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", share=True, server_port=PORT, auth=AUTHENTICATION) diff --git a/toolbox.py b/toolbox.py index 2aaa82e..9705b9a 100644 --- a/toolbox.py +++ b/toolbox.py @@ -207,7 +207,7 @@ def on_file_uploaded(files, chatbot, txt): txt = f'private_upload/{time_tag}' moved_files_str = '\t\n\n'.join(moved_files) chatbot.append(['我上传了文件,请查收', - f'[Local Message] 收到以下文件: \n\n{moved_files_str}\n\n调用路径参数已自动修正到: \n\n{txt}\n\n现在您可以直接选择任意实现性功能']) + f'[Local Message] 收到以下文件: \n\n{moved_files_str}\n\n调用路径参数已自动修正到: \n\n{txt}\n\n现在您点击任意实验功能时,以上文件将被作为输入参数']) return chatbot, txt From 17abd29d5035b5b227deaad69d32cf437b23e542 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 16:50:37 +0800 Subject: [PATCH 23/30] error message change --- config.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/config.py b/config.py index ec04a63..00f7995 100644 --- a/config.py +++ b/config.py @@ -24,12 +24,13 @@ MAX_RETRY = 2 # 选择的OpenAI模型是(gpt4现在只对申请成功的人开放) LLM_MODEL = "gpt-3.5-turbo" -# 检查一下是不是忘了改config -if len(API_KEY) != 51: # 正确的密钥是51位 - assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行" - # 设置并行使用的线程数 CONCURRENT_COUNT = 100 # 设置用户名和密码 -AUTHENTICATION = [] # [("username", "password"), ("username2", "password2"), ...] \ No newline at end of file +AUTHENTICATION = [] # [("username", "password"), ("username2", "password2"), ...] + +# 检查一下是不是忘了改config +if len(API_KEY) != 51: + assert False, "正确的API_KEY密钥是51位,请在config文件中修改API密钥, 添加海外代理之后再运行。" + \ + "(如果您刚更新过代码,请确保旧版config_private文件中没有遗留任何新增键值)" From 5d22785e5ae1bcba2eec5e7e5bfb433081be343b Mon Sep 17 00:00:00 2001 From: Ikko Eltociear Ashimine Date: Wed, 29 Mar 2023 18:57:37 +0900 Subject: [PATCH 24/30] fix typo in predict.py refleshing -> refreshing --- predict.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/predict.py b/predict.py index 90d360b..55a25e6 100644 --- a/predict.py +++ b/predict.py @@ -176,7 +176,7 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt='' chunk = get_full_error(chunk, stream_response) error_msg = chunk.decode() if "reduce the length" in error_msg: - chatbot[-1] = (chatbot[-1][0], "[Local Message] Input (or history) is too long, please reduce input or clear history by refleshing this page.") + chatbot[-1] = (chatbot[-1][0], "[Local Message] Input (or history) is too long, please reduce input or clear history by refreshing this page.") history = [] elif "Incorrect API key" in error_msg: chatbot[-1] = (chatbot[-1][0], "[Local Message] Incorrect API key provided.") From b100680f72907a062731b813e651de738d541c60 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 18:07:33 +0800 Subject: [PATCH 25/30] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/config.py b/config.py index 00f7995..badcd49 100644 --- a/config.py +++ b/config.py @@ -5,6 +5,13 @@ API_URL = "https://api.openai.com/v1/chat/completions" # 改为True应用代理 USE_PROXY = False if USE_PROXY: + + # 填写格式是 [协议]:// [地址] :[端口] , + # 例如 "socks5h://localhost:11284" + # [协议] 常见协议无非socks5h/http,例如 v2*** 和 s** 的默认本地协议是socks5h,cl**h 的默认本地协议是http + # [地址] 懂的都懂,不懂就填localhost或者127.0.0.1肯定错不了(localhost意思是代理软件安装在本机上) + # [端口] 在代理软件的设置里,不同的代理软件界面不一样,但端口号都应该在最显眼的位置上 + # 代理网络的地址,打开你的科学上网软件查看代理的协议(socks5/http)、地址(localhost)和端口(11284) proxies = { "http": "socks5h://localhost:11284", "https": "socks5h://localhost:11284", } print('网络代理状态:运行。') From 23776b90b9ba245fe07ee1634fec2067c7293897 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 19:37:39 +0800 Subject: [PATCH 26/30] handle ip location lookup error --- check_proxy.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/check_proxy.py b/check_proxy.py index d6263ad..39c8972 100644 --- a/check_proxy.py +++ b/check_proxy.py @@ -6,8 +6,11 @@ def check_proxy(proxies): response = requests.get("https://ipapi.co/json/", proxies=proxies, timeout=4) data = response.json() print(f'查询代理的地理位置,返回的结果是{data}') - country = data['country_name'] - result = f"代理配置 {proxies_https}, 代理所在地:{country}" + if 'country_name' in data: + country = data['country_name'] + result = f"代理配置 {proxies_https}, 代理所在地:{country}" + elif 'error' in data: + result = f"代理配置 {proxies_https}, 代理所在地:未知,IP查询频率受限" print(result) return result except: @@ -17,6 +20,7 @@ def check_proxy(proxies): if __name__ == '__main__': + import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染 try: from config_private import proxies # 放自己的秘密如API和代理网址 os.path.exists('config_private.py') except: from config import proxies check_proxy(proxies) \ No newline at end of file From c78254cd865fef421871865be6dc2b5062a8ded0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 20:07:38 +0800 Subject: [PATCH 27/30] Merge branch 'master' of https://github.com/Okabe-Rintarou-0/chatgpt_academic into Okabe-Rintarou-0-master --- config.py | 2 +- main.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/config.py b/config.py index badcd49..644bd4d 100644 --- a/config.py +++ b/config.py @@ -20,7 +20,7 @@ else: print('网络代理状态:未配置。无代理状态下很可能无法访问。') # 发送请求到OpenAI后,等待多久判定为超时 -TIMEOUT_SECONDS = 20 +TIMEOUT_SECONDS = 25 # 网页的端口, -1代表随机端口 WEB_PORT = -1 diff --git a/main.py b/main.py index 50ccf30..8a6db8f 100644 --- a/main.py +++ b/main.py @@ -36,6 +36,7 @@ gr.Chatbot.postprocess = format_io from theme import adjust_theme set_theme = adjust_theme() +can_cancel_events = [] with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: gr.HTML(title_html) with gr.Row(): @@ -52,6 +53,7 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: with gr.Row(): resetBtn = gr.Button("重置", variant="secondary") submitBtn = gr.Button("提交", variant="primary") + stopBtn = gr.Button("停止", variant="stop") with gr.Row(): from check_proxy import check_proxy statusDisplay = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行. \nNetwork: {check_proxy(proxies)}\nModel: {LLM_MODEL}") @@ -77,14 +79,15 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: predict_args = dict(fn=predict, inputs=[txt, top_p, temperature, chatbot, history, system_prompt], outputs=[chatbot, history, statusDisplay], show_progress=True) empty_txt_args = dict(fn=lambda: "", inputs=[], outputs=[txt]) - txt.submit(**predict_args) + can_cancel_events.append(txt.submit(**predict_args)) txt.submit(**empty_txt_args) - submitBtn.click(**predict_args) + can_cancel_events.append(submitBtn.click(**predict_args)) submitBtn.click(**empty_txt_args) resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, statusDisplay]) for k in functional: - functional[k]["Button"].click(predict, + click_handle = functional[k]["Button"].click(predict, [txt, top_p, temperature, chatbot, history, system_prompt, gr.State(True), gr.State(k)], [chatbot, history, statusDisplay], show_progress=True) + can_cancel_events.append(click_handle) file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt]) for k in crazy_functional: click_handle = crazy_functional[k]["Button"].click(crazy_functional[k]["Function"], @@ -92,6 +95,8 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: ) try: click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot]) except: pass + can_cancel_events.append(click_handle) + stopBtn.click(fn=None, inputs=None, outputs=None, cancels=can_cancel_events) # gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数 def auto_opentab_delay(): From e79b0c0835554136efe98508ddfe865fda146e80 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 20:36:58 +0800 Subject: [PATCH 28/30] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=90=8E=E4=B8=8D?= =?UTF-8?q?=E6=B8=85=E7=A9=BA=E8=BE=93=E5=85=A5=E6=A0=8F=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=81=9C=E6=AD=A2=E9=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 5f02a38..acd8d4f 100644 --- a/main.py +++ b/main.py @@ -36,7 +36,7 @@ gr.Chatbot.postprocess = format_io from theme import adjust_theme set_theme = adjust_theme() -can_cancel_events = [] +cancel_handles = [] with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: gr.HTML(title_html) with gr.Row(): @@ -77,18 +77,18 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",) predict_args = dict(fn=predict, inputs=[txt, top_p, temperature, chatbot, history, system_prompt], outputs=[chatbot, history, statusDisplay], show_progress=True) - empty_txt_args = dict(fn=lambda: "", inputs=[], outputs=[txt]) + empty_txt_args = dict(fn=lambda: "", inputs=[], outputs=[txt]) # 用于在提交后清空输入栏 - can_cancel_events.append(txt.submit(**predict_args)) - txt.submit(**empty_txt_args) - can_cancel_events.append(submitBtn.click(**predict_args)) - submitBtn.click(**empty_txt_args) + cancel_handles.append(txt.submit(**predict_args)) + # txt.submit(**empty_txt_args) 在提交后清空输入栏 + cancel_handles.append(submitBtn.click(**predict_args)) + # submitBtn.click(**empty_txt_args) 在提交后清空输入栏 resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, statusDisplay]) stopBtn.click(fn=None, inputs=None, outputs=None, cancels=[submit_event]) for k in functional: click_handle = functional[k]["Button"].click(predict, [txt, top_p, temperature, chatbot, history, system_prompt, gr.State(True), gr.State(k)], [chatbot, history, statusDisplay], show_progress=True) - can_cancel_events.append(click_handle) + cancel_handles.append(click_handle) file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt]) for k in crazy_functional: click_handle = crazy_functional[k]["Button"].click(crazy_functional[k]["Function"], @@ -96,8 +96,8 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: ) try: click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot]) except: pass - can_cancel_events.append(click_handle) - stopBtn.click(fn=None, inputs=None, outputs=None, cancels=can_cancel_events) + cancel_handles.append(click_handle) + stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles) # gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数 def auto_opentab_delay(): From 3ac330dff185fc548b9f5e0161d6160ddd057be2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 29 Mar 2023 20:41:07 +0800 Subject: [PATCH 29/30] bug quick fix --- main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/main.py b/main.py index acd8d4f..f51c28f 100644 --- a/main.py +++ b/main.py @@ -84,7 +84,6 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: cancel_handles.append(submitBtn.click(**predict_args)) # submitBtn.click(**empty_txt_args) 在提交后清空输入栏 resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, statusDisplay]) - stopBtn.click(fn=None, inputs=None, outputs=None, cancels=[submit_event]) for k in functional: click_handle = functional[k]["Button"].click(predict, [txt, top_p, temperature, chatbot, history, system_prompt, gr.State(True), gr.State(k)], [chatbot, history, statusDisplay], show_progress=True) From 403dd2fa5976e52a1cc8f8710457f17ec6fb266c Mon Sep 17 00:00:00 2001 From: binary-husky <96192199+binary-husky@users.noreply.github.com> Date: Wed, 29 Mar 2023 20:47:34 +0800 Subject: [PATCH 30/30] Update main.py --- main.py | 57 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/main.py b/main.py index 681508a..f51c28f 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,15 @@ import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染 -import gradio as gr +import gradio as gr from predict import predict -from toolbox import format_io, find_free_port +from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到 -from config_private import proxies, WEB_PORT, LLM_MODEL +try: from config_private import proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION +except: from config import proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION # 如果WEB_PORT是-1, 则随机选取WEB端口 PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT +AUTHENTICATION = None if AUTHENTICATION == [] else AUTHENTICATION initial_prompt = "Serve me as a writing and programming assistant." title_html = """

ChatGPT 学术优化

""" @@ -15,7 +17,7 @@ title_html = """

ChatGPT 学术优化

""" # 问询记录, python 版本建议3.9+(越新越好) import logging os.makedirs('gpt_log', exist_ok=True) -try:logging.basicConfig(filename='gpt_log/chat_secrets.log', level=logging.INFO, encoding='utf-8') +try:logging.basicConfig(filename='gpt_log/chat_secrets.log', level=logging.INFO, encoding='utf-8') except:logging.basicConfig(filename='gpt_log/chat_secrets.log', level=logging.INFO) print('所有问询记录将自动保存在本地目录./gpt_log/chat_secrets.log, 请注意自我隐私保护哦!') @@ -24,7 +26,7 @@ from functional import get_functionals functional = get_functionals() # 对一些丧心病狂的实验性功能模块进行测试 -from functional_crazy import get_crazy_functionals, on_file_uploaded, on_report_generated +from functional_crazy import get_crazy_functionals crazy_functional = get_crazy_functionals() # 处理markdown文本格式的转变 @@ -34,6 +36,7 @@ gr.Chatbot.postprocess = format_io from theme import adjust_theme set_theme = adjust_theme() +cancel_handles = [] with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: gr.HTML(title_html) with gr.Row(): @@ -42,14 +45,15 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: chatbot.style(height=1000) chatbot.style() history = gr.State([]) - TRUE = gr.State(True) - FALSE = gr.State(False) with gr.Column(scale=1): with gr.Row(): with gr.Column(scale=12): txt = gr.Textbox(show_label=False, placeholder="Input question here.").style(container=False) with gr.Column(scale=1): - submitBtn = gr.Button("提交", variant="primary") + with gr.Row(): + resetBtn = gr.Button("重置", variant="secondary") + submitBtn = gr.Button("提交", variant="primary") + stopBtn = gr.Button("停止", variant="stop") with gr.Row(): from check_proxy import check_proxy statusDisplay = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行. \nNetwork: {check_proxy(proxies)}\nModel: {LLM_MODEL}") @@ -67,36 +71,43 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo: gr.Markdown("上传本地文件供上面的实验性功能调用.") with gr.Row(): file_upload = gr.Files(label='任何文件,但推荐上传压缩文件(zip, tar)', file_count="multiple") - - systemPromptTxt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt).style(container=True) - #inputs, top_p, temperature, top_k, repetition_penalty + system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt).style(container=True) with gr.Accordion("arguments", open=False): top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",) - temperature = gr.Slider(minimum=-0, maximum=5.0, value=1.0, step=0.01, interactive=True, label="Temperature",) + temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",) - txt.submit(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay]) - submitBtn.click(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay], show_progress=True) + predict_args = dict(fn=predict, inputs=[txt, top_p, temperature, chatbot, history, system_prompt], outputs=[chatbot, history, statusDisplay], show_progress=True) + empty_txt_args = dict(fn=lambda: "", inputs=[], outputs=[txt]) # 用于在提交后清空输入栏 + + cancel_handles.append(txt.submit(**predict_args)) + # txt.submit(**empty_txt_args) 在提交后清空输入栏 + cancel_handles.append(submitBtn.click(**predict_args)) + # submitBtn.click(**empty_txt_args) 在提交后清空输入栏 + resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, statusDisplay]) for k in functional: - functional[k]["Button"].click(predict, - [txt, top_p, temperature, chatbot, history, systemPromptTxt, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True) + click_handle = functional[k]["Button"].click(predict, + [txt, top_p, temperature, chatbot, history, system_prompt, gr.State(True), gr.State(k)], [chatbot, history, statusDisplay], show_progress=True) + cancel_handles.append(click_handle) file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt]) for k in crazy_functional: - click_handle = crazy_functional[k]["Button"].click(crazy_functional[k]["Function"], - [txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay] + click_handle = crazy_functional[k]["Button"].click(crazy_functional[k]["Function"], + [txt, top_p, temperature, chatbot, history, system_prompt, gr.State(PORT)], [chatbot, history, statusDisplay] ) try: click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot]) except: pass + cancel_handles.append(click_handle) + stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles) - -# 延迟函数, 做一些准备工作, 最后尝试打开浏览器 +# gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数 def auto_opentab_delay(): import threading, webbrowser, time print(f"URL http://localhost:{PORT}") - def open(): time.sleep(2) - webbrowser.open_new_tab(f'http://localhost:{PORT}') + def open(): + time.sleep(2) + webbrowser.open_new_tab(f'http://localhost:{PORT}') t = threading.Thread(target=open) t.daemon = True; t.start() auto_opentab_delay() demo.title = "ChatGPT 学术优化" -demo.queue().launch(server_name="0.0.0.0", share=True, server_port=PORT) +demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", share=True, server_port=PORT, auth=AUTHENTICATION)