| | from typing import Dict, Any |
| | from transformers import AutoModelForCausalLM, AutoTokenizer |
| | from peft import PeftModel |
| | import torch |
| |
|
| | class EndpointHandler: |
| | def __init__(self, path="."): |
| | |
| | base_model_id = "google/gemma-2b" |
| | self.tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True) |
| | base_model = AutoModelForCausalLM.from_pretrained(base_model_id, trust_remote_code=True) |
| | self.model = PeftModel.from_pretrained(base_model, f"{path}/adapter") |
| | self.model.eval() |
| | self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| | self.model.to(self.device) |
| |
|
| | def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]: |
| | prompt = data["inputs"] if isinstance(data, dict) else data |
| | inputs = self.tokenizer(prompt, return_tensors="pt").to(self.device) |
| | with torch.no_grad(): |
| | output = self.model.generate(**inputs, max_new_tokens=256) |
| | decoded = self.tokenizer.decode(output[0], skip_special_tokens=True) |
| | return {"generated_text": decoded} |
| |
|