Update handler.py
Browse files- handler.py +68 -61
handler.py
CHANGED
|
@@ -1,72 +1,79 @@
|
|
| 1 |
from typing import Dict, List
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
from peft import PeftModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
class EndpointHandler:
|
| 7 |
def __init__(self, path: str):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 35 |
|
| 36 |
def __call__(self, data: Dict) -> List[str]:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
def preprocess(self, request):
|
| 72 |
"""Pre-process request for API compatibility"""
|
|
|
|
| 1 |
from typing import Dict, List
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
from peft import PeftModel
|
| 5 |
+
import os
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
logging.basicConfig(level=logging.INFO)
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
class EndpointHandler:
|
| 12 |
def __init__(self, path: str):
|
| 13 |
+
try:
|
| 14 |
+
logger.info("Loading base model...")
|
| 15 |
+
# Load base model with 8-bit quantization
|
| 16 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
+
"EleutherAI/gpt-j-6B",
|
| 18 |
+
load_in_8bit=True,
|
| 19 |
+
device_map="auto",
|
| 20 |
+
torch_dtype=torch.float16
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
logger.info("Loading adapter weights...")
|
| 24 |
+
# Load the adapter weights
|
| 25 |
+
self.model = PeftModel.from_pretrained(
|
| 26 |
+
base_model,
|
| 27 |
+
path
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Set up tokenizer
|
| 31 |
+
self.tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-j-6B")
|
| 32 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 33 |
+
|
| 34 |
+
logger.info("Model loaded successfully!")
|
| 35 |
+
|
| 36 |
+
except Exception as e:
|
| 37 |
+
logger.error(f"Error initializing model: {str(e)}")
|
| 38 |
+
raise
|
|
|
|
| 39 |
|
| 40 |
def __call__(self, data: Dict) -> List[str]:
|
| 41 |
+
try:
|
| 42 |
+
# Get the question from the input
|
| 43 |
+
question = data.pop("inputs", data)
|
| 44 |
+
if isinstance(question, list):
|
| 45 |
+
question = question[0]
|
| 46 |
+
|
| 47 |
+
# Format prompt exactly as in your test file
|
| 48 |
+
prompt = f"Question: {question}\nAnswer:"
|
| 49 |
+
|
| 50 |
+
# Tokenize exactly as in your test file
|
| 51 |
+
inputs = self.tokenizer(
|
| 52 |
+
prompt,
|
| 53 |
+
return_tensors="pt",
|
| 54 |
+
truncation=True,
|
| 55 |
+
max_length=512
|
| 56 |
+
).to(self.model.device)
|
| 57 |
+
|
| 58 |
+
# Generate with exact same parameters as your test file
|
| 59 |
+
with torch.inference_mode(), torch.cuda.amp.autocast():
|
| 60 |
+
outputs = self.model.generate(
|
| 61 |
+
**inputs,
|
| 62 |
+
max_length=512,
|
| 63 |
+
num_return_sequences=1,
|
| 64 |
+
temperature=0.7,
|
| 65 |
+
do_sample=True,
|
| 66 |
+
use_cache=True
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Decode exactly as in your test file
|
| 70 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 71 |
+
|
| 72 |
+
return [response]
|
| 73 |
+
|
| 74 |
+
except Exception as e:
|
| 75 |
+
logger.error(f"Error generating response: {str(e)}")
|
| 76 |
+
return [f"Error generating response: {str(e)}"]
|
| 77 |
|
| 78 |
def preprocess(self, request):
|
| 79 |
"""Pre-process request for API compatibility"""
|