fb700 commited on
Commit
f775275
1 Parent(s): de6de7f

Upload 6 files

Browse files
Files changed (6) hide show
  1. .flake8 +21 -0
  2. .gitattributes +35 -5
  3. .gitignore +0 -152
  4. README.md +6 -340
  5. app.py +385 -214
  6. requirements.txt +5 -20
.flake8 ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [flake8]
2
+ ignore =
3
+ # E203 whitespace before ':'
4
+ E203
5
+ D203,
6
+ # line too long
7
+ E501
8
+ per-file-ignores =
9
+ # imported but unused
10
+ # __init__.py: F401
11
+ test_*.py: F401
12
+ exclude =
13
+ .git,
14
+ __pycache__,
15
+ docs/source/conf.py,
16
+ old,
17
+ build,
18
+ dist,
19
+ .venv
20
+ pad*.py
21
+ max-complexity = 25
.gitattributes CHANGED
@@ -1,5 +1,35 @@
1
- *.h linguist-detectable=false
2
- *.cpp linguist-detectable=false
3
- *.tex linguist-detectable=false
4
- *.cs linguist-detectable=false
5
- *.tps linguist-detectable=false
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore CHANGED
@@ -1,152 +0,0 @@
1
- # Byte-compiled / optimized / DLL files
2
- __pycache__/
3
- *.py[cod]
4
- *$py.class
5
-
6
- # C extensions
7
- *.so
8
-
9
- # Distribution / packaging
10
- .Python
11
- build/
12
- develop-eggs/
13
- dist/
14
- downloads/
15
- eggs/
16
- .eggs/
17
- lib/
18
- lib64/
19
- parts/
20
- sdist/
21
- var/
22
- wheels/
23
- pip-wheel-metadata/
24
- share/python-wheels/
25
- *.egg-info/
26
- .installed.cfg
27
- *.egg
28
- MANIFEST
29
-
30
- # PyInstaller
31
- # Usually these files are written by a python script from a template
32
- # before PyInstaller builds the exe, so as to inject date/other infos into it.
33
- *.manifest
34
- *.spec
35
- # Installer logs
36
- pip-log.txt
37
- pip-delete-this-directory.txt
38
-
39
- # Unit test / coverage reports
40
- htmlcov/
41
- .tox/
42
- .nox/
43
- .coverage
44
- .coverage.*
45
- .cache
46
- nosetests.xml
47
- coverage.xml
48
- *.cover
49
- *.py,cover
50
- .hypothesis/
51
- .pytest_cache/
52
-
53
- # Translations
54
- *.mo
55
- *.pot
56
- github
57
- .github
58
- TEMP
59
- TRASH
60
-
61
- # Django stuff:
62
- *.log
63
- local_settings.py
64
- db.sqlite3
65
- db.sqlite3-journal
66
-
67
- # Flask stuff:
68
- instance/
69
- .webassets-cache
70
-
71
- # Scrapy stuff:
72
- .scrapy
73
-
74
- # Sphinx documentation
75
- docs/_build/
76
-
77
- # PyBuilder
78
- target/
79
-
80
- # Jupyter Notebook
81
- .ipynb_checkpoints
82
-
83
- # IPython
84
- profile_default/
85
- ipython_config.py
86
-
87
- # pyenv
88
- .python-version
89
-
90
- # pipenv
91
- # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
- # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
- # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
- # install all needed dependencies.
95
- #Pipfile.lock
96
-
97
- # PEP 582; used by e.g. github.com/David-OConnor/pyflow
98
- __pypackages__/
99
-
100
- # Celery stuff
101
- celerybeat-schedule
102
- celerybeat.pid
103
-
104
- # SageMath parsed files
105
- *.sage.py
106
-
107
- # Environments
108
- .env
109
- .venv
110
- env/
111
- venv/
112
- ENV/
113
- env.bak/
114
- venv.bak/
115
-
116
- # Spyder project settings
117
- .spyderproject
118
- .spyproject
119
-
120
- # Rope project settings
121
- .ropeproject
122
-
123
- # mkdocs documentation
124
- /site
125
-
126
- # mypy
127
- .mypy_cache/
128
- .dmypy.json
129
- dmypy.json
130
-
131
- # Pyre type checker
132
- .pyre/
133
-
134
- .vscode
135
- .idea
136
-
137
- history
138
- ssr_conf
139
- config_private.py
140
- gpt_log
141
- private.md
142
- private_upload
143
- other_llms
144
- cradle*
145
- debug*
146
- private*
147
- crazy_functions/test_project/pdf_and_word
148
- crazy_functions/test_samples
149
- request_llm/jittorllms
150
- multi-language
151
- request_llm/moss
152
- media
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -1,346 +1,12 @@
1
  ---
2
- title: ChatImprovement
3
- emoji: 😻
4
- colorFrom: blue
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 3.32.0
8
  app_file: app.py
9
- pinned: false
10
  ---
11
 
