Your Name commited on
Commit
51bde97
1 Parent(s): c06c60b

simplify codes

Browse files
Files changed (4) hide show
  1. functional_crazy.py +0 -26
  2. main.py +11 -14
  3. project_self_analysis.md +122 -0
  4. toolbox.py +31 -1
functional_crazy.py CHANGED
@@ -38,31 +38,5 @@ def get_crazy_functionals():
38
  },
39
  }
40
 
41
- def on_file_uploaded(files, chatbot, txt):
42
- if len(files) == 0: return chatbot, txt
43
- import shutil, os, time, glob
44
- from toolbox import extract_archive
45
- try: shutil.rmtree('./private_upload/')
46
- except: pass
47
- time_tag = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
48
- os.makedirs(f'private_upload/{time_tag}', exist_ok=True)
49
- for file in files:
50
- file_origin_name = os.path.basename(file.orig_name)
51
- shutil.copy(file.name, f'private_upload/{time_tag}/{file_origin_name}')
52
- extract_archive(f'private_upload/{time_tag}/{file_origin_name}',
53
- dest_dir=f'private_upload/{time_tag}/{file_origin_name}.extract')
54
- moved_files = [fp for fp in glob.glob('private_upload/**/*', recursive=True)]
55
- txt = f'private_upload/{time_tag}'
56
- moved_files_str = '\t\n\n'.join(moved_files)
57
- chatbot.append(['我上传了文件,请查收',
58
- f'[Local Message] 收到以下文件: \n\n{moved_files_str}\n\n调用路径参数已自动修正到: \n\n{txt}\n\n现在您可以直接选择任意实现性功能'])
59
- return chatbot, txt
60
 
61
- def on_report_generated(files, chatbot):
62
- from toolbox import find_recent_files
63
- report_files = find_recent_files('gpt_log')
64
- if len(report_files) == 0: return report_files, chatbot
65
- # files.extend(report_files)
66
- chatbot.append(['汇总报告如何远程获取?', '汇总报告已经添加到右侧文件上传区,请查收。'])
67
- return report_files, chatbot
68
 
 
38
  },
39
  }
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
 
 
 
 
 
 
 
42
 
main.py CHANGED
@@ -1,7 +1,7 @@
1
  import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
2
  import gradio as gr
3
  from predict import predict
4
- from toolbox import format_io, find_free_port
5
 
6
  # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
7
  try: from config_private import proxies, WEB_PORT, LLM_MODEL
@@ -25,7 +25,7 @@ from functional import get_functionals
25
  functional = get_functionals()
26
 
27
  # 对一些丧心病狂的实验性功能模块进行测试
28
- from functional_crazy import get_crazy_functionals, on_file_uploaded, on_report_generated
29
  crazy_functional = get_crazy_functionals()
30
 
31
  # 处理markdown文本格式的转变
@@ -43,8 +43,6 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo:
43
  chatbot.style(height=1000)
44
  chatbot.style()
45
  history = gr.State([])
46
- TRUE = gr.State(True)
47
- FALSE = gr.State(False)
48
  with gr.Column(scale=1):
49
  with gr.Row():
50
  with gr.Column(scale=12):
@@ -68,36 +66,35 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo:
68
  gr.Markdown("上传本地文件供上面的实验性功能调用.")
69
  with gr.Row():
70
  file_upload = gr.Files(label='任何文件,但推荐上传压缩文件(zip, tar)', file_count="multiple")
71
-
72
- systemPromptTxt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt).style(container=True)
73
- #inputs, top_p, temperature, top_k, repetition_penalty
74
  with gr.Accordion("arguments", open=False):
75
  top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
76
  temperature = gr.Slider(minimum=-0, maximum=5.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
77
 
78
- txt.submit(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay])
79
- submitBtn.click(predict, [txt, top_p, temperature, chatbot, history, systemPromptTxt], [chatbot, history, statusDisplay], show_progress=True)
80
  for k in functional:
