BlueDice commited on
Commit
97c2bfd
·
1 Parent(s): 9ec4f5f

Create handler.py

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