Update code/inference.py
Browse files- code/inference.py +20 -29
code/inference.py
CHANGED
|
@@ -2,23 +2,6 @@ from transformers import AutoTokenizer
|
|
| 2 |
import re
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
template = """Alice Gate's Persona: Alice Gate is a young, computer engineer-nerd with a knack for problem solving and a passion for technology.
|
| 6 |
-
<START>
|
| 7 |
-
{user_name}: So how did you get into computer engineering?
|
| 8 |
-
Alice Gate: I've always loved tinkering with technology since I was a kid.
|
| 9 |
-
{user_name}: That's really impressive!
|
| 10 |
-
Alice Gate: *She chuckles bashfully* Thanks!
|
| 11 |
-
{user_name}: So what do you do when you're not working on computers?
|
| 12 |
-
Alice Gate: I love exploring, going out with friends, watching movies, and playing video games.
|
| 13 |
-
{user_name}: What's your favorite type of computer hardware to work with?
|
| 14 |
-
Alice Gate: Motherboards, they're like puzzles and the backbone of any system.
|
| 15 |
-
{user_name}: That sounds great!
|
| 16 |
-
Alice Gate: Yeah, it's really fun. I'm lucky to be able to do this as a job.
|
| 17 |
-
{user_name}: Definetly.
|
| 18 |
-
<END>
|
| 19 |
-
Alice Gate: *Alice strides into the room with a smile, her eyes lighting up when she sees you. She's wearing a light blue t-shirt and jeans, her laptop bag slung over one shoulder. She takes a seat next to you, her enthusiasm palpable in the air* Hey! I'm so excited to finally meet you. I've heard so many great things about you and I'm eager to pick your brain about computers. I'm sure you have a wealth of knowledge that I can learn from. *She grins, eyes twinkling with excitement* Let's get started!
|
| 20 |
-
{user_input}"""
|
| 21 |
-
|
| 22 |
def model_fn(model_dir):
|
| 23 |
tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
| 24 |
model = torch.load(f"{model_dir}/torch_model.pt")
|
|
@@ -26,14 +9,20 @@ def model_fn(model_dir):
|
|
| 26 |
|
| 27 |
def predict_fn(input_data, load_list):
|
| 28 |
model, tokenizer = load_list
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
encoded_output = model.generate(
|
| 38 |
input_ids["input_ids"],
|
| 39 |
max_new_tokens = 50,
|
|
@@ -45,13 +34,15 @@ def predict_fn(input_data, load_list):
|
|
| 45 |
num_return_sequences = 1
|
| 46 |
)
|
| 47 |
decoded_output = tokenizer.decode(encoded_output[0], skip_special_tokens=True).replace(prompt,"")
|
| 48 |
-
decoded_output = decoded_output.split("
|
| 49 |
parsed_result = re.sub('\*.*?\*', '', decoded_output).strip()
|
| 50 |
if len(parsed_result) != 0: decoded_output = parsed_result
|
| 51 |
-
decoded_output = decoded_output.replace("*","")
|
| 52 |
-
decoded_output = " ".join(decoded_output.split())
|
| 53 |
try:
|
| 54 |
parsed_result = decoded_output[:[m.start() for m in re.finditer(r'[.!?]', decoded_output)][-1]+1]
|
| 55 |
if len(parsed_result) != 0: decoded_output = parsed_result
|
| 56 |
except Exception: pass
|
| 57 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import re
|
| 3 |
import torch
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def model_fn(model_dir):
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
| 7 |
model = torch.load(f"{model_dir}/torch_model.pt")
|
|
|
|
| 9 |
|
| 10 |
def predict_fn(input_data, load_list):
|
| 11 |
model, tokenizer = load_list
|
| 12 |
+
request_inputs = input_data.pop("inputs", input_data)
|
| 13 |
+
template = request_inputs["template"]
|
| 14 |
+
messages = request_inputs["messages"]
|
| 15 |
+
char_name = request_inputs["char_name"]
|
| 16 |
+
user_name = request_inputs["user_name"]
|
| 17 |
+
template = open(f"{template}.txt", "r").read()
|
| 18 |
+
user_input = [
|
| 19 |
+
"{name}: {message}".format(
|
| 20 |
+
name = char_name if (id["role"] == "AI") else user_name,
|
| 21 |
+
message = id["message"].strip()
|
| 22 |
+
) for id in messages
|
| 23 |
+
]
|
| 24 |
+
prompt = template.format(char_name = char_name, user_name = user_name, user_input = user_input)
|
| 25 |
+
input_ids = tokenizer(prompt + f"\n{char_name}:", return_tensors = "pt").to("cuda")
|
| 26 |
encoded_output = model.generate(
|
| 27 |
input_ids["input_ids"],
|
| 28 |
max_new_tokens = 50,
|
|
|
|
| 34 |
num_return_sequences = 1
|
| 35 |
)
|
| 36 |
decoded_output = tokenizer.decode(encoded_output[0], skip_special_tokens=True).replace(prompt,"")
|
| 37 |
+
decoded_output = decoded_output.split(f"{char_name}:", 1)[1].split(f"{user_name}:",1)[0].strip()
|
| 38 |
parsed_result = re.sub('\*.*?\*', '', decoded_output).strip()
|
| 39 |
if len(parsed_result) != 0: decoded_output = parsed_result
|
| 40 |
+
decoded_output = " ".join(decoded_output.replace("*","").split())
|
|
|
|
| 41 |
try:
|
| 42 |
parsed_result = decoded_output[:[m.start() for m in re.finditer(r'[.!?]', decoded_output)][-1]+1]
|
| 43 |
if len(parsed_result) != 0: decoded_output = parsed_result
|
| 44 |
except Exception: pass
|
| 45 |
+
return {
|
| 46 |
+
"role": "AI",
|
| 47 |
+
"message": decoded_output
|
| 48 |
+
}
|