|
|
import os |
|
|
from typing import Dict, List, Any |
|
|
import torch |
|
|
from unsloth import FastLanguageModel |
|
|
from transformers import AutoTokenizer |
|
|
|
|
|
class EndpointHandler: |
|
|
def __init__(self, model_dir: str = ""): |
|
|
|
|
|
hf_token = os.getenv("HF_TOKEN", None) |
|
|
|
|
|
|
|
|
if model_dir == "/repository" or model_dir.strip() == "": |
|
|
model_dir = "RichardLu/Mistral7b_AE_res" |
|
|
|
|
|
|
|
|
max_seq_length = 2048 |
|
|
dtype = None |
|
|
load_in_4bit = True |
|
|
|
|
|
|
|
|
self.model, self.tokenizer = FastLanguageModel.from_pretrained( |
|
|
model_name=model_dir, |
|
|
max_seq_length=max_seq_length, |
|
|
dtype=dtype, |
|
|
load_in_4bit=load_in_4bit, |
|
|
token=hf_token, |
|
|
trust_remote_code=True |
|
|
) |
|
|
|
|
|
|
|
|
FastLanguageModel.for_inference(self.model) |
|
|
|
|
|
|
|
|
self.instructabsa_instruction = ( |
|
|
"Definition: The output will be the aspects (both implicit and explicit) which have an associated opinion that are extracted from the input text. " |
|
|
"In cases where there are no aspects the output should be noaspectterm.\n" |
|
|
"Positive example 1-\n" |
|
|
"input: With the great variety on the menu, I eat here often and never get bored.\n" |
|
|
"output: menu\n" |
|
|
"Positive example 2-\n" |
|
|
"input: Great food, good size menu, great service and an unpretensious setting.\n" |
|
|
"output: food, menu, service, setting\n" |
|
|
"Negative example 1-\n" |
|
|
"input: They did not have mayonnaise, forgot our toast, left out ingredients (ie cheese in an omelet), below hot temperatures and the bacon was so over cooked it crumbled on the plate when you touched it.\n" |
|
|
"output: toast, mayonnaise, bacon, ingredients, plate\n" |
|
|
"Negative example 2-\n" |
|
|
"input: The seats are uncomfortable if you are sitting against the wall on wooden benches.\n" |
|
|
"output: seats\n" |
|
|
"Neutral example 1-\n" |
|
|
"input: I asked for seltzer with lime, no ice.\n" |
|
|
"output: seltzer with lime\n" |
|
|
"Neutral example 2-\n" |
|
|
"input: They wouldnt even let me finish my glass of wine before offering another.\n" |
|
|
"output: glass of wine\n" |
|
|
"Now complete the following example:" |
|
|
) |
|
|
|
|
|
|
|
|
self.alpaca_prompt = ( |
|
|
"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n" |
|
|
"### Instruction:\n{}\n" |
|
|
"### Input:\n{}\n" |
|
|
"### Response:\n{}" |
|
|
) |
|
|
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
|
|
|
|
input_text = data.get("inputs", "") |
|
|
if not input_text: |
|
|
return [{"error": "No input provided."}] |
|
|
|
|
|
|
|
|
prompt = self.alpaca_prompt.format(self.instructabsa_instruction, input_text, "") |
|
|
|
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
inputs = self.tokenizer(prompt, return_tensors="pt", truncation=True).to(device) |
|
|
|
|
|
|
|
|
output_ids = self.model.generate(**inputs, max_new_tokens=128) |
|
|
output_text = self.tokenizer.decode(output_ids[0], skip_special_tokens=True) |
|
|
|
|
|
|
|
|
if "### Response:" in output_text: |
|
|
predicted_aspects = output_text.split("### Response:")[-1].strip() |
|
|
else: |
|
|
predicted_aspects = output_text.strip() |
|
|
|
|
|
|
|
|
return [{"predicted": predicted_aspects}] |
|
|
|