Delete code
Browse files- code/inference.py +0 -71
- code/requirements.txt +0 -1
code/inference.py
DELETED
|
@@ -1,71 +0,0 @@
|
|
| 1 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
-
import torch
|
| 3 |
-
import re
|
| 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 |
-
def model_fn(model_dir):
|
| 24 |
-
# Load model from HuggingFace Hub
|
| 25 |
-
tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
| 26 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
-
model_dir,
|
| 28 |
-
low_cpu_mem_usage = True,
|
| 29 |
-
trust_remote_code = False,
|
| 30 |
-
torch_dtype = torch.float16,
|
| 31 |
-
).to('cuda')
|
| 32 |
-
return model, tokenizer
|
| 33 |
-
|
| 34 |
-
def create_new_response(result, user_name):
|
| 35 |
-
result = result.rsplit("Alice Gate:", 1)[1].split(f"{user_name}:",1)[0].strip()
|
| 36 |
-
parsed_result = re.sub('\*.*?\*', '', result).strip()
|
| 37 |
-
result = parsed_result if len(parsed_result) != 0 else result.replace("*","")
|
| 38 |
-
result = " ".join(result.split())
|
| 39 |
-
try:
|
| 40 |
-
result = result[:[m.start() for m in re.finditer(r'[.!?]', result)][-1]+1]
|
| 41 |
-
except Exception: pass
|
| 42 |
-
return {
|
| 43 |
-
"message": result
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
def predict_fn(data, model_and_tokenizer):
|
| 47 |
-
# destruct model and tokenizer
|
| 48 |
-
model, tokenizer = model_and_tokenizer
|
| 49 |
-
|
| 50 |
-
# Tokenize sentences
|
| 51 |
-
user_name = inputs["user_name"]
|
| 52 |
-
user_input = "\n".join(inputs["user_input"])
|
| 53 |
-
prompt = template.format(
|
| 54 |
-
user_name = user_name,
|
| 55 |
-
user_input = user_input
|
| 56 |
-
)
|
| 57 |
-
input_ids = tokenizer(
|
| 58 |
-
prompt,
|
| 59 |
-
return_tensors = "pt"
|
| 60 |
-
).to("cuda")
|
| 61 |
-
generator = model.generate(
|
| 62 |
-
input_ids["input_ids"],
|
| 63 |
-
max_new_tokens = 50,
|
| 64 |
-
temperature = 0.5,
|
| 65 |
-
top_p = 0.9,
|
| 66 |
-
top_k = 0,
|
| 67 |
-
repetition_penalty = 1.1,
|
| 68 |
-
pad_token_id = 50256,
|
| 69 |
-
num_return_sequences = 1
|
| 70 |
-
)
|
| 71 |
-
return create_new_response(tokenizer.decode(generator[0], skip_special_tokens=True), user_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
code/requirements.txt
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
accelerate==0.18.0
|
|
|
|
|
|