Devops-hestabit commited on
Commit
96deb47
·
1 Parent(s): 711cfa9

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +63 -0
handler.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForCausalLM
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
+ Alice Gate:"""
22
+
23
+ class EndpointHandler():
24
+
25
+ def __init__(self, path = ""):
26
+ self.tokenizer = AutoTokenizer.from_pretrained(path)
27
+ self.model = AutoModelForCausalLM.from_pretrained(
28
+ path,
29
+ low_cpu_mem_usage = True,
30
+ trust_remote_code = False,
31
+ torch_dtype = torch.float16,
32
+ ).to('cuda')
33
+
34
+ def response(self, result, user_name):
35
+ result = result.rsplit("Alice Gate:", 1)[1].split(f"{user_name}:",1)[0].strip()
36
+ result = re.sub('\*.*?\*', '', result)
37
+ result = " ".join(result.split())
38
+ return {
39
+ "message": result
40
+ }
41
+
42
+ def __call__(self, data):
43
+ inputs = data.pop("inputs", data)
44
+ user_name = inputs["user_name"]
45
+ user_input = "\n".join(inputs["user_input"])
46
+ input_ids = self.tokenizer(
47
+ template.format(
48
+ user_name = user_name,
49
+ user_input = user_input
50
+ ),
51
+ return_tensors = "pt"
52
+ ).to("cuda")
53
+ generator = self.model.generate(
54
+ input_ids["input_ids"],
55
+ max_new_tokens = 50,
56
+ temperature = 0.5,
57
+ top_p = 0.9,
58
+ top_k = 0,
59
+ repetition_penalty = 1.1,
60
+ pad_token_id = 50256,
61
+ num_return_sequences = 1
62
+ )
63
+ return self.response(self.tokenizer.decode(generator[0], skip_special_tokens=True), user_name)