|
import torch |
|
from typing import Dict, List, Any |
|
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline |
|
|
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
|
|
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-128k-instruct", trust_remote_code=True) |
|
model = AutoModelForCausalLM.from_pretrained( |
|
"microsoft/Phi-3-mini-128k-instruct", |
|
trust_remote_code=True |
|
) |
|
|
|
self.pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer) |
|
|
|
def __call__(self, data: Any) -> List[List[Dict[str, float]]]: |
|
inputs = data.pop("inputs", data) |
|
|
|
for key in ['stop_sequences', 'watermark', 'stop']: |
|
if key in inputs: |
|
del inputs[key] |
|
|
|
parameters = data.pop("parameters", None) |
|
|
|
|
|
if parameters is not None: |
|
prediction = self.pipeline(inputs, **parameters) |
|
else: |
|
prediction = self.pipeline(inputs) |
|
|
|
return prediction |