81
  functional[k]["Button"].click(predict,
82
- [txt, top_p, temperature, chatbot, history, systemPromptTxt, TRUE, gr.State(k)], [chatbot, history, statusDisplay], show_progress=True)
83
  file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt])
84
  for k in crazy_functional:
85
  click_handle = crazy_functional[k]["Button"].click(crazy_functional[k]["Function"],
86
- [txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay]
87
  )
88
  try: click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
89
  except: pass
90
 
91
-
92
  # 延迟函数, 做一些准备工作, 最后尝试打开浏览器
93
  def auto_opentab_delay():
94
  import threading, webbrowser, time
95
  print(f"URL http://localhost:{PORT}")
96
- def open(): time.sleep(2)
97
  webbrowser.open_new_tab(f'http://localhost:{PORT}')
98
- t = threading.Thread(target=open)
99
  t.daemon = True; t.start()
100
 
101
  auto_opentab_delay()
102
  demo.title = "ChatGPT 学术优化"
103
  demo.queue().launch(server_name="0.0.0.0", share=True, server_port=PORT)
 
 
 
1
  import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
2
  import gradio as gr
3
  from predict import predict
4
+ from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated
5
 
6
  # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
7
  try: from config_private import proxies, WEB_PORT, LLM_MODEL
 
25
  functional = get_functionals()
26
 
27
  # 对一些丧心病狂的实验性功能模块进行测试
28
+ from functional_crazy import get_crazy_functionals
29
  crazy_functional = get_crazy_functionals()
30
 
31
  # 处理markdown文本格式的转变
 
43
  chatbot.style(height=1000)
44
  chatbot.style()
45
  history = gr.State([])
 
 
46
  with gr.Column(scale=1):
47
  with gr.Row():
48
  with gr.Column(scale=12):
 
66
  gr.Markdown("上传本地文件供上面的实验性功能调用.")
67
  with gr.Row():
68
  file_upload = gr.Files(label='任何文件,但推荐上传压缩文件(zip, tar)', file_count="multiple")
69
+ system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt).style(container=True)
 
 
70
  with gr.Accordion("arguments", open=False):
71
  top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
72
  temperature = gr.Slider(minimum=-0, maximum=5.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
73
 
74
+ txt.submit(predict, [txt, top_p, temperature, chatbot, history, system_prompt], [chatbot, history, statusDisplay])
75
+ submitBtn.click(predict, [txt, top_p, temperature, chatbot, history, system_prompt], [chatbot, history, statusDisplay], show_progress=True)
76
  for k in functional:
77
  functional[k]["Button"].click(predict,
78
+ [txt, top_p, temperature, chatbot, history, system_prompt, gr.State(True), gr.State(k)], [chatbot, history, statusDisplay], show_progress=True)
79
  file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt], [chatbot, txt])
80
  for k in crazy_functional:
81
  click_handle = crazy_functional[k]["Button"].click(crazy_functional[k]["Function"],
82
+ [txt, top_p, temperature, chatbot, history, system_prompt, gr.State(PORT)], [chatbot, history, statusDisplay]
83
  )
84
  try: click_handle.then(on_report_generated, [file_upload, chatbot], [file_upload, chatbot])
85
  except: pass
86
 
 
87
  # 延迟函数, 做一些准备工作, 最后尝试打开浏览器
88
  def auto_opentab_delay():
89
  import threading, webbrowser, time
90
  print(f"URL http://localhost:{PORT}")
91
+ def open_webbrowser(): time.sleep(2)
92
  webbrowser.open_new_tab(f'http://localhost:{PORT}')
93
+ t = threading.Thread(target=open_webbrowser)
94
  t.daemon = True; t.start()
95
 
96
  auto_opentab_delay()
97
  demo.title = "ChatGPT 学术优化"
98
  demo.queue().launch(server_name="0.0.0.0", share=True, server_port=PORT)
