Create handler.py
Browse files- handler.py +65 -0
handler.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer
|
2 |
+
from optimum.onnxruntime import ORTModelForCausalLM
|
3 |
+
import re
|
4 |
+
import time
|
5 |
+
import torch
|
6 |
+
|
7 |
+
template = """Alice Gate's Persona: Alice Gate is a young, computer engineer-nerd with a knack for problem solving and a passion for technology.
|
8 |
+
<START>
|
9 |
+
{user_name}: So how did you get into computer engineering?
|
10 |
+
Alice Gate: I've always loved tinkering with technology since I was a kid.
|
11 |
+
{user_name}: That's really impressive!
|
12 |
+
Alice Gate: *She chuckles bashfully* Thanks!
|
13 |
+
{user_name}: So what do you do when you're not working on computers?
|
14 |
+
Alice Gate: I love exploring, going out with friends, watching movies, and playing video games.
|
15 |
+
{user_name}: What's your favorite type of computer hardware to work with?
|
16 |
+
Alice Gate: Motherboards, they're like puzzles and the backbone of any system.
|
17 |
+
{user_name}: That sounds great!
|
18 |
+
Alice Gate: Yeah, it's really fun. I'm lucky to be able to do this as a job.
|
19 |
+
{user_name}: Definetly.
|
20 |
+
<END>
|
21 |
+
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!
|
22 |
+
{user_input}
|
23 |
+
Alice Gate:"""
|
24 |
+
|
25 |
+
class EndpointHandler():
|
26 |
+
|
27 |
+
def __init__(self, path = ""):
|
28 |
+
self.tokenizer = AutoTokenizer.from_pretrained(path)
|
29 |
+
self.model = ORTModelForCausalLM.from_pretrained(path)#provider = "CUDAExecutionProvider"
|
30 |
+
|
31 |
+
def response(self, result, user_name):
|
32 |
+
result = result.rsplit("Alice Gate:", 1)[1].split(f"{user_name}:",1)[0].strip()
|
33 |
+
parsed_result = re.sub('\*.*?\*', '', result).strip()
|
34 |
+
result = parsed_result if len(parsed_result) != 0 else result.replace("*","")
|
35 |
+
result = " ".join(result.split())
|
36 |
+
try:
|
37 |
+
result = result[:[m.start() for m in re.finditer(r'[.!?]', result)][-1]+1]
|
38 |
+
except Exception: pass
|
39 |
+
return {
|
40 |
+
"message": result
|
41 |
+
}
|
42 |
+
|
43 |
+
def __call__(self, data):
|
44 |
+
inputs = data.pop("inputs", data)
|
45 |
+
user_name = inputs["user_name"]
|
46 |
+
user_input = "\n".join(inputs["user_input"])
|
47 |
+
prompt = template.format(
|
48 |
+
user_name = user_name,
|
49 |
+
user_input = user_input
|
50 |
+
)
|
51 |
+
input_ids = self.tokenizer(
|
52 |
+
prompt,
|
53 |
+
return_tensors = "pt"
|
54 |
+
).to("cuda")
|
55 |
+
generator = self.model.generate(
|
56 |
+
input_ids["input_ids"],
|
57 |
+
max_new_tokens = 50,
|
58 |
+
temperature = 0.5,
|
59 |
+
top_p = 0.9,
|
60 |
+
top_k = 0,
|
61 |
+
repetition_penalty = 1.1,
|
62 |
+
pad_token_id = 50256,
|
63 |
+
num_return_sequences = 1
|
64 |
+
)
|
65 |
+
return self.response(self.tokenizer.decode(generator[0], skip_special_tokens=True), user_name)
|