Commit ·
eadaee3
1
Parent(s): d738823
Upload handler.py
Browse files- handler.py +39 -0
handler.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 4 |
+
from peft import PeftModel
|
| 5 |
+
|
| 6 |
+
class EndpointHandler():
|
| 7 |
+
def __init__(self, path=""):
|
| 8 |
+
base_model_id = "mistralai/Mistral-7B-v0.1"
|
| 9 |
+
bnb_config = BitsAndBytesConfig(
|
| 10 |
+
load_in_4bit=True,
|
| 11 |
+
bnb_4bit_use_double_quant=True,
|
| 12 |
+
bnb_4bit_quant_type="nf4",
|
| 13 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
+
base_model_id, # Mistral, same as before
|
| 18 |
+
quantization_config=bnb_config, # Same quantization config as before
|
| 19 |
+
device_map="auto",
|
| 20 |
+
trust_remote_code=True,
|
| 21 |
+
# use_auth_token=True
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_id, add_bos_token=True, trust_remote_code=True)
|
| 25 |
+
|
| 26 |
+
ft_model = PeftModel.from_pretrained(base_model, "./checkpoint-100")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def __call__(self, eval_prompt):
|
| 31 |
+
|
| 32 |
+
model_input = tokenizer(eval_prompt, return_tensors="pt").to("cuda")
|
| 33 |
+
|
| 34 |
+
ft_model.eval()
|
| 35 |
+
with torch.no_grad():
|
| 36 |
+
prediction = (tokenizer.decode(ft_model.generate(**model_input, max_new_tokens=100, repetition_penalty=1.15)[0], skip_special_tokens=True))
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
return prediction
|