|
|
from typing import Dict, Any |
|
|
import torch |
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
|
|
|
|
|
|
class EndpointHandler: |
|
|
def __init__(self, path: str = ""): |
|
|
""" |
|
|
Initialize the model and tokenizer. |
|
|
|
|
|
Args: |
|
|
path: Path to the model directory (will be "/repository" in endpoint container) |
|
|
""" |
|
|
self.device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
|
|
print(f"Loading tokenizer from {path}...") |
|
|
self.tokenizer = AutoTokenizer.from_pretrained(path) |
|
|
|
|
|
|
|
|
if self.tokenizer.pad_token is None: |
|
|
self.tokenizer.pad_token = self.tokenizer.eos_token |
|
|
|
|
|
print(f"Loading model from {path} on device: {self.device}...") |
|
|
self.model = AutoModelForCausalLM.from_pretrained( |
|
|
path, |
|
|
torch_dtype=torch.float16, |
|
|
trust_remote_code=True, |
|
|
device_map="auto", |
|
|
) |
|
|
|
|
|
self.model.eval() |
|
|
print("✅ Model loaded successfully!") |
|
|
|
|
|
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]: |
|
|
""" |
|
|
Process inference requests. |
|
|
|
|
|
Args: |
|
|
data: Dictionary containing: |
|
|
- inputs: str (code prompt to complete) |
|
|
- parameters: dict (optional, generation parameters) |
|
|
|
|
|
Returns: |
|
|
Dictionary with generated_text key |
|
|
""" |
|
|
|
|
|
inputs = data.get("inputs", "") |
|
|
parameters = data.get("parameters", {}) or {} |
|
|
|
|
|
if not isinstance(inputs, str): |
|
|
raise ValueError("`inputs` must be a string") |
|
|
|
|
|
if not inputs.strip(): |
|
|
raise ValueError("`inputs` cannot be empty") |
|
|
|
|
|
|
|
|
gen_kwargs = { |
|
|
"max_new_tokens": parameters.get("max_new_tokens", 128), |
|
|
"temperature": parameters.get("temperature", 0.2), |
|
|
"top_p": parameters.get("top_p", 0.95), |
|
|
"top_k": parameters.get("top_k", 50), |
|
|
"do_sample": parameters.get("do_sample", True), |
|
|
"repetition_penalty": parameters.get("repetition_penalty", 1.0), |
|
|
} |
|
|
|
|
|
print(f"Generating with parameters: {gen_kwargs}") |
|
|
|
|
|
|
|
|
enc = self.tokenizer(inputs, return_tensors="pt",padding=True, |
|
|
truncation=True, |
|
|
max_length=2048).to(self.device) |
|
|
|
|
|
|
|
|
with torch.no_grad(): |
|
|
out = self.model.generate(**enc, |
|
|
**gen_kwargs, |
|
|
pad_token_id=self.tokenizer.pad_token_id) |
|
|
|
|
|
|
|
|
generated_text = self.tokenizer.decode(out[0], skip_special_tokens=True) |
|
|
|
|
|
return { |
|
|
"generated_text": generated_text |
|
|
} |
|
|
|