English-Chat / app.py
izumo092's picture
Upload 2 files
b23c4d2
raw
history blame
8.62 kB
from transformers.utils.dummy_pt_objects import NezhaModel
from time import process_time, sleep
import re
import torch.nn.utils.prune as prune
from torch import nn
import torch
from transformers import AutoTokenizer
from transformers import OPTForCausalLM
model_name = "facebook/galactica-1.3b"
model = OPTForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
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"๐Ÿ‘ฃYou are {name}, and called {name}. {name} uses a lot of internet slang. {name}:\"AMA.\" He:\"Hey! WYD? What your name?\" {name}:\"I'm {name}. Just call me {name}. lol\""
return name_text
def hobby_text(self):
name = self.name
hobby = self.hobby
hobby_text = f"{name} is interested in {hobby}. {name} spends holiday {hobby}. He:\"BTW, what your hobby?\" {name}:\"I love {hobby} cuz my life is {hobby}. Great!!! โ˜“D\""
return hobby_text
def work_text(self):
name = self.name
work = self.work
work_text = f"{name} is {work}, work as {work}. He:\"Wow! What your work?\" {name}:\"I'm {work}. {work} is my calling af. :)\""
return work_text
def question_text(self):
name = self.name
question_text = f"He:\"Let's change the subject.\" {name}:\"Listen to this story.\""
return question_text
class King():
name = "ใƒฌใ‚คใƒ†"
hobby = "ใ‚ฒใƒผใƒ "
work = "ใŠๅฌขๆง˜"
def name_text(self):
name = self.name
name_text = f"๐Ÿ‘ฃYou are {name}, and called {name}. He:\"What your name?\" {name}:\"I'm {name}. Please call me {name}.\""
return name_text
def hobby_text(self):
name = self.name
hobby = self.hobby
hobby_text = f"{name} is interested in {hobby}. {name} spends holiday {hobby}. He:\"What your hobby?\" {name}:\"My hobby is {hobby}. I enjoy doing {hobby}.\""
return hobby_text
def work_text(self):
name = self.name
work = self.work
work_text = f"{name} is {work}, work as {work}. He:\"What your work?\" {name}:\"I {work} I live my life as {work}.\""
return work_text
def question_text(self):
name = self.name
question_text = f"He:Let's change the subject. {name}:\"Listen to this story.\""
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
# ๆ–‡็ซ ็”Ÿๆˆใ‚’่กŒใ†้–ขๆ•ฐใ€‚ๅ…ƒใซใชใ‚‹ๆ–‡็ซ ใ€ๆœ€ๅคงๆ–‡ๅญ—ๆ•ฐใ€ๆœ€ๅฐๆ–‡ๅญ—ๆ•ฐใ‚’ๅผ•ๆ•ฐใซใ‚‚ใคใ€‚
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=15,
min_new_tokens=10,
do_sample=True,
top_k=500,
top_p=0.95,
padding="do_not_pad",
pad_token_id=tokenizer.bos_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 makeMessage_2(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)
message = outputSentence.replace(".\"", "")
historyList = []
h_append = historyList.append
h_append(text)
h_append(outputSentence)
h_append("He:\"")
text = "".join(historyList)
return message, text
# ๆ–‡็ซ ็”Ÿๆˆใ‚’่กŒใ†้–ขๆ•ฐใ€‚ๅ…ƒใซใชใ‚‹ๆ–‡็ซ ใ€ๆœ€ๅคงๆ–‡ๅญ—ๆ•ฐใ€ๆœ€ๅฐๆ–‡ๅญ—ๆ•ฐใ‚’ๅผ•ๆ•ฐใซใ‚‚ใคใ€‚
def chat(character: int,
name: str,
hobby: str,
work: str,
history: str,
input: str,
state):
lady, friend, robot, king = Lady(), Friend(), Robot(), King()
model_dic = {
1: lady,
2: friend,
3: robot,
4: king
}
start = process_time()
if character in model_dic:
model = model_dic[character]
else:
model = King()
model.name, model.hobby, model.work = name, hobby, work
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(
f"The following is a conversation between a friend and {name}. He:\"")
base_text = "".join(text_list)
if history == "":
history = f"{base_text}"
text = history
text += input + f"\"{name}:\""
m_start = process_time()
result = makeMessage_2(text)
m_end = process_time()
print(f"็”Ÿๆˆ{m_end-m_start}")
message = result[0]
print(message)
while re.search("ใ€‡ใ€‡|โ—‹โ—‹|s>|^๐Ÿ‘ฃ|^ใ€œ|</s>|UNK|@@", message):
print("error")
count = 0
text = history
input = "ไฝ•ใ‹่ณชๅ•ใ—ใฆใใ ใ•ใ„"
text += input + f"ใ€{name}:ใ€Œ"
result = makeMessage(text)
message = result[0]
print(message)
count += 1
if count > 2:
message = "่ฉฑ้กŒใ‚’ๅค‰ใˆใพใ—ใ‚‡ใ†"
break
text = result[1]
end = process_time()
print(end-start)
return message, text, state
import gradio as gr
textbox = gr.Textbox()
historybox = gr.Textbox()
iface = gr.Interface(
chat,
["number","text","text","text","text",textbox, "state"],
["text", historybox, "state"],
css=".footer {display:none !important}",
allow_flagging="never",
title="Loyal-AI-Chat"
)
iface.launch(inline=True, height=800)