|
|
|
from typing import Dict, Any |
|
import torch |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
from accelerate import init_empty_weights, load_checkpoint_and_dispatch |
|
|
|
|
|
class EndpointHandler: |
|
""" |
|
Hugging Face Inference Endpoints 约定的自定义入口: |
|
• __init__(model_dir, **kwargs) —— 加载模型 |
|
• __call__(inputs: Dict) -> Dict —— 处理一次请求 |
|
""" |
|
|
|
def __init__(self, model_dir: str, **kwargs): |
|
|
|
self.tokenizer = AutoTokenizer.from_pretrained( |
|
model_dir, trust_remote_code=True |
|
) |
|
|
|
|
|
with init_empty_weights(): |
|
base_model = AutoModelForCausalLM.from_pretrained( |
|
model_dir, |
|
torch_dtype=torch.float16, |
|
trust_remote_code=True, |
|
) |
|
|
|
|
|
self.model = load_checkpoint_and_dispatch( |
|
base_model, |
|
checkpoint=model_dir, |
|
device_map="auto", |
|
dtype=torch.float16, |
|
) |
|
|
|
|
|
self.generation_kwargs = dict( |
|
max_new_tokens=2048, |
|
do_sample=True, |
|
temperature=0.7, |
|
top_p=0.9, |
|
) |
|
|
|
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]: |
|
prompt = data["inputs"] |
|
|
|
|
|
first_device = next(self.model.parameters()).device |
|
inputs = self.tokenizer(prompt, return_tensors="pt").to(first_device) |
|
|
|
|
|
with torch.inference_mode(): |
|
output_ids = self.model.generate( |
|
**inputs, |
|
**self.generation_kwargs, |
|
) |
|
|
|
return { |
|
"generated_text": self.tokenizer.decode( |
|
output_ids[0], skip_special_tokens=True |
|
) |
|
} |