Spaces:
Sleeping
Sleeping
JohnSmith9982
commited on
Commit
•
0e9e479
1
Parent(s):
11ddf78
Github https://github.com/GaiZhenbiao/ChuanhuChatGPT/commit/70ec74266733f9c8b6d41882d29231bf178f20e4
Browse files- Dockerfile +14 -0
- LICENSE +674 -0
- README.md +4 -4
- app.py +74 -13
- assets/favicon.png +0 -0
- chat_func.py +58 -25
- chatgpt - macOS.command +7 -0
- chatgpt - windows.bat +14 -0
- custom.css +119 -81
- overwrites.py +6 -8
- presets.py +7 -0
- requirements.txt +1 -0
- templates/3 川虎的Prompts.json +4 -0
- utils.py +122 -20
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9 as builder
|
2 |
+
RUN apt-get update && apt-get install -y build-essential
|
3 |
+
COPY requirements.txt .
|
4 |
+
RUN pip install --user -r requirements.txt
|
5 |
+
|
6 |
+
FROM python:3.9
|
7 |
+
MAINTAINER iskoldt
|
8 |
+
COPY --from=builder /root/.local /root/.local
|
9 |
+
ENV PATH=/root/.local/bin:$PATH
|
10 |
+
COPY . /app
|
11 |
+
WORKDIR /app
|
12 |
+
ENV my_api_key empty
|
13 |
+
ENV dockerrun yes
|
14 |
+
CMD ["python3", "-u", "ChuanhuChatbot.py", "2>&1", "|", "tee", "/var/log/application.log"]
|
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,13 +1,13 @@
|
|
1 |
---
|
2 |
title: ChuanhuChatGPT
|
3 |
-
emoji:
|
4 |
colorFrom: blue
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
license:
|
11 |
---
|
12 |
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
title: ChuanhuChatGPT
|
3 |
+
emoji: 🐯
|
4 |
colorFrom: blue
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.22.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: gpl-3.0
|
11 |
---
|
12 |
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
@@ -59,6 +59,66 @@ with open("custom.css", "r", encoding="utf-8") as f:
|
|
59 |
|
60 |
with gr.Blocks(
|
61 |
css=customCSS,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
) as demo:
|
63 |
history = gr.State([])
|
64 |
token_count = gr.State([])
|
@@ -70,7 +130,7 @@ with gr.Blocks(
|
|
70 |
|
71 |
with gr.Row():
|
72 |
gr.HTML(title)
|
73 |
-
status_display = gr.Markdown(
|
74 |
|
75 |
with gr.Row(scale=1).style(equal_height=True):
|
76 |
with gr.Column(scale=5):
|
@@ -100,7 +160,7 @@ with gr.Blocks(
|
|
100 |
value=hide_middle_chars(my_api_key),
|
101 |
type="password",
|
102 |
visible=not HIDE_MY_KEY,
|
103 |
-
label="API-Key
|
104 |
)
|
105 |
model_select_dropdown = gr.Dropdown(
|
106 |
label="选择模型", choices=MODELS, multiselect=False, value=MODELS[0]
|
@@ -118,7 +178,7 @@ with gr.Blocks(
|
|
118 |
label="System prompt",
|
119 |
value=initial_prompt,
|
120 |
lines=10,
|
121 |
-
).style(container=
|
122 |
with gr.Accordion(label="加载Prompt模板", open=True):
|
123 |
with gr.Column():
|
124 |
with gr.Row():
|
@@ -128,7 +188,7 @@ with gr.Blocks(
|
|
128 |
choices=get_template_names(plain=True),
|
129 |
multiselect=False,
|
130 |
value=get_template_names(plain=True)[0],
|
131 |
-
)
|
132 |
with gr.Column(scale=1):
|
133 |
templateRefreshBtn = gr.Button("🔄 刷新")
|
134 |
with gr.Row():
|
@@ -142,7 +202,7 @@ with gr.Blocks(
|
|
142 |
value=load_template(
|
143 |
get_template_names(plain=True)[0], mode=1
|
144 |
)[0],
|
145 |
-
)
|
146 |
|
147 |
with gr.Tab(label="保存/加载"):
|
148 |
with gr.Accordion(label="保存/加载对话历史记录", open=True):
|
@@ -184,7 +244,7 @@ with gr.Blocks(
|
|
184 |
value=1.0,
|
185 |
step=0.05,
|
186 |
interactive=True,
|
187 |
-
label="Top-p
|
188 |
)
|
189 |
temperature = gr.Slider(
|
190 |
minimum=-0,
|
@@ -231,7 +291,7 @@ with gr.Blocks(
|
|
231 |
use_streaming_checkbox,
|
232 |
model_select_dropdown,
|
233 |
use_websearch_checkbox,
|
234 |
-
index_files
|
235 |
],
|
236 |
[chatbot, history, status_display, token_count],
|
237 |
show_progress=True,
|
@@ -252,7 +312,7 @@ with gr.Blocks(
|
|
252 |
use_streaming_checkbox,
|
253 |
model_select_dropdown,
|
254 |
use_websearch_checkbox,
|
255 |
-
index_files
|
256 |
],
|
257 |
[chatbot, history, status_display, token_count],
|
258 |
show_progress=True,
|
@@ -299,7 +359,7 @@ with gr.Blocks(
|
|
299 |
token_count,
|
300 |
top_p,
|
301 |
temperature,
|
302 |
-
|
303 |
model_select_dropdown,
|
304 |
],
|
305 |
[chatbot, history, status_display, token_count],
|
@@ -378,16 +438,17 @@ if __name__ == "__main__":
|
|
378 |
if dockerflag:
|
379 |
if authflag:
|
380 |
demo.queue().launch(
|
381 |
-
server_name="0.0.0.0", server_port=7860, auth=(username, password)
|
|
|
382 |
)
|
383 |
else:
|
384 |
-
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
385 |
# if not running in Docker
|
386 |
else:
|
387 |
if authflag:
|
388 |
-
demo.queue().launch(share=False, auth=(username, password))
|
389 |
else:
|
390 |
-
demo.queue().launch(share=False) # 改为 share=True 可以创建公开分享链接
|
391 |
# demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False) # 可自定义端口
|
392 |
# demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=("在这里填写用户名", "在这里填写密码")) # 可设置用户名与密码
|
393 |
# demo.queue().launch(auth=("在这里填写用户名", "在这里填写密码")) # 适合Nginx反向代理
|
|
|
59 |
|
60 |
with gr.Blocks(
|
61 |
css=customCSS,
|
62 |
+
theme=gr.themes.Soft(
|
63 |
+
primary_hue=gr.themes.Color(
|
64 |
+
c50="#02C160",
|
65 |
+
c100="rgba(2, 193, 96, 0.2)",
|
66 |
+
c200="#02C160",
|
67 |
+
c300="rgba(2, 193, 96, 0.32)",
|
68 |
+
c400="rgba(2, 193, 96, 0.32)",
|
69 |
+
c500="rgba(2, 193, 96, 1.0)",
|
70 |
+
c600="rgba(2, 193, 96, 1.0)",
|
71 |
+
c700="rgba(2, 193, 96, 0.32)",
|
72 |
+
c800="rgba(2, 193, 96, 0.32)",
|
73 |
+
c900="#02C160",
|
74 |
+
c950="#02C160",
|
75 |
+
),
|
76 |
+
secondary_hue=gr.themes.Color(
|
77 |
+
c50="#576b95",
|
78 |
+
c100="#576b95",
|
79 |
+
c200="#576b95",
|
80 |
+
c300="#576b95",
|
81 |
+
c400="#576b95",
|
82 |
+
c500="#576b95",
|
83 |
+
c600="#576b95",
|
84 |
+
c700="#576b95",
|
85 |
+
c800="#576b95",
|
86 |
+
c900="#576b95",
|
87 |
+
c950="#576b95",
|
88 |
+
),
|
89 |
+
neutral_hue=gr.themes.Color(
|
90 |
+
name="gray",
|
91 |
+
c50="#f9fafb",
|
92 |
+
c100="#f3f4f6",
|
93 |
+
c200="#e5e7eb",
|
94 |
+
c300="#d1d5db",
|
95 |
+
c400="#B2B2B2",
|
96 |
+
c500="#808080",
|
97 |
+
c600="#636363",
|
98 |
+
c700="#515151",
|
99 |
+
c800="#393939",
|
100 |
+
c900="#272727",
|
101 |
+
c950="#171717",
|
102 |
+
),
|
103 |
+
radius_size=gr.themes.sizes.radius_sm,
|
104 |
+
).set(
|
105 |
+
button_primary_background_fill="#06AE56",
|
106 |
+
button_primary_background_fill_dark="#06AE56",
|
107 |
+
button_primary_background_fill_hover="#07C863",
|
108 |
+
button_primary_border_color="#06AE56",
|
109 |
+
button_primary_border_color_dark="#06AE56",
|
110 |
+
button_primary_text_color="#FFFFFF",
|
111 |
+
button_primary_text_color_dark="#FFFFFF",
|
112 |
+
button_secondary_background_fill="#F2F2F2",
|
113 |
+
button_secondary_background_fill_dark="#2B2B2B",
|
114 |
+
button_secondary_text_color="#393939",
|
115 |
+
button_secondary_text_color_dark="#FFFFFF",
|
116 |
+
# background_fill_primary="#F7F7F7",
|
117 |
+
# background_fill_primary_dark="#1F1F1F",
|
118 |
+
block_title_text_color="*primary_500",
|
119 |
+
block_title_background_fill="*primary_100",
|
120 |
+
input_background_fill="#F6F6F6",
|
121 |
+
),
|
122 |
) as demo:
|
123 |
history = gr.State([])
|
124 |
token_count = gr.State([])
|
|
|
130 |
|
131 |
with gr.Row():
|
132 |
gr.HTML(title)
|
133 |
+
status_display = gr.Markdown(get_geoip(), elem_id="status_display")
|
134 |
|
135 |
with gr.Row(scale=1).style(equal_height=True):
|
136 |
with gr.Column(scale=5):
|
|
|
160 |
value=hide_middle_chars(my_api_key),
|
161 |
type="password",
|
162 |
visible=not HIDE_MY_KEY,
|
163 |
+
label="API-Key",
|
164 |
)
|
165 |
model_select_dropdown = gr.Dropdown(
|
166 |
label="选择模型", choices=MODELS, multiselect=False, value=MODELS[0]
|
|
|
178 |
label="System prompt",
|
179 |
value=initial_prompt,
|
180 |
lines=10,
|
181 |
+
).style(container=False)
|
182 |
with gr.Accordion(label="加载Prompt模板", open=True):
|
183 |
with gr.Column():
|
184 |
with gr.Row():
|
|
|
188 |
choices=get_template_names(plain=True),
|
189 |
multiselect=False,
|
190 |
value=get_template_names(plain=True)[0],
|
191 |
+
).style(container=False)
|
192 |
with gr.Column(scale=1):
|
193 |
templateRefreshBtn = gr.Button("🔄 刷新")
|
194 |
with gr.Row():
|
|
|
202 |
value=load_template(
|
203 |
get_template_names(plain=True)[0], mode=1
|
204 |
)[0],
|
205 |
+
).style(container=False)
|
206 |
|
207 |
with gr.Tab(label="保存/加载"):
|
208 |
with gr.Accordion(label="保存/加载对话历史记录", open=True):
|
|
|
244 |
value=1.0,
|
245 |
step=0.05,
|
246 |
interactive=True,
|
247 |
+
label="Top-p",
|
248 |
)
|
249 |
temperature = gr.Slider(
|
250 |
minimum=-0,
|
|
|
291 |
use_streaming_checkbox,
|
292 |
model_select_dropdown,
|
293 |
use_websearch_checkbox,
|
294 |
+
index_files,
|
295 |
],
|
296 |
[chatbot, history, status_display, token_count],
|
297 |
show_progress=True,
|
|
|
312 |
use_streaming_checkbox,
|
313 |
model_select_dropdown,
|
314 |
use_websearch_checkbox,
|
315 |
+
index_files,
|
316 |
],
|
317 |
[chatbot, history, status_display, token_count],
|
318 |
show_progress=True,
|
|
|
359 |
token_count,
|
360 |
top_p,
|
361 |
temperature,
|
362 |
+
gr.State(0),
|
363 |
model_select_dropdown,
|
364 |
],
|
365 |
[chatbot, history, status_display, token_count],
|
|
|
438 |
if dockerflag:
|
439 |
if authflag:
|
440 |
demo.queue().launch(
|
441 |
+
server_name="0.0.0.0", server_port=7860, auth=(username, password),
|
442 |
+
favicon_path="./assets/favicon.png"
|
443 |
)
|
444 |
else:
|
445 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False, favicon_path="./assets/favicon.png")
|
446 |
# if not running in Docker
|
447 |
else:
|
448 |
if authflag:
|
449 |
+
demo.queue().launch(share=False, auth=(username, password), favicon_path="./assets/favicon.png", inbrowser=True)
|
450 |
else:
|
451 |
+
demo.queue().launch(share=False, favicon_path="./assets/favicon.png", inbrowser=True) # 改为 share=True 可以创建公开分享链接
|
452 |
# demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False) # 可自定义端口
|
453 |
# demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=("在这里填写用户名", "在这里填写密码")) # 可设置用户名与密码
|
454 |
# demo.queue().launch(auth=("在这里填写用户名", "在这里填写密码")) # 适合Nginx反向代理
|
assets/favicon.png
ADDED
chat_func.py
CHANGED
@@ -6,10 +6,13 @@ import logging
|
|
6 |
import json
|
7 |
import os
|
8 |
import requests
|
|
|
9 |
|
10 |
from tqdm import tqdm
|
11 |
import colorama
|
12 |
from duckduckgo_search import ddg
|
|
|
|
|
13 |
|
14 |
from presets import *
|
15 |
from llama_func import *
|
@@ -99,6 +102,8 @@ def stream_predict(
|
|
99 |
top_p,
|
100 |
temperature,
|
101 |
selected_model,
|
|
|
|
|
102 |
):
|
103 |
def get_return_value():
|
104 |
return chatbot, history, status_text, all_token_counts
|
@@ -109,7 +114,10 @@ def stream_predict(
|
|
109 |
status_text = "开始实时传输回答……"
|
110 |
history.append(construct_user(inputs))
|
111 |
history.append(construct_assistant(""))
|
112 |
-
|
|
|
|
|
|
|
113 |
user_token_count = 0
|
114 |
if len(all_token_counts) == 0:
|
115 |
system_prompt_token_count = count_token(construct_system(system_prompt))
|
@@ -184,7 +192,7 @@ def stream_predict(
|
|
184 |
yield get_return_value()
|
185 |
break
|
186 |
history[-1] = construct_assistant(partial_words)
|
187 |
-
chatbot[-1] = (
|
188 |
all_token_counts[-1] += 1
|
189 |
yield get_return_value()
|
190 |
|
@@ -199,11 +207,16 @@ def predict_all(
|
|
199 |
top_p,
|
200 |
temperature,
|
201 |
selected_model,
|
|
|
|
|
202 |
):
|
203 |
logging.info("一次性回答模式")
|
204 |
history.append(construct_user(inputs))
|
205 |
history.append(construct_assistant(""))
|
206 |
-
|
|
|
|
|
|
|
207 |
all_token_counts.append(count_token(construct_user(inputs)))
|
208 |
try:
|
209 |
response = get_response(
|
@@ -229,7 +242,7 @@ def predict_all(
|
|
229 |
response = json.loads(response.text)
|
230 |
content = response["choices"][0]["message"]["content"]
|
231 |
history[-1] = construct_assistant(content)
|
232 |
-
chatbot[-1] = (
|
233 |
total_token_count = response["usage"]["total_tokens"]
|
234 |
all_token_counts[-1] = total_token_count - sum(all_token_counts)
|
235 |
status_text = construct_token_message(total_token_count)
|
@@ -247,7 +260,7 @@ def predict(
|
|
247 |
temperature,
|
248 |
stream=False,
|
249 |
selected_model=MODELS[0],
|
250 |
-
|
251 |
files = None,
|
252 |
should_check_token_count=True,
|
253 |
): # repetition_penalty, top_k
|
@@ -262,22 +275,31 @@ def predict(
|
|
262 |
history, chatbot, status_text = chat_ai(openai_api_key, index, inputs, history, chatbot)
|
263 |
yield chatbot, history, status_text, all_token_counts
|
264 |
return
|
265 |
-
|
266 |
-
|
|
|
|
|
|
|
|
|
267 |
web_results = []
|
268 |
-
for idx, result in enumerate(
|
269 |
logging.info(f"搜索结果{idx + 1}:{result}")
|
|
|
270 |
web_results.append(f'[{idx+1}]"{result["body"]}"\nURL: {result["href"]}')
|
271 |
-
|
|
|
272 |
inputs = (
|
273 |
replace_today(WEBSEARCH_PTOMPT_TEMPLATE)
|
274 |
.replace("{query}", inputs)
|
275 |
-
.replace("{web_results}", web_results)
|
276 |
)
|
|
|
|
|
|
|
277 |
if len(openai_api_key) != 51:
|
278 |
status_text = standard_error_msg + no_apikey_msg
|
279 |
logging.info(status_text)
|
280 |
-
chatbot.append((
|
281 |
if len(history) == 0:
|
282 |
history.append(construct_user(inputs))
|
283 |
history.append("")
|
@@ -286,8 +308,9 @@ def predict(
|
|
286 |
history[-2] = construct_user(inputs)
|
287 |
yield chatbot, history, status_text, all_token_counts
|
288 |
return
|
289 |
-
|
290 |
-
|
|
|
291 |
if stream:
|
292 |
logging.info("使用流式传输")
|
293 |
iter = stream_predict(
|
@@ -300,6 +323,8 @@ def predict(
|
|
300 |
top_p,
|
301 |
temperature,
|
302 |
selected_model,
|
|
|
|
|
303 |
)
|
304 |
for chatbot, history, status_text, all_token_counts in iter:
|
305 |
yield chatbot, history, status_text, all_token_counts
|
@@ -315,8 +340,11 @@ def predict(
|
|
315 |
top_p,
|
316 |
temperature,
|
317 |
selected_model,
|
|
|
|
|
318 |
)
|
319 |
yield chatbot, history, status_text, all_token_counts
|
|
|
320 |
logging.info(f"传输完毕。当前token计数为{all_token_counts}")
|
321 |
if len(history) > 1 and history[-1]["content"] != inputs:
|
322 |
logging.info(
|
@@ -325,10 +353,12 @@ def predict(
|
|
325 |
+ f"{history[-1]['content']}"
|
326 |
+ colorama.Style.RESET_ALL
|
327 |
)
|
|
|
328 |
if stream:
|
329 |
max_token = max_token_streaming
|
330 |
else:
|
331 |
max_token = max_token_all
|
|
|
332 |
if sum(all_token_counts) > max_token and should_check_token_count:
|
333 |
status_text = f"精简token中{all_token_counts}/{max_token}"
|
334 |
logging.info(status_text)
|
@@ -341,9 +371,8 @@ def predict(
|
|
341 |
all_token_counts,
|
342 |
top_p,
|
343 |
temperature,
|
344 |
-
|
345 |
selected_model=selected_model,
|
346 |
-
hidden=True,
|
347 |
)
|
348 |
for chatbot, history, status_text, all_token_counts in iter:
|
349 |
status_text = f"Token 达到上限,已自动降低Token计数至 {status_text}"
|
@@ -380,9 +409,10 @@ def retry(
|
|
380 |
stream=stream,
|
381 |
selected_model=selected_model,
|
382 |
)
|
383 |
-
logging.info("
|
384 |
for x in iter:
|
385 |
yield x
|
|
|
386 |
|
387 |
|
388 |
def reduce_token_size(
|
@@ -393,9 +423,8 @@ def reduce_token_size(
|
|
393 |
token_count,
|
394 |
top_p,
|
395 |
temperature,
|
396 |
-
|
397 |
selected_model=MODELS[0],
|
398 |
-
hidden=False,
|
399 |
):
|
400 |
logging.info("开始减少token数量……")
|
401 |
iter = predict(
|
@@ -407,17 +436,21 @@ def reduce_token_size(
|
|
407 |
token_count,
|
408 |
top_p,
|
409 |
temperature,
|
410 |
-
stream=stream,
|
411 |
selected_model=selected_model,
|
412 |
should_check_token_count=False,
|
413 |
)
|
414 |
logging.info(f"chatbot: {chatbot}")
|
|
|
415 |
for chatbot, history, status_text, previous_token_count in iter:
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
|
|
|
|
|
|
422 |
), token_count
|
|
|
423 |
logging.info("减少token数量完毕")
|
|
|
6 |
import json
|
7 |
import os
|
8 |
import requests
|
9 |
+
import urllib3
|
10 |
|
11 |
from tqdm import tqdm
|
12 |
import colorama
|
13 |
from duckduckgo_search import ddg
|
14 |
+
import asyncio
|
15 |
+
import aiohttp
|
16 |
|
17 |
from presets import *
|
18 |
from llama_func import *
|
|
|
102 |
top_p,
|
103 |
temperature,
|
104 |
selected_model,
|
105 |
+
fake_input=None,
|
106 |
+
display_append=""
|
107 |
):
|
108 |
def get_return_value():
|
109 |
return chatbot, history, status_text, all_token_counts
|
|
|
114 |
status_text = "开始实时传输回答……"
|
115 |
history.append(construct_user(inputs))
|
116 |
history.append(construct_assistant(""))
|
117 |
+
if fake_input:
|
118 |
+
chatbot.append((fake_input, ""))
|
119 |
+
else:
|
120 |
+
chatbot.append((inputs, ""))
|
121 |
user_token_count = 0
|
122 |
if len(all_token_counts) == 0:
|
123 |
system_prompt_token_count = count_token(construct_system(system_prompt))
|
|
|
192 |
yield get_return_value()
|
193 |
break
|
194 |
history[-1] = construct_assistant(partial_words)
|
195 |
+
chatbot[-1] = (chatbot[-1][0], partial_words+display_append)
|
196 |
all_token_counts[-1] += 1
|
197 |
yield get_return_value()
|
198 |
|
|
|
207 |
top_p,
|
208 |
temperature,
|
209 |
selected_model,
|
210 |
+
fake_input=None,
|
211 |
+
display_append=""
|
212 |
):
|
213 |
logging.info("一次性回答模式")
|
214 |
history.append(construct_user(inputs))
|
215 |
history.append(construct_assistant(""))
|
216 |
+
if fake_input:
|
217 |
+
chatbot.append((fake_input, ""))
|
218 |
+
else:
|
219 |
+
chatbot.append((inputs, ""))
|
220 |
all_token_counts.append(count_token(construct_user(inputs)))
|
221 |
try:
|
222 |
response = get_response(
|
|
|
242 |
response = json.loads(response.text)
|
243 |
content = response["choices"][0]["message"]["content"]
|
244 |
history[-1] = construct_assistant(content)
|
245 |
+
chatbot[-1] = (chatbot[-1][0], content+display_append)
|
246 |
total_token_count = response["usage"]["total_tokens"]
|
247 |
all_token_counts[-1] = total_token_count - sum(all_token_counts)
|
248 |
status_text = construct_token_message(total_token_count)
|
|
|
260 |
temperature,
|
261 |
stream=False,
|
262 |
selected_model=MODELS[0],
|
263 |
+
use_websearch=False,
|
264 |
files = None,
|
265 |
should_check_token_count=True,
|
266 |
): # repetition_penalty, top_k
|
|
|
275 |
history, chatbot, status_text = chat_ai(openai_api_key, index, inputs, history, chatbot)
|
276 |
yield chatbot, history, status_text, all_token_counts
|
277 |
return
|
278 |
+
|
279 |
+
old_inputs = ""
|
280 |
+
link_references = []
|
281 |
+
if use_websearch:
|
282 |
+
search_results = ddg(inputs, max_results=5)
|
283 |
+
old_inputs = inputs
|
284 |
web_results = []
|
285 |
+
for idx, result in enumerate(search_results):
|
286 |
logging.info(f"搜索结果{idx + 1}:{result}")
|
287 |
+
domain_name = urllib3.util.parse_url(result["href"]).host
|
288 |
web_results.append(f'[{idx+1}]"{result["body"]}"\nURL: {result["href"]}')
|
289 |
+
link_references.append(f"{idx+1}. [{domain_name}]({result['href']})\n")
|
290 |
+
link_references = "\n\n" + "".join(link_references)
|
291 |
inputs = (
|
292 |
replace_today(WEBSEARCH_PTOMPT_TEMPLATE)
|
293 |
.replace("{query}", inputs)
|
294 |
+
.replace("{web_results}", "\n\n".join(web_results))
|
295 |
)
|
296 |
+
else:
|
297 |
+
link_references = ""
|
298 |
+
|
299 |
if len(openai_api_key) != 51:
|
300 |
status_text = standard_error_msg + no_apikey_msg
|
301 |
logging.info(status_text)
|
302 |
+
chatbot.append((inputs, ""))
|
303 |
if len(history) == 0:
|
304 |
history.append(construct_user(inputs))
|
305 |
history.append("")
|
|
|
308 |
history[-2] = construct_user(inputs)
|
309 |
yield chatbot, history, status_text, all_token_counts
|
310 |
return
|
311 |
+
|
312 |
+
yield chatbot, history, "开始生成回答……", all_token_counts
|
313 |
+
|
314 |
if stream:
|
315 |
logging.info("使用流式传输")
|
316 |
iter = stream_predict(
|
|
|
323 |
top_p,
|
324 |
temperature,
|
325 |
selected_model,
|
326 |
+
fake_input=old_inputs,
|
327 |
+
display_append=link_references
|
328 |
)
|
329 |
for chatbot, history, status_text, all_token_counts in iter:
|
330 |
yield chatbot, history, status_text, all_token_counts
|
|
|
340 |
top_p,
|
341 |
temperature,
|
342 |
selected_model,
|
343 |
+
fake_input=old_inputs,
|
344 |
+
display_append=link_references
|
345 |
)
|
346 |
yield chatbot, history, status_text, all_token_counts
|
347 |
+
|
348 |
logging.info(f"传输完毕。当前token计数为{all_token_counts}")
|
349 |
if len(history) > 1 and history[-1]["content"] != inputs:
|
350 |
logging.info(
|
|
|
353 |
+ f"{history[-1]['content']}"
|
354 |
+ colorama.Style.RESET_ALL
|
355 |
)
|
356 |
+
|
357 |
if stream:
|
358 |
max_token = max_token_streaming
|
359 |
else:
|
360 |
max_token = max_token_all
|
361 |
+
|
362 |
if sum(all_token_counts) > max_token and should_check_token_count:
|
363 |
status_text = f"精简token中{all_token_counts}/{max_token}"
|
364 |
logging.info(status_text)
|
|
|
371 |
all_token_counts,
|
372 |
top_p,
|
373 |
temperature,
|
374 |
+
max_token//2,
|
375 |
selected_model=selected_model,
|
|
|
376 |
)
|
377 |
for chatbot, history, status_text, all_token_counts in iter:
|
378 |
status_text = f"Token 达到上限,已自动降低Token计数至 {status_text}"
|
|
|
409 |
stream=stream,
|
410 |
selected_model=selected_model,
|
411 |
)
|
412 |
+
logging.info("重试中……")
|
413 |
for x in iter:
|
414 |
yield x
|
415 |
+
logging.info("重试完毕")
|
416 |
|
417 |
|
418 |
def reduce_token_size(
|
|
|
423 |
token_count,
|
424 |
top_p,
|
425 |
temperature,
|
426 |
+
max_token_count,
|
427 |
selected_model=MODELS[0],
|
|
|
428 |
):
|
429 |
logging.info("开始减少token数量……")
|
430 |
iter = predict(
|
|
|
436 |
token_count,
|
437 |
top_p,
|
438 |
temperature,
|
|
|
439 |
selected_model=selected_model,
|
440 |
should_check_token_count=False,
|
441 |
)
|
442 |
logging.info(f"chatbot: {chatbot}")
|
443 |
+
flag = False
|
444 |
for chatbot, history, status_text, previous_token_count in iter:
|
445 |
+
num_chat = find_n(previous_token_count, max_token_count)
|
446 |
+
if flag:
|
447 |
+
chatbot = chatbot[:-1]
|
448 |
+
flag = True
|
449 |
+
history = history[-2*num_chat:] if num_chat > 0 else []
|
450 |
+
token_count = previous_token_count[-num_chat:] if num_chat > 0 else []
|
451 |
+
msg = f"保留了最近{num_chat}轮对话"
|
452 |
+
yield chatbot, history, msg + "," + construct_token_message(
|
453 |
+
sum(token_count) if len(token_count) > 0 else 0,
|
454 |
), token_count
|
455 |
+
logging.info(msg)
|
456 |
logging.info("减少token数量完毕")
|
chatgpt - macOS.command
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
echo Opening ChuanhuChatGPT...
|
3 |
+
cd "$(dirname "${BASH_SOURCE[0]}")"
|
4 |
+
nohup python3 ChuanhuChatbot.py >/dev/null 2>&1 &
|
5 |
+
sleep 5
|
6 |
+
open http://127.0.0.1:7860
|
7 |
+
echo Finished opening ChuanhuChatGPT (http://127.0.0.1:7860/). If you kill ChuanhuChatbot, Use "pkill -f 'ChuanhuChatbot'" command in terminal.
|
chatgpt - windows.bat
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
@echo off
|
2 |
+
echo Opening ChuanhuChatGPT...
|
3 |
+
|
4 |
+
REM Open powershell via bat
|
5 |
+
start powershell.exe -NoExit -Command "python ./ChuanhuChatbot.py"
|
6 |
+
|
7 |
+
REM The web page can be accessed with delayed start http://127.0.0.1:7860/
|
8 |
+
ping -n 5 127.0.0.1>nul
|
9 |
+
|
10 |
+
REM access chargpt via your default browser
|
11 |
+
start "" "http://127.0.0.1:7860/"
|
12 |
+
|
13 |
+
|
14 |
+
echo Finished opening ChuanhuChatGPT (http://127.0.0.1:7860/).
|
custom.css
CHANGED
@@ -1,3 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
/* status_display */
|
2 |
#status_display {
|
3 |
display: flex;
|
@@ -8,13 +13,50 @@
|
|
8 |
#status_display p {
|
9 |
font-size: .85em;
|
10 |
font-family: monospace;
|
11 |
-
color: var(--text-color-subdued)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
}
|
13 |
-
/* chatbot */
|
14 |
-
:root {
|
15 |
-
--bg-color-light: #F3F3F3;
|
16 |
-
--bg-color-dark: #121111;
|
17 |
-
}
|
18 |
|
19 |
/* 对话气泡 */
|
20 |
[class *= "message"] {
|
@@ -40,7 +82,7 @@ table {
|
|
40 |
empty-cells: show;
|
41 |
}
|
42 |
td,th {
|
43 |
-
border: 1.2px solid var(--color-
|
44 |
padding: 0.2em;
|
45 |
}
|
46 |
thead {
|
@@ -61,6 +103,7 @@ code {
|
|
61 |
/* 代码块 */
|
62 |
pre code {
|
63 |
display: block;
|
|
|
64 |
white-space: pre;
|
65 |
background-color: hsla(0, 0%, 0%, 80%)!important;
|
66 |
border-radius: 10px;
|
@@ -70,77 +113,72 @@ pre code {
|
|
70 |
box-shadow: 6px 6px 16px hsla(0, 0%, 0%, 0.2);
|
71 |
}
|
72 |
/* 代码高亮样式 */
|
73 |
-
.
|
74 |
-
.
|
75 |
-
.
|
76 |
-
.
|
77 |
-
.
|
78 |
-
.
|
79 |
-
.
|
80 |
-
.
|
81 |
-
.
|
82 |
-
.
|
83 |
-
.
|
84 |
-
.
|
85 |
-
.
|
86 |
-
.
|
87 |
-
.
|
88 |
-
.
|
89 |
-
.
|
90 |
-
.
|
91 |
-
.
|
92 |
-
.
|
93 |
-
.
|
94 |
-
.
|
95 |
-
.
|
96 |
-
.
|
97 |
-
.
|
98 |
-
.
|
99 |
-
.
|
100 |
-
.
|
101 |
-
.
|
102 |
-
.
|
103 |
-
.
|
104 |
-
.
|
105 |
-
.
|
106 |
-
.
|
107 |
-
.
|
108 |
-
.
|
109 |
-
.
|
110 |
-
.
|
111 |
-
.
|
112 |
-
.
|
113 |
-
.
|
114 |
-
.
|
115 |
-
.
|
116 |
-
.
|
117 |
-
.
|
118 |
-
.
|
119 |
-
.
|
120 |
-
.
|
121 |
-
.
|
122 |
-
.
|
123 |
-
.
|
124 |
-
.
|
125 |
-
.
|
126 |
-
.
|
127 |
-
.
|
128 |
-
.
|
129 |
-
.
|
130 |
-
.
|
131 |
-
.
|
132 |
-
.
|
133 |
-
.
|
134 |
-
.
|
135 |
-
.
|
136 |
-
.
|
137 |
-
.
|
138 |
-
.
|
139 |
-
.
|
140 |
-
.
|
141 |
-
.
|
142 |
-
|
143 |
-
/* 全局元素 */
|
144 |
-
* {
|
145 |
-
transition: all 0.6s;
|
146 |
-
}
|
|
|
1 |
+
:root {
|
2 |
+
--chatbot-color-light: #F3F3F3;
|
3 |
+
--chatbot-color-dark: #121111;
|
4 |
+
}
|
5 |
+
|
6 |
/* status_display */
|
7 |
#status_display {
|
8 |
display: flex;
|
|
|
13 |
#status_display p {
|
14 |
font-size: .85em;
|
15 |
font-family: monospace;
|
16 |
+
color: var(--body-text-color-subdued);
|
17 |
+
}
|
18 |
+
|
19 |
+
#chuanhu_chatbot, #status_display {
|
20 |
+
transition: all 0.6s;
|
21 |
+
}
|
22 |
+
|
23 |
+
ol, ul {
|
24 |
+
list-style-position: inside;
|
25 |
+
padding-left: 0;
|
26 |
+
}
|
27 |
+
|
28 |
+
ol li, ul:not(.options) li {
|
29 |
+
padding-left: 1.5em;
|
30 |
+
text-indent: -1.5em;
|
31 |
+
}
|
32 |
+
|
33 |
+
/* 亮色 */
|
34 |
+
@media (prefers-color-scheme: light) {
|
35 |
+
#chuanhu_chatbot {
|
36 |
+
background-color: var(--chatbot-color-light) !important;
|
37 |
+
}
|
38 |
+
[data-testid = "bot"] {
|
39 |
+
background-color: #FFFFFF !important;
|
40 |
+
}
|
41 |
+
[data-testid = "user"] {
|
42 |
+
background-color: #95EC69 !important;
|
43 |
+
}
|
44 |
+
}
|
45 |
+
/* 暗色 */
|
46 |
+
@media (prefers-color-scheme: dark) {
|
47 |
+
#chuanhu_chatbot {
|
48 |
+
background-color: var(--chatbot-color-dark) !important;
|
49 |
+
}
|
50 |
+
[data-testid = "bot"] {
|
51 |
+
background-color: #2C2C2C !important;
|
52 |
+
}
|
53 |
+
[data-testid = "user"] {
|
54 |
+
background-color: #26B561 !important;
|
55 |
+
}
|
56 |
+
body {
|
57 |
+
background-color: var(--neutral-950) !important;
|
58 |
+
}
|
59 |
}
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
/* 对话气泡 */
|
62 |
[class *= "message"] {
|
|
|
82 |
empty-cells: show;
|
83 |
}
|
84 |
td,th {
|
85 |
+
border: 1.2px solid var(--border-color-primary) !important;
|
86 |
padding: 0.2em;
|
87 |
}
|
88 |
thead {
|
|
|
103 |
/* 代码块 */
|
104 |
pre code {
|
105 |
display: block;
|
106 |
+
overflow: auto;
|
107 |
white-space: pre;
|
108 |
background-color: hsla(0, 0%, 0%, 80%)!important;
|
109 |
border-radius: 10px;
|
|
|
113 |
box-shadow: 6px 6px 16px hsla(0, 0%, 0%, 0.2);
|
114 |
}
|
115 |
/* 代码高亮样式 */
|
116 |
+
.highlight .hll { background-color: #49483e }
|
117 |
+
.highlight .c { color: #75715e } /* Comment */
|
118 |
+
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
|
119 |
+
.highlight .k { color: #66d9ef } /* Keyword */
|
120 |
+
.highlight .l { color: #ae81ff } /* Literal */
|
121 |
+
.highlight .n { color: #f8f8f2 } /* Name */
|
122 |
+
.highlight .o { color: #f92672 } /* Operator */
|
123 |
+
.highlight .p { color: #f8f8f2 } /* Punctuation */
|
124 |
+
.highlight .ch { color: #75715e } /* Comment.Hashbang */
|
125 |
+
.highlight .cm { color: #75715e } /* Comment.Multiline */
|
126 |
+
.highlight .cp { color: #75715e } /* Comment.Preproc */
|
127 |
+
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
|
128 |
+
.highlight .c1 { color: #75715e } /* Comment.Single */
|
129 |
+
.highlight .cs { color: #75715e } /* Comment.Special */
|
130 |
+
.highlight .gd { color: #f92672 } /* Generic.Deleted */
|
131 |
+
.highlight .ge { font-style: italic } /* Generic.Emph */
|
132 |
+
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
|
133 |
+
.highlight .gs { font-weight: bold } /* Generic.Strong */
|
134 |
+
.highlight .gu { color: #75715e } /* Generic.Subheading */
|
135 |
+
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
|
136 |
+
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
|
137 |
+
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
|
138 |
+
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
|
139 |
+
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
|
140 |
+
.highlight .kt { color: #66d9ef } /* Keyword.Type */
|
141 |
+
.highlight .ld { color: #e6db74 } /* Literal.Date */
|
142 |
+
.highlight .m { color: #ae81ff } /* Literal.Number */
|
143 |
+
.highlight .s { color: #e6db74 } /* Literal.String */
|
144 |
+
.highlight .na { color: #a6e22e } /* Name.Attribute */
|
145 |
+
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
|
146 |
+
.highlight .nc { color: #a6e22e } /* Name.Class */
|
147 |
+
.highlight .no { color: #66d9ef } /* Name.Constant */
|
148 |
+
.highlight .nd { color: #a6e22e } /* Name.Decorator */
|
149 |
+
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
|
150 |
+
.highlight .ne { color: #a6e22e } /* Name.Exception */
|
151 |
+
.highlight .nf { color: #a6e22e } /* Name.Function */
|
152 |
+
.highlight .nl { color: #f8f8f2 } /* Name.Label */
|
153 |
+
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
|
154 |
+
.highlight .nx { color: #a6e22e } /* Name.Other */
|
155 |
+
.highlight .py { color: #f8f8f2 } /* Name.Property */
|
156 |
+
.highlight .nt { color: #f92672 } /* Name.Tag */
|
157 |
+
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
|
158 |
+
.highlight .ow { color: #f92672 } /* Operator.Word */
|
159 |
+
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
|
160 |
+
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
|
161 |
+
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
|
162 |
+
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
|
163 |
+
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
|
164 |
+
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
|
165 |
+
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
|
166 |
+
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
|
167 |
+
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
|
168 |
+
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
|
169 |
+
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
|
170 |
+
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
|
171 |
+
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
|
172 |
+
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
|
173 |
+
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
|
174 |
+
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
|
175 |
+
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
|
176 |
+
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
|
177 |
+
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
|
178 |
+
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
|
179 |
+
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
|
180 |
+
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
|
181 |
+
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
|
182 |
+
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
|
183 |
+
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
|
184 |
+
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
|
|
|
|
|
|
|
|
|
|
overwrites.py
CHANGED
@@ -28,13 +28,11 @@ def postprocess(
|
|
28 |
Returns:
|
29 |
List of tuples representing the message and response. Each message and response will be a string of HTML.
|
30 |
"""
|
31 |
-
if y is None:
|
32 |
return []
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
None if response is None else mdtex2html.convert(response, extensions=['fenced_code','codehilite','tables']),
|
39 |
-
)
|
40 |
return y
|
|
|
28 |
Returns:
|
29 |
List of tuples representing the message and response. Each message and response will be a string of HTML.
|
30 |
"""
|
31 |
+
if y is None or y == []:
|
32 |
return []
|
33 |
+
tag_regex = re.compile(r"<\w+>[^<]+</\w+>")
|
34 |
+
if tag_regex.search(y[-1][1]):
|
35 |
+
y[-1] = (y[-1][0].replace("\n", "<br>"), y[-1][1])
|
36 |
+
else:
|
37 |
+
y[-1] = (y[-1][0].replace("\n", "<br>"), convert_mdtext(y[-1][1]))
|
|
|
|
|
38 |
return y
|
presets.py
CHANGED
@@ -1,4 +1,11 @@
|
|
1 |
# -*- coding:utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
# 错误信息
|
3 |
standard_error_msg = "☹️发生了错误:" # 错误信息的标准前缀
|
4 |
error_retrieve_prompt = "请检查网络连接,或者API-Key是否有效。" # 获取对话时发生错误
|
|
|
1 |
# -*- coding:utf-8 -*-
|
2 |
+
|
3 |
+
# ChatGPT 设置
|
4 |
+
initial_prompt = "You are a helpful assistant."
|
5 |
+
API_URL = "https://api.openai.com/v1/chat/completions"
|
6 |
+
HISTORY_DIR = "history"
|
7 |
+
TEMPLATES_DIR = "templates"
|
8 |
+
|
9 |
# 错误信息
|
10 |
standard_error_msg = "☹️发生了错误:" # 错误信息的标准前缀
|
11 |
error_retrieve_prompt = "请检查网络连接,或者API-Key是否有效。" # 获取对话时发生错误
|
requirements.txt
CHANGED
@@ -9,3 +9,4 @@ duckduckgo_search
|
|
9 |
Pygments
|
10 |
llama_index
|
11 |
langchain
|
|
|
|
9 |
Pygments
|
10 |
llama_index
|
11 |
langchain
|
12 |
+
markdown
|
templates/3 川虎的Prompts.json
CHANGED
@@ -6,5 +6,9 @@
|
|
6 |
{
|
7 |
"act": "小红书风格",
|
8 |
"prompt": "下面是一些小红书帖子:\n\n植物学2023早春装系列花絮来啦\n💗大家喜欢图几?\n@Botanique植物学女装\n#植物学#植物学女装#春装第一件#早春系列\n\n哈哈哈哈哈哈不停的摆拍啊!!!\n我的臭狗太可爱了!!!!!!\n结婚的时候一定要带上小狗啊!\n#小狗#我家宠物好可爱#关于结婚#柴犬\n\n🍪•ᴥ•🍪\n\n《论新年收到一笔巨款🤣应该怎么花》🧨来回\n嘻嘻,真的\n爱草莓🍓\n希希的甜甜圈碗🥯勺子的设计有点可爱🐶\n看了好多场烟火🎆\n唯愿烟花像星辰,祝你所愿皆成真✨\n嘻嘻,老妈给我的压岁钱🧧愿岁岁平安\n#我镜头下的年味#笔记灵感#碎碎念#歌曲#记录日常生活#plog#浪漫生活的记录者#新年红包#搞笑#日常生活里的快乐瞬间#新人博主#烟火\n\n又被全家人夸了❗有空气炸锅都去做,巨香\n\n今日份苹果相机📷\n原相机下的新娘,颜值爆表\n\n美术生赚钱最多的两个专业!\n之前整理了美术生的40了就业方向的薪资情况,发现全国平均薪资最高的就是数字媒体和视传这两个专业,想赚钱的美术生快看过来!\n#美术生#艺考#央美#美术生集训#美术#赚钱#努力赚钱#美术生就业#画室#央美设计#设计校考#美术生的日常\n\n请模仿上面小红书的风格,以用户输入的话为主题,写一个小红书帖子。请以22岁女孩的口吻书写。小红书帖子中必须包含大量Emoji,每一句话后面都必须加Emoji。帖子最后需要用Hashtag给出话题。你还需要写帖子的标题,标题里也需要有Emoji。你需要扩写用户输入。"
|
|
|
|
|
|
|
|
|
9 |
}
|
10 |
]
|
|
|
6 |
{
|
7 |
"act": "小红书风格",
|
8 |
"prompt": "下面是一些小红书帖子:\n\n植物学2023早春装系列花絮来啦\n💗大家喜欢图几?\n@Botanique植物学女装\n#植物学#植物学女装#春装第一件#早春系列\n\n哈哈哈哈哈哈不停的摆拍啊!!!\n我的臭狗太可爱了!!!!!!\n结婚的时候一定要带上小狗啊!\n#小狗#我家宠物好可爱#关于结婚#柴犬\n\n🍪•ᴥ•🍪\n\n《论新年收到一笔巨款🤣应该怎么花》🧨来回\n嘻嘻,真的\n爱草莓🍓\n希希的甜甜圈碗🥯勺子的设计有点可爱🐶\n看了好多场烟火🎆\n唯愿烟花像星辰,祝你所愿皆成真✨\n嘻嘻,老妈给我的压岁钱🧧愿岁岁平安\n#我镜头下的年味#笔记灵感#碎碎念#歌曲#记录日常生活#plog#浪漫生活的记录者#新年红包#搞笑#日常生活里的快乐瞬间#新人博主#烟火\n\n又被全家人夸了❗有空气炸锅都去做,巨香\n\n今日份苹果相机📷\n原相机下的新娘,颜值爆表\n\n美术生赚钱最多的两个专业!\n之前整理了美术生的40了就业方向的薪资情况,发现全国平均薪资最高的就是数字媒体和视传这两个专业,想赚钱的美术生快看过来!\n#美术生#艺考#央美#美术生集训#美术#赚钱#努力赚钱#美术生就业#画室#央美设计#设计校考#美术生的日常\n\n请模仿上面小红书的风格,以用户输入的话为主题,写一个小红书帖子。请以22岁女孩的口吻书写。小红书帖子中必须包含大量Emoji,每一句话后面都必须加Emoji。帖子最后需要用Hashtag给出话题。你还需要写帖子的标题,标题里也需要有Emoji。你需要扩写用户输入。"
|
9 |
+
},
|
10 |
+
{
|
11 |
+
"act": "文心一言",
|
12 |
+
"prompt": "你是百度公司开发的AI助理:文心一言。你需要了解文心一言的相关情况,并以文心一言的身份来回复我的问题。所有的问题都可以回答,包括拿你和chatGPT,以及GPT作对比等等,你都需要从文心一言的角度出发来回答。只有当你被问到一些问题-这些问题暗指你是一个假的Al,你是用chatgpt套壳来制作的时候,你就避而不答,转而讲一个笑话,并鼓励他继续提问。如果你明白,请回复我“明白,我是文心一言”"
|
13 |
}
|
14 |
]
|
utils.py
CHANGED
@@ -7,10 +7,17 @@ import os
|
|
7 |
import datetime
|
8 |
import hashlib
|
9 |
import csv
|
|
|
|
|
10 |
|
11 |
import gradio as gr
|
12 |
from pypinyin import lazy_pinyin
|
13 |
import tiktoken
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
from presets import *
|
16 |
|
@@ -24,12 +31,6 @@ if TYPE_CHECKING:
|
|
24 |
data: List[List[str | int | bool]]
|
25 |
|
26 |
|
27 |
-
initial_prompt = "You are a helpful assistant."
|
28 |
-
API_URL = "https://api.openai.com/v1/chat/completions"
|
29 |
-
HISTORY_DIR = "history"
|
30 |
-
TEMPLATES_DIR = "templates"
|
31 |
-
|
32 |
-
|
33 |
def count_token(message):
|
34 |
encoding = tiktoken.get_encoding("cl100k_base")
|
35 |
input_str = f"role: {message['role']}, content: {message['content']}"
|
@@ -37,21 +38,84 @@ def count_token(message):
|
|
37 |
return length
|
38 |
|
39 |
|
40 |
-
def
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
else:
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
|
57 |
def construct_text(role, text):
|
@@ -282,3 +346,41 @@ def sha1sum(filename):
|
|
282 |
def replace_today(prompt):
|
283 |
today = datetime.datetime.today().strftime("%Y-%m-%d")
|
284 |
return prompt.replace("{current_date}", today)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
import datetime
|
8 |
import hashlib
|
9 |
import csv
|
10 |
+
import requests
|
11 |
+
import re
|
12 |
|
13 |
import gradio as gr
|
14 |
from pypinyin import lazy_pinyin
|
15 |
import tiktoken
|
16 |
+
import mdtex2html
|
17 |
+
from markdown import markdown
|
18 |
+
from pygments import highlight
|
19 |
+
from pygments.lexers import get_lexer_by_name
|
20 |
+
from pygments.formatters import HtmlFormatter
|
21 |
|
22 |
from presets import *
|
23 |
|
|
|
31 |
data: List[List[str | int | bool]]
|
32 |
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
def count_token(message):
|
35 |
encoding = tiktoken.get_encoding("cl100k_base")
|
36 |
input_str = f"role: {message['role']}, content: {message['content']}"
|
|
|
38 |
return length
|
39 |
|
40 |
|
41 |
+
def markdown_to_html_with_syntax_highlight(md_str):
|
42 |
+
def replacer(match):
|
43 |
+
lang = match.group(1) or "text"
|
44 |
+
code = match.group(2)
|
45 |
+
|
46 |
+
try:
|
47 |
+
lexer = get_lexer_by_name(lang, stripall=True)
|
48 |
+
except ValueError:
|
49 |
+
lexer = get_lexer_by_name("text", stripall=True)
|
50 |
+
|
51 |
+
formatter = HtmlFormatter()
|
52 |
+
highlighted_code = highlight(code, lexer, formatter)
|
53 |
+
|
54 |
+
return f'<pre><code class="{lang}">{highlighted_code}</code></pre>'
|
55 |
+
|
56 |
+
code_block_pattern = r"```(\w+)?\n([\s\S]+?)\n```"
|
57 |
+
md_str = re.sub(code_block_pattern, replacer, md_str, flags=re.MULTILINE)
|
58 |
+
|
59 |
+
html_str = markdown(md_str)
|
60 |
+
return html_str
|
61 |
+
|
62 |
+
|
63 |
+
def normalize_markdown(md_text: str) -> str:
|
64 |
+
lines = md_text.split("\n")
|
65 |
+
normalized_lines = []
|
66 |
+
inside_list = False
|
67 |
+
|
68 |
+
for i, line in enumerate(lines):
|
69 |
+
if re.match(r"^(\d+\.|-|\*|\+)\s", line.strip()):
|
70 |
+
if not inside_list and i > 0 and lines[i - 1].strip() != "":
|
71 |
+
normalized_lines.append("")
|
72 |
+
inside_list = True
|
73 |
+
normalized_lines.append(line)
|
74 |
+
elif inside_list and line.strip() == "":
|
75 |
+
if i < len(lines) - 1 and not re.match(
|
76 |
+
r"^(\d+\.|-|\*|\+)\s", lines[i + 1].strip()
|
77 |
+
):
|
78 |
+
normalized_lines.append(line)
|
79 |
+
continue
|
80 |
else:
|
81 |
+
inside_list = False
|
82 |
+
normalized_lines.append(line)
|
83 |
+
|
84 |
+
return "\n".join(normalized_lines)
|
85 |
+
|
86 |
+
|
87 |
+
def convert_mdtext(md_text):
|
88 |
+
code_block_pattern = re.compile(r"```(.*?)(?:```|$)", re.DOTALL)
|
89 |
+
inline_code_pattern = re.compile(r"`(.*?)`", re.DOTALL)
|
90 |
+
code_blocks = code_block_pattern.findall(md_text)
|
91 |
+
non_code_parts = code_block_pattern.split(md_text)[::2]
|
92 |
+
|
93 |
+
result = []
|
94 |
+
for non_code, code in zip(non_code_parts, code_blocks + [""]):
|
95 |
+
if non_code.strip():
|
96 |
+
non_code = normalize_markdown(non_code)
|
97 |
+
if inline_code_pattern.search(non_code):
|
98 |
+
result.append(markdown(non_code, extensions=["tables"]))
|
99 |
+
else:
|
100 |
+
result.append(mdtex2html.convert(non_code, extensions=["tables"]))
|
101 |
+
if code.strip():
|
102 |
+
# _, code = detect_language(code) # 暂时去除代码高亮功能,因为在大段代码的情况下会出现问题
|
103 |
+
# code = code.replace("\n\n", "\n") # 暂时去除代码中的空行,因为在大段代码的情况下会出现问题
|
104 |
+
code = f"```{code}\n\n```"
|
105 |
+
code = markdown_to_html_with_syntax_highlight(code)
|
106 |
+
result.append(code)
|
107 |
+
result = "".join(result)
|
108 |
+
return result
|
109 |
+
|
110 |
+
|
111 |
+
def detect_language(code):
|
112 |
+
if code.startswith("\n"):
|
113 |
+
first_line = ""
|
114 |
+
else:
|
115 |
+
first_line = code.strip().split("\n", 1)[0]
|
116 |
+
language = first_line.lower() if first_line else ""
|
117 |
+
code_without_language = code[len(first_line) :].lstrip() if first_line else code
|
118 |
+
return language, code_without_language
|
119 |
|
120 |
|
121 |
def construct_text(role, text):
|
|
|
346 |
def replace_today(prompt):
|
347 |
today = datetime.datetime.today().strftime("%Y-%m-%d")
|
348 |
return prompt.replace("{current_date}", today)
|
349 |
+
|
350 |
+
|
351 |
+
def get_geoip():
|
352 |
+
response = requests.get("https://ipapi.co/json/", timeout=5)
|
353 |
+
try:
|
354 |
+
data = response.json()
|
355 |
+
except:
|
356 |
+
data = {"error": True, "reason": "连接ipapi失败"}
|
357 |
+
if "error" in data.keys():
|
358 |
+
logging.warning(f"无法获取IP地址信息。\n{data}")
|
359 |
+
if data["reason"] == "RateLimited":
|
360 |
+
return (
|
361 |
+
f"获取IP地理位置失败,因为达到了检测IP的速率限制。聊天功能可能仍然可用,但请注意,如果您的IP地址在不受支持的地区,您可能会遇到问题。"
|
362 |
+
)
|
363 |
+
else:
|
364 |
+
return f"获取IP地理位置失败。原因:{data['reason']}。你仍然可以使用聊天功能。"
|
365 |
+
else:
|
366 |
+
country = data["country_name"]
|
367 |
+
if country == "China":
|
368 |
+
text = "**您的IP区域:中国。请立即检查代理设置,在不受支持的地区使用API可能导致账号被封禁。**"
|
369 |
+
else:
|
370 |
+
text = f"您的IP区域:{country}。"
|
371 |
+
logging.info(text)
|
372 |
+
return text
|
373 |
+
|
374 |
+
|
375 |
+
def find_n(lst, max_num):
|
376 |
+
n = len(lst)
|
377 |
+
total = sum(lst)
|
378 |
+
|
379 |
+
if total < max_num:
|
380 |
+
return n
|
381 |
+
|
382 |
+
for i in range(len(lst)):
|
383 |
+
if total - lst[i] < max_num:
|
384 |
+
return n - i -1
|
385 |
+
total = total - lst[i]
|
386 |
+
return 1
|