Spaces:
Sleeping
Sleeping
Merge branch 'ifyz-patch-1' into test-3-26
Browse files- README.md +23 -7
- crazy_functions/解析项目源代码.py +6 -6
- crazy_functions/读文章写摘要.py +3 -3
- main.py +12 -7
- predict.py +25 -1
- toolbox.py +6 -1
README.md
CHANGED
@@ -60,7 +60,7 @@ chat分析报告生成 | [实验性功能] 运行后自动生成总结汇报
|
|
60 |
|
61 |
## 直接运行 (Windows or Linux or MacOS)
|
62 |
|
63 |
-
```
|
64 |
# 下载项目
|
65 |
git clone https://github.com/binary-husky/chatgpt_academic.git
|
66 |
cd chatgpt_academic
|
@@ -73,9 +73,16 @@ python -m pip install -r requirements.txt
|
|
73 |
python main.py
|
74 |
|
75 |
# 测试实验性功能
|
76 |
-
|
77 |
-
input区域 输入 ./crazy_functions/test_project/
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
```
|
80 |
|
81 |
|
@@ -93,9 +100,18 @@ docker build -t gpt-academic .
|
|
93 |
docker run --rm -it --net=host gpt-academic
|
94 |
|
95 |
# 测试实验性功能
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
```
|
100 |
|
101 |
|
|
|
60 |
|
61 |
## 直接运行 (Windows or Linux or MacOS)
|
62 |
|
63 |
+
``` sh
|
64 |
# 下载项目
|
65 |
git clone https://github.com/binary-husky/chatgpt_academic.git
|
66 |
cd chatgpt_academic
|
|
|
73 |
python main.py
|
74 |
|
75 |
# 测试实验性功能
|
76 |
+
## 测试C++项目头文件分析
|
77 |
+
input区域 输入 ./crazy_functions/test_project/cpp/libJPG , 然后点击 "[实验] 解析整个C++项目(input输入项目根路径)"
|
78 |
+
## 测试给Latex项目写摘要
|
79 |
+
input区域 输入 ./crazy_functions/test_project/latex/attention , 然后点击 "[实验] 读tex论文写摘要(input输入项目根路径)"
|
80 |
+
## 测试Python项目分析
|
81 |
+
input区域 输入 ./crazy_functions/test_project/python/dqn , 然后点击 "[实验] 解析整个py项目(input输入项目根路径)"
|
82 |
+
## 测试自我代码解读
|
83 |
+
点击 "[实验] 请解析并解构此项目本身"
|
84 |
+
## 测试实验功能模板函数(要求gpt回答几个数的平方是什么),您可以根据此函数为模板,实现更复杂的功能
|
85 |
+
点击 "[实验] 实验功能函数模板"
|
86 |
```
|
87 |
|
88 |
|
|
|
100 |
docker run --rm -it --net=host gpt-academic
|
101 |
|
102 |
# 测试实验性功能
|
103 |
+
## 测试自我代码解读
|
104 |
+
点击 "[实验] 请解析并解构此项目本身"
|
105 |
+
## 测试实验功能模板函数(要求gpt回答几个数的平方是什么),您可以根据此函数为模板,实现更复杂的功能
|
106 |
+
点击 "[实验] 实验功能函数模板"
|
107 |
+
##(请注意在docker中运行时,需要额外注意程序的文件访问权限问题)
|
108 |
+
## 测试C++项目头文件分析
|
109 |
+
input区域 输入 ./crazy_functions/test_project/cpp/libJPG , 然后点击 "[实验] 解析整个C++项目(input输入项目根路径)"
|
110 |
+
## 测试给Latex项目写摘要
|
111 |
+
input区域 输入 ./crazy_functions/test_project/latex/attention , 然后点击 "[实验] 读tex论文写摘要(input输入项目根路径)"
|
112 |
+
## 测试Python项目分析
|
113 |
+
input区域 输入 ./crazy_functions/test_project/python/dqn , 然后点击 "[实验] 解析整个py项目(input输入项目根路径)"
|
114 |
+
|
115 |
```
|
116 |
|
117 |
|
crazy_functions/解析项目源代码.py
CHANGED
@@ -9,9 +9,9 @@ def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot,
|
|
9 |
with open(fp, 'r', encoding='utf-8') as f:
|
10 |
file_content = f.read()
|
11 |
|
12 |
-
|
13 |
-
i_say =
|
14 |
-
i_say_show_user =
|
15 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
16 |
yield chatbot, history, '正常'
|
17 |
|
@@ -56,9 +56,9 @@ def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTx
|
|
56 |
with open(fp, 'r', encoding='utf-8') as f:
|
57 |
file_content = f.read()
|
58 |
|
59 |
-
|
60 |
-
i_say =
|
61 |
-
i_say_show_user =
|
62 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
63 |
yield chatbot, history, '正常'
|
64 |
|
|
|
9 |
with open(fp, 'r', encoding='utf-8') as f:
|
10 |
file_content = f.read()
|
11 |
|
12 |
+
prefix = "接下来请你逐文件分析下面的工程" if index==0 else ""
|
13 |
+
i_say = prefix + f'请对下面的程序文件做一个概述文件名是{os.path.relpath(fp, project_folder)},文件代码是 ```{file_content}```'
|
14 |
+
i_say_show_user = prefix + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
|
15 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
16 |
yield chatbot, history, '正常'
|
17 |
|
|
|
56 |
with open(fp, 'r', encoding='utf-8') as f:
|
57 |
file_content = f.read()
|
58 |
|
59 |
+
prefix = "接下来请你分析自己的程序构成,别紧张," if index==0 else ""
|
60 |
+
i_say = prefix + f'请对下面的程序文件做一个概述文件名是{fp},文件代码是 ```{file_content}```'
|
61 |
+
i_say_show_user = prefix + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
|
62 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
63 |
yield chatbot, history, '正常'
|
64 |
|
crazy_functions/读文章写摘要.py
CHANGED
@@ -10,9 +10,9 @@ def 解析Paper(file_manifest, project_folder, top_p, temperature, chatbot, hist
|
|
10 |
with open(fp, 'r', encoding='utf-8') as f:
|
11 |
file_content = f.read()
|
12 |
|
13 |
-
|
14 |
-
i_say =
|
15 |
-
i_say_show_user =
|
16 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
17 |
print('[1] yield chatbot, history')
|
18 |
yield chatbot, history, '正常'
|
|
|
10 |
with open(fp, 'r', encoding='utf-8') as f:
|
11 |
file_content = f.read()
|
12 |
|
13 |
+
prefix = "接下来请你逐文件分析下面的论文文件,概括其内容" if index==0 else ""
|
14 |
+
i_say = prefix + f'请对下面的文章片段用中文做一个概述,文件名是{os.path.relpath(fp, project_folder)},文章内容是 ```{file_content}```'
|
15 |
+
i_say_show_user = prefix + f'[{index}/{len(file_manifest)}] 请对下面的文章片段做一个概述: {os.path.abspath(fp)}'
|
16 |
chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
|
17 |
print('[1] yield chatbot, history')
|
18 |
yield chatbot, history, '正常'
|
main.py
CHANGED
@@ -1,11 +1,15 @@
|
|
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 |
-
|
|
|
7 |
except: from config import proxies, WEB_PORT
|
8 |
|
|
|
9 |
PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
|
10 |
|
11 |
initial_prompt = "Serve me as a writing and programming assistant."
|
@@ -13,20 +17,21 @@ title_html = """<h1 align="center">ChatGPT 学术优化</h1>"""
|
|
13 |
|
14 |
import logging
|
15 |
os.makedirs('gpt_log', exist_ok=True)
|
16 |
-
logging.basicConfig(filename='gpt_log/chat_secrets.log', level=logging.INFO, encoding='utf-8')
|
17 |
print('所有问询记录将自动保存在本地目录./gpt_log/chat_secrets.log,请注意自我隐私保护哦!')
|
18 |
|
19 |
-
#
|
20 |
from functional import get_functionals
|
21 |
functional = get_functionals()
|
22 |
|
23 |
-
#
|
24 |
from functional_crazy import get_crazy_functionals
|
25 |
crazy_functional = get_crazy_functionals()
|
26 |
|
|
|
27 |
gr.Chatbot.postprocess = format_io
|
28 |
|
29 |
-
with gr.Blocks() as demo:
|
30 |
gr.HTML(title_html)
|
31 |
with gr.Row():
|
32 |
with gr.Column(scale=2):
|
@@ -66,7 +71,7 @@ with gr.Blocks() as demo:
|
|
66 |
crazy_functional[k]["Button"].click(crazy_functional[k]["Function"],
|
67 |
[txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay])
|
68 |
|
69 |
-
|
70 |
def auto_opentab_delay():
|
71 |
import threading, webbrowser, time
|
72 |
print(f"URL http://localhost:{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
|
5 |
+
#Gradio的分析功能
|
6 |
+
gr.analytics_enabled = False
|
7 |
|
8 |
+
# 建议您复制一个config_private.py放自己的秘密,如API和代理网址,避免不小心传github被别人看到
|
9 |
+
try: from config_private import proxies, WEB_PORT
|
10 |
except: from config import proxies, WEB_PORT
|
11 |
|
12 |
+
# 如果WEB_PORT是-1,则随机选取WEB端口
|
13 |
PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
|
14 |
|
15 |
initial_prompt = "Serve me as a writing and programming assistant."
|
|
|
17 |
|
18 |
import logging
|
19 |
os.makedirs('gpt_log', exist_ok=True)
|
20 |
+
logging.basicConfig(filename='gpt_log/chat_secrets.log', level=logging.INFO, encoding='utf-8') # python 版本建议3.9+(越新越好)
|
21 |
print('所有问询记录将自动保存在本地目录./gpt_log/chat_secrets.log,请注意自我隐私保护哦!')
|
22 |
|
23 |
+
# 一些普通功能模块
|
24 |
from functional import get_functionals
|
25 |
functional = get_functionals()
|
26 |
|
27 |
+
# 对一些丧心病狂的实验性功能模块进行测试
|
28 |
from functional_crazy import get_crazy_functionals
|
29 |
crazy_functional = get_crazy_functionals()
|
30 |
|
31 |
+
# 处理markdown文本格式的转变
|
32 |
gr.Chatbot.postprocess = format_io
|
33 |
|
34 |
+
with gr.Blocks(theme=gr.themes.Default(font=[ "Arial", "sans-serif","Microsoft YaHei"],font_mono=["Arial", "sans-serif","Microsoft YaHei"])) as demo:
|
35 |
gr.HTML(title_html)
|
36 |
with gr.Row():
|
37 |
with gr.Column(scale=2):
|
|
|
71 |
crazy_functional[k]["Button"].click(crazy_functional[k]["Function"],
|
72 |
[txt, top_p, temperature, chatbot, history, systemPromptTxt, gr.State(PORT)], [chatbot, history, statusDisplay])
|
73 |
|
74 |
+
# 延迟函数,做一些准备工作,最后尝试打开浏览器
|
75 |
def auto_opentab_delay():
|
76 |
import threading, webbrowser, time
|
77 |
print(f"URL http://localhost:{PORT}")
|
predict.py
CHANGED
@@ -15,6 +15,9 @@ except: from config import proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY
|
|
15 |
timeout_bot_msg = '[local] Request timeout, network error. please check proxy settings in config.py.'
|
16 |
|
17 |
def get_full_error(chunk, stream_response):
|
|
|
|
|
|
|
18 |
while True:
|
19 |
try:
|
20 |
chunk += next(stream_response)
|
@@ -23,6 +26,16 @@ def get_full_error(chunk, stream_response):
|
|
23 |
return chunk
|
24 |
|
25 |
def predict_no_ui(inputs, top_p, temperature, history=[]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt="", stream=False)
|
27 |
|
28 |
retry = 0
|
@@ -47,7 +60,15 @@ def predict_no_ui(inputs, top_p, temperature, history=[]):
|
|
47 |
|
48 |
def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt='',
|
49 |
stream = True, additional_fn=None):
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
if additional_fn is not None:
|
52 |
import functional
|
53 |
importlib.reload(functional)
|
@@ -115,6 +136,9 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
|
|
115 |
return
|
116 |
|
117 |
def generate_payload(inputs, top_p, temperature, history, system_prompt, stream):
|
|
|
|
|
|
|
118 |
headers = {
|
119 |
"Content-Type": "application/json",
|
120 |
"Authorization": f"Bearer {API_KEY}"
|
|
|
15 |
timeout_bot_msg = '[local] Request timeout, network error. please check proxy settings in config.py.'
|
16 |
|
17 |
def get_full_error(chunk, stream_response):
|
18 |
+
"""
|
19 |
+
获取完整的从Openai返回的报错
|
20 |
+
"""
|
21 |
while True:
|
22 |
try:
|
23 |
chunk += next(stream_response)
|
|
|
26 |
return chunk
|
27 |
|
28 |
def predict_no_ui(inputs, top_p, temperature, history=[]):
|
29 |
+
"""
|
30 |
+
发送至chatGPT,等待回复,一次性完成,不显示中间过程。
|
31 |
+
predict函数的简化版。
|
32 |
+
用于payload比较大的情况,或者用于实现多线、带嵌套的复杂功能。
|
33 |
+
|
34 |
+
inputs 是本次问询的输入
|
35 |
+
top_p, temperature是chatGPT的内部调优参数
|
36 |
+
history 是之前的对话列表
|
37 |
+
(注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误,然后raise ConnectionAbortedError)
|
38 |
+
"""
|
39 |
headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt="", stream=False)
|
40 |
|
41 |
retry = 0
|
|
|
60 |
|
61 |
def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt='',
|
62 |
stream = True, additional_fn=None):
|
63 |
+
"""
|
64 |
+
发送至chatGPT,流式获取输出。
|
65 |
+
用于基础的对话功能。
|
66 |
+
inputs 是本次问询的输入
|
67 |
+
top_p, temperature是chatGPT的内部调优参数
|
68 |
+
history 是之前的对话列表(注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误)
|
69 |
+
chatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容
|
70 |
+
additional_fn代表点击的哪个按钮,按钮见functional.py
|
71 |
+
"""
|
72 |
if additional_fn is not None:
|
73 |
import functional
|
74 |
importlib.reload(functional)
|
|
|
136 |
return
|
137 |
|
138 |
def generate_payload(inputs, top_p, temperature, history, system_prompt, stream):
|
139 |
+
"""
|
140 |
+
整合所有信息,选择LLM模型,生成http请求,为发送请求做准备
|
141 |
+
"""
|
142 |
headers = {
|
143 |
"Content-Type": "application/json",
|
144 |
"Authorization": f"Bearer {API_KEY}"
|
toolbox.py
CHANGED
@@ -10,7 +10,10 @@ def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temp
|
|
10 |
try: from config_private import TIMEOUT_SECONDS, MAX_RETRY
|
11 |
except: from config import TIMEOUT_SECONDS, MAX_RETRY
|
12 |
from predict import predict_no_ui
|
|
|
|
|
13 |
mutable = [None, '']
|
|
|
14 |
def mt(i_say, history):
|
15 |
while True:
|
16 |
try:
|
@@ -25,14 +28,16 @@ def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temp
|
|
25 |
mutable[1] = 'Warning! Input file is too long, cut into half. '
|
26 |
except TimeoutError as e:
|
27 |
mutable[0] = '[Local Message] Failed with timeout'
|
28 |
-
|
29 |
thread_name = threading.Thread(target=mt, args=(i_say, history)); thread_name.start()
|
|
|
30 |
cnt = 0
|
31 |
while thread_name.is_alive():
|
32 |
cnt += 1
|
33 |
chatbot[-1] = (i_say_show_user, f"[Local Message] {mutable[1]}waiting gpt response {cnt}/{TIMEOUT_SECONDS*2*(MAX_RETRY+1)}"+''.join(['.']*(cnt%4)))
|
34 |
yield chatbot, history, '正常'
|
35 |
time.sleep(1)
|
|
|
36 |
gpt_say = mutable[0]
|
37 |
return gpt_say
|
38 |
|
|
|
10 |
try: from config_private import TIMEOUT_SECONDS, MAX_RETRY
|
11 |
except: from config import TIMEOUT_SECONDS, MAX_RETRY
|
12 |
from predict import predict_no_ui
|
13 |
+
# 多线程的时候,需要一个mutable结构在不同线程之间传递信息
|
14 |
+
# list就是最简单的mutable结构,我们第一个位置放gpt输出,第二个位置传递报错信息
|
15 |
mutable = [None, '']
|
16 |
+
# multi-threading worker
|
17 |
def mt(i_say, history):
|
18 |
while True:
|
19 |
try:
|
|
|
28 |
mutable[1] = 'Warning! Input file is too long, cut into half. '
|
29 |
except TimeoutError as e:
|
30 |
mutable[0] = '[Local Message] Failed with timeout'
|
31 |
+
# 创建新线程发出http请求
|
32 |
thread_name = threading.Thread(target=mt, args=(i_say, history)); thread_name.start()
|
33 |
+
# 原来的线程则负责持续更新UI,实现一个超时倒计时,并等待新线程的任务完成
|
34 |
cnt = 0
|
35 |
while thread_name.is_alive():
|
36 |
cnt += 1
|
37 |
chatbot[-1] = (i_say_show_user, f"[Local Message] {mutable[1]}waiting gpt response {cnt}/{TIMEOUT_SECONDS*2*(MAX_RETRY+1)}"+''.join(['.']*(cnt%4)))
|
38 |
yield chatbot, history, '正常'
|
39 |
time.sleep(1)
|
40 |
+
# 把gpt的输出从mutable中取出来
|
41 |
gpt_say = mutable[0]
|
42 |
return gpt_say
|
43 |
|