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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# pylint: disable=broad-exception-caught, redefined-outer-name, missing-function-docstring, missing-module-docstring, too-many-arguments, line-too-long, invalid-name, redefined-builtin, redefined-argument-from-local
|
2 |
+
# import gradio as gr
|
3 |
|
4 |
+
# model_name = "models/THUDM/chatglm2-6b-int4"
|
5 |
+
# gr.load(model_name).lauch()
|
|
|
6 |
|
7 |
+
# %%writefile demo-4bit.py
|
|
|
8 |
|
9 |
+
from textwrap import dedent
|
10 |
+
|
11 |
+
import gradio as gr
|
12 |
+
import mdtex2html
|
13 |
+
import torch
|
14 |
+
from loguru import logger
|
15 |
+
|
16 |
+
# credit to https://github.com/THUDM/ChatGLM2-6B/blob/main/web_demo.py
|
17 |
+
# while mistakes are mine
|
18 |
+
from transformers import AutoModel, AutoTokenizer
|
19 |
+
|
20 |
+
# model_name = "THUDM/chatglm2-6b"
|
21 |
+
# model_name = "THUDM/chatglm2-6b-int4"
|
22 |
+
model_name = "gorkemgoknar/gpt2chatbotenglish"
|
23 |
+
|
24 |
+
RETRY_FLAG = False
|
25 |
+
|
26 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
27 |
+
|
28 |
+
# model = AutoModel.from_pretrained(model_name, trust_remote_code=True).cuda()
|
29 |
+
|
30 |
+
# 4/8 bit
|
31 |
+
# model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True).quantize(4).cuda()
|
32 |
+
|
33 |
+
has_cuda = torch.cuda.is_available()
|
34 |
+
# has_cuda = False # force cpu
|
35 |
+
|
36 |
+
if has_cuda:
|
37 |
+
model = AutoModel.from_pretrained(
|
38 |
+
model_name, trust_remote_code=True
|
39 |
+
).cuda() # 3.92G
|
40 |
+
else:
|
41 |
+
model = AutoModel.from_pretrained(
|
42 |
+
model_name, trust_remote_code=True
|
43 |
+
).half() # .float() .half().float()
|
44 |
+
|
45 |
+
model = model.eval()
|
46 |
+
|
47 |
+
_ = """Override Chatbot.postprocess"""
|
48 |
+
|
49 |
+
|
50 |
+
def postprocess(self, y):
|
51 |
+
if y is None:
|
52 |
+
return []
|
53 |
+
for i, (message, response) in enumerate(y):
|
54 |
+
y[i] = (
|
55 |
+
None if message is None else mdtex2html.convert((message)),
|
56 |
+
None if response is None else mdtex2html.convert(response),
|
57 |
+
)
|
58 |
+
return y
|
59 |
+
|
60 |
+
|
61 |
+
gr.Chatbot.postprocess = postprocess
|
62 |
+
|
63 |
+
|
64 |
+
def parse_text(text):
|
65 |
+
"""copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/"""
|
66 |
+
lines = text.split("\n")
|
67 |
+
lines = [line for line in lines if line != ""]
|
68 |
+
count = 0
|
69 |
+
for i, line in enumerate(lines):
|
70 |
+
if "```" in line:
|
71 |
+
count += 1
|
72 |
+
items = line.split("`")
|
73 |
+
if count % 2 == 1:
|
74 |
+
lines[i] = f'<pre><code class="language-{items[-1]}">'
|
75 |
+
else:
|
76 |
+
lines[i] = "<br></code></pre>"
|
77 |
+
else:
|
78 |
+
if i > 0:
|
79 |
+
if count % 2 == 1:
|
80 |
+
line = line.replace("`", r"\`")
|
81 |
+
line = line.replace("<", "<")
|
82 |
+
line = line.replace(">", ">")
|
83 |
+
line = line.replace(" ", " ")
|
84 |
+
line = line.replace("*", "*")
|
85 |
+
line = line.replace("_", "_")
|
86 |
+
line = line.replace("-", "-")
|
87 |
+
line = line.replace(".", ".")
|
88 |
+
line = line.replace("!", "!")
|
89 |
+
line = line.replace("(", "(")
|
90 |
+
line = line.replace(")", ")")
|
91 |
+
line = line.replace("$", "$")
|
92 |
+
lines[i] = "<br>" + line
|
93 |
+
text = "".join(lines)
|
94 |
+
return text
|
95 |
+
|
96 |
+
|
97 |
+
def predict(
|
98 |
+
RETRY_FLAG, input, chatbot, max_length, top_p, temperature, history, past_key_values
|
99 |
+
):
|
100 |
+
try:
|
101 |
+
chatbot.append((parse_text(input), ""))
|
102 |
+
except Exception as exc:
|
103 |
+
logger.error(exc)
|
104 |
+
logger.debug(f"{chatbot=}")
|
105 |
+
_ = """
|
106 |
+
if chatbot:
|
107 |
+
chatbot[-1] = (parse_text(input), str(exc))
|
108 |
+
yield chatbot, history, past_key_values
|
109 |
+
# """
|
110 |
+
yield chatbot, history, past_key_values
|
111 |
+
|
112 |
+
for response, history, past_key_values in model.stream_chat(
|
113 |
+
tokenizer,
|
114 |
+
input,
|
115 |
+
history,
|
116 |
+
past_key_values=past_key_values,
|
117 |
+
return_past_key_values=True,
|
118 |
+
max_length=max_length,
|
119 |
+
top_p=top_p,
|
120 |
+
temperature=temperature,
|
121 |
+
):
|
122 |
+
chatbot[-1] = (parse_text(input), parse_text(response))
|
123 |
+
|
124 |
+
yield chatbot, history, past_key_values
|
125 |
+
|
126 |
+
|
127 |
+
def trans_api(input, max_length=4096, top_p=0.8, temperature=0.2):
|
128 |
+
if max_length < 10:
|
129 |
+
max_length = 4096
|
130 |
+
if top_p < 0.1 or top_p > 1:
|
131 |
+
top_p = 0.85
|
132 |
+
if temperature <= 0 or temperature > 1:
|
133 |
+
temperature = 0.01
|
134 |
+
try:
|
135 |
+
res, _ = model.chat(
|
136 |
+
tokenizer,
|
137 |
+
input,
|
138 |
+
history=[],
|
139 |
+
past_key_values=None,
|
140 |
+
max_length=max_length,
|
141 |
+
top_p=top_p,
|
142 |
+
temperature=temperature,
|
143 |
+
)
|
144 |
+
# logger.debug(f"{res=} \n{_=}")
|
145 |
+
except Exception as exc:
|
146 |
+
logger.error(f"{exc=}")
|
147 |
+
res = str(exc)
|
148 |
+
|
149 |
+
return res
|
150 |
+
|
151 |
+
|
152 |
+
def reset_user_input():
|
153 |
+
return gr.update(value="")
|
154 |
+
|
155 |
+
|
156 |
+
def reset_state():
|
157 |
+
return [], [], None
|
158 |
+
|
159 |
+
|
160 |
+
# Delete last turn
|
161 |
+
def delete_last_turn(chat, history):
|
162 |
+
if chat and history:
|
163 |
+
chat.pop(-1)
|
164 |
+
history.pop(-1)
|
165 |
+
return chat, history
|
166 |
+
|
167 |
+
|
168 |
+
# Regenerate response
|
169 |
+
def retry_last_answer(
|
170 |
+
user_input, chatbot, max_length, top_p, temperature, history, past_key_values
|
171 |
+
):
|
172 |
+
if chatbot and history:
|
173 |
+
# Removing the previous conversation from chat
|
174 |
+
chatbot.pop(-1)
|
175 |
+
# Setting up a flag to capture a retry
|
176 |
+
RETRY_FLAG = True
|
177 |
+
# Getting last message from user
|
178 |
+
user_input = history[-1][0]
|
179 |
+
# Removing bot response from the history
|
180 |
+
history.pop(-1)
|
181 |
+
|
182 |
+
yield from predict(
|
183 |
+
RETRY_FLAG,
|
184 |
+
user_input,
|
185 |
+
chatbot,
|
186 |
+
max_length,
|
187 |
+
top_p,
|
188 |
+
temperature,
|
189 |
+
history,
|
190 |
+
past_key_values,
|
191 |
+
)
|
192 |
+
|
193 |
+
|
194 |
+
with gr.Blocks(title="ChatGLM2-6B-int4", theme=gr.themes.Soft(text_size="sm")) as demo:
|
195 |
+
# gr.HTML("""<h1 align="center">ChatGLM2-6B-int4</h1>""")
|
196 |
+
gr.HTML(
|
197 |
+
"""<center><a href="https://huggingface.co/spaces/mikeee/chatglm2-6b-4bit?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>To avoid the queue and for faster inference Duplicate this Space and upgrade to GPU</center>"""
|
198 |
+
)
|
199 |
+
|
200 |
+
with gr.Accordion("Info", open=False):
|
201 |
+
_ = """
|
202 |
+
## ChatGLM2-6B-int4
|
203 |
+
|
204 |
+
With a GPU, a query takes from a few seconds to a few tens of seconds, dependent on the number of words/characters
|
205 |
+
the question and responses contain. The quality of the responses varies quite a bit it seems. Even the same
|
206 |
+
question with the same parameters, asked at different times, can result in quite different responses.
|
207 |
+
|
208 |
+
* Low temperature: responses will be more deterministic and focused; High temperature: responses more creative.
|
209 |
+
|
210 |
+
* Suggested temperatures -- translation: up to 0.3; chatting: > 0.4
|
211 |
+
|
212 |
+
* Top P controls dynamic vocabulary selection based on context.
|
213 |
+
|
214 |
+
For a table of example values for different scenarios, refer to [this](https://community.openai.com/t/cheat-sheet-mastering-temperature-and-top-p-in-chatgpt-api-a-few-tips-and-tricks-on-controlling-the-creativity-deterministic-output-of-prompt-responses/172683)
|
215 |
+
|
216 |
+
If the instance is not on a GPU (T4), it will be very slow. You can try to run the colab notebook [chatglm2-6b-4bit colab notebook](https://colab.research.google.com/drive/1WkF7kOjVCcBBatDHjaGkuJHnPdMWNtbW?usp=sharing) for a spin.
|
217 |
+
|
218 |
+
The T4 GPU is sponsored by a community GPU grant from Huggingface. Thanks a lot!
|
219 |
+
"""
|
220 |
+
gr.Markdown(dedent(_))
|
221 |
+
chatbot = gr.Chatbot()
|
222 |
+
with gr.Row():
|
223 |
+
with gr.Column(scale=4):
|
224 |
+
with gr.Column(scale=12):
|
225 |
+
user_input = gr.Textbox(
|
226 |
+
show_label=False,
|
227 |
+
placeholder="Input...",
|
228 |
+
).style(container=False)
|
229 |
+
RETRY_FLAG = gr.Checkbox(value=False, visible=False)
|
230 |
+
with gr.Column(min_width=32, scale=1):
|
231 |
+
with gr.Row():
|
232 |
+
submitBtn = gr.Button("Submit", variant="primary")
|
233 |
+
deleteBtn = gr.Button("Delete last turn", variant="secondary")
|
234 |
+
retryBtn = gr.Button("Regenerate", variant="secondary")
|
235 |
+
with gr.Column(scale=1):
|
236 |
+
emptyBtn = gr.Button("Clear History")
|
237 |
+
max_length = gr.Slider(
|
238 |
+
0,
|
239 |
+
32768,
|
240 |
+
value=8192,
|
241 |
+
step=1.0,
|
242 |
+
label="Maximum length",
|
243 |
+
interactive=True,
|
244 |
+
)
|
245 |
+
top_p = gr.Slider(
|
246 |
+
0, 1, value=0.85, step=0.01, label="Top P", interactive=True
|
247 |
+
)
|
248 |
+
temperature = gr.Slider(
|
249 |
+
0.01, 1, value=0.95, step=0.01, label="Temperature", interactive=True
|
250 |
+
)
|
251 |
+
|
252 |
+
history = gr.State([])
|
253 |
+
past_key_values = gr.State(None)
|
254 |
+
|
255 |
+
user_input.submit(
|
256 |
+
predict,
|
257 |
+
[
|
258 |
+
RETRY_FLAG,
|
259 |
+
user_input,
|
260 |
+
chatbot,
|
261 |
+
max_length,
|
262 |
+
top_p,
|
263 |
+
temperature,
|
264 |
+
history,
|
265 |
+
past_key_values,
|
266 |
+
],
|
267 |
+
[chatbot, history, past_key_values],
|
268 |
+
show_progress="full",
|
269 |
+
)
|
270 |
+
submitBtn.click(
|
271 |
+
predict,
|
272 |
+
[
|
273 |
+
RETRY_FLAG,
|
274 |
+
user_input,
|
275 |
+
chatbot,
|
276 |
+
max_length,
|
277 |
+
top_p,
|
278 |
+
temperature,
|
279 |
+
history,
|
280 |
+
past_key_values,
|
281 |
+
],
|
282 |
+
[chatbot, history, past_key_values],
|
283 |
+
show_progress="full",
|
284 |
+
api_name="predict",
|
285 |
+
)
|
286 |
+
submitBtn.click(reset_user_input, [], [user_input])
|
287 |
+
|
288 |
+
emptyBtn.click(
|
289 |
+
reset_state, outputs=[chatbot, history, past_key_values], show_progress="full"
|
290 |
+
)
|
291 |
+
|
292 |
+
retryBtn.click(
|
293 |
+
retry_last_answer,
|
294 |
+
inputs=[
|
295 |
+
user_input,
|
296 |
+
chatbot,
|
297 |
+
max_length,
|
298 |
+
top_p,
|
299 |
+
temperature,
|
300 |
+
history,
|
301 |
+
past_key_values,
|
302 |
+
],
|
303 |
+
# outputs = [chatbot, history, last_user_message, user_message]
|
304 |
+
outputs=[chatbot, history, past_key_values],
|
305 |
+
)
|
306 |
+
deleteBtn.click(delete_last_turn, [chatbot, history], [chatbot, history])
|
307 |
+
|
308 |
+
with gr.Accordion("Example inputs", open=True):
|
309 |
+
etext = """In America, where cars are an important part of the national psyche, a decade ago people had suddenly started to drive less, which had not happened since the oil shocks of the 1970s. """
|
310 |
+
examples = gr.Examples(
|
311 |
+
examples=[
|
312 |
+
["Explain the plot of Cinderella in a sentence."],
|
313 |
+
[
|
314 |
+
"How long does it take to become proficient in French, and what are the best methods for retaining information?"
|
315 |
+
],
|
316 |
+
["What are some common mistakes to avoid when writing code?"],
|
317 |
+
["Build a prompt to generate a beautiful portrait of a horse"],
|
318 |
+
["Suggest four metaphors to describe the benefits of AI"],
|
319 |
+
["Write a pop song about leaving home for the sandy beaches."],
|
320 |
+
["Write a summary demonstrating my ability to tame lions"],
|
321 |
+
["鲁迅和周树人什么关系"],
|
322 |
+
["从前有一头牛,这头牛后面有什么?"],
|
323 |
+
["正无穷大加一大于正无穷大吗?"],
|
324 |
+
["正无穷大加正无穷大大于正无穷大吗?"],
|
325 |
+
["-2的平方根等于什么"],
|
326 |
+
["树上有5只鸟,猎人开枪打死了一只。树上还有几只鸟?"],
|
327 |
+
["树上有11只鸟,猎人开枪打死了一只。树上还有几只鸟?提示:需考虑鸟可能受惊吓飞走。"],
|
328 |
+
["鲁迅和周树人什么关系 用英文回答"],
|
329 |
+
["以红楼梦的行文风格写一张委婉的请假条。不少于320字。"],
|
330 |
+
[f"{etext} 翻成中文,列出3个版本"],
|
331 |
+
[f"{etext} \n 翻成中文,保留原意,但使用文学性的语言。不要写解释。列出3个版本"],
|
332 |
+
["js 判断一个数是不是质数"],
|
333 |
+
["js 实现python 的 range(10)"],
|
334 |
+
["js 实现python 的 [*(range(10)]"],
|
335 |
+
["假定 1 + 2 = 4, 试求 7 + 8"],
|
336 |
+
["Erkläre die Handlung von Cinderella in einem Satz."],
|
337 |
+
["Erkläre die Handlung von Cinderella in einem Satz. Auf Deutsch"],
|
338 |
+
],
|
339 |
+
inputs=[user_input],
|
340 |
+
examples_per_page=30,
|
341 |
+
)
|
342 |
+
|
343 |
+
with gr.Accordion("For Chat/Translation API", open=False, visible=False):
|
344 |
+
input_text = gr.Text()
|
345 |
+
tr_btn = gr.Button("Go", variant="primary")
|
346 |
+
out_text = gr.Text()
|
347 |
+
tr_btn.click(
|
348 |
+
trans_api,
|
349 |
+
[input_text, max_length, top_p, temperature],
|
350 |
+
out_text,
|
351 |
+
# show_progress="full",
|
352 |
+
api_name="tr",
|
353 |
+
)
|
354 |
+
_ = """
|
355 |
+
input_text.submit(
|
356 |
+
trans_api,
|
357 |
+
[input_text, max_length, top_p, temperature],
|
358 |
+
out_text,
|
359 |
+
show_progress="full",
|
360 |
+
api_name="tr1",
|
361 |
+
)
|
362 |
+
# """
|
363 |
+
|
364 |
+
# demo.queue().launch(share=False, inbrowser=True)
|
365 |
+
# demo.queue().launch(share=True, inbrowser=True, debug=True)
|
366 |
+
|
367 |
+
demo.queue().launch(debug=True)
|
app1.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
# Title and input
|
4 |
+
st.title("Reverse Text")
|
5 |
+
input_text = st.text_input("Enter some text")
|
6 |
+
|
7 |
+
# Reverse the input text
|
8 |
+
reversed_text = input_text[::-1]
|
9 |
+
|
10 |
+
# Display the reversed text
|
11 |
+
st.write("Reversed text:", reversed_text)
|
app22.py
CHANGED
@@ -4,6 +4,8 @@ import streamlit as st
|
|
4 |
|
5 |
model = st.text_input("model name: ")
|
6 |
|
|
|
|
|
7 |
while model == "":
|
8 |
time.sleep(0.1)
|
9 |
|
|
|
4 |
|
5 |
model = st.text_input("model name: ")
|
6 |
|
7 |
+
model = "gorkemgoknar/gpt2chatbotenglish"
|
8 |
+
|
9 |
while model == "":
|
10 |
time.sleep(0.1)
|
11 |
|
requirements.txt
CHANGED
Binary files a/requirements.txt and b/requirements.txt differ
|
|