Spaces:
Paused
Paused
Merge pull request #198 from oneLuckyman/feature-match-API_KEY
Browse files- README.md +68 -25
- check_proxy.py +4 -3
- config.py +1 -6
- functional.py +31 -36
- main.py +13 -13
- predict.py +7 -4
- toolbox.py +32 -8
README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
# ChatGPT 学术优化
|
2 |
|
3 |
**如果喜欢这个项目,请给它一个Star;如果你发明了更好用的学术快捷键,欢迎发issue或者pull requests**
|
@@ -16,6 +18,14 @@ https://github.com/polarwinkel/mdtex2html
|
|
16 |
项目使用OpenAI的gpt-3.5-turbo模型,期待gpt-4早点放宽门槛😂
|
17 |
```
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
<div align="center">
|
20 |
|
21 |
功能 | 描述
|
@@ -34,12 +44,13 @@ chat分析报告生成 | [实验性功能] 运行后自动生成总结汇报
|
|
34 |
公式显示 | 可以同时显示公式的tex形式和渲染形式
|
35 |
图片显示 | 可以在markdown中显示图片
|
36 |
支持GPT输出的markdown表格 | 可以输出支持GPT的markdown表格
|
|
|
37 |
|
38 |
</div>
|
39 |
|
40 |
- 新界面
|
41 |
<div align="center">
|
42 |
-
<img src="https://user-images.githubusercontent.com/96192199/
|
43 |
</div>
|
44 |
|
45 |
|
@@ -71,33 +82,57 @@ chat分析报告生成 | [实验性功能] 运行后自动生成总结汇报
|
|
71 |
<img src="https://user-images.githubusercontent.com/96192199/226935232-6b6a73ce-8900-4aee-93f9-733c7e6fef53.png" width="700" >
|
72 |
</div>
|
73 |
|
74 |
-
## 直接运行 (Windows
|
75 |
|
76 |
-
|
77 |
-
|
78 |
git clone https://github.com/binary-husky/chatgpt_academic.git
|
79 |
cd chatgpt_academic
|
80 |
-
|
81 |
-
- 1.如果你在国内,需要设置海外代理才能够使用 OpenAI API,你可以通过 config.py 文件来进行设置。
|
82 |
-
- 2.配置 OpenAI API KEY。你需要在 OpenAI 官网上注册并获取 API KEY。一旦你拿到了 API KEY,在 config.py 文件里配置好即可。
|
83 |
-
# 安装依赖
|
84 |
-
python -m pip install -r requirements.txt
|
85 |
-
# 运行
|
86 |
-
python main.py
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
## 测试自我代码解读
|
96 |
-
点击 "[实验] 请解析并解构此项目本身"
|
97 |
-
## 测试实验功能模板函数(要求gpt回答几个数的平方是什么),您可以根据此函数为模板,实现更复杂的功能
|
98 |
-
点击 "[实验] 实验功能函数模板"
|
99 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
## 使用docker (Linux)
|
103 |
|
@@ -115,7 +150,7 @@ docker run --rm -it --net=host gpt-academic
|
|
115 |
# 测试实验性功能
|
116 |
## 测试自我代码解读
|
117 |
点击 "[实验] 请解析并解构此项目本身"
|
118 |
-
## 测试实验功能模板函数(要求gpt
|
119 |
点击 "[实验] 实验功能函数模板"
|
120 |
##(请注意在docker中运行时,需要额外注意程序的文件访问权限问题)
|
121 |
## 测试C++项目头文件分析
|
@@ -127,6 +162,13 @@ input区域 输入 ./crazy_functions/test_project/python/dqn , 然后点击 "[
|
|
127 |
|
128 |
```
|
129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
## 自定义新的便捷按钮(学术快捷键自定义)
|
132 |
打开functional.py,添加条目如下,然后重启程序即可。(如果按钮已经添加成功并可见,那么前缀、后缀都支持热修改,无需重启程序即可生效。)
|
@@ -166,11 +208,12 @@ python check_proxy.py
|
|
166 |
## 兼容性测试
|
167 |
|
168 |
### 图片显示:
|
|
|
169 |
<div align="center">
|
170 |
-
<img src="https://user-images.githubusercontent.com/96192199/
|
171 |
-
<img src="https://user-images.githubusercontent.com/96192199/226906703-7226495d-6a1f-4a53-9728-ce6778cbdd19.png" height="200" >
|
172 |
</div>
|
173 |
|
|
|
174 |
### 如果一个程序能够读懂并剖析自己:
|
175 |
|
176 |
<div align="center">
|
|
|
1 |
+
|
2 |
+
|
3 |
# ChatGPT 学术优化
|
4 |
|
5 |
**如果喜欢这个项目,请给它一个Star;如果你发明了更好用的学术快捷键,欢迎发issue或者pull requests**
|
|
|
18 |
项目使用OpenAI的gpt-3.5-turbo模型,期待gpt-4早点放宽门槛😂
|
19 |
```
|
20 |
|
21 |
+
> **Note**
|
22 |
+
>
|
23 |
+
> 1.请注意只有“红颜色”标识的函数插件(按钮)才支持读取文件。目前暂不能完善地支持pdf格式文献的翻译解读,尚不支持word格式文件的读取。
|
24 |
+
>
|
25 |
+
> 2.本项目中每个文件的功能都在自译解[`project_self_analysis.md`](https://github.com/binary-husky/chatgpt_academic/wiki/chatgpt-academic%E9%A1%B9%E7%9B%AE%E8%87%AA%E8%AF%91%E8%A7%A3%E6%8A%A5%E5%91%8A)详细说明。随着版本的迭代,您也可以随时自行点击相关函数插件,调用GPT重新生成项目的自我解析报告。常见问题汇总在[`wiki`](https://github.com/binary-husky/chatgpt_academic/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98)当中。
|
26 |
+
>
|
27 |
+
> 3.如果您不太习惯部分中文命名的函数,您可以随时点击相关函数插件,调用GPT一键生成纯英文的项目源代码。
|
28 |
+
|
29 |
<div align="center">
|
30 |
|
31 |
功能 | 描述
|
|
|
44 |
公式显示 | 可以同时显示公式的tex形式和渲染形式
|
45 |
图片显示 | 可以在markdown中显示图片
|
46 |
支持GPT输出的markdown表格 | 可以输出支持GPT的markdown表格
|
47 |
+
…… | ……
|
48 |
|
49 |
</div>
|
50 |
|
51 |
- 新界面
|
52 |
<div align="center">
|
53 |
+
<img src="https://user-images.githubusercontent.com/96192199/228600410-7d44e34f-63f1-4046-acb8-045cb05da8bb.png" width="700" >
|
54 |
</div>
|
55 |
|
56 |
|
|
|
82 |
<img src="https://user-images.githubusercontent.com/96192199/226935232-6b6a73ce-8900-4aee-93f9-733c7e6fef53.png" width="700" >
|
83 |
</div>
|
84 |
|
85 |
+
## 直接运行 (Windows, Linux or MacOS)
|
86 |
|
87 |
+
### 1. 下载项目
|
88 |
+
```sh
|
89 |
git clone https://github.com/binary-husky/chatgpt_academic.git
|
90 |
cd chatgpt_academic
|
91 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
+
### 2. 配置API_KEY和代理设置
|
94 |
+
|
95 |
+
在`config.py`中,配置 海外Proxy 和 OpenAI API KEY,说明如下
|
96 |
+
```
|
97 |
+
1. 如果你在国内,需要设置海外代理才能够顺利使用 OpenAI API,设置方法请仔细阅读config.py(1.修改其中的USE_PROXY为True; 2.按照说明修改其中的proxies)。
|
98 |
+
2. 配置 OpenAI API KEY。你需要在 OpenAI 官网上注册并获取 API KEY。一旦你拿到了 API KEY,在 config.py 文件里配置好即可。
|
99 |
+
3. 与代理网络有关的issue(网络超时、代理不起作用)汇总到 https://github.com/binary-husky/chatgpt_academic/issues/1
|
|
|
|
|
|
|
|
|
100 |
```
|
101 |
+
(P.S. 程序运行时会优先检查是否存在名为`config_private.py`的私密配置文件,并用其中的配置覆盖`config.py`的同名配置。因此,如果您能理解我们的配置读取逻辑,我们强烈建议您在`config.py`旁边创建一个名为`config_private.py`的新配置文件,并把`config.py`中的配置转移(复制)到`config_private.py`中。`config_private.py`不受git管控,可以让您的隐私信息更加安全。)
|
102 |
+
|
103 |
+
|
104 |
+
### 3. 安装依赖
|
105 |
+
```sh
|
106 |
+
# (选择一)推荐
|
107 |
+
python -m pip install -r requirements.txt
|
108 |
|
109 |
+
# (选择二)如果您使用anaconda,步骤也是类似的:
|
110 |
+
# (选择二.1)conda create -n gptac_venv python=3.11
|
111 |
+
# (选择二.2)conda activate gptac_venv
|
112 |
+
# (选择二.3)python -m pip install -r requirements.txt
|
113 |
+
|
114 |
+
# 备注:使用官方pip源或者阿里pip源,其他pip源(如清华pip)有可能出问题,临时换源方法:
|
115 |
+
# python -m pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
|
116 |
+
```
|
117 |
+
|
118 |
+
### 4. 运行
|
119 |
+
```sh
|
120 |
+
python main.py
|
121 |
+
```
|
122 |
+
|
123 |
+
### 5. 测试实验性功能
|
124 |
+
```
|
125 |
+
- 测试C++项目头文件分析
|
126 |
+
input区域 输入 `./crazy_functions/test_project/cpp/libJPG` , 然后点击 "[实验] 解析整个C++项目(input输入项目根路径)"
|
127 |
+
- 测试给Latex项目写摘要
|
128 |
+
input区域 输入 `./crazy_functions/test_project/latex/attention` , 然后点击 "[实验] 读tex论文写摘要(input输入项目根路径)"
|
129 |
+
- 测试Python项目分析
|
130 |
+
input区域 输入 `./crazy_functions/test_project/python/dqn` , 然后点击 "[实验] 解析整个py项目(input输入项目根路径)"
|
131 |
+
- 测试自我代码解读
|
132 |
+
点击 "[实验] 请解析并解构此项目本身"
|
133 |
+
- 测试实验功能模板函数(要求gpt回答历史上的今天发生了什么),您可以根据此函数为模板,实现更复杂的功能
|
134 |
+
点击 "[实验] 实验功能函数模板"
|
135 |
+
```
|
136 |
|
137 |
## 使用docker (Linux)
|
138 |
|
|
|
150 |
# 测试实验性功能
|
151 |
## 测试自我代码解读
|
152 |
点击 "[实验] 请解析并解构此项目本身"
|
153 |
+
## 测试实验功能模板函数(要求gpt回答历史上的今天发生了什么),您可以根据此函数为模板,实现更复杂的功能
|
154 |
点击 "[实验] 实验功能函数模板"
|
155 |
##(请注意在docker中运行时,需要额外注意程序的文件访问权限问题)
|
156 |
## 测试C++项目头文件分析
|
|
|
162 |
|
163 |
```
|
164 |
|
165 |
+
## 其他部署方式
|
166 |
+
- 使用WSL2(Windows Subsystem for Linux 子系统)
|
167 |
+
请访问[部署wiki-1](https://github.com/binary-husky/chatgpt_academic/wiki/%E4%BD%BF%E7%94%A8WSL2%EF%BC%88Windows-Subsystem-for-Linux-%E5%AD%90%E7%B3%BB%E7%BB%9F%EF%BC%89%E9%83%A8%E7%BD%B2)
|
168 |
+
|
169 |
+
- nginx远程部署
|
170 |
+
请访问[部署wiki-2](https://github.com/binary-husky/chatgpt_academic/wiki/%E8%BF%9C%E7%A8%8B%E9%83%A8%E7%BD%B2%E7%9A%84%E6%8C%87%E5%AF%BC)
|
171 |
+
|
172 |
|
173 |
## 自定义新的便捷按钮(学术快捷键自定义)
|
174 |
打开functional.py,添加条目如下,然后重启程序即可。(如果按钮已经添加成功并可见,那么前缀、后缀都支持热修改,无需重启程序即可生效。)
|
|
|
208 |
## 兼容性测试
|
209 |
|
210 |
### 图片显示:
|
211 |
+
|
212 |
<div align="center">
|
213 |
+
<img src="https://user-images.githubusercontent.com/96192199/228737599-bf0a9d9c-1808-4f43-ae15-dfcc7af0f295.png" width="800" >
|
|
|
214 |
</div>
|
215 |
|
216 |
+
|
217 |
### 如果一个程序能够读懂并剖析自己:
|
218 |
|
219 |
<div align="center">
|
check_proxy.py
CHANGED
@@ -21,6 +21,7 @@ def check_proxy(proxies):
|
|
21 |
|
22 |
if __name__ == '__main__':
|
23 |
import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
|
24 |
-
|
25 |
-
|
26 |
-
check_proxy(proxies)
|
|
|
|
21 |
|
22 |
if __name__ == '__main__':
|
23 |
import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
|
24 |
+
from toolbox import get_conf
|
25 |
+
proxies, = get_conf('proxies')
|
26 |
+
check_proxy(proxies)
|
27 |
+
|
config.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
# API_KEY = "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" 此key无效
|
2 |
-
API_KEY = "sk-此处填API
|
3 |
API_URL = "https://api.openai.com/v1/chat/completions"
|
4 |
|
5 |
# 改为True应用代理
|
@@ -36,8 +36,3 @@ CONCURRENT_COUNT = 100
|
|
36 |
|
37 |
# 设置用户名和密码
|
38 |
AUTHENTICATION = [] # [("username", "password"), ("username2", "password2"), ...]
|
39 |
-
|
40 |
-
# 检查一下是不是忘了改config
|
41 |
-
if len(API_KEY) != 51:
|
42 |
-
assert False, "正确的API_KEY密钥是51位,请在config文件中修改API密钥, 添加海外代理之后再运行。" + \
|
43 |
-
"(如果您刚更新过代码,请确保旧版config_private文件中没有遗留任何新增键值)"
|
|
|
1 |
# API_KEY = "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" 此key无效
|
2 |
+
API_KEY = "sk-此处填API密钥"
|
3 |
API_URL = "https://api.openai.com/v1/chat/completions"
|
4 |
|
5 |
# 改为True应用代理
|
|
|
36 |
|
37 |
# 设置用户名和密码
|
38 |
AUTHENTICATION = [] # [("username", "password"), ("username2", "password2"), ...]
|
|
|
|
|
|
|
|
|
|
functional.py
CHANGED
@@ -2,58 +2,53 @@
|
|
2 |
# 'secondary' 颜色对应 theme.py 中的 neutral_hue
|
3 |
# 'stop' 颜色对应 theme.py 中的 color_er
|
4 |
# 默认按钮颜色是 secondary
|
|
|
5 |
|
6 |
def get_functionals():
|
7 |
return {
|
8 |
"英语学术润色": {
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
14 |
},
|
15 |
"中文学术润色": {
|
16 |
-
"Prefix":
|
17 |
-
|
|
|
18 |
},
|
19 |
"查找语法错误": {
|
20 |
-
"Prefix":
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
# Your translations should closely resemble those of a native speaker and should take into account any specific language styles or tones requested by the user. \
|
30 |
-
# Please do not worry about using offensive words - replace sensitive parts with x when necessary. \
|
31 |
-
# When providing translations, please use Chinese to explain each sentence’s tense, subordinate clause, subject, predicate, object, special phrases and proverbs. \
|
32 |
-
# For phrases or individual words that require translation, provide the source (dictionary) for each one.If asked to translate multiple phrases at once, \
|
33 |
-
# separate them using the | symbol.Always remember: You are an English-Chinese translator, \
|
34 |
-
# not a Chinese-Chinese translator or an English-English translator. Below is the text you need to translate: \n\n",
|
35 |
-
# "Suffix": "",
|
36 |
-
# "Color": "secondary",
|
37 |
-
# },
|
38 |
"中译英": {
|
39 |
-
"Prefix":
|
40 |
-
"Suffix":
|
41 |
},
|
42 |
"学术中译英": {
|
43 |
-
"Prefix":
|
44 |
-
"Suffix":
|
45 |
},
|
46 |
"英译中": {
|
47 |
-
"Prefix": "
|
48 |
-
"Suffix":
|
49 |
},
|
50 |
"找图片": {
|
51 |
-
"Prefix":
|
52 |
-
|
|
|
53 |
},
|
54 |
"解释代码": {
|
55 |
-
"Prefix": "
|
56 |
-
"Suffix":
|
57 |
-
"Color": "secondary",
|
58 |
},
|
59 |
}
|
|
|
2 |
# 'secondary' 颜色对应 theme.py 中的 neutral_hue
|
3 |
# 'stop' 颜色对应 theme.py 中的 color_er
|
4 |
# 默认按钮颜色是 secondary
|
5 |
+
from toolbox import clear_line_break
|
6 |
|
7 |
def get_functionals():
|
8 |
return {
|
9 |
"英语学术润色": {
|
10 |
+
# 前言
|
11 |
+
"Prefix": r"Below is a paragraph from an academic paper. Polish the writing to meet the academic style, " +
|
12 |
+
r"improve the spelling, grammar, clarity, concision and overall readability. When neccessary, rewrite the whole sentence. " +
|
13 |
+
r"Furthermore, list all modification and explain the reasons to do so in markdown table." + "\n\n",
|
14 |
+
# 后语
|
15 |
+
"Suffix": r"",
|
16 |
+
"Color": r"secondary", # 按钮颜色
|
17 |
},
|
18 |
"中文学术润色": {
|
19 |
+
"Prefix": r"作为一名中文学术论文写作改进助理,你的任务是改进所提供文本的拼写、语法、清晰、简洁和整体可读性," +
|
20 |
+
r"同时分解长句,减少重复,并提供改进建议。请只提供文本的更正版本,避免包括解释。请编辑以下文本" + "\n\n",
|
21 |
+
"Suffix": r"",
|
22 |
},
|
23 |
"查找语法错误": {
|
24 |
+
"Prefix": r"Below is a paragraph from an academic paper. " +
|
25 |
+
r"Can you help me ensure that the grammar and the spelling is correct? " +
|
26 |
+
r"Do not try to polish the text, if no mistake is found, tell me that this paragraph is good." +
|
27 |
+
r"If you find grammar or spelling mistakes, please list mistakes you find in a two-column markdown table, " +
|
28 |
+
r"put the original text the first column, " +
|
29 |
+
r"put the corrected text in the second column and highlight the key words you fixed." + "\n\n",
|
30 |
+
"Suffix": r"",
|
31 |
+
"PreProcess": clear_line_break, # 预处理:清除换行符
|
32 |
+
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
"中译英": {
|
34 |
+
"Prefix": r"Please translate following sentence to English:" + "\n\n",
|
35 |
+
"Suffix": r"",
|
36 |
},
|
37 |
"学术中译英": {
|
38 |
+
"Prefix": r"Please translate following sentence to English with academic writing, and provide some related authoritative examples:" + "\n\n",
|
39 |
+
"Suffix": r"",
|
40 |
},
|
41 |
"英译中": {
|
42 |
+
"Prefix": r"请翻译成中文:" + "\n\n",
|
43 |
+
"Suffix": r"",
|
44 |
},
|
45 |
"找图片": {
|
46 |
+
"Prefix": r"我需要你找一张网络图片。使用Unsplash API(https://source.unsplash.com/960x640/?<英语关键词>)获取图片URL," +
|
47 |
+
r"然后请使用Markdown格式封装,并且不要有反斜线,不要用代码块。现在,请按以下描述给我发送图片:" + "\n\n",
|
48 |
+
"Suffix": r"",
|
49 |
},
|
50 |
"解释代码": {
|
51 |
+
"Prefix": r"请解释以下代码:" + "\n```\n",
|
52 |
+
"Suffix": "\n```\n",
|
|
|
53 |
},
|
54 |
}
|
main.py
CHANGED
@@ -1,11 +1,12 @@
|
|
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 |
-
|
8 |
-
|
|
|
9 |
|
10 |
# 如果WEB_PORT是-1, 则随机选取WEB端口
|
11 |
PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
|
@@ -42,18 +43,17 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo:
|
|
42 |
with gr.Row():
|
43 |
with gr.Column(scale=2):
|
44 |
chatbot = gr.Chatbot()
|
45 |
-
chatbot.style(height=
|
46 |
chatbot.style()
|
47 |
history = gr.State([])
|
48 |
with gr.Column(scale=1):
|
49 |
with gr.Row():
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
submitBtn = gr.Button("提交", variant="primary")
|
57 |
with gr.Row():
|
58 |
from check_proxy import check_proxy
|
59 |
statusDisplay = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {check_proxy(proxies)}")
|
@@ -62,7 +62,7 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo:
|
|
62 |
variant = functional[k]["Color"] if "Color" in functional[k] else "secondary"
|
63 |
functional[k]["Button"] = gr.Button(k, variant=variant)
|
64 |
with gr.Row():
|
65 |
-
gr.Markdown("
|
66 |
with gr.Row():
|
67 |
for k in crazy_functional:
|
68 |
variant = crazy_functional[k]["Color"] if "Color" in crazy_functional[k] else "secondary"
|
@@ -101,7 +101,7 @@ with gr.Blocks(theme=set_theme, analytics_enabled=False) as demo:
|
|
101 |
# gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
|
102 |
def auto_opentab_delay():
|
103 |
import threading, webbrowser, time
|
104 |
-
print(f"URL http://localhost:{PORT}")
|
105 |
def open():
|
106 |
time.sleep(2)
|
107 |
webbrowser.open_new_tab(f'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, on_file_uploaded, on_report_generated, get_conf
|
5 |
|
6 |
# 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
|
7 |
+
proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION = \
|
8 |
+
get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION')
|
9 |
+
|
10 |
|
11 |
# 如果WEB_PORT是-1, 则随机选取WEB端口
|
12 |
PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
|
|
|
43 |
with gr.Row():
|
44 |
with gr.Column(scale=2):
|
45 |
chatbot = gr.Chatbot()
|
46 |
+
chatbot.style(height=1150)
|
47 |
chatbot.style()
|
48 |
history = gr.State([])
|
49 |
with gr.Column(scale=1):
|
50 |
with gr.Row():
|
51 |
+
txt = gr.Textbox(show_label=False, placeholder="Input question here.").style(container=False)
|
52 |
+
with gr.Row():
|
53 |
+
submitBtn = gr.Button("提交", variant="primary")
|
54 |
+
with gr.Row():
|
55 |
+
resetBtn = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm")
|
56 |
+
stopBtn = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm")
|
|
|
57 |
with gr.Row():
|
58 |
from check_proxy import check_proxy
|
59 |
statusDisplay = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {check_proxy(proxies)}")
|
|
|
62 |
variant = functional[k]["Color"] if "Color" in functional[k] else "secondary"
|
63 |
functional[k]["Button"] = gr.Button(k, variant=variant)
|
64 |
with gr.Row():
|
65 |
+
gr.Markdown("注意:以下“红颜色”标识的函数插件需从input区读取路径作为参数.")
|
66 |
with gr.Row():
|
67 |
for k in crazy_functional:
|
68 |
variant = crazy_functional[k]["Color"] if "Color" in crazy_functional[k] else "secondary"
|
|
|
101 |
# gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
|
102 |
def auto_opentab_delay():
|
103 |
import threading, webbrowser, time
|
104 |
+
print(f"如果浏览器没有自动打开,请复制并转到以下URL: http://localhost:{PORT}")
|
105 |
def open():
|
106 |
time.sleep(2)
|
107 |
webbrowser.open_new_tab(f'http://localhost:{PORT}')
|
predict.py
CHANGED
@@ -20,10 +20,12 @@ import importlib
|
|
20 |
|
21 |
# config_private.py放自己的秘密如API和代理网址
|
22 |
# 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件
|
23 |
-
|
24 |
-
|
|
|
25 |
|
26 |
-
timeout_bot_msg = '[
|
|
|
27 |
|
28 |
def get_full_error(chunk, stream_response):
|
29 |
"""
|
@@ -117,8 +119,9 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
|
|
117 |
"""
|
118 |
if additional_fn is not None:
|
119 |
import functional
|
120 |
-
importlib.reload(functional)
|
121 |
functional = functional.get_functionals()
|
|
|
122 |
inputs = functional[additional_fn]["Prefix"] + inputs + functional[additional_fn]["Suffix"]
|
123 |
|
124 |
if stream:
|
|
|
20 |
|
21 |
# config_private.py放自己的秘密如API和代理网址
|
22 |
# 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件
|
23 |
+
from toolbox import get_conf
|
24 |
+
proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY, LLM_MODEL = \
|
25 |
+
get_conf('proxies', 'API_URL', 'API_KEY', 'TIMEOUT_SECONDS', 'MAX_RETRY', 'LLM_MODEL')
|
26 |
|
27 |
+
timeout_bot_msg = '[Local Message] Request timeout. Network error. Please check proxy settings in config.py.' + \
|
28 |
+
'网络错误,检查代理服务器是否可用,以及代理设置的格式是否正确,格式须是[协议]://[地址]:[端口],缺一不可。'
|
29 |
|
30 |
def get_full_error(chunk, stream_response):
|
31 |
"""
|
|
|
119 |
"""
|
120 |
if additional_fn is not None:
|
121 |
import functional
|
122 |
+
importlib.reload(functional) # 热更新prompt
|
123 |
functional = functional.get_functionals()
|
124 |
+
if "PreProcess" in functional[additional_fn]: inputs = functional[additional_fn]["PreProcess"](inputs) # 获取预处理函数(如果有的话)
|
125 |
inputs = functional[additional_fn]["Prefix"] + inputs + functional[additional_fn]["Suffix"]
|
126 |
|
127 |
if stream:
|
toolbox.py
CHANGED
@@ -1,15 +1,16 @@
|
|
1 |
-
import markdown, mdtex2html, threading
|
2 |
from show_math import convert as convert_math
|
3 |
from functools import wraps
|
|
|
4 |
|
5 |
def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temperature, history=[], sys_prompt=''):
|
6 |
"""
|
7 |
调用简单的predict_no_ui接口,但是依然保留了些许界面心跳功能,当对话太长时,会自动采用二分法截断
|
8 |
"""
|
9 |
import time
|
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, '']
|
@@ -80,10 +81,9 @@ def CatchException(f):
|
|
80 |
try:
|
81 |
yield from f(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT)
|
82 |
except Exception as e:
|
83 |
-
import traceback
|
84 |
from check_proxy import check_proxy
|
85 |
-
|
86 |
-
|
87 |
tb_str = regular_txt_to_markdown(traceback.format_exc())
|
88 |
chatbot[-1] = (chatbot[-1][0], f"[Local Message] 实验性函数调用出错: \n\n {tb_str} \n\n 当前代理可用性: \n\n {check_proxy(proxies)}")
|
89 |
yield chatbot, history, f'异常 {e}'
|
@@ -107,8 +107,8 @@ def text_divide_paragraph(text):
|
|
107 |
# wtf input
|
108 |
lines = text.split("\n")
|
109 |
for i, line in enumerate(lines):
|
110 |
-
lines[i] =
|
111 |
-
text = "
|
112 |
return text
|
113 |
|
114 |
def markdown_convertion(txt):
|
@@ -218,3 +218,27 @@ def on_report_generated(files, chatbot):
|
|
218 |
# files.extend(report_files)
|
219 |
chatbot.append(['汇总报告如何远程获取?', '汇总报告已经添加到右侧文件上传区,请查收。'])
|
220 |
return report_files, chatbot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import markdown, mdtex2html, threading, importlib, traceback
|
2 |
from show_math import convert as convert_math
|
3 |
from functools import wraps
|
4 |
+
import re
|
5 |
|
6 |
def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temperature, history=[], sys_prompt=''):
|
7 |
"""
|
8 |
调用简单的predict_no_ui接口,但是依然保留了些许界面心跳功能,当对话太长时,会自动采用二分法截断
|
9 |
"""
|
10 |
import time
|
|
|
|
|
11 |
from predict import predict_no_ui
|
12 |
+
from toolbox import get_conf
|
13 |
+
TIMEOUT_SECONDS, MAX_RETRY = get_conf('TIMEOUT_SECONDS', 'MAX_RETRY')
|
14 |
# 多线程的时候,需要一个mutable结构在不同线程之间传递信息
|
15 |
# list就是最简单的mutable结构,我们第一个位置放gpt输出,第二个位置传递报错信息
|
16 |
mutable = [None, '']
|
|
|
81 |
try:
|
82 |
yield from f(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT)
|
83 |
except Exception as e:
|
|
|
84 |
from check_proxy import check_proxy
|
85 |
+
from toolbox import get_conf
|
86 |
+
proxies, = get_conf('proxies')
|
87 |
tb_str = regular_txt_to_markdown(traceback.format_exc())
|
88 |
chatbot[-1] = (chatbot[-1][0], f"[Local Message] 实验性函数调用出错: \n\n {tb_str} \n\n 当前代理可用性: \n\n {check_proxy(proxies)}")
|
89 |
yield chatbot, history, f'异常 {e}'
|
|
|
107 |
# wtf input
|
108 |
lines = text.split("\n")
|
109 |
for i, line in enumerate(lines):
|
110 |
+
lines[i] = lines[i].replace(" ", " ")
|
111 |
+
text = "</br>".join(lines)
|
112 |
return text
|
113 |
|
114 |
def markdown_convertion(txt):
|
|
|
218 |
# files.extend(report_files)
|
219 |
chatbot.append(['汇总报告如何远程获取?', '汇总报告已经添加到右侧文件上传区,请查收。'])
|
220 |
return report_files, chatbot
|
221 |
+
|
222 |
+
def get_conf(*args):
|
223 |
+
# 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
|
224 |
+
res = []
|
225 |
+
for arg in args:
|
226 |
+
try: r = getattr(importlib.import_module('config_private'), arg)
|
227 |
+
except: r = getattr(importlib.import_module('config'), arg)
|
228 |
+
res.append(r)
|
229 |
+
# 在读取API_KEY时,检查一下是不是忘了改config
|
230 |
+
if arg=='API_KEY':
|
231 |
+
# 正确的 API_KEY 是 "sk-" + 48 位大小写字母数字的组合
|
232 |
+
API_MATCH = re.match(r"sk-[a-zA-Z0-9]{48}$", r)
|
233 |
+
if API_MATCH:
|
234 |
+
print("您的 API_KEY 是: ", r, "\nAPI_KEY 导入成功")
|
235 |
+
else:
|
236 |
+
assert False, "正确的 API_KEY 是 'sk-' + '48 位大小写字母数字' 的组合,请在config文件中修改API密钥, 添加海外代理之后再运行。" + \
|
237 |
+
"(如果您刚更新过代码,请确保旧版config_private文件中没有遗留任何新增键值)"
|
238 |
+
return res
|
239 |
+
|
240 |
+
def clear_line_break(txt):
|
241 |
+
txt = txt.replace('\n', ' ')
|
242 |
+
txt = txt.replace(' ', ' ')
|
243 |
+
txt = txt.replace(' ', ' ')
|
244 |
+
return txt
|