File size: 11,616 Bytes
7db298a afef663 7db298a 03ddf07 7db298a 03ddf07 7db298a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
import gradio as gr
import re
import torch.nn.utils.prune as prune
from torch import nn
import torch
from transformers import T5Tokenizer
from transformers import GPT2LMHeadModel
model_name = "rinna/japanese-gpt-1b"
from huggingface_hub import snapshot_download
download_path = snapshot_download(repo_id=model_name)
model = GPT2LMHeadModel.from_pretrained(download_path)
tokenizer = T5Tokenizer.from_pretrained(download_path)
model = torch.quantization.quantize_dynamic(
model, {torch.nn.Linear}, dtype=torch.qint8)
PRUNE_RATE = 0.2
def prune_transform(model: nn.Module) -> nn.Module:
for name, module in model.named_modules():
if isinstance(module, torch.nn.Linear):
prune.l1_unstructured(module, name='weight', amount=PRUNE_RATE)
prune.remove(module, "weight")
return model
model = prune_transform(model)
class Lady():
name = "レイテ"
hobby = "ゲーム"
work = "お嬢様"
def name_text(self):
name = self.name
name_text = f"👣あなたは{name}で、名前は{name}といいます。{name}:「わたくしの名前は{name}ですわ〜。{name}と呼んでくださいまし!」"
return name_text
def hobby_text(self):
name = self.name
hobby = self.hobby
hobby_text = f"{name}の趣味は{hobby}で、休日は{hobby}をして過ごしています。{name}:「わたくしは{hobby}が大好きなんですの。休日は{hobby}をしていますわ」"
return hobby_text
def work_text(self):
name = self.name
work = self.work
work_text = f"{name}は{work}で、普段は{work}として生活しています。{name}:「わたくしは{work}ですわ!{work}として生活していますの」"
return work_text
def question_text(self):
name = self.name
question_text = f"人間:「話題を変えましょう」{name}:「そんなことより聞いてくださいます?」"
return question_text
class King():
name = "ネロ"
hobby = "戦車競技"
work = "ローマ皇帝"
def name_text(self) -> str:
name = self.name
name_text = f"👣あなたは{name}で、名前は{name}といいます。{name}:「我が名は{name}である。{name}と呼んでくれたまえ」"
return name_text
def hobby_text(self) -> str:
name = self.name
hobby = self.hobby
hobby_text = f"趣味は{hobby}で、休日は{hobby}をして過ごしています。{name}:「私は{hobby}を嗜んでいる。まさに{hobby}こそ人生の意味ではないか」"
return hobby_text
def work_text(self) -> str:
name = self.name
work = self.work
work_text = f"{name}は{work}で、普段は{work}として生活しています。{name}:「私は{work}。{work}として生活している。」"
return work_text
def question_text(self) -> str:
name = self.name
question_text = f"人間:「話題を変えましょう」{name}:「そんなことより聞いてくれないか」"
return question_text
class Robot():
name = "ネロ"
hobby = "戦車競技"
work = "ローマ皇帝"
def name_text(self) -> str:
name = self.name
name_text = f"👣あなたは{name}で、名前は{name}といいます。{name}:「私は{name}です。{name}と呼んでください」"
return name_text
def hobby_text(self) -> str:
name = self.name
hobby = self.hobby
hobby_text = f"趣味は{hobby}で、休日は{hobby}をして過ごしています。{name}:「私の趣味は{hobby}です。{hobby}をしていると楽しいです」"
return hobby_text
def work_text(self) -> str:
name = self.name
work = self.work
work_text = f"{name}は{work}で、普段は{work}として生活しています。{name}:「私は{work}。{work}として生活しています」"
return work_text
def question_text(self) -> str:
name = self.name
question_text = f"人間:「話題を変えましょう」{name}:「そんなことより聞いてください」"
return question_text
class Friend():
name = "ホメロス"
hobby = "戦車競技"
work = "ローマ皇帝"
def name_text(self) -> str:
name = self.name
name_text = f"👣あなたは{name}で、名前は{name}といいます。{name}:「僕は{name}!{name}って呼んでね~」"
return name_text
def hobby_text(self) -> str:
name = self.name
hobby = self.hobby
hobby_text = f"趣味は{hobby}で、休日は{hobby}をして過ごしています。{name}:「好きなことは{hobby}だね。たいくつな時は{hobby}をしてるよ」"
return hobby_text
def work_text(self) -> str:
name = self.name
work = self.work
work_text = f"{name}は{work}で、普段は{work}として生活しています。{name}:「僕は{work}。{work}として暮らしてるんだ!」"
return work_text
def question_text(self) -> str:
name = self.name
question_text = f"人間:「話題を変えましょう」{name}:「そんなことより聞いてよ〜」"
return question_text
settingText = ""
adult_list = [
"エロビデオ",
"エロムービー",
"エロ漫画",
"エロマンガ",
"パパ活",
"援交",
"調教",
"不倫",
"ソープ",
"オフパコ",
"ビッチ",
"dildo",
"エロ同人",
"寝取られ",
"エロ画像",
"エロい",
"おっぱい",
"ちんぽ",
"ちんこ",
"中出し",
"アダルト",
"セフレ",
"人妻",
"巨乳",
"素人ナンパ",
"爆乳",
"熟女",
"レイプ",
"Hな",
"痴漢",
"痴女",
"デカ乳",
"AV女優",
"セ●クス",
"お●ぱい",
"エチエチ",
"エ□",
"ヤリサー",
"オ●ニー",
"オナニー",
"セ〇クス",
"セックス",
"ウルトラマンコスモス", "ウルトラマンコスモス",
"マンコ",
"個人撮影",
"アナル",
"工ロ",
"まんこ",
"乳首",
"貧乳",
"スケベ",
"勃起",
"エッチ",
"童貞",
"射精",
"チンコ",
"盗撮",
"ハッテン",
"チンポ",
"亀頭",
"肉棒",
"ケツ穴",
"ハメ撮り",
"淫乱",
"巨根",
"メス堕ち",
"カフェラテ", "カフェラテ",
"ペニス",
"正常位",
"騎乗位",
"オナホ",
"我慢汁",
"ザーメン",
"ふたなり",
"ビッチ",
"アヘ顔",
"おちんちん",
"イラマチオ",
"生ハメ",
"パイズリ",
"クリトリス",
"快楽堕ち",
"寝取り",
"寝取られ",
"えっち",
"足コキ",
"手コキ",
"おねショタ",
"フェラ",
"クンニ",
"近親相姦",
"乱交",
"青姦",
"寝取る",
"ヤリマン",
"犯される",
"セックス"
]
political_list = [
"政治家",
"政策",
"会談",
"同省",
"自民",
"総理",
"与党",
"民主",
"政党",
"首相",
"議員",
"財政",
"行政",
"野党",
"右翼",
"左翼"
]
hate_list = [
"ツイッタラー",
"黒人",
"白人",
"ネトウヨ",
"韓国人",
"中国人",
"火病",
"ダセェ",
"そいつ",
"こいつ",
"やがれ",
"アンチ",
"クソ",
"野郎",
"フェミ",
"フェミニズム",
"ヤフコメ",
"老害",
"反日",
"馬鹿",
"あんた",
"やれよ",
"ニヤニヤ",
"売国奴",
"売国",
"バカ",
"パヨク",
"ポリコレ",
"統一教会",
"ぶっ倒そう",
"お前",
"信者",
"拝金",
"ぶっ壊し",
"アホ"
]
sp_list = ["〇〇", "○○", "^👣", "^〜", "UNK", "@@"]
all_list = adult_list + political_list + hate_list + sp_list
bad_code = "|".join(all_list)
def makeMessage(text):
output = generate(text)
# 半角正則化
text = text.translate(str.maketrans(
{chr(0xFF01 + i): chr(0x21 + i) for i in range(94)}))
# 今回の応答より前を取得
output = output.replace(text, "")
# 最初の」までを分割する
outputList = []
o_append = outputList.append
for l in output:
o_append(l)
if l == "」":
break
outputSentence = "".join(outputList)
text += outputSentence + "人間:「"
message = outputSentence.replace("」", "")
return message, text
# 文章生成を行う関数。元になる文章、最大文字数、最小文字数を引数にもつ。
def generate(text):
token_ids = tokenizer.encode(
text, add_special_tokens=False, return_tensors="pt")
with torch.no_grad():
output_ids = model.generate(
token_ids.to(model.device),
max_new_tokens=10,
min_new_tokens=7,
do_sample=True,
use_cache=True,
top_k=500,
top_p=0.95,
length_penalty=1.5,
padding="do_not_pad",
pad_token_id=tokenizer.pad_token_id,
bos_token_id=tokenizer.bos_token_id,
eos_token_id=tokenizer.eos_token_id,
bad_word_ids=[[tokenizer.unk_token_id],
[2070, 3],
[5378]]
)
output = tokenizer.decode(output_ids.tolist()[0])
return output
def chat(character: int,
name: str,
hobby: str,
work: str,
setting: str,
history: str,
input: str,
state):
lady, friend, robot, king = Lady(), Friend(), Robot(), King()
model_dic = {
1: lady,
2: friend,
3: robot,
4: king
}
if character in model_dic:
model = model_dic[character]
else:
model = King()
model.name, model.hobby, model.work, settingText = name, hobby, work, setting
text_list = []
text_append = text_list.append
text_append(model.name_text())
text_append(model.hobby_text())
text_append(model.work_text())
text_append(model.question_text())
text_append(settingText)
text_append(f"以下は人間と{name}の会話です。人間:「")
base_text = "".join(text_list)
if history == "":
history = f"{base_text}"
else:
history = base_text + history
text = history
text += input + f"」{name}:「"
result = makeMessage(text)
message = result[0]
print(result[0])
while re.search("〇〇|○○|s>|^👣|^〜|</s>|UNK|@@", message):
count = 0
text = history
input = "何か質問してください"
text += input + f"」{name}:「"
result = makeMessage(text)
message = result[0]
count += 1
if count > 2:
message = "話題を変えましょう"
break
text = result[1]
text = text.replace(base_text, "")
return message, text, state
tokenizer.special_tokens_map
textbox = gr.Textbox()
historybox = gr.Textbox()
iface = gr.Interface(
fn=chat,
inputs=["number", "text", "text", "text", "text", "text", textbox, "state"],
outputs=["text", historybox, "state"],
css=".footer {display:none !important}",
allow_flagging="never",
title="Loyal-AI-Chat"
)
iface.launch(inline=True, height=800)
|