99
+
100
+ # -> 代码控制在100行以内
project_self_analysis.md ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # chatgpt-academic项目分析报告
2
+ (Author补充:以下分析均由本项目调用ChatGPT一键生成,如果有不准确的地方全怪GPT)
3
+
4
+ ## [0/10] 程序摘要: check_proxy.py
5
+
6
+ 这个程序是一个用来检查代理服务器是否有效的 Python 程序代码。程序文件名为 check_proxy.py。其中定义了一个函数 check_proxy,该函数接收一个代理配置信息 proxies,使用 requests 库向一个代理服务器发送请求,获取该代理的所在地信息并返回。如果请求超时或者异常,该函数将返回一个代理无效的结果。
7
+
8
+ 程序代码分为两个部分,首先是 check_proxy 函数的定义部分,其次是程序文件的入口部分,在该部分代码中,程序从 config_private.py 文件或者 config.py 文件中加载代理配置信息,然后调用 check_proxy 函数来检测代理服务器是否有效。如果配置文件 config_private.py 存在,则会加载其中的代理配置信息,否则会从 config.py 文件中读取。
9
+
10
+ ## [1/10] 程序摘要: config.py
11
+
12
+ 本程序文件名为config.py,主要功能是存储应用所需的常量和配置信息。
13
+
14
+ 其中,包含了应用所需的OpenAI API密钥、API接口地址、网络代理设置、超时设置、网络端口和OpenAI模型选择等信息,在运行应用前需要进行相应的配置。在未配置网络代理时,程序给出了相应的警告提示。
15
+
16
+ 此外,还包含了一个检查函数,用于检查是否忘记修改API密钥。
17
+
18
+ 总之,config.py文件是应用中的一个重要配置文件,用来存储应用所需的常量和配置信息,需要在应用运行前进行相应的配置。
19
+
20
+ ## [2/10] 程序摘要: config_private.py
21
+
22
+ 该文件是一个配置文件,命名为config_private.py。它是一个Python脚本,用于配置OpenAI的API密钥、模型和其它相关设置。该配置文件还可以设置是否使用代理。如果使用代理,需要设置代理协议、地址和端口。在设置代理之后,该文件还包括一些用于测试代理是否正常工作的代码。该文件还包括超时时间、随机端口、重试次数等设置。在文件末尾,还有一个检查代码,如果没有更改API密钥,则抛出异常。
23
+
24
+ ## [3/10] 程序摘要: functional.py
25
+
26
+ 该程序文件名为 functional.py,其中包含一个名为 get_functionals 的函数,该函数返回一个字典,该字典包含了各种翻译、校对等功能的名称、前缀、后缀以及默认按钮颜色等信息。具体功能包括:英语学术润色、中文学术润色、查找语法错误、中英互译、中译英、学术中译英、英译中、解释代码等。该程序的作用为提供各种翻译、校对等功能的模板,以便后续程序可以直接调用。
27
+
28
+ (Author补充:这个文件汇总了模块化的Prompt调用,如果发现了新的好用Prompt,别藏着哦^_^速速PR)
29
+
30
+
31
+ ## [4/10] 程序摘要: functional_crazy.py
32
+
33
+ 这个程序文件 functional_crazy.py 导入了一些 python 模块,并提供了一个函数 get_crazy_functionals(),该函数返回不同实验功能的描述和函数。其中,使用的的模块包括:
34
+
35
+ - crazy_functions.读文章写摘要 中的 读文章写摘要
36
+ - crazy_functions.生成函数注释 中的 批量生成函数注释
37
+ - crazy_functions.解析项目源代码 中的 解析项目本身、解析一个Python项目、解析一个C项目的头文件、解析一个C项目
38
+ - crazy_functions.高级功能函数模板 中的 高阶功能模板函数
39
+
40
+ 返回的实验功能函数包括:
41
+
42
+ - "[实验] 请解析并解构此项目本身",包含函数:解析项目本身
43
+ - "[实验] 解析整个py项目(配合input输入框)",包含函数:解析一个Python项目
44
+ - "[实验] 解析整个C++项目头文件(配合input输入框)",包含函数:解析一个C项目的头文件
45
+ - "[实验] 解析整个C++项目(配合input输入框)",包含函数:解析一个C项目
46
+ - "[实验] 读tex论文写摘要(配合input输入框)",包含函数:读文章写摘要
47
+ - "[实验] 批量生成函数注释(配合input输入框)",包含函数:批量生成函数注释
48
+ - "[实验] 实验功能函数模板",包含函数:高阶功能模板函数
49
+
50
+ 这些函数用于系统开发和测试,方便开发者进行特定程序语言后台功能开发的测试和实验,增加系统可靠稳定性和用户友好性。
51
+
52
+ (Author补充:这个文件汇总了模块化的函数,如此设计以方便任何新功能的加入)
53
+
54
+ ## [5/10] 程序摘要: main.py
55
+
56
+ 该程序是一个基于Gradio框架的聊天机器人应用程序。用户可以通过输入问题来获取答案,并与聊天机器人进行对话。该应用程序还集成了一些实验性功能模块,用户可以通过上传本地文件或点击相关按钮来使用这些模块。程序还可以生成对话日志,并且具有一些外观上的调整。在运行时,它会自动打开一个网页并在本地启动服务器。
57
+
58
+
59
+ ## [6/10] 程序摘要: predict.py
60
+
61
+ 该程序文件名为predict.py,主要是针对一个基于ChatGPT的聊天机器人进行交互和预测。
62
+
63
+ 第一部分是导入所需的库和配置文件。
64
+
65
+ 第二部分是一个用于获取Openai返回的完整错误信息的函数。
66
+
67
+ 第三部分是用于一次性完成向ChatGPT发送请求和等待回复的函数。
68
+
69
+ 第四部分是用于基础的对话功能的函数,通过stream参数可以选择是否显示中间的过程。
70
+
71
+ 第五部分是用于整合所需信息和选择LLM模型生成的HTTP请求。
72
+
73
+ (Author补充:主要是predict_no_ui和predict两个函数。前者不用stream,方便、高效、易用。后者用stream,展现效果好。)
74
+
75
+ ## [7/10] 程序摘要: show_math.py
76
+
77
+ 这是一个名为show_math.py的Python程序文件,主要用于将Markdown-LaTeX混合文本转换为HTML格式,并包括MathML数学公式。程序使用latex2mathml.converter库将LaTeX公式转换为MathML格式,并使用正则表达式递归地翻译输入的Markdown-LaTeX混合文本。程序包括转换成双美元符号($$)形式、转换成单美元符号($)形式、转换成\[\]形式以及转换成\(\)形式的LaTeX数学公式。如果转换中出现错误,程序将返回相应的错误消息。
78
+
79
+ ## [8/10] 程序摘要: theme.py
80
+
81
+ 这是一个名为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。
82
+
83
+ ## [9/10] 程序摘要: toolbox.py
84
+
85
+ 该文件为Python程序文件,文件名为toolbox.py。主要功能包括:
86
+
87
+ 1. 导入markdown、mdtex2html、threading、functools等模块。
88
+ 2. 定义函数predict_no_ui_but_counting_down,用于生成对话。
89
+ 3. 定义函数write_results_to_file,用于将对话记录生成Markdown文件。
90
+ 4. 定义函数regular_txt_to_markdown,将普通文本转换为Markdown格式的文本。
91
+ 5. 定义装饰器函数CatchException,用于捕获函数执行异常并返回生成器。
92
+ 6. 定义函数report_execption,用于向chatbot中添加错误信息。
93
+ 7. 定义函数text_divide_paragraph,用于将文本按照段落分隔符分割开,生成带有段落标签的HTML代码。
94
+ 8. 定义函数markdown_convertion,用于将Markdown格式的文本转换为HTML格式。
95
+ 9. 定义函数format_io,用于将输入和输出解析为HTML格式。
96
+ 10. 定义函数find_free_port,用于返回当前系统中可用的未使用端口。
97
+ 11. 定义函数extract_archive,用于解压归档文件。
98
+ 12. 定义函数find_recent_files,用于查找最近创建的文件。
99
+ 13. 定义函数on_file_uploaded,用于处理上传文件的操作。
100
+ 14. 定义函数on_report_generated,用于处理生成报告文件的操作。
101
+
102
+ ## 程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能。
103
+
104
+ 这是一个基于Gradio框架的聊天机器人应用,支持通过文本聊天来获取答案,并可以使用一系列实验性功能模块,例如生成函数注释、解析项目源代码、读取Latex论文写摘要等。 程序架构分为前端和后端两个部分。前端使用Gradio实现,包括用户输入区域、应答区域、按钮、调用方式等。后端使用Python实现,包括聊天机器人模型、实验性功能模块、模板模块、管理模块、主程序模块等。
105
+
106
+ 每个程序文件的功能如下:
107
+
108
+ | 文件名 | 功能描述 |
109
+ |:----:|:----:|
110
+ | check_proxy.py | 检查代理服务器是否有效 |
111
+ | config.py | 存储应用所需的常量和配置信息 |
112
+ | config_private.py | 存储Openai的API密钥、模型和其他相关设置 |
113
+ | functional.py | 提供各种翻译、校对等实用模板 |
114
+ | functional_crazy.py | 提供一些实验性质的高级功能 |
115
+ | main.py | 基于Gradio框架的聊天机器人应用程序的主程序 |
116
+ | predict.py | 用于chatbot预测方案创建,向ChatGPT发送请求和获取回复 |
117
+ | show_math.py | 将Markdown-LaTeX混合文本转换为HTML格式,并包括MathML数学公式 |
118
+ | theme.py | 设置Gradio界面的颜色和字体主题 |
119
+ | toolbox.py | 定义一系列工具函数,用于对输入输出进行格式转换、文件操作、异常捕捉和处理等 |
120
+
121
+ 这些程序文件共同组成了一个聊天机器人应用程序的前端和后端实现,使用户可以方便地进行聊天,并可以使用相应的实验功能模块。
122
+
toolbox.py CHANGED
@@ -184,4 +184,34 @@ def find_recent_files(directory):
184
  if created_time >= one_minute_ago:
