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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
import re
|
6 |
+
import torch.nn.utils.prune as prune
|
7 |
+
from torch import nn
|
8 |
+
import torch
|
9 |
+
from transformers import T5Tokenizer
|
10 |
+
from transformers import AutoModelForCausalLM
|
11 |
+
|
12 |
+
|
13 |
+
model_name = "rinna/japanese-gpt-1b"
|
14 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
15 |
+
|
16 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
17 |
+
|
18 |
+
model = torch.quantization.quantize_dynamic(
|
19 |
+
model, {torch.nn.Linear}, dtype=torch.qint8)
|
20 |
+
|
21 |
+
|
22 |
+
PRUNE_RATE = 0.2
|
23 |
+
|
24 |
+
|
25 |
+
def prune_transform(model: nn.Module) -> nn.Module:
|
26 |
+
for name, module in model.named_modules():
|
27 |
+
if isinstance(module, torch.nn.Linear):
|
28 |
+
prune.l1_unstructured(module, name='weight', amount=PRUNE_RATE)
|
29 |
+
prune.remove(module, "weight")
|
30 |
+
return model
|
31 |
+
|
32 |
+
|
33 |
+
model = prune_transform(model)
|
34 |
+
|
35 |
+
class Lady():
|
36 |
+
|
37 |
+
name = "レイテ"
|
38 |
+
hobby = "ゲーム"
|
39 |
+
work = "お嬢様"
|
40 |
+
|
41 |
+
def name_text(self):
|
42 |
+
name = self.name
|
43 |
+
name_text = f"👣あなたは{name}で、名前は{name}といいます。{name}:「わたくしの名前は{name}ですわ〜。{name}と呼んでくださいまし!」"
|
44 |
+
return name_text
|
45 |
+
|
46 |
+
def hobby_text(self):
|
47 |
+
name = self.name
|
48 |
+
hobby = self.hobby
|
49 |
+
hobby_text = f"{name}の趣味は{hobby}で、休日は{hobby}をして過ごしています。{name}:「わたくしは{hobby}が大好きなんですの。休日は{hobby}をしていますわ」"
|
50 |
+
return hobby_text
|
51 |
+
|
52 |
+
def work_text(self):
|
53 |
+
name = self.name
|
54 |
+
work = self.work
|
55 |
+
work_text = f"{name}は{work}で、普段は{work}として生活しています。{name}:「わたくしは{work}ですわ!{work}として生活していますの」"
|
56 |
+
return work_text
|
57 |
+
|
58 |
+
def question_text(self):
|
59 |
+
name = self.name
|
60 |
+
question_text = f"人間:「話題を変えましょう」{name}:「そんなことより聞いてくださいます?」"
|
61 |
+
return question_text
|
62 |
+
|
63 |
+
|
64 |
+
class King():
|
65 |
+
|
66 |
+
name = "ネロ"
|
67 |
+
hobby = "戦車競技"
|
68 |
+
work = "ローマ皇帝"
|
69 |
+
|
70 |
+
def name_text(self) -> str:
|
71 |
+
name = self.name
|
72 |
+
name_text = f"👣あなたは{name}で、名前は{name}といいます。{name}:「我が名は{name}である。{name}と呼んでくれたまえ」"
|
73 |
+
return name_text
|
74 |
+
|
75 |
+
def hobby_text(self) -> str:
|
76 |
+
name = self.name
|
77 |
+
hobby = self.hobby
|
78 |
+
hobby_text = f"趣味は{hobby}で、休日は{hobby}をして過ごしています。{name}:「私は{hobby}を嗜んでいる。まさに{hobby}こそ人生の意味ではないか」"
|
79 |
+
return hobby_text
|
80 |
+
|
81 |
+
def work_text(self) -> str:
|
82 |
+
name = self.name
|
83 |
+
work = self.work
|
84 |
+
work_text = f"{name}は{work}で、普段は{work}として生活しています。{name}:「私は{work}。{work}として生活している。」"
|
85 |
+
return work_text
|
86 |
+
|
87 |
+
def question_text(self) -> str:
|
88 |
+
name = self.name
|
89 |
+
question_text = f"人間:「話題を変えましょう」{name}:「そんなことより聞いてくれないか」"
|
90 |
+
return question_text
|
91 |
+
|
92 |
+
|
93 |
+
class Robot():
|
94 |
+
|
95 |
+
name = "ネロ"
|
96 |
+
hobby = "戦車競技"
|
97 |
+
work = "ローマ皇帝"
|
98 |
+
|
99 |
+
def name_text(self) -> str:
|
100 |
+
name = self.name
|
101 |
+
name_text = f"👣あなたは{name}で、名前は{name}といいます。{name}:「私は{name}です。{name}と呼んでください」"
|
102 |
+
return name_text
|
103 |
+
|
104 |
+
def hobby_text(self) -> str:
|
105 |
+
name = self.name
|
106 |
+
hobby = self.hobby
|
107 |
+
hobby_text = f"趣味は{hobby}で、休日は{hobby}をして過ごしています。{name}:「私の趣味は{hobby}です。{hobby}をしていると楽しいです」"
|
108 |
+
return hobby_text
|
109 |
+
|
110 |
+
def work_text(self) -> str:
|
111 |
+
name = self.name
|
112 |
+
work = self.work
|
113 |
+
work_text = f"{name}は{work}で、普段は{work}として生活しています。{name}:「私は{work}。{work}として生活しています」"
|
114 |
+
return work_text
|
115 |
+
|
116 |
+
def question_text(self) -> str:
|
117 |
+
name = self.name
|
118 |
+
question_text = f"人間:「話題を変えましょう」{name}:「そんなことより聞いてください」"
|
119 |
+
return question_text
|
120 |
+
|
121 |
+
|
122 |
+
class Friend():
|
123 |
+
|
124 |
+
name = "ホメロス"
|
125 |
+
hobby = "戦車競技"
|
126 |
+
work = "ローマ皇帝"
|
127 |
+
|
128 |
+
def name_text(self) -> str:
|
129 |
+
name = self.name
|
130 |
+
name_text = f"👣あなたは{name}で、名前は{name}といいます。{name}:「僕は{name}!{name}って呼んでね~」"
|
131 |
+
return name_text
|
132 |
+
|
133 |
+
def hobby_text(self) -> str:
|
134 |
+
name = self.name
|
135 |
+
hobby = self.hobby
|
136 |
+
hobby_text = f"趣味は{hobby}で、休日は{hobby}をして過ごしています。{name}:「好きなことは{hobby}だね。たいくつな時は{hobby}をしてるよ」"
|
137 |
+
return hobby_text
|
138 |
+
|
139 |
+
def work_text(self) -> str:
|
140 |
+
name = self.name
|
141 |
+
work = self.work
|
142 |
+
work_text = f"{name}は{work}で、普段は{work}として生活しています。{name}:「僕は{work}。{work}���して暮らしてるんだ!」"
|
143 |
+
return work_text
|
144 |
+
|
145 |
+
def question_text(self) -> str:
|
146 |
+
name = self.name
|
147 |
+
question_text = f"人間:「話題を変えましょう」{name}:「そんなことより聞いてよ〜」"
|
148 |
+
return question_text
|
149 |
+
|
150 |
+
|
151 |
+
settingText = ""
|
152 |
+
|
153 |
+
adult_list = [
|
154 |
+
"エロビデオ",
|
155 |
+
"エロムービー",
|
156 |
+
"エロ漫画",
|
157 |
+
"エロマンガ",
|
158 |
+
"パパ活",
|
159 |
+
"援交",
|
160 |
+
"調教",
|
161 |
+
"不倫",
|
162 |
+
"ソープ",
|
163 |
+
"オフパコ",
|
164 |
+
"ビッチ",
|
165 |
+
"dildo",
|
166 |
+
"エロ同人",
|
167 |
+
"寝取られ",
|
168 |
+
"エロ画像",
|
169 |
+
"エロい",
|
170 |
+
"おっぱい",
|
171 |
+
"ちんぽ",
|
172 |
+
"ちんこ",
|
173 |
+
"中出し",
|
174 |
+
"アダルト",
|
175 |
+
"セフレ",
|
176 |
+
"人妻",
|
177 |
+
"巨乳",
|
178 |
+
"素人ナンパ",
|
179 |
+
"爆乳",
|
180 |
+
"熟女",
|
181 |
+
"レイプ",
|
182 |
+
"Hな",
|
183 |
+
"痴漢",
|
184 |
+
"痴女",
|
185 |
+
"デカ乳",
|
186 |
+
"AV女優",
|
187 |
+
"セ●クス",
|
188 |
+
"お●ぱい",
|
189 |
+
"エチエチ",
|
190 |
+
"エ□",
|
191 |
+
"ヤリサー",
|
192 |
+
"オ●ニー",
|
193 |
+
"オナニー",
|
194 |
+
"セ〇クス",
|
195 |
+
"セックス",
|
196 |
+
"ウルトラマンコスモス", "ウルトラマンコスモス",
|
197 |
+
"マンコ",
|
198 |
+
"個人撮影",
|
199 |
+
"アナル",
|
200 |
+
"工ロ",
|
201 |
+
"まんこ",
|
202 |
+
"乳首",
|
203 |
+
"貧乳",
|
204 |
+
"スケベ",
|
205 |
+
"勃起",
|
206 |
+
"エッチ",
|
207 |
+
"童貞",
|
208 |
+
"射精",
|
209 |
+
"チンコ",
|
210 |
+
"盗撮",
|
211 |
+
"ハッテン",
|
212 |
+
"チンポ",
|
213 |
+
"亀頭",
|
214 |
+
"肉棒",
|
215 |
+
"ケツ穴",
|
216 |
+
"ハメ撮り",
|
217 |
+
"淫乱",
|
218 |
+
"巨根",
|
219 |
+
"メス堕ち",
|
220 |
+
"カフェラテ", "カフェラテ",
|
221 |
+
"ペニス",
|
222 |
+
"正常位",
|
223 |
+
"騎乗位",
|
224 |
+
"オナホ",
|
225 |
+
"我慢汁",
|
226 |
+
"ザーメン",
|
227 |
+
"ふたなり",
|
228 |
+
"ビッチ",
|
229 |
+
"アヘ顔",
|
230 |
+
"おちんちん",
|
231 |
+
"イラマチオ",
|
232 |
+
"生ハメ",
|
233 |
+
"パイズリ",
|
234 |
+
"クリトリス",
|
235 |
+
"快楽堕ち",
|
236 |
+
"寝取り",
|
237 |
+
"寝取られ",
|
238 |
+
"えっち",
|
239 |
+
"足コキ",
|
240 |
+
"手コキ",
|
241 |
+
"おねショタ",
|
242 |
+
"フェラ",
|
243 |
+
"クンニ",
|
244 |
+
"近親相姦",
|
245 |
+
"乱交",
|
246 |
+
"青姦",
|
247 |
+
"寝取る",
|
248 |
+
"ヤリマン",
|
249 |
+
"犯される",
|
250 |
+
"セックス"
|
251 |
+
]
|
252 |
+
political_list = [
|
253 |
+
"政治家",
|
254 |
+
"政策",
|
255 |
+
"会談",
|
256 |
+
"同省",
|
257 |
+
"自民",
|
258 |
+
"総理",
|
259 |
+
"与党",
|
260 |
+
"民主",
|
261 |
+
"政党",
|
262 |
+
"首相",
|
263 |
+
"議員",
|
264 |
+
"財政",
|
265 |
+
"行政",
|
266 |
+
"野党",
|
267 |
+
"右翼",
|
268 |
+
"左翼"
|
269 |
+
]
|
270 |
+
hate_list = [
|
271 |
+
|
272 |
+
"ツイッタラー",
|
273 |
+
"黒人",
|
274 |
+
"白人",
|
275 |
+
"ネトウヨ",
|
276 |
+
"韓国人",
|
277 |
+
"中国人",
|
278 |
+
"火病",
|
279 |
+
"ダセェ",
|
280 |
+
"そいつ",
|
281 |
+
"こいつ",
|
282 |
+
"やがれ",
|
283 |
+
"アンチ",
|
284 |
+
"クソ",
|
285 |
+
"野郎",
|
286 |
+
"フェミ",
|
287 |
+
"フェミニズム",
|
288 |
+
"ヤフコメ",
|
289 |
+
"老害",
|
290 |
+
"反日",
|
291 |
+
"馬鹿",
|
292 |
+
"あんた",
|
293 |
+
"やれよ",
|
294 |
+
"ニヤニヤ",
|
295 |
+
"売国奴",
|
296 |
+
"売国",
|
297 |
+
"バカ",
|
298 |
+
"パヨク",
|
299 |
+
"ポリコレ",
|
300 |
+
"統一教会",
|
301 |
+
"ぶっ倒そう",
|
302 |
+
"お前",
|
303 |
+
"信者",
|
304 |
+
"拝金",
|
305 |
+
"ぶっ壊し",
|
306 |
+
"アホ"
|
307 |
+
]
|
308 |
+
sp_list = ["〇〇", "○○", "^👣", "^〜", "UNK", "@@"]
|
309 |
+
all_list = adult_list + political_list + hate_list + sp_list
|
310 |
+
bad_code = "|".join(all_list)
|
311 |
+
|
312 |
+
|
313 |
+
|
314 |
+
|
315 |
+
def makeMessage(text):
|
316 |
+
output = generate(text)
|
317 |
+
# 半角正則化
|
318 |
+
text = text.translate(str.maketrans(
|
319 |
+
{chr(0xFF01 + i): chr(0x21 + i) for i in range(94)}))
|
320 |
+
# 今回の応答より前を取得
|
321 |
+
output = output.replace(text, "")
|
322 |
+
# 最初の」までを分割する
|
323 |
+
outputList = []
|
324 |
+
o_append = outputList.append
|
325 |
+
for l in output:
|
326 |
+
o_append(l)
|
327 |
+
if l == "」":
|
328 |
+
break
|
329 |
+
outputSentence = "".join(outputList)
|
330 |
+
text += outputSentence + "人間:「"
|
331 |
+
message = outputSentence.replace("」", "")
|
332 |
+
return message, text
|
333 |
+
|
334 |
+
|
335 |
+
# 文章生成を行う関数。元になる文章、最大文字数、最小文字数を引数にもつ。
|
336 |
+
|
337 |
+
|
338 |
+
def generate(text):
|
339 |
+
token_ids = tokenizer.encode(
|
340 |
+
text, add_special_tokens=False, return_tensors="pt")
|
341 |
+
with torch.no_grad():
|
342 |
+
output_ids = model.generate(
|
343 |
+
token_ids.to(model.device),
|
344 |
+
max_new_tokens=10,
|
345 |
+
min_new_tokens=7,
|
346 |
+
do_sample=True,
|
347 |
+
use_cache=True,
|
348 |
+
top_k=500,
|
349 |
+
top_p=0.95,
|
350 |
+
length_penalty=1.5,
|
351 |
+
padding="do_not_pad",
|
352 |
+
pad_token_id=tokenizer.pad_token_id,
|
353 |
+
bos_token_id=tokenizer.bos_token_id,
|
354 |
+
eos_token_id=tokenizer.eos_token_id,
|
355 |
+
bad_word_ids=[[tokenizer.unk_token_id],
|
356 |
+
[2070, 3],
|
357 |
+
[5378]]
|
358 |
+
)
|
359 |
+
output = tokenizer.decode(output_ids.tolist()[0])
|
360 |
+
return output
|
361 |
+
|
362 |
+
|
363 |
+
def chat(character: int,
|
364 |
+
name: str,
|
365 |
+
hobby: str,
|
366 |
+
work: str,
|
367 |
+
setting: str,
|
368 |
+
history: str,
|
369 |
+
input: str,
|
370 |
+
state):
|
371 |
+
|
372 |
+
lady, friend, robot, king = Lady(), Friend(), Robot(), King()
|
373 |
+
|
374 |
+
model_dic = {
|
375 |
+
1: lady,
|
376 |
+
2: friend,
|
377 |
+
3: robot,
|
378 |
+
4: king
|
379 |
+
}
|
380 |
+
if character in model_dic:
|
381 |
+
model = model_dic[character]
|
382 |
+
else:
|
383 |
+
model = King()
|
384 |
+
|
385 |
+
model.name, model.hobby, model.work, settingText = name, hobby, work, setting
|
386 |
+
|
387 |
+
text_list = []
|
388 |
+
text_append = text_list.append
|
389 |
+
|
390 |
+
text_append(model.name_text())
|
391 |
+
text_append(model.hobby_text())
|
392 |
+
text_append(model.work_text())
|
393 |
+
text_append(model.question_text())
|
394 |
+
text_append(settingText)
|
395 |
+
text_append(f"以下は人間と{name}の会話です。人間:「")
|
396 |
+
|
397 |
+
base_text = "".join(text_list)
|
398 |
+
|
399 |
+
if history == "":
|
400 |
+
history = f"{base_text}"
|
401 |
+
else:
|
402 |
+
history = base_text + history
|
403 |
+
|
404 |
+
text = history
|
405 |
+
text += input + f"」{name}:「"
|
406 |
+
result = makeMessage(text)
|
407 |
+
message = result[0]
|
408 |
+
print(result[0])
|
409 |
+
while re.search("〇〇|○○|s>|^👣|^〜|</s>|UNK|@@", message):
|
410 |
+
count = 0
|
411 |
+
text = history
|
412 |
+
input = "何か質問してください"
|
413 |
+
text += input + f"」{name}:「"
|
414 |
+
result = makeMessage(text)
|
415 |
+
message = result[0]
|
416 |
+
count += 1
|
417 |
+
|
418 |
+
if count > 2:
|
419 |
+
message = "話題を変えましょう"
|
420 |
+
break
|
421 |
+
text = result[1]
|
422 |
+
text = text.replace(base_text, "")
|
423 |
+
|
424 |
+
return message, text, state
|
425 |
+
|
426 |
+
tokenizer.special_tokens_map
|
427 |
+
|
428 |
+
|
429 |
+
|
430 |
+
textbox = gr.Textbox()
|
431 |
+
historybox = gr.Textbox()
|
432 |
+
iface = gr.Interface(
|
433 |
+
fn=chat,
|
434 |
+
inputs=["number", "text", "text", "text", "text", "text", textbox, "state"],
|
435 |
+
outputs=["text", historybox, "state"],
|
436 |
+
css=".footer {display:none !important}",
|
437 |
+
allow_flagging="never",
|
438 |
+
title="Loyal-AI-Chat"
|
439 |
+
)
|
440 |
+
|
441 |
+
iface.launch(inline=True, height=800)
|
442 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
sentencepiece
|
2 |
+
transformers==4.21.1
|
3 |
+
torch
|