12
- # ChatGPT 学术优化
13
- > **Note**
14
- >
15
- > 2023.5.27 对Gradio依赖进行了调整,Fork并解决了官方Gradio的若干Bugs。请及时**更新代码**并重新更新pip依赖。安装依赖时,请严格选择`requirements.txt`中**指定的版本**:
16
- >
17
- > `pip install -r requirements.txt`
18
- >
19
-
20
- # <img src="docs/logo.png" width="40" > GPT 学术优化 (GPT Academic)
21
-
22
- **如果喜欢这个项目,请给它一个Star;如果你发明了更好用的快捷键或函数插件,欢迎发pull requests**
23
-
24
- If you like this project, please give it a Star. If you've come up with more useful academic shortcuts or functional plugins, feel free to open an issue or pull request. We also have a README in [English|](docs/README_EN.md)[日本語|](docs/README_JP.md)[한국어|](https://github.com/mldljyh/ko_gpt_academic)[Русский|](docs/README_RS.md)[Français](docs/README_FR.md) translated by this project itself.
25
- To translate this project to arbitary language with GPT, read and run [`multi_language.py`](multi_language.py) (experimental).
26
-
27
- > **Note**
28
- >
29
- > 1.请注意只有**红颜色**标识的函数插件(按钮)才支持读取文件,部分插件位于插件区的**下拉菜单**中。另外我们以**最高优先级**欢迎和处理任何新插件的PR!
30
- >
31
- > 2.本项目中每个文件的功能都在自译解[`self_analysis.md`](https://github.com/binary-husky/gpt_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/gpt_academic/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98)当中。[安装方法](#installation)。
32
- >
33
- > 3.本项目兼容并鼓励尝试国产大语言模型chatglm和RWKV, 盘古等等。支持多个api-key共存,可在配置文件中填写如`API_KEY="openai-key1,openai-key2,api2d-key3"`。需要临时更换`API_KEY`时,在输入区输入临时的`API_KEY`然后回车键提交后即可生效。
34
-
35
-
36
-
37
-
38
- <div align="center">
39
-
40
- 功能 | 描述
41
- --- | ---
42
- 一键润色 | 支持一键润色、一键查找论文语法错误
43
- 一键中英互译 | 一键中英互译
44
- 一键代码解释 | 显示代码、解释代码、生成代码、给代码加注释
45
- [自定义快捷键](https://www.bilibili.com/video/BV14s4y1E7jN) | 支持自定义快捷键
46
- 模块化设计 | 支持自定义强大的[函数插件](https://github.com/binary-husky/gpt_academic/tree/master/crazy_functions),插件支持[热更新](https://github.com/binary-husky/gpt_academic/wiki/%E5%87%BD%E6%95%B0%E6%8F%92%E4%BB%B6%E6%8C%87%E5%8D%97)
47
- [自我程序剖析](https://www.bilibili.com/video/BV1cj411A7VW) | [函数插件] [一键读懂](https://github.com/binary-husky/gpt_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)本项目的源代码
48
- [程序剖析](https://www.bilibili.com/video/BV1cj411A7VW) | [函数插件] 一键可以剖析其他Python/C/C++/Java/Lua/...项目树
49
- 读论文、[翻译](https://www.bilibili.com/video/BV1KT411x7Wn)论文 | [函数插件] 一键解读latex/pdf论文全文并生成摘要
50
- Latex全文[翻译](https://www.bilibili.com/video/BV1nk4y1Y7Js/)、[润色](https://www.bilibili.com/video/BV1FT411H7c5/) | [函数插件] 一键翻译或润色latex论文
51
- 批量注释生成 | [函数插件] 一键批量生成函数注释
52
- Markdown[中英互译](https://www.bilibili.com/video/BV1yo4y157jV/) | [函数插件] 看到上面5种语言的[README](https://github.com/binary-husky/gpt_academic/blob/master/docs/README_EN.md)了吗?
53
- chat分析报告生成 | [函数插件] 运行后自动生成总结汇报
54
- [PDF论文全文翻译功能](https://www.bilibili.com/video/BV1KT411x7Wn) | [函数插件] PDF论文提取题目&摘要+翻译全文(多线程)
55
- [Arxiv小助手](https://www.bilibili.com/video/BV1LM4y1279X) | [函数插件] 输入arxiv文章url即可一键翻译摘要+下载PDF
56
- [谷歌学术统合小助手](https://www.bilibili.com/video/BV19L411U7ia) | [函数插件] 给定任意谷歌学术搜索页面URL,让gpt帮你[写relatedworks](https://www.bilibili.com/video/BV1GP411U7Az/)
57
- 互联网信息聚合+GPT | [函数插件] 一键[让GPT先从互联网获取信息](https://www.bilibili.com/video/BV1om4y127ck),再回答问题,让信息永不过时
58
- ⭐Arxiv论文精细翻译 | [函数插件] 一键[以超高质量翻译arxiv论文](https://www.bilibili.com/video/BV1dz4y1v77A/),迄今为止最好的论文翻译工具⭐
59
- 公式/图片/表格显示 | 可以同时显示公式的[tex形式和渲染形式](https://user-images.githubusercontent.com/96192199/230598842-1d7fcddd-815d-40ee-af60-baf488a199df.png),支持公式、代码高亮
60
- 多线程函数插件支持 | 支持多线调用chatgpt,一键处理[海量文本](https://www.bilibili.com/video/BV1FT411H7c5/)或程序
61
- 启动暗色gradio[主题](https://github.com/binary-husky/gpt_academic/issues/173) | 在浏览器url后面添加```/?__theme=dark```可以切换dark主题
62
- [多LLM模型](https://www.bilibili.com/video/BV1wT411p7yf)支持 | 同时被GPT3.5、GPT4、[清华ChatGLM](https://github.com/THUDM/ChatGLM-6B)、[复旦MOSS](https://github.com/OpenLMLab/MOSS)同时伺候的感觉一定会很不错吧?
63
- 更多LLM模型接入,支持[huggingface部署](https://huggingface.co/spaces/qingxu98/gpt-academic) | 加入Newbing接口(新必应),引入清华[Jittorllms](https://github.com/Jittor/JittorLLMs)支持[LLaMA](https://github.com/facebookresearch/llama),[RWKV](https://github.com/BlinkDL/ChatRWKV)和[盘古α](https://openi.org.cn/pangu/)
64
- 更多新功能展示(图像生成等) …… | 见本文档结尾处 ……
65
-
66
- </div>
67
-
68
-
69
- - 新界面(修改`config.py`中的LAYOUT选项即可实现“左右布局”和“上下布局”的切换)
70
- <div align="center">
71
- <img src="https://user-images.githubusercontent.com/96192199/230361456-61078362-a966-4eb5-b49e-3c62ef18b860.gif" width="700" >
72
- </div>
73
-
74
-
75
- - 所有按钮都通过读取functional.py动态生成,可随意加自定义功能,解放粘贴板
76
- <div align="center">
77
- <img src="https://user-images.githubusercontent.com/96192199/231975334-b4788e91-4887-412f-8b43-2b9c5f41d248.gif" width="700" >
78
- </div>
79
-
80
- - 润色/纠错
81
- <div align="center">
82
- <img src="https://user-images.githubusercontent.com/96192199/231980294-f374bdcb-3309-4560-b424-38ef39f04ebd.gif" width="700" >
83
- </div>
84
-
85
- - 如果输出包含公式,会同时以tex形式和渲染形式显示,方便复制和阅读
86
- <div align="center">
87
- <img src="https://user-images.githubusercontent.com/96192199/230598842-1d7fcddd-815d-40ee-af60-baf488a199df.png" width="700" >
88
- </div>
89
-
90
- - 懒得看项目代码?整个工程直接给chatgpt炫嘴里
91
- <div align="center">
92
- <img src="https://user-images.githubusercontent.com/96192199/226935232-6b6a73ce-8900-4aee-93f9-733c7e6fef53.png" width="700" >
93
- </div>
94
-
95
- - 多种大语言模型混合调用(ChatGLM + OpenAI-GPT3.5 + [API2D](https://api2d.com/)-GPT4)
96
- <div align="center">
97
- <img src="https://user-images.githubusercontent.com/96192199/232537274-deca0563-7aa6-4b5d-94a2-b7c453c47794.png" width="700" >
98
- </div>
99
-
100
- ---
101
- # Installation
102
- ## 安装-方法1:直接运行 (Windows, Linux or MacOS)
103
-
104
- 1. 下载项目
105
- ```sh
106
- git clone https://github.com/binary-husky/gpt_academic.git
107
- cd gpt_academic
108
- ```
109
-
110
- 2. 配置API_KEY
111
-
112
- 在`config.py`中,配置API KEY等设置,[点击查看特殊网络环境设置方法](https://github.com/binary-husky/gpt_academic/issues/1) 。
113
-
114
- (P.S. 程序运行时会优先检查是否存在名为`config_private.py`的私密配置文件,并用其中的配置覆盖`config.py`的同名配置。因此,如果您能理解我们的配置读取逻辑,我们强烈建议您在`config.py`旁边创建一个名为`config_private.py`的新配置文件,并把`config.py`中的配置转移(复制)到`config_private.py`中。`config_private.py`不受git管控,可以让您的隐私信息更加安全。P.S.项目同样支持通过`环境变量`配置大多数选项,环境变量的书写格式参考`docker-compose`文件。读取优先级: `环境变量` > `config_private.py` > `config.py`)
115
-
116
-
117
- 3. 安装依赖
118
- ```sh
119
- # (选择I: 如熟悉python)(python版本3.9以上,越新越好),备注:使用官方pip源或者阿里pip源,临时换源方法:python -m pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
120
- python -m pip install -r requirements.txt
121
-
122
- # (选择II: 如不熟悉python)使用anaconda,步骤也是类似的 (https://www.bilibili.com/video/BV1rc411W7Dr):
123
- conda create -n gptac_venv python=3.11 # 创建anaconda环境
124
- conda activate gptac_venv # 激活anaconda环境
125
- python -m pip install -r requirements.txt # 这个步骤和pip安装一样的步骤
126
- ```
127
-
128
-
129
- <details><summary>如果需要支持清华ChatGLM/复旦MOSS作为后端,请点击展开此处</summary>
130
- <p>
131
-
132
- 【可选步骤】如果需要支持清华ChatGLM/复旦MOSS作为后端,需要额外安装更多依赖(前提条件:熟悉Python + 用过Pytorch + 电脑配置够强):
133
- ```sh
134
- # 【可选步骤I】支持清华ChatGLM。清华ChatGLM备注:如果遇到"Call ChatGLM fail 不能正常加载ChatGLM的参数" 错误,参考如下: 1:以上默认安装的为torch+cpu版,使用cuda需要卸载torch重新安装torch+cuda; 2:如因本机配置不够无法加载模型,可以修改request_llm/bridge_chatglm.py中的模型精度, 将 AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True) 都修改为 AutoTokenizer.from_pretrained("THUDM/chatglm-6b-int4", trust_remote_code=True)
135
- python -m pip install -r request_llm/requirements_chatglm.txt
136
-
137
- # 【可选步骤II】支持复旦MOSS
138
- python -m pip install -r request_llm/requirements_moss.txt
139
- git clone https://github.com/OpenLMLab/MOSS.git request_llm/moss # 注意执行此行代码时,必须处于项目根路径
140
-
141
- # 【可选步骤III】确保config.py配置文件的AVAIL_LLM_MODELS包含了期望的模型,目前支持的全部模型如下(jittorllms系列目前仅支持docker方案):
142
- AVAIL_LLM_MODELS = ["gpt-3.5-turbo", "api2d-gpt-3.5-turbo", "gpt-4", "api2d-gpt-4", "chatglm", "newbing", "moss"] # + ["jittorllms_rwkv", "jittorllms_pangualpha", "jittorllms_llama"]
143
- ```
144
-
145
- </p>
146
- </details>
147
-
148
-
149
-
150
- 4. 运行
151
- ```sh
152
- python main.py
153
- ```
154
-
155
- ## 安装-方法2:使用Docker
156
-
157
- 1. 仅ChatGPT(推荐大多数人选择,等价于docker-compose方案1)
158
-
159
- ``` sh
160
- git clone https://github.com/binary-husky/gpt_academic.git # 下载项目
161
- cd gpt_academic # 进入路径
162
- nano config.py # 用任意文本编辑器编辑config.py, 配置 “Proxy”, “API_KEY” 以及 “WEB_PORT” (例如50923) 等
163
- docker build -t gpt-academic . # 安装
164
-
165
- #(最后一步-选择1)在Linux环境下,用`--net=host`更方便快捷
166
- docker run --rm -it --net=host gpt-academic
167
- #(最后一步-选择2)在macOS/windows环境下,只能用-p选项将容器上的端口(例如50923)暴露给主机上的端口
168
- docker run --rm -it -e WEB_PORT=50923 -p 50923:50923 gpt-academic
169
- ```
170
- P.S. 如果需要依赖Latex的插件功能,请见Wiki。另外,您也可以直接使用docker-compose获取Latex功能(修改docker-compose.yml,保留方案4并删除其他方案)。
171
-
172
- 2. ChatGPT + ChatGLM + MOSS(需要熟悉Docker)
173
-
174
- ``` sh
175
- # 修改docker-compose.yml,保留方案2并删除其他方案。修改docker-compose.yml中方案2的配置,参考其中注释即可
176
- docker-compose up
177
- ```
178
-
179
- 3. ChatGPT + LLAMA + 盘古 + RWKV(需要熟悉Docker)
180
- ``` sh
181
- # 修改docker-compose.yml,保留方案3并删除其他方案。修改docker-compose.yml中方案3的配置,参考其中注释即可
182
- docker-compose up
183
- ```
184
-
185
-
186
- ## 安装-方法3:其他部署姿势
187
- 1. 一键运行脚本。
188
- 完全不熟悉python环境的Windows用户可以下载[Release](https://github.com/binary-husky/gpt_academic/releases)中发布的一键运行脚本安装无本地模型的版本。
189
- 脚本的贡献来源是[oobabooga](https://github.com/oobabooga/one-click-installers)。
190
-
191
- 2. 使用docker-compose运行。
192
- 请阅读docker-compose.yml后,按照其中的提示操作即可
193
-
194
- 3. 如何使用反代URL
195
- 按照`config.py`中的说明配置API_URL_REDIRECT即可。
196
-
197
- 4. 微软云AzureAPI
198
- 按照`config.py`中的说明配置即可(AZURE_ENDPOINT等四个配置)
199
-
200
- 5. 远程云服务器部署(需要云服务器知识与经验)。
201
- 请访问[部署wiki-1](https://github.com/binary-husky/gpt_academic/wiki/%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%9C%E7%A8%8B%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97)
202
-
203
- 6. 使用WSL2(Windows Subsystem for Linux 子系统)。
204
- 请访问[部署wiki-2](https://github.com/binary-husky/gpt_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)
205
-
206
- 7. 如何在二级网址(如`http://localhost/subpath`)下运行。
207
- 请访问[FastAPI运行说明](docs/WithFastapi.md)
208
-
209
- ---
210
- # Advanced Usage
211
- ## 自定义新的便捷按钮 / 自定义函数插件
212
-
213
- 1. 自定义新的便捷按钮(学术快捷键)
214
- 任意文本编辑器打开`core_functional.py`,添加条目如下,然后重启程序即可。(如果按钮已经添加成功并可见,那么前缀、后缀都支持热修改,无需重启程序即可生效。)
215
- 例如
216
- ```
217
- "超级英译中": {
218
- # 前缀,会被加在你的输入之前。例如,用来描述你的要求,例如翻译、解释代码、润色等等
219
- "Prefix": "请翻译把下面一段内容成中文,然后用一个markdown表格逐一解释文中出现的专有名词:\n\n",
220
-
221
- # 后缀,会被加在你的输入之后。例如,配合前缀可以把你的输入内容用引号圈起来。
222
- "Suffix": "",
223
- },
224
- ```
225
- <div align="center">
226
- <img src="https://user-images.githubusercontent.com/96192199/226899272-477c2134-ed71-4326-810c-29891fe4a508.png" width="500" >
227
- </div>
228
-
229
- 2. 自定义函数插件
230
-
231
- 编写强大的函数插件来执行任何你想得到的和想不到的任务。
232
- 本项目的插件编写、调试难度很低,只要您具备一定的python基础知识,就可以仿照我们提供的模板实现自己的插件功能。
233
- 详情请参考[函数插件指南](https://github.com/binary-husky/gpt_academic/wiki/%E5%87%BD%E6%95%B0%E6%8F%92%E4%BB%B6%E6%8C%87%E5%8D%97)。
234
-
235
- ---
236
- # Latest Update
237
- ## 新功能动态
238
-
239
- 1. 对话保存功能。在函数插件区调用 `保存当前的对话` 即可将当前对话保存为可读+可复原的html文件,
240
- 另外在函数插件区(下拉菜单)调用 `载入对话历史存档` ,即可还原之前的会话。
241
- Tip:不指定文件直接点击 `载入对话历史存档` 可以查看历史html存档缓存。
242
- <div align="center">
243
- <img src="https://user-images.githubusercontent.com/96192199/235222390-24a9acc0-680f-49f5-bc81-2f3161f1e049.png" width="500" >
244
- </div>
245
-
246
- 2. ⭐Latex/Arxiv论文翻译功能⭐
247
- <div align="center">
248
- <img src="https://github.com/binary-husky/gpt_academic/assets/96192199/002a1a75-ace0-4e6a-94e2-ec1406a746f1" height="250" > ===>
249
- <img src="https://github.com/binary-husky/gpt_academic/assets/96192199/9fdcc391-f823-464f-9322-f8719677043b" height="250" >
250
- </div>
251
-
252
- 3. 生成报告。大部分插件都会在执行结束后,生成工作报告
253
- <div align="center">
254
- <img src="https://user-images.githubusercontent.com/96192199/227503770-fe29ce2c-53fd-47b0-b0ff-93805f0c2ff4.png" height="250" >
255
- <img src="https://user-images.githubusercontent.com/96192199/227504617-7a497bb3-0a2a-4b50-9a8a-95ae60ea7afd.png" height="250" >
256
- </div>
257
-
258
- 4. 模块化功能设计,简单的接口却能支持强大的功能
259
- <div align="center">
260
- <img src="https://user-images.githubusercontent.com/96192199/229288270-093643c1-0018-487a-81e6-1d7809b6e90f.png" height="400" >
261
- <img src="https://user-images.githubusercontent.com/96192199/227504931-19955f78-45cd-4d1c-adac-e71e50957915.png" height="400" >
262
- </div>
263
-
264
- 5. 译解其他开源项目
265
- <div align="center">
266
- <img src="https://user-images.githubusercontent.com/96192199/226935232-6b6a73ce-8900-4aee-93f9-733c7e6fef53.png" height="250" >
267
- <img src="https://user-images.githubusercontent.com/96192199/226969067-968a27c1-1b9c-486b-8b81-ab2de8d3f88a.png" height="250" >
268
- </div>
269
-
270
- 6. 装饰[live2d](https://github.com/fghrsh/live2d_demo)的小功能(默认关闭,需要修改`config.py`)
271
- <div align="center">
272
- <img src="https://user-images.githubusercontent.com/96192199/236432361-67739153-73e8-43fe-8111-b61296edabd9.png" width="500" >
273
- </div>
274
-
275
- 7. 新增MOSS大语言模型支持
276
- <div align="center">
277
- <img src="https://user-images.githubusercontent.com/96192199/236639178-92836f37-13af-4fdd-984d-b4450fe30336.png" width="500" >
278
- </div>
279
-
280
- 8. OpenAI图像生成
281
- <div align="center">
282
- <img src="https://github.com/binary-husky/gpt_academic/assets/96192199/bc7ab234-ad90-48a0-8d62-f703d9e74665" width="500" >
283
- </div>
284
-
285
- 9. OpenAI音频解析与总结
286
- <div align="center">
287
- <img src="https://github.com/binary-husky/gpt_academic/assets/96192199/709ccf95-3aee-498a-934a-e1c22d3d5d5b" width="500" >
288
- </div>
289
-
290
- 10. Latex全文校对纠错
291
- <div align="center">
292
- <img src="https://github.com/binary-husky/gpt_academic/assets/96192199/651ccd98-02c9-4464-91e1-77a6b7d1b033" height="200" > ===>
293
- <img src="https://github.com/binary-husky/gpt_academic/assets/96192199/476f66d9-7716-4537-b5c1-735372c25adb" height="200">
294
- </div>
295
-
296
-
297
-
298
- ## 版本:
299
- - version 3.5(Todo): 使用自然语言调用本项目的所有函数插件(高优先级)
300
- - version 3.4: +arxiv论文翻译、latex论文批改功能
301
- - version 3.3: +互联网信息综合功能
302
- - version 3.2: 函数插件支持更多参数接口 (保存对话功能, 解读任意语言代码+同时询问任意的LLM组合)
303
- - version 3.1: 支持同时问询多个gpt模型!支持api2d,支持多个apikey负载均衡
304
- - version 3.0: 对chatglm和其他小型llm的支持
305
- - version 2.6: 重构了插件结构,提高了交互性,加入更多插件
306
- - version 2.5: 自更新,解决总结大工程源代码时文本过长、token溢出的问题
307
- - version 2.4: (1)新增PDF全文翻译功能; (2)新增输入区切换位置的功能; (3)新增垂直布局选项; (4)多线程函数插件优化。
308
- - version 2.3: 增强多线程交互性
309
- - version 2.2: 函数插件支持热重载
310
- - version 2.1: 可折叠式布局
311
- - version 2.0: 引入模块化函数插件
312
- - version 1.0: 基础功能
313
-
314
- gpt_academic开发者QQ群-2:610599535
315
-
316
- - 已知问题
317
- - 某些浏览器翻译插件干扰此软件前端的运行
318
- - 官方Gradio目前有很多兼容性Bug,请务必使用`requirement.txt`安装Gradio
319
-
320
- ## 参考与学习
321
-
322
- ```
323
- 代码中参考了很多其他优秀项目中的设计,顺序不分先后:
324
-
325
- # 清华ChatGLM-6B:
326
- https://github.com/THUDM/ChatGLM-6B
327
-
328
- # 清华JittorLLMs:
329
- https://github.com/Jittor/JittorLLMs
330
-
331
- # ChatPaper:
332
- https://github.com/kaixindelele/ChatPaper
333
-
334
- # Edge-GPT:
335
- https://github.com/acheong08/EdgeGPT
336
-
337
- # ChuanhuChatGPT:
338
- https://github.com/GaiZhenbiao/ChuanhuChatGPT
339
-
340
- # Oobabooga one-click installer:
341
- https://github.com/oobabooga/one-click-installers
342
-
343
- # More:
344
- https://github.com/gradio-app/gradio
345
- https://github.com/fghrsh/live2d_demo
346
- ```
 
1
  ---
2
+ title: chatglm2 6b int4
3
+ emoji: 🌖
4
+ colorFrom: purple
5
  colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 3.35.2
8
  app_file: app.py
9
+ pinned: true
10
  ---
11
 
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py CHANGED
@@ -1,215 +1,386 @@
1
- import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
2
-
3
- def main():
4
- import subprocess, sys
5
- subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'gradio-stable-fork'])
6
- import gradio as gr
7
- if gr.__version__ not in ['3.28.3','3.32.3']: assert False, "请用 pip install -r requirements.txt 安装依赖"
8
- from request_llm.bridge_all import predict
9
- from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, DummyWith
10
- # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
11
- proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION, CHATBOT_HEIGHT, LAYOUT, API_KEY, AVAIL_LLM_MODELS = \
12
- get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION', 'CHATBOT_HEIGHT', 'LAYOUT', 'API_KEY', 'AVAIL_LLM_MODELS')
13
-
14
- # 如果WEB_PORT是-1, 则随机选取WEB端口
15
- PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
16
- if not AUTHENTICATION: AUTHENTICATION = None
17
-
18
- from check_proxy import get_current_version
19
- initial_prompt = "Serve me as a writing and programming assistant."
20
- title_html = f'<h1 align=\"center\">ChatGPT 学术优化 {get_current_version()}<a href="https://huggingface.co/fb700/chatglm-fitness-RLHF">(模型by帛凡Ai)</a></h1>'
21
- description = """代码开源和更新[地址🚀](https://github.com/binary-husky/chatgpt_academic),感谢热情的[开发者们❤️](https://github.com/binary-husky/chatgpt_academic/graphs/contributors)"""
22
-
23
- # 问询记录, python 版本建议3.9+(越新越好)
24
- import logging
25
- os.makedirs("gpt_log", exist_ok=True)
26
- try:logging.basicConfig(filename="gpt_log/chat_secrets.log", level=logging.INFO, encoding="utf-8")
27
- except:logging.basicConfig(filename="gpt_log/chat_secrets.log", level=logging.INFO)
28
- print("所有问询记录将自动保存在本地目录./gpt_log/chat_secrets.log, 请注意自我隐私保护哦!")
29
-
30
- # 一些普通功能模块
31
- from core_functional import get_core_functions
32
- functional = get_core_functions()
33
-
34
- # 高级函数插件
35
- from crazy_functional import get_crazy_functions
36
- crazy_fns = get_crazy_functions()
37
-
38
- # 处理markdown文本格式的转变
39
- gr.Chatbot.postprocess = format_io
40
-
41
- # 做一些外观色彩上的调整
42
- from theme import adjust_theme, advanced_css
43
- set_theme = adjust_theme()
44
-
45
- # 代理与自动更新
46
- from check_proxy import check_proxy, auto_update, warm_up_modules
47
- proxy_info = check_proxy(proxies)
48
-
49
- gr_L1 = lambda: gr.Row().style()
50
- gr_L2 = lambda scale: gr.Column(scale=scale)
51
- if LAYOUT == "TOP-DOWN":
52
- gr_L1 = lambda: DummyWith()
53
- gr_L2 = lambda scale: gr.Row()
54
- CHATBOT_HEIGHT /= 2
55
-
56
- cancel_handles = []
57
- with gr.Blocks(title="ChatGPT 学术优化", theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
58
- gr.HTML(title_html)
59
- gr.HTML('''<center>本应用基于chatglm6b【帛凡 AI】的微调模型进行部署,模型中文总结能力优于GPT3.5,欢迎体验<a href="https://huggingface.co/fb700/chatglm-fitness-RLHF">下载地址</a></center>''')
60
- cookies = gr.State({'api_key': API_KEY, 'llm_model': LLM_MODEL})
61
- with gr_L1():
62
- with gr_L2(scale=2):
63
- chatbot = gr.Chatbot(label=f"当前模型:{LLM_MODEL}")
64
- chatbot.style(height=CHATBOT_HEIGHT)
65
- history = gr.State([])
66
- with gr_L2(scale=1):
67
- with gr.Accordion("输入区", open=True) as area_input_primary:
68
- with gr.Row():
69
- txt = gr.Textbox(show_label=False, lines=2, placeholder="输入问题或API密钥,输入多个密钥时,用英文逗号间隔。支持OpenAI密钥和API2D密钥共存。").style(container=False)
70
- with gr.Row():
71
- submitBtn = gr.Button("提交", variant="primary")
72
- with gr.Row():
73
- resetBtn = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm")
74
- stopBtn = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm")
75
- clearBtn = gr.Button("清除", variant="secondary", visible=False); clearBtn.style(size="sm")
76
- with gr.Row():
77
- status = gr.Markdown(f"Tip: 按Enter提交, 按Shift+Enter换行。当前模型: {LLM_MODEL} \n {proxy_info}")
78
- with gr.Accordion("基础功能区", open=True) as area_basic_fn:
79
- with gr.Row():
80
- for k in functional:
81
- if ("Visible" in functional[k]) and (not functional[k]["Visible"]): continue
82
- variant = functional[k]["Color"] if "Color" in functional[k] else "secondary"
83
- functional[k]["Button"] = gr.Button(k, variant=variant)
84
- with gr.Accordion("函数插件区", open=True) as area_crazy_fn:
85
- with gr.Row():
86
- gr.Markdown("注意:以下“红颜色”标识的函数插件需从输入区读取路径作为参数.")
87
- with gr.Row():
88
- for k in crazy_fns:
89
- if not crazy_fns[k].get("AsButton", True): continue
90
- variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
91
- crazy_fns[k]["Button"] = gr.Button(k, variant=variant)
92
- crazy_fns[k]["Button"].style(size="sm")
93
- with gr.Row():
94
- with gr.Accordion("更多函数插件", open=True):
95
- dropdown_fn_list = [k for k in crazy_fns.keys() if not crazy_fns[k].get("AsButton", True)]
96
- with gr.Row():
97
- dropdown = gr.Dropdown(dropdown_fn_list, value=r"打开插件列表", label="").style(container=False)
98
- with gr.Row():
99
- plugin_advanced_arg = gr.Textbox(show_label=True, label="高级参数输入区", visible=False,
100
- placeholder="这里是特殊函数插件的高级参数输入区").style(container=False)
101
- with gr.Row():
102
- switchy_bt = gr.Button(r"请先从插件列表中选择", variant="secondary")
103
- with gr.Row():
104
- with gr.Accordion("点击展开“文件上传区”。上传本地文件可供红色函数插件调用。", open=False) as area_file_up:
105
- file_upload = gr.Files(label="任何文件, 但推荐上传压缩文件(zip, tar)", file_count="multiple")
106
- with gr.Accordion("更换模型 & SysPrompt & 交互界面布局", open=(LAYOUT == "TOP-DOWN")):
107
- system_prompt = gr.Textbox(show_label=True, placeholder=f"System Prompt", label="System prompt", value=initial_prompt)
108
- top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
109
- temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
110
- max_length_sl = gr.Slider(minimum=256, maximum=40960, value=5120, step=1, interactive=True, label="Local LLM MaxLength",)
111
- checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "底部输入区", "输入清除键", "插件参数区"], value=["基础功能区", "函数插件区"], label="显示/隐藏功能区")
112
- md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="更换LLM模型/请求源").style(container=False)
113
-
114
- gr.Markdown(description)
115
- with gr.Accordion("备选输入区", open=True, visible=False) as area_input_secondary:
116
- with gr.Row():
117
- txt2 = gr.Textbox(show_label=False, placeholder="Input question here.", label="输入区2").style(container=False)
118
- with gr.Row():
119
- submitBtn2 = gr.Button("提交", variant="primary")
120
- with gr.Row():
121
- resetBtn2 = gr.Button("重置", variant="secondary"); resetBtn2.style(size="sm")
122
- stopBtn2 = gr.Button("停止", variant="secondary"); stopBtn2.style(size="sm")
123
- clearBtn2 = gr.Button("清除", variant="secondary", visible=False); clearBtn2.style(size="sm")
124
- # 功能区显示开关与功能区的互动
125
- def fn_area_visibility(a):
126
- ret = {}
127
- ret.update({area_basic_fn: gr.update(visible=("基础功能区" in a))})
128
- ret.update({area_crazy_fn: gr.update(visible=("函数插件区" in a))})
129
- ret.update({area_input_primary: gr.update(visible=("底部输入区" not in a))})
130
- ret.update({area_input_secondary: gr.update(visible=("底部输入区" in a))})
131
- ret.update({clearBtn: gr.update(visible=("输入清除键" in a))})
132
- ret.update({clearBtn2: gr.update(visible=("输入清除键" in a))})
133
- ret.update({plugin_advanced_arg: gr.update(visible=("插件参数区" in a))})
134
- if "底部输入区" in a: ret.update({txt: gr.update(value="")})
135
- return ret
136
- checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2, clearBtn, clearBtn2, plugin_advanced_arg] )
137
- # 整理反复出现的控件句柄组合
138
- input_combo = [cookies, max_length_sl, md_dropdown, txt, txt2, top_p, temperature, chatbot, history, system_prompt, plugin_advanced_arg]
139
- output_combo = [cookies, chatbot, history, status]
140
- predict_args = dict(fn=ArgsGeneralWrapper(predict), inputs=input_combo, outputs=output_combo)
141
- # 提交按钮、重置按钮
142
- cancel_handles.append(txt.submit(**predict_args))
143
- cancel_handles.append(txt2.submit(**predict_args))
144
- cancel_handles.append(submitBtn.click(**predict_args))
145
- cancel_handles.append(submitBtn2.click(**predict_args))
146
- resetBtn.click(lambda: ([], [], "已重置"), None, [chatbot, history, status])
147
- resetBtn2.click(lambda: ([], [], "已重置"), None, [chatbot, history, status])
148
- clearBtn.click(lambda: ("",""), None, [txt, txt2])
149
- clearBtn2.click(lambda: ("",""), None, [txt, txt2])
150
- # 基础功能区的回调函数注册
151
- for k in functional:
152
- if ("Visible" in functional[k]) and (not functional[k]["Visible"]): continue
153
- click_handle = functional[k]["Button"].click(fn=ArgsGeneralWrapper(predict), inputs=[*input_combo, gr.State(True), gr.State(k)], outputs=output_combo)
154
- cancel_handles.append(click_handle)
155
- # 文件上传区,接收文件后与chatbot的互动
156
- file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt, txt2, checkboxes], [chatbot, txt, txt2])
157
- # 函数插件-固定按钮区
158
- for k in crazy_fns:
159
- if not crazy_fns[k].get("AsButton", True): continue
160
- click_handle = crazy_fns[k]["Button"].click(ArgsGeneralWrapper(crazy_fns[k]["Function"]), [*input_combo, gr.State(PORT)], output_combo)
161
- click_handle.then(on_report_generated, [cookies, file_upload, chatbot], [cookies, file_upload, chatbot])
162
- cancel_handles.append(click_handle)
163
- # 函数插件-下拉菜单与随变按钮的互动
164
- def on_dropdown_changed(k):
165
- variant = crazy_fns[k]["Color"] if "Color" in crazy_fns[k] else "secondary"
166
- ret = {switchy_bt: gr.update(value=k, variant=variant)}
167
- if crazy_fns[k].get("AdvancedArgs", False): # 是否唤起高级插件参数区
168
- ret.update({plugin_advanced_arg: gr.update(visible=True, label=f"插件[{k}]的高级参数说明:" + crazy_fns[k].get("ArgsReminder", [f"没有提供高级参数功能说明"]))})
169
  else:
170
- ret.update({plugin_advanced_arg: gr.update(visible=False, label=f"插件[{k}]不需要高级参数。")})
171
- return ret
172
- dropdown.select(on_dropdown_changed, [dropdown], [switchy_bt, plugin_advanced_arg] )
173
- def on_md_dropdown_changed(k):
174
- return {chatbot: gr.update(label="当前模型:"+k)}
175
- md_dropdown.select(on_md_dropdown_changed, [md_dropdown], [chatbot] )
176
- # 随变按钮的回调函数注册
177
- def route(k, *args, **kwargs):
178
- if k in [r"打开插件列表", r"请先从插件列表中选择"]: return
179
- yield from ArgsGeneralWrapper(crazy_fns[k]["Function"])(*args, **kwargs)
180
- click_handle = switchy_bt.click(route,[switchy_bt, *input_combo, gr.State(PORT)], output_combo)
181
- click_handle.then(on_report_generated, [cookies, file_upload, chatbot], [cookies, file_upload, chatbot])
182
- cancel_handles.append(click_handle)
183
- # 终止按钮的回调函数注册
184
- stopBtn.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
185
- stopBtn2.click(fn=None, inputs=None, outputs=None, cancels=cancel_handles)
186
-
187
- # gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
188
- def auto_opentab_delay():
189
- import threading, webbrowser, time
190
- print(f"如果浏览器没有自动打开,请复制并转到以下URL:")
191
- print(f"\t(亮色主题): http://localhost:{PORT}")
192
- print(f"\t(暗色主题): http://localhost:{PORT}/?__theme=dark")
193
- def open():
194
- time.sleep(2) # 打开浏览器
195
- DARK_MODE, = get_conf('DARK_MODE')
196
- if DARK_MODE: webbrowser.open_new_tab(f"http://localhost:{PORT}/?__theme=dark")
197
- else: webbrowser.open_new_tab(f"http://localhost:{PORT}")
198
- threading.Thread(target=open, name="open-browser", daemon=True).start()
199
- threading.Thread(target=auto_update, name="self-upgrade", daemon=True).start()
200
- threading.Thread(target=warm_up_modules, name="warm-up", daemon=True).start()
201
-
202
- auto_opentab_delay()
203
- demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", share=False, favicon_path="docs/logo.png", blocked_paths=["config.py","config_private.py","docker-compose.yml","Dockerfile"])
204
-
205
- # 如果需要在二级路径下运行
206
- # CUSTOM_PATH, = get_conf('CUSTOM_PATH')
207
- # if CUSTOM_PATH != "/":
208
- # from toolbox import run_gradio_in_subpath
209
- # run_gradio_in_subpath(demo, auth=AUTHENTICATION, port=PORT, custom_path=CUSTOM_PATH)
210
- # else:
211
- # demo.launch(server_name="0.0.0.0", server_port=PORT, auth=AUTHENTICATION, favicon_path="docs/logo.png",
212
- # blocked_paths=["config.py","config_private.py","docker-compose.yml","Dockerfile"])
213
-
214
- if __name__ == "__main__":
215
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Credit to https://github.com/THUDM/ChatGLM2-6B/blob/main/web_demo.py while mistakes are mine."""
2
+ # pylint: disable=broad-exception-caught, redefined-outer-name, missing-function-docstring, missing-module-docstring, too-many-arguments, line-too-long, invalid-name, redefined-builtin, redefined-argument-from-local
3
+ # import gradio as gr
4
+
5
+ # model_name = "models/THUDM/chatglm2-6b-int4"
6
+ # gr.load(model_name).lauch()
7
+
8
+ # %%writefile demo-4bit.py
9
+
10
+ import os
11
+ import time
12
+ from textwrap import dedent
13
+
14
+ import gradio as gr
15
+ import mdtex2html
16
+ import torch
17
+ from loguru import logger
18
+ from transformers import AutoModel, AutoTokenizer
19
+
20
+ # fix timezone in Linux
21
+ os.environ["TZ"] = "Asia/Shanghai"
22
+ try:
23
+ time.tzset() # type: ignore # pylint: disable=no-member
24
+ except Exception:
25
+ # Windows
26
+ logger.warning("Windows, cant run time.tzset()")
27
+
28
+ # model_name = "THUDM/chatglm2-6b" # 7x?G
29
+ model_name = "THUDM/chatglm2-6b-int4" # 3.9G
30
+
31
+ RETRY_FLAG = False
32
+
33
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
34
+
35
+ # model = AutoModel.from_pretrained(model_name, trust_remote_code=True).cuda()
36
+
37
+ # 4/8 bit
38
+ # model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True).quantize(4).cuda()
39
+
40
+ has_cuda = torch.cuda.is_available()
41
+ # has_cuda = False # force cpu
42
+
43
+ if has_cuda:
44
+ if model_name.endswith("int4"):
45
+ model = AutoModel.from_pretrained(model_name, trust_remote_code=True).cuda()
46
+ else:
47
+ model = (
48
+ AutoModel.from_pretrained(model_name, trust_remote_code=True).cuda().half()
49
+ )
50
+ else:
51
+ model = AutoModel.from_pretrained(
52
+ model_name, trust_remote_code=True
53
+ ).float() # .half().float(), .float() required for CPU
54
+
55
+ model = model.eval()
56
+
57
+ _ = """Override Chatbot.postprocess"""
58
+
59
+
60
+ def postprocess(self, y):
61
+ if y is None:
62
+ return []
63
+ for i, (message, response) in enumerate(y):
64
+ y[i] = (
65
+ None if message is None else mdtex2html.convert((message)),
66
+ None if response is None else mdtex2html.convert(response),
67
+ )
68
+ return y
69
+
70
+
71
+ gr.Chatbot.postprocess = postprocess
72
+
73
+
74
+ def parse_text(text):
75
+ """Copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/."""
76
+ lines = text.split("\n")
77
+ lines = [line for line in lines if line != ""]
78
+ count = 0
79
+ for i, line in enumerate(lines):
80
+ if "```" in line:
81
+ count += 1
82
+ items = line.split("`")
83
+ if count % 2 == 1:
84
+ lines[i] = f'<pre><code class="language-{items[-1]}">'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  else:
86
+ lines[i] = "<br></code></pre>"
87
+ else:
88
+ if i > 0:
89
+ if count % 2 == 1:
90
+ line = line.replace("`", r"\`")
91
+ line = line.replace("<", "&lt;")
92
+ line = line.replace(">", "&gt;")
93
+ line = line.replace(" ", "&nbsp;")
94
+ line = line.replace("*", "&ast;")
95
+ line = line.replace("_", "&lowbar;")
96
+ line = line.replace("-", "&#45;")
97
+ line = line.replace(".", "&#46;")
98
+ line = line.replace("!", "&#33;")
99
+ line = line.replace("(", "&#40;")
100
+ line = line.replace(")", "&#41;")
101
+ line = line.replace("$", "&#36;")
102
+ lines[i] = "<br>" + line
103
+ text = "".join(lines)
104
+ return text
105
+
106
+
107
+ def predict(
108
+ RETRY_FLAG, input, chatbot, max_length, top_p, temperature, history, past_key_values
109
+ ):
110
+ try:
111
+ chatbot.append((parse_text(input), ""))
112
+ except Exception as exc:
113
+ logger.error(exc)
114
+ logger.debug(f"{chatbot=}")
115
+ _ = """
116
+ if chatbot:
117
+ chatbot[-1] = (parse_text(input), str(exc))
118
+ yield chatbot, history, past_key_values
119
+ # """
120
+ yield chatbot, history, past_key_values
121
+
122
+ for response, history, past_key_values in model.stream_chat(
123
+ tokenizer,
124
+ input,
125
+ history,
126
+ past_key_values=past_key_values,
127
+ return_past_key_values=True,
128
+ max_length=max_length,
129
+ top_p=top_p,
130
+ temperature=temperature,
131
+ ):
132
+ chatbot[-1] = (parse_text(input), parse_text(response))
133
+
134
+ yield chatbot, history, past_key_values
135
+
136
+
137
+ def trans_api(input, max_length=4096, top_p=0.8, temperature=0.2):
138
+ if max_length < 10:
139
+ max_length = 4096
140
+ if top_p < 0.1 or top_p > 1:
141
+ top_p = 0.85
142
+ if temperature <= 0 or temperature > 1:
143
+ temperature = 0.01
144
+ try:
145
+ res, _ = model.chat(
146
+ tokenizer,
147
+ input,
148
+ history=[],
149
+ past_key_values=None,
150
+ max_length=max_length,
151
+ top_p=top_p,
152
+ temperature=temperature,
153
+ )
154
+ # logger.debug(f"{res=} \n{_=}")
155
+ except Exception as exc:
156
+ logger.error(f"{exc=}")
157
+ res = str(exc)
158
+
159
+ return res
160
+
161
+
162
+ def reset_user_input():
163
+ return gr.update(value="")
164
+
165
+
166
+ def reset_state():
167
+ return [], [], None
168
+
169
+
170
+ # Delete last turn
171
+ def delete_last_turn(chat, history):
172
+ if chat and history:
173
+ chat.pop(-1)
174
+ history.pop(-1)
175
+ return chat, history
176
+
177
+
178
+ # Regenerate response
179
+ def retry_last_answer(
180
+ user_input, chatbot, max_length, top_p, temperature, history, past_key_values
181
+ ):
182
+ if chatbot and history:
183
+ # Removing the previous conversation from chat
184
+ chatbot.pop(-1)
185
+ # Setting up a flag to capture a retry
186
+ RETRY_FLAG = True
187
+ # Getting last message from user
188
+ user_input = history[-1][0]
189
+ # Removing bot response from the history
190
+ history.pop(-1)
191
+
192
+ yield from predict(
193
+ RETRY_FLAG, # type: ignore
194
+ user_input,
195
+ chatbot,
196
+ max_length,
197
+ top_p,
198
+ temperature,
199
+ history,
200
+ past_key_values,
201
+ )
202
+
203
+
204
+ with gr.Blocks(title="ChatGLM2-6B-int4", theme=gr.themes.Soft(text_size="sm")) as demo:
205
+ # gr.HTML("""<h1 align="center">ChatGLM2-6B-int4</h1>""")
206
+ gr.HTML(
207
+ """<center><a href="https://huggingface.co/spaces/mikeee/chatglm2-6b-4bit?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>To avoid the queue and for faster inference Duplicate this Space and upgrade to GPU</center>"""
208
+ )
209
+
210
+ with gr.Accordion("🎈 Info", open=False):
211
+ _ = f"""
212
+ ## {model_name}
213
+
214
+ Try to refresh the browser and try again when occasionally an error occurs.
215
+
216
+ With a GPU, a query takes from a few seconds to a few tens of seconds, dependent on the number of words/characters
217
+ the question and responses contain. The quality of the responses varies quite a bit it seems. Even the same
218
+ question with the same parameters, asked at different times, can result in quite different responses.
219
+
220
+ * Low temperature: responses will be more deterministic and focused; High temperature: responses more creative.
221
+
222
+ * Suggested temperatures -- translation: up to 0.3; chatting: > 0.4
223
+
224
+ * Top P controls dynamic vocabulary selection based on context.
225
+
226
+ For a table of example values for different scenarios, refer to [this](https://community.openai.com/t/cheat-sheet-mastering-temperature-and-top-p-in-chatgpt-api-a-few-tips-and-tricks-on-controlling-the-creativity-deterministic-output-of-prompt-responses/172683)
227
+
228
+ If the instance is not on a GPU (T4), it will be very slow. You can try to run the colab notebook [chatglm2-6b-4bit colab notebook](https://colab.research.google.com/drive/1WkF7kOjVCcBBatDHjaGkuJHnPdMWNtbW?usp=sharing) for a spin.
229
+
230
+ The T4 GPU is sponsored by a community GPU grant from Huggingface. Thanks a lot!
231
+ """
232
+ gr.Markdown(dedent(_))
233
+ chatbot = gr.Chatbot()
234
+ with gr.Row():
235
+ with gr.Column(scale=4):
236
+ with gr.Column(scale=12):
237
+ user_input = gr.Textbox(
238
+ show_label=False,
239
+ placeholder="Input...",
240
+ ).style(container=False)
241
+ RETRY_FLAG = gr.Checkbox(value=False, visible=False)
242
+ with gr.Column(min_width=32, scale=1):
243
+ with gr.Row():
244
+ submitBtn = gr.Button("Submit", variant="primary")
245
+ deleteBtn = gr.Button("Delete last turn", variant="secondary")
246
+ retryBtn = gr.Button("Regenerate", variant="secondary")
247
+ with gr.Column(scale=1):
248
+ emptyBtn = gr.Button("Clear History")
249
+ max_length = gr.Slider(
250
+ 0,
251
+ 32768,
252
+ value=8192,
253
+ step=1.0,
254
+ label="Maximum length",
255
+ interactive=True,
256
+ )
257
+ top_p = gr.Slider(
258
+ 0, 1, value=0.85, step=0.01, label="Top P", interactive=True
259
+ )
260
+ temperature = gr.Slider(
261
+ 0.01, 1, value=0.95, step=0.01, label="Temperature", interactive=True
262
+ )
263
+
264
+ history = gr.State([])
265
+ past_key_values = gr.State(None)
266
+
267
+ user_input.submit(
268
+ predict,
269
+ [
270
+ RETRY_FLAG,
271
+ user_input,
272
+ chatbot,
273
+ max_length,
274
+ top_p,
275
+ temperature,
276
+ history,
277
+ past_key_values,
278
+ ],
279
+ [chatbot, history, past_key_values],
280
+ show_progress="full",
281
+ )
282
+ submitBtn.click(
283
+ predict,
284
+ [
285
+ RETRY_FLAG,
286
+ user_input,
287
+ chatbot,
288
+ max_length,
289
+ top_p,
290
+ temperature,
291
+ history,
292
+ past_key_values,
293
+ ],
294
+ [chatbot, history, past_key_values],
295
+ show_progress="full",
296
+ api_name="predict",
297
+ )
298
+ submitBtn.click(reset_user_input, [], [user_input])
299
+
300
+ emptyBtn.click(
301
+ reset_state, outputs=[chatbot, history, past_key_values], show_progress="full"
302
+ )
303
+
304
+ retryBtn.click(
305
+ retry_last_answer,
306
+ inputs=[
307
+ user_input,
308
+ chatbot,
309
+ max_length,
310
+ top_p,
311
+ temperature,
312
+ history,
313
+ past_key_values,
314
+ ],
315
+ # outputs = [chatbot, history, last_user_message, user_message]
316
+ outputs=[chatbot, history, past_key_values],
317
+ )
318
+ deleteBtn.click(delete_last_turn, [chatbot, history], [chatbot, history])
319
+
320
+ with gr.Accordion("Example inputs", open=True):
321
+ etext = """In America, where cars are an important part of the national psyche, a decade ago people had suddenly started to drive less, which had not happened since the oil shocks of the 1970s. """
322
+ examples = gr.Examples(
323
+ examples=[
324
+ ["What NFL team won the Super Bowl in the year Justin Bieber was born? "],
325
+ ["What NFL team won the Super Bowl in the year Justin Bieber was born? Think step by step."],
326
+ ["Explain the plot of Cinderella in a sentence."],
327
+ [
328
+ "How long does it take to become proficient in French, and what are the best methods for retaining information?"
329
+ ],
330
+ ["What are some common mistakes to avoid when writing code?"],
331
+ ["Build a prompt to generate a beautiful portrait of a horse"],
332
+ ["Suggest four metaphors to describe the benefits of AI"],
333
+ ["Write a pop song about leaving home for the sandy beaches."],
334
+ ["Write a summary demonstrating my ability to tame lions"],
335
+ ["鲁迅和周树人什么关系"],
336
+ ["从前有一头牛,这头牛后面有什么?"],
337
+ ["正无穷大加一大于正无穷大吗?"],
338
+ ["正无穷大加正无穷大大于正无穷大吗?"],
339
+ ["-2的平方根等于什么"],
340
+ ["树上有5只鸟,猎人开枪打死了一只。树上还有几只鸟?"],
341
+ ["树上有11只鸟,猎人开枪打死了一只。树上还有几只鸟?提示:需考虑鸟可能受惊吓飞走。"],
342
+ ["鲁迅和周树人什么关系 用英文回答"],
343
+ ["以红楼梦的行文风格写一张委婉的请假条。不少于320字。"],
344
+ [f"{etext} 翻成中文,列出3个版本"],
345
+ [f"{etext} \n 翻成中文,保留原意,但使用文学性的语言。不要写解释。列出3个版本"],
346
+ ["js 判断一个数是不是质数"],
347
+ ["js 实现python 的 range(10)"],
348
+ ["js 实现python 的 [*(range(10)]"],
349
+ ["假定 1 + 2 = 4, 试求 7 + 8"],
350
+ ["Erkläre die Handlung von Cinderella in einem Satz."],
351
+ ["Erkläre die Handlung von Cinderella in einem Satz. Auf Deutsch"],
352
+ ],
353
+ inputs=[user_input],
354
+ examples_per_page=30,
355
+ )
356
+
357
+ with gr.Accordion("For Chat/Translation API", open=False, visible=False):
358
+ input_text = gr.Text()
359
+ tr_btn = gr.Button("Go", variant="primary")
360
+ out_text = gr.Text()
361
+ tr_btn.click(
362
+ trans_api,
363
+ [input_text, max_length, top_p, temperature],
364
+ out_text,
365
+ # show_progress="full",
366
+ api_name="tr",
367
+ )
368
+ _ = """
369
+ input_text.submit(
370
+ trans_api,
371
+ [input_text, max_length, top_p, temperature],
372
+ out_text,
373
+ show_progress="full",
374
+ api_name="tr1",
375
+ )
376
+ # """
377
+
378
+ # demo.queue().launch(share=False, inbrowser=True)
379
+ # demo.queue().launch(share=True, inbrowser=True, debug=True)
380
+
381
+ # concurrency_count > 1 requires more memory, max_size: queue size
382
+ # T4 medium: 30GB, model size: ~4G concurrency_count = 6
383
+ # leave one for api access
384
+ # reduce to 5 if OOM occurs to often
385
+
386
+ demo.queue(concurrency_count=6, max_size=30).launch(debug=True)
requirements.txt CHANGED
@@ -1,24 +1,9 @@
1
- tiktoken>=0.3.3
2
- requests[socks]
3
- transformers
4
- python-markdown-math
5
- beautifulsoup4
6
- prompt_toolkit
7
- latex2mathml
8
- python-docx
9
- mdtex2html
10
- colorama
11
- Markdown
12
- pygments
13
- pymupdf
14
- openai
15
- numpy
16
- arxiv
17
- rich
18
- pdfminer
19
  protobuf
20
- transformers==4.27.1
21
  cpm_kernels
22
- torch>=1.10
 
23
  mdtex2html
24
  sentencepiece
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  protobuf
2
+ transformers==4.30.2
3
  cpm_kernels
4
+ torch>=2.0
5
+ # gradio
6
  mdtex2html
7
  sentencepiece
8
+ accelerate
9
+ loguru