185
  recent_files.append(file_path)
186
 
187
- return recent_files
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  if created_time >= one_minute_ago:
185
  recent_files.append(file_path)
186
 
187
+ return recent_files
188
+
189
+
190
+ def on_file_uploaded(files, chatbot, txt):
191
+ if len(files) == 0: return chatbot, txt
192
+ import shutil, os, time, glob
193
+ from toolbox import extract_archive
194
+ try: shutil.rmtree('./private_upload/')
195
+ except: pass
196
+ time_tag = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
197
+ os.makedirs(f'private_upload/{time_tag}', exist_ok=True)
198
+ for file in files:
199
+ file_origin_name = os.path.basename(file.orig_name)
200
+ shutil.copy(file.name, f'private_upload/{time_tag}/{file_origin_name}')
201
+ extract_archive(f'private_upload/{time_tag}/{file_origin_name}',
202
+ dest_dir=f'private_upload/{time_tag}/{file_origin_name}.extract')
203
+ moved_files = [fp for fp in glob.glob('private_upload/**/*', recursive=True)]
204
+ txt = f'private_upload/{time_tag}'
205
+ moved_files_str = '\t\n\n'.join(moved_files)
206
+ chatbot.append(['我上传了文件,请查收',
207
+ f'[Local Message] 收到以下文件: \n\n{moved_files_str}\n\n调用路径参数已自动修正到: \n\n{txt}\n\n现在您可以直接选择任意实现性功能'])
208
+ return chatbot, txt
209
+
210
+
211
+ def on_report_generated(files, chatbot):
212
+ from toolbox import find_recent_files
213
+ report_files = find_recent_files('gpt_log')
214
+ if len(report_files) == 0: return report_files, chatbot
215
+ # files.extend(report_files)
216
+ chatbot.append(['汇总报告如何远程获取?', '汇总报告已经添加到右侧文件上传区,请查收。'])
217
+ return report_files, chatbot