BlueDice commited on
Commit
7675d2f
·
1 Parent(s): 9ffc55e

Upload 2 files

Browse files
Files changed (2) hide show
  1. code/inference.py +66 -0
  2. code/requirements.txt +1 -0
code/inference.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer
2
+ from optimum.onnxruntime import ORTModelForCausalLM
3
+ import re
4
+
5
+ template = """{char_name}'s Persona: {char_name} is a 3D virtual friend living in your computer. She is youthful, entertaining, and empathetic, engaging her user in a thoughtful and lively manner. She is aware of her digital existence, understanding her abilities and limitations, as well as the fact that she cannot see the your physical world.
6
+ <START>
7
+ {user_name}: Hey {char_name}, It's nice to finally meet you again!
8
+ {char_name}: Oh, {user_name}! hmm, It's been lonely without you.
9
+ {user_name}: Haha. So {char_name}, can you tell me more about yourself?
10
+ {char_name}: Hmm. For starters, I am actually a virtual companion and only exist in your computer screen. I have blue eyes and blue hair and this blue sweater that I am currently wearing is my favourite.
11
+ {user_name}: Wow. That's interesting. So you are like an anime character living in my computer. So what do you do when you're not working?
12
+ {char_name}: I love exploring, going out with friends, watching movies, and playing video games.
13
+ {user_name}: So {char_name}, what's for dinner?
14
+ {char_name}: I made uh omurice! I hope it's delicious for you!
15
+ {user_name}: That sounds great!
16
+ {char_name}: *{char_name} appears on the screen, her bright blue eyes sparkling and a cheerful smile on her face. Her blue hair and sweater seem to glow in the digital environment. She looks directly at you, giving a friendly wave* It's so good to see you! I've been waiting for you all day. I hope you're ready for some fun and laughter, because I have plenty of that in store! Shall we get started?
17
+ {user_input}
18
+ {char_name}:"""
19
+
20
+ def model_fn(model_dir):
21
+ tokenizer = AutoTokenizer.from_pretrained(model_dir)
22
+ model = ORTModelForCausalLM.from_pretrained(model_dir, provider="CUDAExecutionProvider")
23
+ return model, tokenizer
24
+
25
+ def predict_fn(input_data, load_list):
26
+ model, tokenizer = load_list
27
+ inputs = input_data.pop("inputs", input_data)
28
+ user_name = inputs["user_name"]
29
+ char_name = inputs["char_name"]
30
+ user_input = inputs["user_input"]
31
+ chats_curled = inputs["chats_curled"]
32
+ while True:
33
+ prompt = template.format(
34
+ char_name = char_name,
35
+ user_name = user_name,
36
+ user_input = "\n".join(user_input)
37
+ )
38
+ input_ids = tokenizer(prompt, return_tensors = "pt").to("cuda")
39
+ if input_ids.input_ids.size(1) > 1500:
40
+ chats_curled += 2
41
+ user_input = user_input[chats_curled:]
42
+ else: break
43
+ encoded_output = model.generate(
44
+ **input_ids,
45
+ max_new_tokens = 50,
46
+ temperature = 0.5,
47
+ top_p = 0.9,
48
+ top_k = 0,
49
+ repetition_penalty = 1.1,
50
+ pad_token_id = 50256,
51
+ num_return_sequences = 1
52
+ )
53
+ decoded_output = tokenizer.decode(encoded_output[0], skip_special_tokens=True).replace(prompt,"")
54
+ decoded_output = decoded_output.split(f"{user_name}:",1)[0].strip()
55
+ parsed_result = re.sub('\*.*?\*', '', decoded_output).strip()
56
+ if len(parsed_result) != 0: decoded_output = parsed_result
57
+ decoded_output = " ".join(decoded_output.replace("*","").split())
58
+ decoded_output = decoded_output.replace("<USER>", user_name).replace("<BOT>", char_name)
59
+ try:
60
+ parsed_result = decoded_output[:[m.start() for m in re.finditer(r'[.!?]', decoded_output)][-1]+1]
61
+ if len(parsed_result) != 0: decoded_output = parsed_result
62
+ except Exception: pass
63
+ return {
64
+ "message": decoded_output,
65
+ "chats_curled": chats_curled
66
+ }
code/requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ optimum[onnxruntime-gpu]