Spaces:
Runtime error
Runtime error
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) | |