Numberlla commited on
Commit
a86fc20
1 Parent(s): 8adc473

Upload 16 files

Browse files
Files changed (15) hide show
  1. .gitattributes +5 -34
  2. .gitignore +142 -0
  3. Dockerfile +13 -0
  4. LICENSE +674 -0
  5. README.md +259 -12
  6. check_proxy.py +27 -0
  7. config.py +46 -0
  8. functional.py +70 -0
  9. functional_crazy.py +84 -0
  10. predict.py +246 -0
  11. project_self_analysis.md +175 -0
  12. requirements.txt +5 -0
  13. show_math.py +80 -0
  14. theme.py +94 -0
  15. toolbox.py +325 -0
.gitattributes CHANGED
@@ -1,34 +1,5 @@
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
- *.tflite filter=lfs diff=lfs merge=lfs -text
29
- *.tgz filter=lfs diff=lfs merge=lfs -text
30
- *.wasm filter=lfs diff=lfs merge=lfs -text
31
- *.xz filter=lfs diff=lfs merge=lfs -text
32
- *.zip filter=lfs diff=lfs merge=lfs -text
33
- *.zst filter=lfs diff=lfs merge=lfs -text
34
- *tfevents* filter=lfs diff=lfs merge=lfs -text
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
.gitignore ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
136
+ history
137
+ ssr_conf
138
+ config_private.py
139
+ gpt_log
140
+ private.md
141
+ private_upload
142
+ other_llms
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11
2
+
3
+ RUN echo '[global]' > /etc/pip.conf && \
4
+ echo 'index-url = https://mirrors.aliyun.com/pypi/simple/' >> /etc/pip.conf && \
5
+ echo 'trusted-host = mirrors.aliyun.com' >> /etc/pip.conf
6
+
7
+ RUN pip3 install gradio requests[socks] mdtex2html
8
+
9
+ COPY . /gpt
10
+ WORKDIR /gpt
11
+
12
+
13
+ CMD ["python3", "main.py"]
LICENSE ADDED
@@ -0,0 +1,674 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+ Preamble
9
+
10
+ The GNU General Public License is a free, copyleft license for
11
+ software and other kinds of works.
12
+
13
+ The licenses for most software and other practical works are designed
14
+ to take away your freedom to share and change the works. By contrast,
15
+ the GNU General Public License is intended to guarantee your freedom to
16
+ share and change all versions of a program--to make sure it remains free
17
+ software for all its users. We, the Free Software Foundation, use the
18
+ GNU General Public License for most of our software; it applies also to
19
+ any other work released this way by its authors. You can apply it to
20
+ your programs, too.
21
+
22
+ When we speak of free software, we are referring to freedom, not
23
+ price. Our General Public Licenses are designed to make sure that you
24
+ have the freedom to distribute copies of free software (and charge for
25
+ them if you wish), that you receive source code or can get it if you
26
+ want it, that you can change the software or use pieces of it in new
27
+ free programs, and that you know you can do these things.
28
+
29
+ To protect your rights, we need to prevent others from denying you
30
+ these rights or asking you to surrender the rights. Therefore, you have
31
+ certain responsibilities if you distribute copies of the software, or if
32
+ you modify it: responsibilities to respect the freedom of others.
33
+
34
+ For example, if you distribute copies of such a program, whether
35
+ gratis or for a fee, you must pass on to the recipients the same
36
+ freedoms that you received. You must make sure that they, too, receive
37
+ or can get the source code. And you must show them these terms so they
38
+ know their rights.
39
+
40
+ Developers that use the GNU GPL protect your rights with two steps:
41
+ (1) assert copyright on the software, and (2) offer you this License
42
+ giving you legal permission to copy, distribute and/or modify it.
43
+
44
+ For the developers' and authors' protection, the GPL clearly explains
45
+ that there is no warranty for this free software. For both users' and
46
+ authors' sake, the GPL requires that modified versions be marked as
47
+ changed, so that their problems will not be attributed erroneously to
48
+ authors of previous versions.
49
+
50
+ Some devices are designed to deny users access to install or run
51
+ modified versions of the software inside them, although the manufacturer
52
+ can do so. This is fundamentally incompatible with the aim of
53
+ protecting users' freedom to change the software. The systematic
54
+ pattern of such abuse occurs in the area of products for individuals to
55
+ use, which is precisely where it is most unacceptable. Therefore, we
56
+ have designed this version of the GPL to prohibit the practice for those
57
+ products. If such problems arise substantially in other domains, we
58
+ stand ready to extend this provision to those domains in future versions
59
+ of the GPL, as needed to protect the freedom of users.
60
+
61
+ Finally, every program is threatened constantly by software patents.
62
+ States should not allow patents to restrict development and use of
63
+ software on general-purpose computers, but in those that do, we wish to
64
+ avoid the special danger that patents applied to a free program could
65
+ make it effectively proprietary. To prevent this, the GPL assures that
66
+ patents cannot be used to render the program non-free.
67
+
68
+ The precise terms and conditions for copying, distribution and
69
+ modification follow.
70
+
71
+ TERMS AND CONDITIONS
72
+
73
+ 0. Definitions.
74
+
75
+ "This License" refers to version 3 of the GNU General Public License.
76
+
77
+ "Copyright" also means copyright-like laws that apply to other kinds of
78
+ works, such as semiconductor masks.
79
+
80
+ "The Program" refers to any copyrightable work licensed under this
81
+ License. Each licensee is addressed as "you". "Licensees" and
82
+ "recipients" may be individuals or organizations.
83
+
84
+ To "modify" a work means to copy from or adapt all or part of the work
85
+ in a fashion requiring copyright permission, other than the making of an
86
+ exact copy. The resulting work is called a "modified version" of the
87
+ earlier work or a work "based on" the earlier work.
88
+
89
+ A "covered work" means either the unmodified Program or a work based
90
+ on the Program.
91
+
92
+ To "propagate" a work means to do anything with it that, without
93
+ permission, would make you directly or secondarily liable for
94
+ infringement under applicable copyright law, except executing it on a
95
+ computer or modifying a private copy. Propagation includes copying,
96
+ distribution (with or without modification), making available to the
97
+ public, and in some countries other activities as well.
98
+
99
+ To "convey" a work means any kind of propagation that enables other
100
+ parties to make or receive copies. Mere interaction with a user through
101
+ a computer network, with no transfer of a copy, is not conveying.
102
+
103
+ An interactive user interface displays "Appropriate Legal Notices"
104
+ to the extent that it includes a convenient and prominently visible
105
+ feature that (1) displays an appropriate copyright notice, and (2)
106
+ tells the user that there is no warranty for the work (except to the
107
+ extent that warranties are provided), that licensees may convey the
108
+ work under this License, and how to view a copy of this License. If
109
+ the interface presents a list of user commands or options, such as a
110
+ menu, a prominent item in the list meets this criterion.
111
+
112
+ 1. Source Code.
113
+
114
+ The "source code" for a work means the preferred form of the work
115
+ for making modifications to it. "Object code" means any non-source
116
+ form of a work.
117
+
118
+ A "Standard Interface" means an interface that either is an official
119
+ standard defined by a recognized standards body, or, in the case of
120
+ interfaces specified for a particular programming language, one that
121
+ is widely used among developers working in that language.
122
+
123
+ The "System Libraries" of an executable work include anything, other
124
+ than the work as a whole, that (a) is included in the normal form of
125
+ packaging a Major Component, but which is not part of that Major
126
+ Component, and (b) serves only to enable use of the work with that
127
+ Major Component, or to implement a Standard Interface for which an
128
+ implementation is available to the public in source code form. A
129
+ "Major Component", in this context, means a major essential component
130
+ (kernel, window system, and so on) of the specific operating system
131
+ (if any) on which the executable work runs, or a compiler used to
132
+ produce the work, or an object code interpreter used to run it.
133
+
134
+ The "Corresponding Source" for a work in object code form means all
135
+ the source code needed to generate, install, and (for an executable
136
+ work) run the object code and to modify the work, including scripts to
137
+ control those activities. However, it does not include the work's
138
+ System Libraries, or general-purpose tools or generally available free
139
+ programs which are used unmodified in performing those activities but
140
+ which are not part of the work. For example, Corresponding Source
141
+ includes interface definition files associated with source files for
142
+ the work, and the source code for shared libraries and dynamically
143
+ linked subprograms that the work is specifically designed to require,
144
+ such as by intimate data communication or control flow between those
145
+ subprograms and other parts of the work.
146
+
147
+ The Corresponding Source need not include anything that users
148
+ can regenerate automatically from other parts of the Corresponding
149
+ Source.
150
+
151
+ The Corresponding Source for a work in source code form is that
152
+ same work.
153
+
154
+ 2. Basic Permissions.
155
+
156
+ All rights granted under this License are granted for the term of
157
+ copyright on the Program, and are irrevocable provided the stated
158
+ conditions are met. This License explicitly affirms your unlimited
159
+ permission to run the unmodified Program. The output from running a
160
+ covered work is covered by this License only if the output, given its
161
+ content, constitutes a covered work. This License acknowledges your
162
+ rights of fair use or other equivalent, as provided by copyright law.
163
+
164
+ You may make, run and propagate covered works that you do not
165
+ convey, without conditions so long as your license otherwise remains
166
+ in force. You may convey covered works to others for the sole purpose
167
+ of having them make modifications exclusively for you, or provide you
168
+ with facilities for running those works, provided that you comply with
169
+ the terms of this License in conveying all material for which you do
170
+ not control copyright. Those thus making or running the covered works
171
+ for you must do so exclusively on your behalf, under your direction
172
+ and control, on terms that prohibit them from making any copies of
173
+ your copyrighted material outside their relationship with you.
174
+
175
+ Conveying under any other circumstances is permitted solely under
176
+ the conditions stated below. Sublicensing is not allowed; section 10
177
+ makes it unnecessary.
178
+
179
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180
+
181
+ No covered work shall be deemed part of an effective technological
182
+ measure under any applicable law fulfilling obligations under article
183
+ 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184
+ similar laws prohibiting or restricting circumvention of such
185
+ measures.
186
+
187
+ When you convey a covered work, you waive any legal power to forbid
188
+ circumvention of technological measures to the extent such circumvention
189
+ is effected by exercising rights under this License with respect to
190
+ the covered work, and you disclaim any intention to limit operation or
191
+ modification of the work as a means of enforcing, against the work's
192
+ users, your or third parties' legal rights to forbid circumvention of
193
+ technological measures.
194
+
195
+ 4. Conveying Verbatim Copies.
196
+
197
+ You may convey verbatim copies of the Program's source code as you
198
+ receive it, in any medium, provided that you conspicuously and
199
+ appropriately publish on each copy an appropriate copyright notice;
200
+ keep intact all notices stating that this License and any
201
+ non-permissive terms added in accord with section 7 apply to the code;
202
+ keep intact all notices of the absence of any warranty; and give all
203
+ recipients a copy of this License along with the Program.
204
+
205
+ You may charge any price or no price for each copy that you convey,
206
+ and you may offer support or warranty protection for a fee.
207
+
208
+ 5. Conveying Modified Source Versions.
209
+
210
+ You may convey a work based on the Program, or the modifications to
211
+ produce it from the Program, in the form of source code under the
212
+ terms of section 4, provided that you also meet all of these conditions:
213
+
214
+ a) The work must carry prominent notices stating that you modified
215
+ it, and giving a relevant date.
216
+
217
+ b) The work must carry prominent notices stating that it is
218
+ released under this License and any conditions added under section
219
+ 7. This requirement modifies the requirement in section 4 to
220
+ "keep intact all notices".
221
+
222
+ c) You must license the entire work, as a whole, under this
223
+ License to anyone who comes into possession of a copy. This
224
+ License will therefore apply, along with any applicable section 7
225
+ additional terms, to the whole of the work, and all its parts,
226
+ regardless of how they are packaged. This License gives no
227
+ permission to license the work in any other way, but it does not
228
+ invalidate such permission if you have separately received it.
229
+
230
+ d) If the work has interactive user interfaces, each must display
231
+ Appropriate Legal Notices; however, if the Program has interactive
232
+ interfaces that do not display Appropriate Legal Notices, your
233
+ work need not make them do so.
234
+
235
+ A compilation of a covered work with other separate and independent
236
+ works, which are not by their nature extensions of the covered work,
237
+ and which are not combined with it such as to form a larger program,
238
+ in or on a volume of a storage or distribution medium, is called an
239
+ "aggregate" if the compilation and its resulting copyright are not
240
+ used to limit the access or legal rights of the compilation's users
241
+ beyond what the individual works permit. Inclusion of a covered work
242
+ in an aggregate does not cause this License to apply to the other
243
+ parts of the aggregate.
244
+
245
+ 6. Conveying Non-Source Forms.
246
+
247
+ You may convey a covered work in object code form under the terms
248
+ of sections 4 and 5, provided that you also convey the
249
+ machine-readable Corresponding Source under the terms of this License,
250
+ in one of these ways:
251
+
252
+ a) Convey the object code in, or embodied in, a physical product
253
+ (including a physical distribution medium), accompanied by the
254
+ Corresponding Source fixed on a durable physical medium
255
+ customarily used for software interchange.
256
+
257
+ b) Convey the object code in, or embodied in, a physical product
258
+ (including a physical distribution medium), accompanied by a
259
+ written offer, valid for at least three years and valid for as
260
+ long as you offer spare parts or customer support for that product
261
+ model, to give anyone who possesses the object code either (1) a
262
+ copy of the Corresponding Source for all the software in the
263
+ product that is covered by this License, on a durable physical
264
+ medium customarily used for software interchange, for a price no
265
+ more than your reasonable cost of physically performing this
266
+ conveying of source, or (2) access to copy the
267
+ Corresponding Source from a network server at no charge.
268
+
269
+ c) Convey individual copies of the object code with a copy of the
270
+ written offer to provide the Corresponding Source. This
271
+ alternative is allowed only occasionally and noncommercially, and
272
+ only if you received the object code with such an offer, in accord
273
+ with subsection 6b.
274
+
275
+ d) Convey the object code by offering access from a designated
276
+ place (gratis or for a charge), and offer equivalent access to the
277
+ Corresponding Source in the same way through the same place at no
278
+ further charge. You need not require recipients to copy the
279
+ Corresponding Source along with the object code. If the place to
280
+ copy the object code is a network server, the Corresponding Source
281
+ may be on a different server (operated by you or a third party)
282
+ that supports equivalent copying facilities, provided you maintain
283
+ clear directions next to the object code saying where to find the
284
+ Corresponding Source. Regardless of what server hosts the
285
+ Corresponding Source, you remain obligated to ensure that it is
286
+ available for as long as needed to satisfy these requirements.
287
+
288
+ e) Convey the object code using peer-to-peer transmission, provided
289
+ you inform other peers where the object code and Corresponding
290
+ Source of the work are being offered to the general public at no
291
+ charge under subsection 6d.
292
+
293
+ A separable portion of the object code, whose source code is excluded
294
+ from the Corresponding Source as a System Library, need not be
295
+ included in conveying the object code work.
296
+
297
+ A "User Product" is either (1) a "consumer product", which means any
298
+ tangible personal property which is normally used for personal, family,
299
+ or household purposes, or (2) anything designed or sold for incorporation
300
+ into a dwelling. In determining whether a product is a consumer product,
301
+ doubtful cases shall be resolved in favor of coverage. For a particular
302
+ product received by a particular user, "normally used" refers to a
303
+ typical or common use of that class of product, regardless of the status
304
+ of the particular user or of the way in which the particular user
305
+ actually uses, or expects or is expected to use, the product. A product
306
+ is a consumer product regardless of whether the product has substantial
307
+ commercial, industrial or non-consumer uses, unless such uses represent
308
+ the only significant mode of use of the product.
309
+
310
+ "Installation Information" for a User Product means any methods,
311
+ procedures, authorization keys, or other information required to install
312
+ and execute modified versions of a covered work in that User Product from
313
+ a modified version of its Corresponding Source. The information must
314
+ suffice to ensure that the continued functioning of the modified object
315
+ code is in no case prevented or interfered with solely because
316
+ modification has been made.
317
+
318
+ If you convey an object code work under this section in, or with, or
319
+ specifically for use in, a User Product, and the conveying occurs as
320
+ part of a transaction in which the right of possession and use of the
321
+ User Product is transferred to the recipient in perpetuity or for a
322
+ fixed term (regardless of how the transaction is characterized), the
323
+ Corresponding Source conveyed under this section must be accompanied
324
+ by the Installation Information. But this requirement does not apply
325
+ if neither you nor any third party retains the ability to install
326
+ modified object code on the User Product (for example, the work has
327
+ been installed in ROM).
328
+
329
+ The requirement to provide Installation Information does not include a
330
+ requirement to continue to provide support service, warranty, or updates
331
+ for a work that has been modified or installed by the recipient, or for
332
+ the User Product in which it has been modified or installed. Access to a
333
+ network may be denied when the modification itself materially and
334
+ adversely affects the operation of the network or violates the rules and
335
+ protocols for communication across the network.
336
+
337
+ Corresponding Source conveyed, and Installation Information provided,
338
+ in accord with this section must be in a format that is publicly
339
+ documented (and with an implementation available to the public in
340
+ source code form), and must require no special password or key for
341
+ unpacking, reading or copying.
342
+
343
+ 7. Additional Terms.
344
+
345
+ "Additional permissions" are terms that supplement the terms of this
346
+ License by making exceptions from one or more of its conditions.
347
+ Additional permissions that are applicable to the entire Program shall
348
+ be treated as though they were included in this License, to the extent
349
+ that they are valid under applicable law. If additional permissions
350
+ apply only to part of the Program, that part may be used separately
351
+ under those permissions, but the entire Program remains governed by
352
+ this License without regard to the additional permissions.
353
+
354
+ When you convey a copy of a covered work, you may at your option
355
+ remove any additional permissions from that copy, or from any part of
356
+ it. (Additional permissions may be written to require their own
357
+ removal in certain cases when you modify the work.) You may place
358
+ additional permissions on material, added by you to a covered work,
359
+ for which you have or can give appropriate copyright permission.
360
+
361
+ Notwithstanding any other provision of this License, for material you
362
+ add to a covered work, you may (if authorized by the copyright holders of
363
+ that material) supplement the terms of this License with terms:
364
+
365
+ a) Disclaiming warranty or limiting liability differently from the
366
+ terms of sections 15 and 16 of this License; or
367
+
368
+ b) Requiring preservation of specified reasonable legal notices or
369
+ author attributions in that material or in the Appropriate Legal
370
+ Notices displayed by works containing it; or
371
+
372
+ c) Prohibiting misrepresentation of the origin of that material, or
373
+ requiring that modified versions of such material be marked in
374
+ reasonable ways as different from the original version; or
375
+
376
+ d) Limiting the use for publicity purposes of names of licensors or
377
+ authors of the material; or
378
+
379
+ e) Declining to grant rights under trademark law for use of some
380
+ trade names, trademarks, or service marks; or
381
+
382
+ f) Requiring indemnification of licensors and authors of that
383
+ material by anyone who conveys the material (or modified versions of
384
+ it) with contractual assumptions of liability to the recipient, for
385
+ any liability that these contractual assumptions directly impose on
386
+ those licensors and authors.
387
+
388
+ All other non-permissive additional terms are considered "further
389
+ restrictions" within the meaning of section 10. If the Program as you
390
+ received it, or any part of it, contains a notice stating that it is
391
+ governed by this License along with a term that is a further
392
+ restriction, you may remove that term. If a license document contains
393
+ a further restriction but permits relicensing or conveying under this
394
+ License, you may add to a covered work material governed by the terms
395
+ of that license document, provided that the further restriction does
396
+ not survive such relicensing or conveying.
397
+
398
+ If you add terms to a covered work in accord with this section, you
399
+ must place, in the relevant source files, a statement of the
400
+ additional terms that apply to those files, or a notice indicating
401
+ where to find the applicable terms.
402
+
403
+ Additional terms, permissive or non-permissive, may be stated in the
404
+ form of a separately written license, or stated as exceptions;
405
+ the above requirements apply either way.
406
+
407
+ 8. Termination.
408
+
409
+ You may not propagate or modify a covered work except as expressly
410
+ provided under this License. Any attempt otherwise to propagate or
411
+ modify it is void, and will automatically terminate your rights under
412
+ this License (including any patent licenses granted under the third
413
+ paragraph of section 11).
414
+
415
+ However, if you cease all violation of this License, then your
416
+ license from a particular copyright holder is reinstated (a)
417
+ provisionally, unless and until the copyright holder explicitly and
418
+ finally terminates your license, and (b) permanently, if the copyright
419
+ holder fails to notify you of the violation by some reasonable means
420
+ prior to 60 days after the cessation.
421
+
422
+ Moreover, your license from a particular copyright holder is
423
+ reinstated permanently if the copyright holder notifies you of the
424
+ violation by some reasonable means, this is the first time you have
425
+ received notice of violation of this License (for any work) from that
426
+ copyright holder, and you cure the violation prior to 30 days after
427
+ your receipt of the notice.
428
+
429
+ Termination of your rights under this section does not terminate the
430
+ licenses of parties who have received copies or rights from you under
431
+ this License. If your rights have been terminated and not permanently
432
+ reinstated, you do not qualify to receive new licenses for the same
433
+ material under section 10.
434
+
435
+ 9. Acceptance Not Required for Having Copies.
436
+
437
+ You are not required to accept this License in order to receive or
438
+ run a copy of the Program. Ancillary propagation of a covered work
439
+ occurring solely as a consequence of using peer-to-peer transmission
440
+ to receive a copy likewise does not require acceptance. However,
441
+ nothing other than this License grants you permission to propagate or
442
+ modify any covered work. These actions infringe copyright if you do
443
+ not accept this License. Therefore, by modifying or propagating a
444
+ covered work, you indicate your acceptance of this License to do so.
445
+
446
+ 10. Automatic Licensing of Downstream Recipients.
447
+
448
+ Each time you convey a covered work, the recipient automatically
449
+ receives a license from the original licensors, to run, modify and
450
+ propagate that work, subject to this License. You are not responsible
451
+ for enforcing compliance by third parties with this License.
452
+
453
+ An "entity transaction" is a transaction transferring control of an
454
+ organization, or substantially all assets of one, or subdividing an
455
+ organization, or merging organizations. If propagation of a covered
456
+ work results from an entity transaction, each party to that
457
+ transaction who receives a copy of the work also receives whatever
458
+ licenses to the work the party's predecessor in interest had or could
459
+ give under the previous paragraph, plus a right to possession of the
460
+ Corresponding Source of the work from the predecessor in interest, if
461
+ the predecessor has it or can get it with reasonable efforts.
462
+
463
+ You may not impose any further restrictions on the exercise of the
464
+ rights granted or affirmed under this License. For example, you may
465
+ not impose a license fee, royalty, or other charge for exercise of
466
+ rights granted under this License, and you may not initiate litigation
467
+ (including a cross-claim or counterclaim in a lawsuit) alleging that
468
+ any patent claim is infringed by making, using, selling, offering for
469
+ sale, or importing the Program or any portion of it.
470
+
471
+ 11. Patents.
472
+
473
+ A "contributor" is a copyright holder who authorizes use under this
474
+ License of the Program or a work on which the Program is based. The
475
+ work thus licensed is called the contributor's "contributor version".
476
+
477
+ A contributor's "essential patent claims" are all patent claims
478
+ owned or controlled by the contributor, whether already acquired or
479
+ hereafter acquired, that would be infringed by some manner, permitted
480
+ by this License, of making, using, or selling its contributor version,
481
+ but do not include claims that would be infringed only as a
482
+ consequence of further modification of the contributor version. For
483
+ purposes of this definition, "control" includes the right to grant
484
+ patent sublicenses in a manner consistent with the requirements of
485
+ this License.
486
+
487
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
488
+ patent license under the contributor's essential patent claims, to
489
+ make, use, sell, offer for sale, import and otherwise run, modify and
490
+ propagate the contents of its contributor version.
491
+
492
+ In the following three paragraphs, a "patent license" is any express
493
+ agreement or commitment, however denominated, not to enforce a patent
494
+ (such as an express permission to practice a patent or covenant not to
495
+ sue for patent infringement). To "grant" such a patent license to a
496
+ party means to make such an agreement or commitment not to enforce a
497
+ patent against the party.
498
+
499
+ If you convey a covered work, knowingly relying on a patent license,
500
+ and the Corresponding Source of the work is not available for anyone
501
+ to copy, free of charge and under the terms of this License, through a
502
+ publicly available network server or other readily accessible means,
503
+ then you must either (1) cause the Corresponding Source to be so
504
+ available, or (2) arrange to deprive yourself of the benefit of the
505
+ patent license for this particular work, or (3) arrange, in a manner
506
+ consistent with the requirements of this License, to extend the patent
507
+ license to downstream recipients. "Knowingly relying" means you have
508
+ actual knowledge that, but for the patent license, your conveying the
509
+ covered work in a country, or your recipient's use of the covered work
510
+ in a country, would infringe one or more identifiable patents in that
511
+ country that you have reason to believe are valid.
512
+
513
+ If, pursuant to or in connection with a single transaction or
514
+ arrangement, you convey, or propagate by procuring conveyance of, a
515
+ covered work, and grant a patent license to some of the parties
516
+ receiving the covered work authorizing them to use, propagate, modify
517
+ or convey a specific copy of the covered work, then the patent license
518
+ you grant is automatically extended to all recipients of the covered
519
+ work and works based on it.
520
+
521
+ A patent license is "discriminatory" if it does not include within
522
+ the scope of its coverage, prohibits the exercise of, or is
523
+ conditioned on the non-exercise of one or more of the rights that are
524
+ specifically granted under this License. You may not convey a covered
525
+ work if you are a party to an arrangement with a third party that is
526
+ in the business of distributing software, under which you make payment
527
+ to the third party based on the extent of your activity of conveying
528
+ the work, and under which the third party grants, to any of the
529
+ parties who would receive the covered work from you, a discriminatory
530
+ patent license (a) in connection with copies of the covered work
531
+ conveyed by you (or copies made from those copies), or (b) primarily
532
+ for and in connection with specific products or compilations that
533
+ contain the covered work, unless you entered into that arrangement,
534
+ or that patent license was granted, prior to 28 March 2007.
535
+
536
+ Nothing in this License shall be construed as excluding or limiting
537
+ any implied license or other defenses to infringement that may
538
+ otherwise be available to you under applicable patent law.
539
+
540
+ 12. No Surrender of Others' Freedom.
541
+
542
+ If conditions are imposed on you (whether by court order, agreement or
543
+ otherwise) that contradict the conditions of this License, they do not
544
+ excuse you from the conditions of this License. If you cannot convey a
545
+ covered work so as to satisfy simultaneously your obligations under this
546
+ License and any other pertinent obligations, then as a consequence you may
547
+ not convey it at all. For example, if you agree to terms that obligate you
548
+ to collect a royalty for further conveying from those to whom you convey
549
+ the Program, the only way you could satisfy both those terms and this
550
+ License would be to refrain entirely from conveying the Program.
551
+
552
+ 13. Use with the GNU Affero General Public License.
553
+
554
+ Notwithstanding any other provision of this License, you have
555
+ permission to link or combine any covered work with a work licensed
556
+ under version 3 of the GNU Affero General Public License into a single
557
+ combined work, and to convey the resulting work. The terms of this
558
+ License will continue to apply to the part which is the covered work,
559
+ but the special requirements of the GNU Affero General Public License,
560
+ section 13, concerning interaction through a network will apply to the
561
+ combination as such.
562
+
563
+ 14. Revised Versions of this License.
564
+
565
+ The Free Software Foundation may publish revised and/or new versions of
566
+ the GNU General Public License from time to time. Such new versions will
567
+ be similar in spirit to the present version, but may differ in detail to
568
+ address new problems or concerns.
569
+
570
+ Each version is given a distinguishing version number. If the
571
+ Program specifies that a certain numbered version of the GNU General
572
+ Public License "or any later version" applies to it, you have the
573
+ option of following the terms and conditions either of that numbered
574
+ version or of any later version published by the Free Software
575
+ Foundation. If the Program does not specify a version number of the
576
+ GNU General Public License, you may choose any version ever published
577
+ by the Free Software Foundation.
578
+
579
+ If the Program specifies that a proxy can decide which future
580
+ versions of the GNU General Public License can be used, that proxy's
581
+ public statement of acceptance of a version permanently authorizes you
582
+ to choose that version for the Program.
583
+
584
+ Later license versions may give you additional or different
585
+ permissions. However, no additional obligations are imposed on any
586
+ author or copyright holder as a result of your choosing to follow a
587
+ later version.
588
+
589
+ 15. Disclaimer of Warranty.
590
+
591
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592
+ APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593
+ HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594
+ OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596
+ PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597
+ IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598
+ ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599
+
600
+ 16. Limitation of Liability.
601
+
602
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604
+ THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605
+ GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606
+ USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607
+ DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608
+ PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609
+ EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610
+ SUCH DAMAGES.
611
+
612
+ 17. Interpretation of Sections 15 and 16.
613
+
614
+ If the disclaimer of warranty and limitation of liability provided
615
+ above cannot be given local legal effect according to their terms,
616
+ reviewing courts shall apply local law that most closely approximates
617
+ an absolute waiver of all civil liability in connection with the
618
+ Program, unless a warranty or assumption of liability accompanies a
619
+ copy of the Program in return for a fee.
620
+
621
+ END OF TERMS AND CONDITIONS
622
+
623
+ How to Apply These Terms to Your New Programs
624
+
625
+ If you develop a new program, and you want it to be of the greatest
626
+ possible use to the public, the best way to achieve this is to make it
627
+ free software which everyone can redistribute and change under these terms.
628
+
629
+ To do so, attach the following notices to the program. It is safest
630
+ to attach them to the start of each source file to most effectively
631
+ state the exclusion of warranty; and each file should have at least
632
+ the "copyright" line and a pointer to where the full notice is found.
633
+
634
+ <one line to give the program's name and a brief idea of what it does.>
635
+ Copyright (C) <year> <name of author>
636
+
637
+ This program is free software: you can redistribute it and/or modify
638
+ it under the terms of the GNU General Public License as published by
639
+ the Free Software Foundation, either version 3 of the License, or
640
+ (at your option) any later version.
641
+
642
+ This program is distributed in the hope that it will be useful,
643
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
644
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645
+ GNU General Public License for more details.
646
+
647
+ You should have received a copy of the GNU General Public License
648
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
649
+
650
+ Also add information on how to contact you by electronic and paper mail.
651
+
652
+ If the program does terminal interaction, make it output a short
653
+ notice like this when it starts in an interactive mode:
654
+
655
+ <program> Copyright (C) <year> <name of author>
656
+ This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657
+ This is free software, and you are welcome to redistribute it
658
+ under certain conditions; type `show c' for details.
659
+
660
+ The hypothetical commands `show w' and `show c' should show the appropriate
661
+ parts of the General Public License. Of course, your program's commands
662
+ might be different; for a GUI interface, you would use an "about box".
663
+
664
+ You should also get your employer (if you work as a programmer) or school,
665
+ if any, to sign a "copyright disclaimer" for the program, if necessary.
666
+ For more information on this, and how to apply and follow the GNU GPL, see
667
+ <https://www.gnu.org/licenses/>.
668
+
669
+ The GNU General Public License does not permit incorporating your program
670
+ into proprietary programs. If your program is a subroutine library, you
671
+ may consider it more useful to permit linking proprietary applications with
672
+ the library. If this is what you want to do, use the GNU Lesser General
673
+ Public License instead of this License. But first, please read
674
+ <https://www.gnu.org/licenses/why-not-lgpl.html>.
README.md CHANGED
@@ -1,12 +1,259 @@
1
- ---
2
- title: B788
3
- emoji:
4
- colorFrom: pink
5
- colorTo: purple
6
- sdk: gradio
7
- sdk_version: 3.23.0
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ # ChatGPT 学术优化
4
+
5
+ **如果喜欢这个项目,请给它一个Star;如果你发明了更好用的学术快捷键,欢迎发issue或者pull requests(dev分支)**
6
+
7
+ If you like this project, please give it a Star. If you've come up with more useful academic shortcuts, feel free to open an issue or pull request (to `dev` branch).
8
+
9
+ ```
10
+ 代码中参考了很多其他优秀项目中的设计,主要包括:
11
+
12
+ # 借鉴项目1:借鉴了ChuanhuChatGPT中读取OpenAI json的方法、记录历史问询记录的方法以及gradio queue的使用技巧
13
+ https://github.com/GaiZhenbiao/ChuanhuChatGPT
14
+
15
+ # 借鉴项目2:借鉴了mdtex2html中公式处理的方法
16
+ https://github.com/polarwinkel/mdtex2html
17
+
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
+ 功能 | 描述
32
+ --- | ---
33
+ 一键润色 | 支持一键润色、一键查找论文语法错误
34
+ 一键中英互译 | 一键中英互译
35
+ 一键代码解释 | 可以正确显示代码、解释代码
36
+ 自定义快捷键 | 支持自定义快捷键
37
+ 配置代理服务器 | 支持配置代理服务器
38
+ 模块化设计 | 支持自定义高阶的实验性功能
39
+ 自我程序剖析 | [实验性功能] 一键读懂本项目的源代码
40
+ 程序剖析 | [实验性功能] 一键可以剖析其他Python/C++项目
41
+ 读论文 | [实验性功能] 一键解读latex论文全文并生成摘要
42
+ 批量注释生成 | [实验性功能] 一键批量生成函数注释
43
+ chat分析报告生成 | [实验性功能] 运行后自动生成总结汇报
44
+ 公式显示 | 可以同时显示公式的tex形式和渲染形式
45
+ 图片显示 | 可以在markdown中显示图片
46
+ 支持GPT输出的markdown表格 | 可以输出支持GPT的markdown表格
47
+ …… | ……
48
+
49
+ </div>
50
+
51
+ <!-- - 新界面(左:master主分支, 右:dev开发前沿) -->
52
+ - 新界面
53
+ <div align="center">
54
+ <img src="https://user-images.githubusercontent.com/96192199/229222589-b30ff298-adb1-4e1e-8352-466085919bfb.png" width="700" >
55
+ </div>
56
+
57
+
58
+ - 所有按钮都通过读取functional.py动态生成,可随意加自定义功能,解放粘贴板
59
+ <div align="center">
60
+ <img src="img/公式.gif" width="700" >
61
+ </div>
62
+
63
+ - 润色/纠错
64
+ <div align="center">
65
+ <img src="img/润色.gif" width="700" >
66
+ </div>
67
+
68
+
69
+ - 支持GPT输出的markdown表格
70
+ <div align="center">
71
+ <img src="img/demo2.jpg" width="500" >
72
+ </div>
73
+
74
+ - 如果输出包含公式,会同时以tex形式和渲染形式显示,方便复制和阅读
75
+ <div align="center">
76
+ <img src="img/demo.jpg" width="500" >
77
+ </div>
78
+
79
+
80
+ - 懒得看项目代码?整个工程直接给chatgpt炫嘴里
81
+ <div align="center">
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
+
139
+ ``` sh
140
+ # 下载项目
141
+ git clone https://github.com/binary-husky/chatgpt_academic.git
142
+ cd chatgpt_academic
143
+ # 配置 海外Proxy 和 OpenAI API KEY
144
+ 用任意文本编辑器编辑 config.py
145
+ # 安装
146
+ docker build -t gpt-academic .
147
+ # 运行
148
+ docker run --rm -it --net=host gpt-academic
149
+
150
+ # 测试实验性功能
151
+ ## 测试自我代码解读
152
+ 点击 "[实验] 请解析并解构此项目本身"
153
+ ## 测试实验功能模板函数(要求gpt回答历史上的今天发生了什么),您可以根据此函数为模板,实现更复杂的功能
154
+ 点击 "[实验] 实验功能函数模板"
155
+ ##(请注意在docker中运行时,需要额外注意程序的文件访问权限问题)
156
+ ## 测试C++项目头文件分析
157
+ input区域 输入 ./crazy_functions/test_project/cpp/libJPG , 然后点击 "[实验] 解析整个C++项目(input输入项目根路径)"
158
+ ## 测试给Latex项目写摘要
159
+ input区域 输入 ./crazy_functions/test_project/latex/attention , 然后点击 "[实验] 读tex论文写摘要(input输入项目根路径)"
160
+ ## 测试Python项目分析
161
+ input区域 输入 ./crazy_functions/test_project/python/dqn , 然后点击 "[实验] 解析整个py项目(input输入项目根路径)"
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,添加条目如下,然后重启程序即可。(如果按钮已经添加成功并可见,那么前缀、后缀都支持热修改,无需重启程序即可生效。)
175
+ 例如
176
+ ```
177
+ "超级英译中": {
178
+
179
+ # 前缀,会被加在你的输入之前。例如,用来描述你的要求,例如翻译、解释代码、润色等等
180
+ "Prefix": "请翻译把下面一段内容成中文,然后用一个markdown表格逐一解释文中出现的专有名词:\n\n",
181
+
182
+ # 后缀,会被加在你的输入之后。例如,配合前缀可以把你的输入内容用引号圈起来。
183
+ "Suffix": "",
184
+
185
+ },
186
+ ```
187
+ <div align="center">
188
+ <img src="https://user-images.githubusercontent.com/96192199/226899272-477c2134-ed71-4326-810c-29891fe4a508.png" width="500" >
189
+ </div>
190
+
191
+
192
+ 如果你发明了更好用的学术快捷键,欢迎发issue或者pull requests!
193
+
194
+ ## 配置代理
195
+
196
+ 在```config.py```中修改端口与代理软件对应
197
+
198
+ <div align="center">
199
+ <img src="https://user-images.githubusercontent.com/96192199/226571294-37a47cd9-4d40-4c16-97a2-d360845406f7.png" width="500" >
200
+ <img src="https://user-images.githubusercontent.com/96192199/226838985-e5c95956-69c2-4c23-a4dd-cd7944eeb451.png" width="500" >
201
+ </div>
202
+
203
+ 配置完成后,你可以用以下命令测试代理是否工作,如果一切正常,下面的代码将输出你的代理服务器所在地:
204
+ ```
205
+ python check_proxy.py
206
+ ```
207
+
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">
220
+ <img src="https://user-images.githubusercontent.com/96192199/226936850-c77d7183-0749-4c1c-9875-fd4891842d0c.png" width="800" >
221
+ </div>
222
+
223
+ <div align="center">
224
+ <img src="https://user-images.githubusercontent.com/96192199/226936618-9b487e4b-ab5b-4b6e-84c6-16942102e917.png" width="800" >
225
+ </div>
226
+
227
+ ### 其他任意Python/Cpp项目剖析:
228
+ <div align="center">
229
+ <img src="https://user-images.githubusercontent.com/96192199/226935232-6b6a73ce-8900-4aee-93f9-733c7e6fef53.png" width="800" >
230
+ </div>
231
+
232
+ <div align="center">
233
+ <img src="https://user-images.githubusercontent.com/96192199/226969067-968a27c1-1b9c-486b-8b81-ab2de8d3f88a.png" width="800" >
234
+ </div>
235
+
236
+ ### Latex论文一键阅读理解与摘要生成
237
+ <div align="center">
238
+ <img src="https://user-images.githubusercontent.com/96192199/227504406-86ab97cd-f208-41c3-8e4a-7000e51cf980.png" width="800" >
239
+ </div>
240
+
241
+ ### 自动报告生成
242
+ <div align="center">
243
+ <img src="https://user-images.githubusercontent.com/96192199/227503770-fe29ce2c-53fd-47b0-b0ff-93805f0c2ff4.png" height="300" >
244
+ <img src="https://user-images.githubusercontent.com/96192199/227504617-7a497bb3-0a2a-4b50-9a8a-95ae60ea7afd.png" height="300" >
245
+ <img src="https://user-images.githubusercontent.com/96192199/227504005-efeaefe0-b687-49d0-bf95-2d7b7e66c348.png" height="300" >
246
+ </div>
247
+
248
+ ### 模块化功能设计
249
+ <div align="center">
250
+ <img src="https://user-images.githubusercontent.com/96192199/229288270-093643c1-0018-487a-81e6-1d7809b6e90f.png" height="400" >
251
+ <img src="https://user-images.githubusercontent.com/96192199/227504931-19955f78-45cd-4d1c-adac-e71e50957915.png" height="400" >
252
+ </div>
253
+
254
+ ## Todo:
255
+
256
+ - (Top Priority) 调用另一个开源项目text-generation-webui的web接口,使用其他llm模型
257
+ - 总结大工程源代码时,文本过长、token溢出的问题(目前的方法是直接二分丢弃处理溢出,过于粗暴,有效信息大量丢失)
258
+ - UI不够美观
259
+
check_proxy.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ def check_proxy(proxies):
3
+ import requests
4
+ proxies_https = proxies['https'] if proxies is not None else '无'
5
+ try:
6
+ response = requests.get("https://ipapi.co/json/", proxies=proxies, timeout=4, verify=False)
7
+ data = response.json()
8
+ print(f'查询代理的地理位置,返回的结果是{data}')
9
+ if 'country_name' in data:
10
+ country = data['country_name']
11
+ result = f"代理配置 {proxies_https}, 代理所在地:{country}"
12
+ elif 'error' in data:
13
+ result = f"代理配置 {proxies_https}, 代理所在地:未知,IP查询频率受限"
14
+ print(result)
15
+ return result
16
+ except:
17
+ result = f"代理配置 {proxies_https}, 代理所在地查询超时,代理可能无效"
18
+ print(result)
19
+ return result
20
+
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 ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # [step 1]>> 例如: API_KEY = "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" (此key无效)
2
+ API_KEY = "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"
3
+
4
+ # [step 2]>> 改为True应用代理,如果直接在海外服务器部署,此处不修改
5
+ USE_PROXY = True
6
+ if USE_PROXY:
7
+ # 填写格式是 [协议]:// [地址] :[端口],填写之前不要忘记把USE_PROXY改成True,如果直接在海外服务器部署,此处不修改
8
+ # 例如 "socks5h://localhost:11284"
9
+ # [协议] 常见协议无非socks5h/http; 例如 v2**y 和 ss* 的默认本地协议是socks5h; 而cl**h 的默认本地协议是http
10
+ # [地址] 懂的都懂,不懂就填localhost或者127.0.0.1肯定错不了(localhost意思是代理软件安装在本机上)
11
+ # [端口] 在代理软件的设置里找。虽然不同的代理软件界面不一样,但端口号都应该在最显眼的位置上
12
+
13
+ # 代理网络的地址,打开你的科学上网软件查看代理的协议(socks5/http)、地址(localhost)和端口(11284)
14
+ proxies = {
15
+ # [协议]:// [地址] :[端口]
16
+ "http": "127.0.0.1:10087",
17
+ "https": "127.0.0.1:10087",
18
+ }
19
+ else:
20
+ proxies = None
21
+
22
+
23
+ # [step 3]>> 以下配置可以优化体验,但大部分场合下并不需要修改
24
+ # 对话窗的高度
25
+ CHATBOT_HEIGHT = 1116
26
+
27
+ # 发送请求到OpenAI后,等待多久判定为超时
28
+ TIMEOUT_SECONDS = 25
29
+
30
+ # 网页的端口, -1代表随机端口
31
+ WEB_PORT = -1
32
+
33
+ # 如果OpenAI不响应(网络卡顿、代理失败、KEY失效),重试的次数限制
34
+ MAX_RETRY = 2
35
+
36
+ # OpenAI模型选择是(gpt4现在只对申请成功的人开放)
37
+ LLM_MODEL = "gpt-3.5-turbo"
38
+
39
+ # OpenAI的API_URL
40
+ API_URL = "https://api.openai.com/v1/chat/completions"
41
+
42
+ # 设置并行使用的线程数
43
+ CONCURRENT_COUNT = 100
44
+
45
+ # 设置用户名和密码
46
+ AUTHENTICATION = [] # [("username", "password"), ("username2", "password2"), ...]
functional.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 'primary' 颜色对应 theme.py 中的 primary_hue
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 necessary, 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"Can you help me ensure that the grammar and the spelling is correct? " +
25
+ r"Do not try to polish the text, if no mistake is found, tell me that this paragraph is good." +
26
+ r"If you find grammar or spelling mistakes, please list mistakes you find in a two-column markdown table, " +
27
+ r"put the original text the first column, " +
28
+ r"put the corrected text in the second column and highlight the key words you fixed.""\n"
29
+ r"Example:""\n"
30
+ r"Paragraph: How is you? Do you knows what is it?""\n"
31
+ r"| Original sentence | Corrected sentence |""\n"
32
+ r"| :--- | :--- |""\n"
33
+ r"| How **is** you? | How **are** you? |""\n"
34
+ r"| Do you **knows** what **is** **it**? | Do you **know** what **it** **is** ? |""\n"
35
+ r"Below is a paragraph from an academic paper. "
36
+ r"You need to report all grammar and spelling mistakes as the example before."
37
+ + "\n\n",
38
+ "Suffix": r"",
39
+ "PreProcess": clear_line_break, # 预处理:清除换行符
40
+ },
41
+ "中译英": {
42
+ "Prefix": r"Please translate following sentence to English:" + "\n\n",
43
+ "Suffix": r"",
44
+ },
45
+ "学术中英互译": {
46
+ "Prefix": r"I want you to act as a scientific English-Chinese translator, " +
47
+ r"I will provide you with some paragraphs in one language " +
48
+ r"and your task is to accurately and academically translate the paragraphs only into the other language. " +
49
+ r"Do not repeat the original provided paragraphs after translation. " +
50
+ r"You should use artificial intelligence tools, " +
51
+ r"such as natural language processing, and rhetorical knowledge " +
52
+ r"and experience about effective writing techniques to reply. " +
53
+ r"I'll give you my paragraphs as follows, tell me what language it is written in, and then translate:" + "\n\n",
54
+ "Suffix": "",
55
+ "Color": "secondary",
56
+ },
57
+ "英译中": {
58
+ "Prefix": r"请翻译成中文:" + "\n\n",
59
+ "Suffix": r"",
60
+ },
61
+ "找图片": {
62
+ "Prefix": r"我需要你找一张网络图片。使用Unsplash API(https://source.unsplash.com/960x640/?<英语关键词>)获取图片URL," +
63
+ r"然后请使用Markdown格式封装,并且不要有反斜线,不要用代码块。现在,请按以下描述给我发送图片:" + "\n\n",
64
+ "Suffix": r"",
65
+ },
66
+ "解释代码": {
67
+ "Prefix": r"请解释以下代码:" + "\n```\n",
68
+ "Suffix": "\n```\n",
69
+ },
70
+ }
functional_crazy.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from toolbox import HotReload # HotReload 的意思是热更新,修改函数插件后,不需要重启程序,代码直接生效
2
+
3
+ # UserVisibleLevel是过滤器参数。
4
+ # 由于UI界面空间有限,所以通过这种方式决定UI界面中显示哪些插件
5
+ # 默认函数插件 VisibleLevel 是 0
6
+ # 当 UserVisibleLevel >= 函数插件的 VisibleLevel 时,该函数插件才会被显示出来
7
+ UserVisibleLevel = 1
8
+
9
+
10
+ def get_crazy_functionals():
11
+ from crazy_functions.读文章写摘要 import 读文章写摘要
12
+ from crazy_functions.生成函数注释 import 批量生成函数注释
13
+ from crazy_functions.解析项目源代码 import 解析项目本身
14
+ from crazy_functions.解析项目源代码 import 解析一个Python项目
15
+ from crazy_functions.解析项目源代码 import 解析一个C项目的头文件
16
+ from crazy_functions.解析项目源代码 import 解析一个C项目
17
+ from crazy_functions.高级功能函数模板 import 高阶功能模板函数
18
+ from crazy_functions.代码重写为全英文_多线程 import 全项目切换英文
19
+
20
+ function_plugins = {
21
+ "请解析并解构此项目本身(源码自译解)": {
22
+ "AsButton": False, # 加入下拉菜单中
23
+ "Function": 解析项目本身
24
+ },
25
+ "解析整个Py项目": {
26
+ "Color": "stop", # 按钮颜色
27
+ "Function": 解析一个Python项目
28
+ },
29
+ "解析整个C++项目头文件": {
30
+ "Color": "stop", # 按钮颜色
31
+ "Function": 解析一个C项目的头文件
32
+ },
33
+ "解析整个C++项目(.cpp/.h)": {
34
+ "Color": "stop", # 按钮颜色
35
+ "AsButton": False, # 加入下拉菜单中
36
+ "Function": 解析一个C项目
37
+ },
38
+ "读Tex论文写摘要": {
39
+ "Color": "stop", # 按钮颜色
40
+ "Function": 读文章写摘要
41
+ },
42
+ "批量生成函数注释": {
43
+ "Color": "stop", # 按钮颜色
44
+ "Function": 批量生成函数注释
45
+ },
46
+ "[多线程demo] 把本项目源代码切换成全英文": {
47
+ # HotReload 的意思是热更新,修改函数插件代码后,不需要重启程序,代码直接生效
48
+ "Function": HotReload(全项目切换英文)
49
+ },
50
+ "[函数插件模板demo] 历史上的今天": {
51
+ # HotReload 的意思是热更新,修改函数插件代码后,不需要重启程序,代码直接生效
52
+ "Function": HotReload(高阶功能模板函数)
53
+ },
54
+ }
55
+
56
+ # VisibleLevel=1 经过测试,但功能上距离达到完美状态还差一点点
57
+ if UserVisibleLevel >= 1:
58
+ from crazy_functions.批量总结PDF文档 import 批量总结PDF文档
59
+ from crazy_functions.批量总结PDF文档pdfminer import 批量总结PDF文档pdfminer
60
+ from crazy_functions.总结word文档 import 总结word文档
61
+ function_plugins.update({
62
+ "[仅供开发调试] 批量总结PDF文档": {
63
+ "Color": "stop",
64
+ "Function": HotReload(批量总结PDF文档) # HotReload 的意思是热更新,修改函数插件代码后,不需要重启程序,代码直接生效
65
+ },
66
+ "[仅供开发调试] 批量总结PDF文档pdfminer": {
67
+ "Color": "stop",
68
+ "AsButton": False, # 加入下拉菜单中
69
+ "Function": HotReload(批量总结PDF文档pdfminer)
70
+ },
71
+ "[仅供开发调试] 批量总结Word文档": {
72
+ "Color": "stop",
73
+ "Function": HotReload(总结word文档)
74
+ },
75
+ })
76
+
77
+ # VisibleLevel=2 尚未充分测试的函数插件,放在这里
78
+ if UserVisibleLevel >= 2:
79
+ function_plugins.update({
80
+ })
81
+
82
+ return function_plugins
83
+
84
+
predict.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 借鉴了 https://github.com/GaiZhenbiao/ChuanhuChatGPT 项目
2
+
3
+ """
4
+ 该文件中主要包含三个函数
5
+
6
+ 不具备多线程能力的函数:
7
+ 1. predict: 正常对话时使用,具备完备的交互功能,不可多线程
8
+
9
+ 具备多线程调用能力的函数
10
+ 2. predict_no_ui:高级实验性功能模块调用,不会实时显示在界面上,参数简单,可以多线程并行,方便实现复杂的功能逻辑
11
+ 3. predict_no_ui_long_connection:在实验过程中发现调用predict_no_ui处理长文档时,和openai的连接容易断掉,这个函数用stream的方式解决这个问题,同样支持多线程
12
+ """
13
+
14
+ import json
15
+ import gradio as gr
16
+ import logging
17
+ import traceback
18
+ import requests
19
+ import importlib
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
+ """
32
+ 获取完整的从Openai返回的报错
33
+ """
34
+ while True:
35
+ try:
36
+ chunk += next(stream_response)
37
+ except:
38
+ break
39
+ return chunk
40
+
41
+ def predict_no_ui(inputs, top_p, temperature, history=[], sys_prompt=""):
42
+ """
43
+ 发送至chatGPT,等待回复,一次性完成,不显示中间过程。
44
+ predict函数的简化版。
45
+ 用于payload比较大的情况,或者用于实现多线、带嵌套的复杂功能。
46
+
47
+ inputs 是本次问询的输入
48
+ top_p, temperature是chatGPT的内部调优参数
49
+ history 是之前的对话列表
50
+ (注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误,然后raise ConnectionAbortedError)
51
+ """
52
+ headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt=sys_prompt, stream=False)
53
+
54
+ retry = 0
55
+ while True:
56
+ try:
57
+ # make a POST request to the API endpoint, stream=False
58
+ response = requests.post(API_URL, headers=headers, proxies=proxies,
59
+ json=payload, stream=False, timeout=TIMEOUT_SECONDS*2); break
60
+ except requests.exceptions.ReadTimeout as e:
61
+ retry += 1
62
+ traceback.print_exc()
63
+ if retry > MAX_RETRY: raise TimeoutError
64
+ if MAX_RETRY!=0: print(f'请求超时,正在重试 ({retry}/{MAX_RETRY}) ……')
65
+
66
+ try:
67
+ result = json.loads(response.text)["choices"][0]["message"]["content"]
68
+ return result
69
+ except Exception as e:
70
+ if "choices" not in response.text: print(response.text)
71
+ raise ConnectionAbortedError("Json解析不合常规,可能是文本过长" + response.text)
72
+
73
+
74
+ def predict_no_ui_long_connection(inputs, top_p, temperature, history=[], sys_prompt=""):
75
+ """
76
+ 发送至chatGPT,等待回复,一次性完成,不显示中间过程。但内部用stream的方法避免有人中途掐网线。
77
+ """
78
+ headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt=sys_prompt, stream=True)
79
+
80
+ retry = 0
81
+ while True:
82
+ try:
83
+ # make a POST request to the API endpoint, stream=False
84
+ response = requests.post(API_URL, headers=headers, proxies=proxies,
85
+ json=payload, stream=True, timeout=TIMEOUT_SECONDS); break
86
+ except requests.exceptions.ReadTimeout as e:
87
+ retry += 1
88
+ traceback.print_exc()
89
+ if retry > MAX_RETRY: raise TimeoutError
90
+ if MAX_RETRY!=0: print(f'请求超时,正在重试 ({retry}/{MAX_RETRY}) ……')
91
+
92
+ stream_response = response.iter_lines()
93
+ result = ''
94
+ while True:
95
+ try: chunk = next(stream_response).decode()
96
+ except StopIteration: break
97
+ if len(chunk)==0: continue
98
+ if not chunk.startswith('data:'):
99
+ error_msg = get_full_error(chunk.encode('utf8'), stream_response).decode()
100
+ if "reduce the length" in error_msg:
101
+ raise ConnectionAbortedError("OpenAI拒绝了请求:" + error_msg)
102
+ else:
103
+ raise RuntimeError("OpenAI拒绝了请求:" + error_msg)
104
+ json_data = json.loads(chunk.lstrip('data:'))['choices'][0]
105
+ delta = json_data["delta"]
106
+ if len(delta) == 0: break
107
+ if "role" in delta: continue
108
+ if "content" in delta: result += delta["content"]; print(delta["content"], end='')
109
+ else: raise RuntimeError("意外Json结构:"+delta)
110
+ if json_data['finish_reason'] == 'length':
111
+ raise ConnectionAbortedError("正常结束,但显示Token不足。")
112
+ return result
113
+
114
+
115
+ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt='',
116
+ stream = True, additional_fn=None):
117
+ """
118
+ 发送至chatGPT,流式获取输出。
119
+ 用于基础的对话功能。
120
+ inputs 是本次问询的输入
121
+ top_p, temperature是chatGPT的内部调优参数
122
+ history 是之前的对话列表(注意无论是inputs还是history,内容太长了都会触发token数量溢出的错误)
123
+ chatbot 为WebUI中显示的对话列表,修改它,然后yeild出去,可以直接修改对话界面内容
124
+ additional_fn代表点击的哪个按钮,按钮见functional.py
125
+ """
126
+ if additional_fn is not None:
127
+ import functional
128
+ importlib.reload(functional) # 热更新prompt
129
+ functional = functional.get_functionals()
130
+ if "PreProcess" in functional[additional_fn]: inputs = functional[additional_fn]["PreProcess"](inputs) # 获取预处理函数(如果有的话)
131
+ inputs = functional[additional_fn]["Prefix"] + inputs + functional[additional_fn]["Suffix"]
132
+
133
+ if stream:
134
+ raw_input = inputs
135
+ logging.info(f'[raw_input] {raw_input}')
136
+ chatbot.append((inputs, ""))
137
+ yield chatbot, history, "等待响应"
138
+
139
+ headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt, stream)
140
+ history.append(inputs); history.append(" ")
141
+
142
+ retry = 0
143
+ while True:
144
+ try:
145
+ # make a POST request to the API endpoint, stream=True
146
+ response = requests.post(API_URL, headers=headers, proxies=proxies,
147
+ json=payload, stream=True, timeout=TIMEOUT_SECONDS);break
148
+ except:
149
+ retry += 1
150
+ chatbot[-1] = ((chatbot[-1][0], timeout_bot_msg))
151
+ retry_msg = f",正在重试 ({retry}/{MAX_RETRY}) ……" if MAX_RETRY > 0 else ""
152
+ yield chatbot, history, "请求超时"+retry_msg
153
+ if retry > MAX_RETRY: raise TimeoutError
154
+
155
+ gpt_replying_buffer = ""
156
+
157
+ is_head_of_the_stream = True
158
+ if stream:
159
+ stream_response = response.iter_lines()
160
+ while True:
161
+ chunk = next(stream_response)
162
+ # print(chunk.decode()[6:])
163
+ if is_head_of_the_stream:
164
+ # 数据流的第一帧不携带content
165
+ is_head_of_the_stream = False; continue
166
+
167
+ if chunk:
168
+ try:
169
+ if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
170
+ # 判定为数据流的结束,gpt_replying_buffer也写完了
171
+ logging.info(f'[response] {gpt_replying_buffer}')
172
+ break
173
+ # 处理数据流的主体
174
+ chunkjson = json.loads(chunk.decode()[6:])
175
+ status_text = f"finish_reason: {chunkjson['choices'][0]['finish_reason']}"
176
+ # 如果这里抛出异常,一般是文本过长,详情见get_full_error的输出
177
+ gpt_replying_buffer = gpt_replying_buffer + json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"]
178
+ history[-1] = gpt_replying_buffer
179
+ chatbot[-1] = (history[-2], history[-1])
180
+ yield chatbot, history, status_text
181
+
182
+ except Exception as e:
183
+ traceback.print_exc()
184
+ yield chatbot, history, "Json解析不合常规"
185
+ chunk = get_full_error(chunk, stream_response)
186
+ error_msg = chunk.decode()
187
+ if "reduce the length" in error_msg:
188
+ chatbot[-1] = (chatbot[-1][0], "[Local Message] Input (or history) is too long, please reduce input or clear history by refreshing this page.")
189
+ history = []
190
+ elif "Incorrect API key" in error_msg:
191
+ chatbot[-1] = (chatbot[-1][0], "[Local Message] Incorrect API key provided.")
192
+ else:
193
+ from toolbox import regular_txt_to_markdown
194
+ tb_str = regular_txt_to_markdown(traceback.format_exc())
195
+ chatbot[-1] = (chatbot[-1][0], f"[Local Message] Json Error \n\n {tb_str} \n\n {regular_txt_to_markdown(chunk.decode()[4:])}")
196
+ yield chatbot, history, "Json解析不合常规" + error_msg
197
+ return
198
+
199
+ def generate_payload(inputs, top_p, temperature, history, system_prompt, stream):
200
+ """
201
+ 整合所有信息,选择LLM模型,生成http请求,为发送请求做准备
202
+ """
203
+ headers = {
204
+ "Content-Type": "application/json",
205
+ "Authorization": f"Bearer {API_KEY}"
206
+ }
207
+
208
+ conversation_cnt = len(history) // 2
209
+
210
+ messages = [{"role": "system", "content": system_prompt}]
211
+ if conversation_cnt:
212
+ for index in range(0, 2*conversation_cnt, 2):
213
+ what_i_have_asked = {}
214
+ what_i_have_asked["role"] = "user"
215
+ what_i_have_asked["content"] = history[index]
216
+ what_gpt_answer = {}
217
+ what_gpt_answer["role"] = "assistant"
218
+ what_gpt_answer["content"] = history[index+1]
219
+ if what_i_have_asked["content"] != "":
220
+ if what_gpt_answer["content"] == "": continue
221
+ if what_gpt_answer["content"] == timeout_bot_msg: continue
222
+ messages.append(what_i_have_asked)
223
+ messages.append(what_gpt_answer)
224
+ else:
225
+ messages[-1]['content'] = what_gpt_answer['content']
226
+
227
+ what_i_ask_now = {}
228
+ what_i_ask_now["role"] = "user"
229
+ what_i_ask_now["content"] = inputs
230
+ messages.append(what_i_ask_now)
231
+
232
+ payload = {
233
+ "model": LLM_MODEL,
234
+ "messages": messages,
235
+ "temperature": temperature, # 1.0,
236
+ "top_p": top_p, # 1.0,
237
+ "n": 1,
238
+ "stream": stream,
239
+ "presence_penalty": 0,
240
+ "frequency_penalty": 0,
241
+ }
242
+
243
+ print(f" {LLM_MODEL} : {conversation_cnt} : {inputs}")
244
+ return headers,payload
245
+
246
+
project_self_analysis.md ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # chatgpt-academic项目自译解报告
2
+ (Author补充:以下分析均由本项目调用ChatGPT一键生成,如果有不准确的地方,全怪GPT😄)
3
+
4
+ ## [0/18] 程序摘要: functional_crazy.py
5
+
6
+ 这是一个功能扩展的程序,文件名为 `functional_crazy.py`。代码的主要功能是通过提供一系列函数插件,增强程序的功能,让用户可以通过界面中的按钮,快速调用对应的函数插件实现相应的操作。代码中使用了 `HotReload` 函数插件,可以在不重启程序的情况下更新函数插件的代码,让其生效。同时,通过 `UserVisibleLevel` 变量的设置,可以控制哪些插件会在UI界面显示出来。函数插件列表包括了以下功能:解析项目本身、解析一个Python项目、解析一个C++项目头文件、解析一个C++项目、读取文章并生成摘要、批量生成函数注释、全项目切换成英文、批量总结PDF文档、批量总结PDF文档pdfminer、批量总结Word文档、高阶功能模板函数、以及其他未经充分测试的函数插件。
7
+
8
+ ## [1/18] 程序摘要: main.py
9
+
10
+ 该程序是一个基于Gradio构建的对话生成模型的Web界面示例,包含了以下主要功能:
11
+
12
+ 1.加载模型并对用户输入进行响应;
13
+ 2.通过调用外部函数库来获取用户的输入,并在模型生成的过程中进行处理;
14
+ 3.支持用户上传本地文件,供外部函数库调用;
15
+ 4.支持停止当前的生成过程;
16
+ 5.保存用户的历史记录,并将其记录在本地日志文件中,以供后续分析和使用。
17
+
18
+ 该程序需要依赖于一些外部库和软件包,如Gradio、torch等。用户需要确保这些依赖项已经安装,并且在运行该程序前对config_private.py配置文件进行相应的修改。
19
+
20
+ ## [2/18] 程序摘要: functional.py
21
+
22
+ 该文件定义了一个名为“functional”的函数,函数的作用是返回一个包含多个字典(键值对)的字典,每个键值对表示一种功能。该字典的键值由功能名称和对应的数据组成。其中的每个字典都包含4个键值对,分别为“Prefix”、“Suffix”、“Color”和“PreProcess”,分别表示前缀、后缀、按钮颜色和预处理函数。如果某些键值对没有给出,那么程序中默认相应的值,如按钮颜色默认为“secondary”等。每个功能描述了不同的学术润色/翻译/其他服务,如“英语学术润色”、“中文学术润色”、“查找语法错误”等。函数还引用了一个名为“clear_line_break”的函数,用于预处理修改前的文本。
23
+
24
+ ## [3/18] 程序摘要: show_math.py
25
+
26
+ 该程序文件名为show_math.py,主要用途是将Markdown和LaTeX混合格式转换成带有MathML的HTML格式。该程序通过递归地处理LaTeX和Markdown混合段落逐一转换成HTML/MathML标记出来,并在LaTeX公式创建中进行错误处理。在程序文件中定义了3个变量,分别是incomplete,convError和convert,其中convert函数是用来执行转换的主要函数。程序使用正则表达式进行LaTeX格式和Markdown段落的分割,从而实现转换。如果在Latex转换过程中发生错误,程序将输出相应的错误信息。
27
+
28
+ ## [4/18] 程序摘要: predict.py
29
+
30
+ 本程序文件的文件名为"./predict.py",主要包含三个函数:
31
+
32
+ 1. predict:正常对话时使用,具备完备的交互功能,不可多线程;
33
+ 2. predict_no_ui:高级实验性功能模块调用,不会实时显示在界面上,参数简单,可以多线程并行,方便实现复杂的功能逻辑;
34
+ 3. predict_no_ui_long_connection:在实验过程中发现调用predict_no_ui处理长文档时,和openai的连接容易断掉,这个函数用stream的方式解决这个问题,同样支持多线程。
35
+
36
+ 其中,predict函数用于基础的对话功能,发送至chatGPT,流式获取输出,根据点击的哪个按钮,进行对话预处理等额外操作;predict_no_ui函数用于payload比较大的情况,或者用于实现多线、带嵌套的复杂功能;predict_no_ui_long_connection实现调用predict_no_ui处理长文档时,避免连接断掉的情况,支持多线程。
37
+
38
+ ## [5/18] 程序摘要: check_proxy.py
39
+
40
+ 该程序文件名为check_proxy.py,主要功能是检查代理服务器的可用性并返回代理服务器的地理位置信息或错误提示。具体实现方式如下:
41
+
42
+ 首先使用requests模块向指定网站(https://ipapi.co/json/)发送GET请求,请求结果以JSON格式返回。如果代理服务器参数(proxies)是有效的且没有指明'https'代理,则用默认字典值'无'替代。
43
+
44
+ 然后,程序会解析返回的JSON数据,并根据数据中是否包含国家名字字段来判断代理服务器的地理位置。如果有国家名字字段,则将其打印出来并返回代理服务器的相关信息。如果没有国家名字字段,但有错误信息字段,则返回其他错误提示信息。
45
+
46
+ 在程序执行前,程序会先设置环境变量no_proxy,并使用toolbox模块中的get_conf函数从配置文件中读取代理参数。
47
+
48
+ 最后,检测程序会输出检查结果并返回对应的结果字符串。
49
+
50
+ ## [6/18] 程序摘要: config_private.py
51
+
52
+ 本程序文件名为`config_private.py`,其功能为配置私有信息以便在主程序中使用。主要功能包括:
53
+
54
+ - 配置OpenAI API的密钥和API URL
55
+ - 配置是否使用代理,如果使用代理配置代理地址和端口
56
+ - 配置发送请求的超时时间和失败重试次数的限制
57
+ - 配置并行使用线程数和用户名密码
58
+ - 提供检查功能以确保API密钥已经正确设置
59
+
60
+ 其中,需要特别注意的是:最后一个检查功能要求在运行之前必须将API密钥正确设置,否则程序会直接退出。
61
+
62
+ ## [7/18] 程序摘要: config.py
63
+
64
+ 该程序文件是一个配置文件,用于配置OpenAI的API参数和优化体验的相关参数,具体包括以下几个步骤:
65
+
66
+ 1.设置OpenAI的API密钥。
67
+
68
+ 2.选择是否使用代理,如果使用则需要设置代理地址和端口等参数。
69
+
70
+ 3.设置请求OpenAI后的超时时间、网页的端口、重试次数、选择的OpenAI模型、API的网址等。
71
+
72
+ 4.设置并行使用的线程数和用户名密码。
73
+
74
+ 该程序文件的作用为在使用OpenAI API时进行相关参数的配置,以保证请求的正确性和速度,并且优化使用体验。
75
+
76
+ ## [8/18] 程序摘要: theme.py
77
+
78
+ 该程序是一个自定义Gradio主题的Python模块。主题文件名为"./theme.py"。程序引入了Gradio模块,并定义了一个名为"adjust_theme()"的函数。该函数根据输入值调整Gradio的默认主题,返回一个包含所需自定义属性的主题对象。主题属性包括颜色、字体、过渡、阴影、按钮边框和渐变等。主题颜色列表包括石板色、灰色、锌色、中性色、石头色、红色、橙色、琥珀色、黄色、酸橙色、绿色、祖母绿、青蓝色、青色、天蓝色、蓝色、靛蓝色、紫罗兰色、紫色、洋红色、粉红色和玫瑰色。如果Gradio版本较旧,则不能自定义字体和颜色。
79
+
80
+ ## [9/18] 程序摘要: toolbox.py
81
+
82
+ 该程序文件包含了一系列函数,用于实现聊天程序所需的各种功能,如预测对话、将对话记录写入文件、将普通文本转换为Markdown格式文本、装饰器函数CatchException和HotReload等。其中一些函数用到了第三方库,如Python-Markdown、mdtex2html、zipfile、tarfile、rarfile和py7zr。除此之外,还有一些辅助函数,如get_conf、clear_line_break和extract_archive等。主要功能包括:
83
+
84
+ 1. 导入markdown、mdtex2html、threading、functools等模块。
85
+ 2. 定义函数predict_no_ui_but_counting_down,用于生成对话。
86
+ 3. 定义函数write_results_to_file,用于将对话记录生成Markdown文件。
87
+ 4. 定义函数regular_txt_to_markdown,将普通文本转换为Markdown格式的文本。
88
+ 5. 定义装饰器函数CatchException,用于捕获函数执行异常并返回生成器。
89
+ 6. 定义函数report_execption,用于向chatbot中添加错误信息。
90
+ 7. 定义函数text_divide_paragraph,用于将文本按照段落分隔符分割开,生成带有段落标签的HTML代码。
91
+ 8. 定义函数markdown_convertion,用于将Markdown格式的文本转换为HTML格式。
92
+ 9. 定义函数format_io,用于将输入和输出解析为HTML格式。
93
+ 10. 定义函数find_free_port,用于返回当前系统中可用的未使用端口。
94
+ 11. 定义函数extract_archive,用于解压归档文件。
95
+ 12. 定义函数find_recent_files,用于查找最近创建的文件。
96
+ 13. 定义函数on_file_uploaded,用于处理上传文件的操作。
97
+ 14. 定义函数on_report_generated,用于处理生成报告文件的操作。
98
+
99
+
100
+ ## [10/18] 程序摘要: crazy_functions/生成函数注释.py
101
+
102
+ 该程序文件是一个Python脚本,文件名为“生成函数注释.py”,位于“./crazy_functions/”目录下。该程序实现了一个批量生成函数注释的功能,可以对指定文件夹下的所有Python和C++源代码文件中的所有函数进行注释,使用Markdown表格输出注释结果。
103
+
104
+ 该程序引用了predict.py和toolbox.py两个模块,其中predict.py实现了一个基于GPT模型的文本生成功能,用于生成函数注释,而toolbox.py实现了一些工具函数,包括异常处理函数、文本写入函数等。另外,该程序还定义了两个函数,一个是“生成函数注释”函数,用于处理单个文件的注释生成;另一个是“批量生成函数注释”函数,用于批量处理多个文件的注释生成。
105
+
106
+ ## [11/18] 程序摘要: crazy_functions/读文章写摘要.py
107
+
108
+ 这个程序文件是一个名为“读文章写摘要”的函数。该函数的输入包括文章的文本内容、top_p(生成文本时选择最可能的词语的概率阈值)、temperature(控制生成文本的随机性的因子)、对话历史等参数,以及一个聊天机器人和一个系统提示的文本。该函数的主要工作是解析一组.tex文件,���后生成一段学术性语言的中文和英文摘要。在解析过程中,该函数使用一个名为“toolbox”的模块中的辅助函数和一个名为“predict”的模块中的函数来执行GPT-2模型的推理工作,然后将结果返回给聊天机器人。另外,该程序还包括一个名为“fast_debug”的bool型变量,用于调试和测试。
109
+
110
+ ## [12/18] 程序摘要: crazy_functions/代码重写为全英文_多线程.py
111
+
112
+ 该程序文件实现了一个多线程操作,用于将指定目录下的所有 Python 文件中的中文转化为英文,并将转化后的文件存入另一个目录中。具体实现过程如下:
113
+
114
+ 1. 集合目标文件路径并清空历史记录。
115
+ 2. 循环目标文件,对每个文件启动一个线程进行任务操作。
116
+ 3. 各个线程同时开始执行任务函数,并在任务完成后将转化后的文件写入指定目录,最终生成一份任务执行报告。
117
+
118
+ ## [13/18] 程序摘要: crazy_functions/高级功能函数模板.py
119
+
120
+ 该程序文件名为高级功能函数模板.py,它包含了一个名为“高阶功能模板函数”的函数,这个函数可以作为开发新功能函数的模板。该函数引用了predict.py和toolbox.py文件中的函数。在该函数内部,它首先清空了历史记录,然后对于今天和今天以后的四天,它问用户历史中哪些事件发生在这些日期,并列举两条事件并发送相关的图片。在向用户询问问题时,使用了GPT进行响应。由于请求GPT需要一定的时间,所以函数会在重新显示状态之前等待一段时间。在每次与用户的互动中,使用yield关键字生成器函数来输出聊天机器人的当前状态,包括聊天消息、历史记录和状态('正常')。最后,程序调用write_results_to_file函数将聊天的结果写入文件,以供后续的评估和分析。
121
+
122
+ ## [14/18] 程序摘要: crazy_functions/总结word文档.py
123
+
124
+ 该程序文件名为总结word文档.py,主要功能是批量总结Word文档。具体实现过程是解析docx格式和doc格式文件,生成文件内容,然后使用自然语言处理工具对文章内容做中英文概述,最后给出建议。该程序需要依赖python-docx和pywin32,如果没有安装,会给出安装建议。
125
+
126
+ ## [15/18] 程序摘要: crazy_functions/批量总结PDF文档pdfminer.py
127
+
128
+ 该程序文件名为pdfminer.py,位于./crazy_functions/目录下。程序实现了批量读取PDF文件,并使用pdfminer解析PDF文件内容。此外,程序还根据解析得到的文本内容,调用机器学习模型生成对每篇文章的概述,最终生成全文摘要。程序中还对模块依赖进行了导入检查,若缺少依赖,则会提供安装建议。
129
+
130
+ ## [16/18] 程序摘要: crazy_functions/解析项目源代码.py
131
+
132
+ 这个程序文件中包含了几个函数,分别是:
133
+
134
+ 1. `解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, history, systemPromptTxt)`:通过输入文件路径列表对程序文件进行逐文件分析,根据分析结果做出整体功能和构架的概括,并生成包括每个文件功能的markdown表格。
135
+ 2. `解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT)`:对当前文件夹下的所有Python文件及其子文件夹进行逐文件分析,并生成markdown表格。
136
+ 3. `解析一个Python项目(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT)`:对指定路径下的所有Python文件及其子文件夹进行逐文件分析,并生成markdown表格。
137
+ 4. `解析一个C项目的头文件(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT)`:对指定路径下的所有头文件进行逐文件分析,并生成markdown表格。
138
+ 5. `解析一个C项目(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT)`:对指定路径下的所有.h、.cpp、.c文件及其子文件夹进行逐文件分析,并生成markdown表格。
139
+
140
+ 程序中还包含了一些辅助函数和变量,如CatchException装饰器函数,report_execption函数、write_results_to_file函数等。在执行过程中还会调用其他模块中的函数,如toolbox模块的函数和predict模块的函数。
141
+
142
+ ## [17/18] 程序摘要: crazy_functions/批量总结PDF文档.py
143
+
144
+ 这个程序文件是一个名为“批量总结PDF文档”的函数插件。它导入了predict和toolbox模块,并定义了一些函数,包括is_paragraph_break,normalize_text和clean_text。这些函数是对输入文本进行预处理和清洗的功能函数。主要的功能函数是解析PDF,它打开每个PDF文件并将其内容存储在file_content变量中,然后传递给聊天机器人,以产生一句话的概括。在解析PDF文件之后,该函数连接了所有文件的摘要,以产生一段学术语言和英文摘要。最后,函数批量处理目标文件夹中的所有PDF文件,并输出结果。
145
+
146
+ ## 根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能。
147
+
148
+ 该程序是一个聊天机器人,使用了OpenAI的GPT语言模型以及一些特殊的辅助功能去处理各种学术写作和科研润色任务。整个程序由一些函数组成,每个函数都代表了不同的学术润色/翻译/其他服务。
149
+
150
+ 下面是程序中每个文件的功能列表:
151
+
152
+ | 文件名 | 功能 |
153
+ |--------|--------|
154
+ | functional_crazy.py | 实现高级功能函数模板和其他一些辅助功能函数 |
155
+ | main.py | 程序的主要入口,负责程序的启动和UI的展示 |
156
+ | functional.py | 定义各种功能按钮的颜色和响应函数 |
157
+ | show_math.py | 解析LaTeX文本,将其转换为Markdown格式 |
158
+ | predict.py | 基础的对话功能,用于与chatGPT进行交互 |
159
+ | check_proxy.py | 检查代理设置的正确性 |
160
+ | config_private.py | 配置程序的API密钥和其他私有信息 |
161
+ | config.py | 配置OpenAI的API参数和程序的其他属性 |
162
+ | theme.py | 设置程序主题样式 |
163
+ | toolbox.py | 存放一些辅助函数供程序使用 |
164
+ | crazy_functions/生成函数注释.py | 生成Python文件中所有函数的注释 |
165
+ | crazy_functions/读文章写摘要.py | 解析文章文本,生成中英文摘要 |
166
+ | crazy_functions/代码重写为全英文_多线程.py | 将中文代码内容转化为英文 |
167
+ | crazy_functions/高级功能函数模板.py | 实现高级功能函数模板 |
168
+ | crazy_functions/总结word文档.py | 解析Word文件,生成文章内容的概要 |
169
+ | crazy_functions/批量总结PDF文档pdfminer.py | 解析PDF文件,生成文章内容的概要(使用pdfminer库) |
170
+ | crazy_functions/批量总结PDF文档.py | 解析PDF文件,生成文章内容的概要(使用PyMuPDF库) |
171
+ | crazy_functions/解析项目源代码.py | 解析C/C++源代码,生成markdown表格 |
172
+ | crazy_functions/批量总结PDF文档.py | 对PDF文件进行批量摘要生成 |
173
+
174
+ 总的来说,该程序提供了一系列的学术润色和翻译的工具,支持对各种类型的文件进行分析和处理。同时也提供了对话式用户界面,便于用户使用和交互。
175
+
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio>=3.23
2
+ requests[socks]
3
+ mdtex2html
4
+ Markdown
5
+ latex2mathml
show_math.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This program is written by: https://github.com/polarwinkel/mdtex2html
2
+
3
+ from latex2mathml.converter import convert as tex2mathml
4
+ import re
5
+
6
+ incomplete = '<font style="color:orange;" class="tooltip">&#9888;<span class="tooltiptext">formula incomplete</span></font>'
7
+ convError = '<font style="color:red" class="tooltip">&#9888;<span class="tooltiptext">LaTeX-convert-error</span></font>'
8
+
9
+ def convert(mdtex, extensions=[], splitParagraphs=True):
10
+ ''' converts recursively the Markdown-LaTeX-mixture to HTML with MathML '''
11
+ found = False
12
+ # handle all paragraphs separately (prevents aftereffects)
13
+ if splitParagraphs:
14
+ parts = re.split("\n\n", mdtex)
15
+ result = ''
16
+ for part in parts:
17
+ result += convert(part, extensions, splitParagraphs=False)
18
+ return result
19
+ # find first $$-formula:
20
+ parts = re.split('\${2}', mdtex, 2)
21
+ if len(parts)>1:
22
+ found = True
23
+ result = convert(parts[0], extensions, splitParagraphs=False)+'\n'
24
+ try:
25
+ result += '<div class="blockformula">'+tex2mathml(parts[1])+'</div>\n'
26
+ except:
27
+ result += '<div class="blockformula">'+convError+'</div>'
28
+ if len(parts)==3:
29
+ result += convert(parts[2], extensions, splitParagraphs=False)
30
+ else:
31
+ result += '<div class="blockformula">'+incomplete+'</div>'
32
+ # else find first $-formulas:
33
+ else:
34
+ parts = re.split('\${1}', mdtex, 2)
35
+ if len(parts)>1 and not found:
36
+ found = True
37
+ try:
38
+ mathml = tex2mathml(parts[1])
39
+ except:
40
+ mathml = convError
41
+ if parts[0].endswith('\n\n') or parts[0]=='': # make sure textblock starts before formula!
42
+ parts[0]=parts[0]+'&#x200b;'
43
+ if len(parts)==3:
44
+ result = convert(parts[0]+mathml+parts[2], extensions, splitParagraphs=False)
45
+ else:
46
+ result = convert(parts[0]+mathml+incomplete, extensions, splitParagraphs=False)
47
+ # else find first \[..\]-equation:
48
+ else:
49
+ parts = re.split(r'\\\[', mdtex, 1)
50
+ if len(parts)>1 and not found:
51
+ found = True
52
+ result = convert(parts[0], extensions, splitParagraphs=False)+'\n'
53
+ parts = re.split(r'\\\]', parts[1], 1)
54
+ try:
55
+ result += '<div class="blockformula">'+tex2mathml(parts[0])+'</div>\n'
56
+ except:
57
+ result += '<div class="blockformula">'+convError+'</div>'
58
+ if len(parts)==2:
59
+ result += convert(parts[1], extensions, splitParagraphs=False)
60
+ else:
61
+ result += '<div class="blockformula">'+incomplete+'</div>'
62
+ # else find first \(..\)-equation:
63
+ else:
64
+ parts = re.split(r'\\\(', mdtex, 1)
65
+ if len(parts)>1 and not found:
66
+ found = True
67
+ subp = re.split(r'\\\)', parts[1], 1)
68
+ try:
69
+ mathml = tex2mathml(subp[0])
70
+ except:
71
+ mathml = convError
72
+ if parts[0].endswith('\n\n') or parts[0]=='': # make sure textblock starts before formula!
73
+ parts[0]=parts[0]+'&#x200b;'
74
+ if len(subp)==2:
75
+ result = convert(parts[0]+mathml+subp[1], extensions, splitParagraphs=False)
76
+ else:
77
+ result = convert(parts[0]+mathml+incomplete, extensions, splitParagraphs=False)
78
+ if not found:
79
+ result = mdtex
80
+ return result
theme.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # gradio可用颜色列表
4
+ # gr.themes.utils.colors.slate (石板色)
5
+ # gr.themes.utils.colors.gray (灰色)
6
+ # gr.themes.utils.colors.zinc (锌色)
7
+ # gr.themes.utils.colors.neutral (中性色)
8
+ # gr.themes.utils.colors.stone (石头色)
9
+ # gr.themes.utils.colors.red (红色)
10
+ # gr.themes.utils.colors.orange (橙色)
11
+ # gr.themes.utils.colors.amber (琥珀色)
12
+ # gr.themes.utils.colors.yellow (黄色)
13
+ # gr.themes.utils.colors.lime (酸橙色)
14
+ # gr.themes.utils.colors.green (绿色)
15
+ # gr.themes.utils.colors.emerald (祖母绿)
16
+ # gr.themes.utils.colors.teal (青蓝色)
17
+ # gr.themes.utils.colors.cyan (青色)
18
+ # gr.themes.utils.colors.sky (天蓝色)
19
+ # gr.themes.utils.colors.blue (蓝色)
20
+ # gr.themes.utils.colors.indigo (靛蓝色)
21
+ # gr.themes.utils.colors.violet (紫罗兰色)
22
+ # gr.themes.utils.colors.purple (紫色)
23
+ # gr.themes.utils.colors.fuchsia (洋红色)
24
+ # gr.themes.utils.colors.pink (粉红色)
25
+ # gr.themes.utils.colors.rose (玫瑰色)
26
+
27
+ def adjust_theme():
28
+ try:
29
+ color_er = gr.themes.utils.colors.pink
30
+ set_theme = gr.themes.Default(
31
+ primary_hue=gr.themes.utils.colors.orange,
32
+ neutral_hue=gr.themes.utils.colors.gray,
33
+ font=["sans-serif", "Microsoft YaHei", "ui-sans-serif", "system-ui", "sans-serif", gr.themes.utils.fonts.GoogleFont("Source Sans Pro")],
34
+ font_mono=["ui-monospace", "Consolas", "monospace", gr.themes.utils.fonts.GoogleFont("IBM Plex Mono")])
35
+ set_theme.set(
36
+ # Colors
37
+ input_background_fill_dark="*neutral_800",
38
+ # Transition
39
+ button_transition="none",
40
+ # Shadows
41
+ button_shadow="*shadow_drop",
42
+ button_shadow_hover="*shadow_drop_lg",
43
+ button_shadow_active="*shadow_inset",
44
+ input_shadow="0 0 0 *shadow_spread transparent, *shadow_inset",
45
+ input_shadow_focus="0 0 0 *shadow_spread *secondary_50, *shadow_inset",
46
+ input_shadow_focus_dark="0 0 0 *shadow_spread *neutral_700, *shadow_inset",
47
+ checkbox_label_shadow="*shadow_drop",
48
+ block_shadow="*shadow_drop",
49
+ form_gap_width="1px",
50
+ # Button borders
51
+ input_border_width="1px",
52
+ input_background_fill="white",
53
+ # Gradients
54
+ stat_background_fill="linear-gradient(to right, *primary_400, *primary_200)",
55
+ stat_background_fill_dark="linear-gradient(to right, *primary_400, *primary_600)",
56
+ error_background_fill=f"linear-gradient(to right, {color_er.c100}, *background_fill_secondary)",
57
+ error_background_fill_dark="*background_fill_primary",
58
+ checkbox_label_background_fill="linear-gradient(to top, *neutral_50, white)",
59
+ checkbox_label_background_fill_dark="linear-gradient(to top, *neutral_900, *neutral_800)",
60
+ checkbox_label_background_fill_hover="linear-gradient(to top, *neutral_100, white)",
61
+ checkbox_label_background_fill_hover_dark="linear-gradient(to top, *neutral_900, *neutral_800)",
62
+ button_primary_background_fill="linear-gradient(to bottom right, *primary_100, *primary_300)",
63
+ button_primary_background_fill_dark="linear-gradient(to bottom right, *primary_500, *primary_600)",
64
+ button_primary_background_fill_hover="linear-gradient(to bottom right, *primary_100, *primary_200)",
65
+ button_primary_background_fill_hover_dark="linear-gradient(to bottom right, *primary_500, *primary_500)",
66
+ button_primary_border_color_dark="*primary_500",
67
+ button_secondary_background_fill="linear-gradient(to bottom right, *neutral_100, *neutral_200)",
68
+ button_secondary_background_fill_dark="linear-gradient(to bottom right, *neutral_600, *neutral_700)",
69
+ button_secondary_background_fill_hover="linear-gradient(to bottom right, *neutral_100, *neutral_100)",
70
+ button_secondary_background_fill_hover_dark="linear-gradient(to bottom right, *neutral_600, *neutral_600)",
71
+ button_cancel_background_fill=f"linear-gradient(to bottom right, {color_er.c100}, {color_er.c200})",
72
+ button_cancel_background_fill_dark=f"linear-gradient(to bottom right, {color_er.c600}, {color_er.c700})",
73
+ button_cancel_background_fill_hover=f"linear-gradient(to bottom right, {color_er.c100}, {color_er.c100})",
74
+ button_cancel_background_fill_hover_dark=f"linear-gradient(to bottom right, {color_er.c600}, {color_er.c600})",
75
+ button_cancel_border_color=color_er.c200,
76
+ button_cancel_border_color_dark=color_er.c600,
77
+ button_cancel_text_color=color_er.c600,
78
+ button_cancel_text_color_dark="white",
79
+ )
80
+ except:
81
+ set_theme = None; print('gradio版本较旧, 不能自定义字体和颜色')
82
+ return set_theme
83
+
84
+ advanced_css = """
85
+ .markdown-body table {
86
+ border: 1px solid #ddd;
87
+ border-collapse: collapse;
88
+ }
89
+
90
+ .markdown-body th, .markdown-body td {
91
+ border: 1px solid #ddd;
92
+ padding: 5px;
93
+ }
94
+ """
toolbox.py ADDED
@@ -0,0 +1,325 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import markdown, mdtex2html, threading, importlib, traceback, importlib, inspect, re
2
+ from show_math import convert as convert_math
3
+ from functools import wraps, lru_cache
4
+
5
+ def get_reduce_token_percent(text):
6
+ try:
7
+ # text = "maximum context length is 4097 tokens. However, your messages resulted in 4870 tokens"
8
+ pattern = r"(\d+)\s+tokens\b"
9
+ match = re.findall(pattern, text)
10
+ EXCEED_ALLO = 500 # 稍微留一点余地,否则在回复时会因余量太少出问题
11
+ max_limit = float(match[0]) - EXCEED_ALLO
12
+ current_tokens = float(match[1])
13
+ ratio = max_limit/current_tokens
14
+ assert ratio > 0 and ratio < 1
15
+ return ratio, str(int(current_tokens-max_limit))
16
+ except:
17
+ return 0.5, '不详'
18
+
19
+ def predict_no_ui_but_counting_down(i_say, i_say_show_user, chatbot, top_p, temperature, history=[], sys_prompt='', long_connection=True):
20
+ """
21
+ 调用简单的predict_no_ui接口,但是依然保留了些许界面心跳功能,当对话太长时,会自动采用二分法截断
22
+ i_say: 当前输入
23
+ i_say_show_user: 显示到对话界面上的当前输入,例如,输入整个文件时,你绝对不想把文件的内容都糊到对话界面上
24
+ chatbot: 对话界面句柄
25
+ top_p, temperature: gpt参数
26
+ history: gpt参数 对话历史
27
+ sys_prompt: gpt参数 sys_prompt
28
+ long_connection: 是否采用更稳定的连接方式(推荐)
29
+ """
30
+ import time
31
+ from predict import predict_no_ui, predict_no_ui_long_connection
32
+ from toolbox import get_conf
33
+ TIMEOUT_SECONDS, MAX_RETRY = get_conf('TIMEOUT_SECONDS', 'MAX_RETRY')
34
+ # 多线程的时候,需要一个mutable结构在不同线程之间传递信息
35
+ # list就是最简单的mutable结构,我们第一个位置放gpt输出,第二个位置传递报错信息
36
+ mutable = [None, '']
37
+ # multi-threading worker
38
+ def mt(i_say, history):
39
+ while True:
40
+ try:
41
+ if long_connection:
42
+ mutable[0] = predict_no_ui_long_connection(inputs=i_say, top_p=top_p, temperature=temperature, history=history, sys_prompt=sys_prompt)
43
+ else:
44
+ mutable[0] = predict_no_ui(inputs=i_say, top_p=top_p, temperature=temperature, history=history, sys_prompt=sys_prompt)
45
+ break
46
+ except ConnectionAbortedError as token_exceeded_error:
47
+ # 尝试计算比例,尽可能多地保留文本
48
+ p_ratio, n_exceed = get_reduce_token_percent(str(token_exceeded_error))
49
+ if len(history) > 0:
50
+ history = [his[ int(len(his) *p_ratio): ] for his in history if his is not None]
51
+ else:
52
+ i_say = i_say[: int(len(i_say) *p_ratio) ]
53
+ mutable[1] = f'警告,文本过长将进行截断,Token溢出数:{n_exceed},截断比例:{(1-p_ratio):.0%}。'
54
+ except TimeoutError as e:
55
+ mutable[0] = '[Local Message] 请求超时。'
56
+ raise TimeoutError
57
+ except Exception as e:
58
+ mutable[0] = f'[Local Message] 异常:{str(e)}.'
59
+ raise RuntimeError(f'[Local Message] 异常:{str(e)}.')
60
+ # 创建新线程发出http请求
61
+ thread_name = threading.Thread(target=mt, args=(i_say, history)); thread_name.start()
62
+ # 原来的线程则负责持续更新UI,实现一个超时倒计时,并等待新线程的任务完成
63
+ cnt = 0
64
+ while thread_name.is_alive():
65
+ cnt += 1
66
+ chatbot[-1] = (i_say_show_user, f"[Local Message] {mutable[1]}waiting gpt response {cnt}/{TIMEOUT_SECONDS*2*(MAX_RETRY+1)}"+''.join(['.']*(cnt%4)))
67
+ yield chatbot, history, '正常'
68
+ time.sleep(1)
69
+ # 把gpt的输出从mutable中取出来
70
+ gpt_say = mutable[0]
71
+ if gpt_say=='[Local Message] Failed with timeout.': raise TimeoutError
72
+ return gpt_say
73
+
74
+ def write_results_to_file(history, file_name=None):
75
+ """
76
+ 将对话记录history以Markdown格式写入文件中。如果没有指定文件名,则使用当前时间生成文件名。
77
+ """
78
+ import os, time
79
+ if file_name is None:
80
+ # file_name = time.strftime("chatGPT分析报告%Y-%m-%d-%H-%M-%S", time.localtime()) + '.md'
81
+ file_name = 'chatGPT分析报告' + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) + '.md'
82
+ os.makedirs('./gpt_log/', exist_ok=True)
83
+ with open(f'./gpt_log/{file_name}', 'w', encoding = 'utf8') as f:
84
+ f.write('# chatGPT 分析报告\n')
85
+ for i, content in enumerate(history):
86
+ try: # 这个bug没找到触发条件,暂时先这样顶一下
87
+ if type(content) != str: content = str(content)
88
+ except:
89
+ continue
90
+ if i%2==0: f.write('## ')
91
+ f.write(content)
92
+ f.write('\n\n')
93
+ res = '以上材料已经被写入' + os.path.abspath(f'./gpt_log/{file_name}')
94
+ print(res)
95
+ return res
96
+
97
+ def regular_txt_to_markdown(text):
98
+ """
99
+ 将普通文本转换为Markdown格式的文本。
100
+ """
101
+ text = text.replace('\n', '\n\n')
102
+ text = text.replace('\n\n\n', '\n\n')
103
+ text = text.replace('\n\n\n', '\n\n')
104
+ return text
105
+
106
+ def CatchException(f):
107
+ """
108
+ 装饰器函数,捕捉函数f中的异常并封装到一个生成器中返回,并显示到聊天当中。
109
+ """
110
+ @wraps(f)
111
+ def decorated(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT):
112
+ try:
113
+ yield from f(txt, top_p, temperature, chatbot, history, systemPromptTxt, WEB_PORT)
114
+ except Exception as e:
115
+ from check_proxy import check_proxy
116
+ from toolbox import get_conf
117
+ proxies, = get_conf('proxies')
118
+ tb_str = regular_txt_to_markdown(traceback.format_exc())
119
+ chatbot[-1] = (chatbot[-1][0], f"[Local Message] 实验性函数调用出错: \n\n {tb_str} \n\n 当前代理可用性: \n\n {check_proxy(proxies)}")
120
+ yield chatbot, history, f'异常 {e}'
121
+ return decorated
122
+
123
+ def HotReload(f):
124
+ """
125
+ 装饰器函数,实现函数插件热更新
126
+ """
127
+ @wraps(f)
128
+ def decorated(*args, **kwargs):
129
+ fn_name = f.__name__
130
+ f_hot_reload = getattr(importlib.reload(inspect.getmodule(f)), fn_name)
131
+ yield from f_hot_reload(*args, **kwargs)
132
+ return decorated
133
+
134
+ def report_execption(chatbot, history, a, b):
135
+ """
136
+ 向chatbot中添加错误信息
137
+ """
138
+ chatbot.append((a, b))
139
+ history.append(a); history.append(b)
140
+
141
+ def text_divide_paragraph(text):
142
+ """
143
+ 将文本按照段落分隔符分割开,生成带有段落标签的HTML代码。
144
+ """
145
+ if '```' in text:
146
+ # careful input
147
+ return text
148
+ else:
149
+ # wtf input
150
+ lines = text.split("\n")
151
+ for i, line in enumerate(lines):
152
+ lines[i] = lines[i].replace(" ", "&nbsp;")
153
+ text = "</br>".join(lines)
154
+ return text
155
+
156
+ def markdown_convertion(txt):
157
+ """
158
+ 将Markdown格式的文本转换为HTML格式。如果包含数学公式,则先将公式转换为HTML格式。
159
+ """
160
+ pre = '<div class="markdown-body">'
161
+ suf = '</div>'
162
+ if ('$' in txt) and ('```' not in txt):
163
+ return pre + markdown.markdown(txt,extensions=['fenced_code','tables']) + '<br><br>' + markdown.markdown(convert_math(txt, splitParagraphs=False),extensions=['fenced_code','tables']) + suf
164
+ else:
165
+ return pre + markdown.markdown(txt,extensions=['fenced_code','tables']) + suf
166
+
167
+
168
+ def format_io(self, y):
169
+ """
170
+ 将输入和输出解析为HTML格式。将y中最后一项的输入部分段落化,并将输出部分的Markdown和数学公式转换为HTML格式。
171
+ """
172
+ if y is None or y == []: return []
173
+ i_ask, gpt_reply = y[-1]
174
+ i_ask = text_divide_paragraph(i_ask) # 输入部分太自由,预处理一波
175
+ y[-1] = (
176
+ None if i_ask is None else markdown.markdown(i_ask, extensions=['fenced_code','tables']),
177
+ None if gpt_reply is None else markdown_convertion(gpt_reply)
178
+ )
179
+ return y
180
+
181
+
182
+ def find_free_port():
183
+ """
184
+ 返回当前系统中可用的未使用端口。
185
+ """
186
+ import socket
187
+ from contextlib import closing
188
+ with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
189
+ s.bind(('', 0))
190
+ s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
191
+ return s.getsockname()[1]
192
+
193
+
194
+ def extract_archive(file_path, dest_dir):
195
+ import zipfile
196
+ import tarfile
197
+ import os
198
+ # Get the file extension of the input file
199
+ file_extension = os.path.splitext(file_path)[1]
200
+
201
+ # Extract the archive based on its extension
202
+ if file_extension == '.zip':
203
+ with zipfile.ZipFile(file_path, 'r') as zipobj:
204
+ zipobj.extractall(path=dest_dir)
205
+ print("Successfully extracted zip archive to {}".format(dest_dir))
206
+
207
+ elif file_extension in ['.tar', '.gz', '.bz2']:
208
+ with tarfile.open(file_path, 'r:*') as tarobj:
209
+ tarobj.extractall(path=dest_dir)
210
+ print("Successfully extracted tar archive to {}".format(dest_dir))
211
+
212
+ # 第三方库,需要预先pip install rarfile
213
+ # 此外,Windows上还需要安装winrar软件,配置其Path环境变量,如"C:\Program Files\WinRAR"才可以
214
+ elif file_extension == '.rar':
215
+ try:
216
+ import rarfile
217
+ with rarfile.RarFile(file_path) as rf:
218
+ rf.extractall(path=dest_dir)
219
+ print("Successfully extracted rar archive to {}".format(dest_dir))
220
+ except:
221
+ print("Rar format requires additional dependencies to install")
222
+ return '\n\n需要安装pip install rarfile来解压rar文件'
223
+
224
+ # 第三方库,需要预先pip install py7zr
225
+ elif file_extension == '.7z':
226
+ try:
227
+ import py7zr
228
+ with py7zr.SevenZipFile(file_path, mode='r') as f:
229
+ f.extractall(path=dest_dir)
230
+ print("Successfully extracted 7z archive to {}".format(dest_dir))
231
+ except:
232
+ print("7z format requires additional dependencies to install")
233
+ return '\n\n需要安装pip install py7zr来解压7z文件'
234
+ else:
235
+ return ''
236
+ return ''
237
+
238
+ def find_recent_files(directory):
239
+ """
240
+ me: find files that is created with in one minutes under a directory with python, write a function
241
+ gpt: here it is!
242
+ """
243
+ import os
244
+ import time
245
+ current_time = time.time()
246
+ one_minute_ago = current_time - 60
247
+ recent_files = []
248
+
249
+ for filename in os.listdir(directory):
250
+ file_path = os.path.join(directory, filename)
251
+ if file_path.endswith('.log'): continue
252
+ created_time = os.path.getctime(file_path)
253
+ if created_time >= one_minute_ago:
254
+ if os.path.isdir(file_path): continue
255
+ recent_files.append(file_path)
256
+
257
+ return recent_files
258
+
259
+
260
+ def on_file_uploaded(files, chatbot, txt):
261
+ if len(files) == 0: return chatbot, txt
262
+ import shutil, os, time, glob
263
+ from toolbox import extract_archive
264
+ try: shutil.rmtree('./private_upload/')
265
+ except: pass
266
+ time_tag = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
267
+ os.makedirs(f'private_upload/{time_tag}', exist_ok=True)
268
+ err_msg = ''
269
+ for file in files:
270
+ file_origin_name = os.path.basename(file.orig_name)
271
+ shutil.copy(file.name, f'private_upload/{time_tag}/{file_origin_name}')
272
+ err_msg += extract_archive(f'private_upload/{time_tag}/{file_origin_name}',
273
+ dest_dir=f'private_upload/{time_tag}/{file_origin_name}.extract')
274
+ moved_files = [fp for fp in glob.glob('private_upload/**/*', recursive=True)]
275
+ txt = f'private_upload/{time_tag}'
276
+ moved_files_str = '\t\n\n'.join(moved_files)
277
+ chatbot.append(['我上传了文件,请查收',
278
+ f'[Local Message] 收到以下文件: \n\n{moved_files_str}'+
279
+ f'\n\n调用路径参数已自动修正到: \n\n{txt}'+
280
+ f'\n\n现在您点击任意实验功能时,以上文件将被作为输入参数'+err_msg])
281
+ return chatbot, txt
282
+
283
+
284
+ def on_report_generated(files, chatbot):
285
+ from toolbox import find_recent_files
286
+ report_files = find_recent_files('gpt_log')
287
+ if len(report_files) == 0: return report_files, chatbot
288
+ # files.extend(report_files)
289
+ chatbot.append(['汇总报告如何远程获取?', '汇总报告已经添加到右侧“文件上传区”(可能处于折叠状态),请查收。'])
290
+ return report_files, chatbot
291
+
292
+ @lru_cache(maxsize=128)
293
+ def read_single_conf_with_lru_cache(arg):
294
+ try: r = getattr(importlib.import_module('config_private'), arg)
295
+ except: r = getattr(importlib.import_module('config'), arg)
296
+ # 在读取API_KEY时,检查一下是不是忘了改config
297
+ if arg=='API_KEY':
298
+ # 正确的 API_KEY 是 "sk-" + 48 位大小写字母数字的组合
299
+ API_MATCH = re.match(r"sk-[a-zA-Z0-9]{48}$", r)
300
+ if API_MATCH:
301
+ print(f"[API_KEY] 您的 API_KEY 是: {r[:15]}*** API_KEY 导入成功")
302
+ else:
303
+ assert False, "正确的 API_KEY 是 'sk-' + '48 位大小写字母数字' 的组合,请在config文件中修改API密钥, 添加海外代理之后再运行。" + \
304
+ "(如果您刚更新过代码,请确保旧版config_private文件中没有遗留任何新增键值)"
305
+ if arg=='proxies':
306
+ if r is None:
307
+ print('[PROXY] 网络代理状态:未配置。无代理状态下很可能无法访问。建议:检查USE_PROXY选项是否修改。')
308
+ else:
309
+ print('[PROXY] 网络代理状态:已配置。配置信息如下:', r)
310
+ assert isinstance(r, dict), 'proxies格式错误,请注意proxies选项的格式,不要遗漏括号。'
311
+ return r
312
+
313
+ def get_conf(*args):
314
+ # 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
315
+ res = []
316
+ for arg in args:
317
+ r = read_single_conf_with_lru_cache(arg)
318
+ res.append(r)
319
+ return res
320
+
321
+ def clear_line_break(txt):
322
+ txt = txt.replace('\n', ' ')
323
+ txt = txt.replace(' ', ' ')
324
+ txt = txt.replace(' ', ' ')
325
+ return txt