Update handler.py
Browse files- handler.py +42 -42
handler.py
CHANGED
|
@@ -6,17 +6,15 @@ from unsloth import FastLanguageModel
|
|
| 6 |
class EndpointHandler:
|
| 7 |
def __init__(self, model_dir: str = ""):
|
| 8 |
print(f"[DEBUG] Original model_dir: {model_dir}")
|
| 9 |
-
#
|
| 10 |
-
# (Note: In an endpoint, you may want to allow model_dir to be passed in,
|
| 11 |
-
# but here we override it for clarity.)
|
| 12 |
model_dir = "RichardLu/mistral7b_aspectsentiment_res"
|
| 13 |
print(f"[DEBUG] Using model_dir: {model_dir}")
|
| 14 |
|
| 15 |
# Inference configuration.
|
| 16 |
max_seq_length = 2048
|
| 17 |
-
load_in_4bit = True #
|
| 18 |
|
| 19 |
-
# Load the model and tokenizer
|
| 20 |
self.model, self.tokenizer = FastLanguageModel.from_pretrained(
|
| 21 |
model_name=model_dir,
|
| 22 |
max_seq_length=max_seq_length,
|
|
@@ -24,54 +22,56 @@ class EndpointHandler:
|
|
| 24 |
)
|
| 25 |
print("[DEBUG] Model and tokenizer loaded successfully.")
|
| 26 |
|
| 27 |
-
#
|
| 28 |
FastLanguageModel.for_inference(self.model)
|
| 29 |
|
| 30 |
# Define the instructabsa instruction text with examples.
|
| 31 |
self.instructabsa_instruction = """Definition: The output will be 'positive' if the aspect identified in the sentence contains a positive sentiment. If the sentiment of the identified aspect in the input is negative the answer will be 'negative'.
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
# Define the Alpaca-style prompt template.
|
| 55 |
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.
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
# Build the
|
| 71 |
-
full_input = f"{review_text} The aspect is {aspect}."
|
| 72 |
prompt = self.alpaca_prompt.format(self.instructabsa_instruction, full_input, "")
|
| 73 |
|
| 74 |
-
# Set device
|
| 75 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 76 |
inputs = self.tokenizer(prompt, return_tensors="pt", truncation=True).to(device)
|
| 77 |
|
|
|
|
| 6 |
class EndpointHandler:
|
| 7 |
def __init__(self, model_dir: str = ""):
|
| 8 |
print(f"[DEBUG] Original model_dir: {model_dir}")
|
| 9 |
+
# Override model_dir with your checkpoint path.
|
|
|
|
|
|
|
| 10 |
model_dir = "RichardLu/mistral7b_aspectsentiment_res"
|
| 11 |
print(f"[DEBUG] Using model_dir: {model_dir}")
|
| 12 |
|
| 13 |
# Inference configuration.
|
| 14 |
max_seq_length = 2048
|
| 15 |
+
load_in_4bit = True # or False as needed.
|
| 16 |
|
| 17 |
+
# Load the model and tokenizer.
|
| 18 |
self.model, self.tokenizer = FastLanguageModel.from_pretrained(
|
| 19 |
model_name=model_dir,
|
| 20 |
max_seq_length=max_seq_length,
|
|
|
|
| 22 |
)
|
| 23 |
print("[DEBUG] Model and tokenizer loaded successfully.")
|
| 24 |
|
| 25 |
+
# Set the model to inference mode.
|
| 26 |
FastLanguageModel.for_inference(self.model)
|
| 27 |
|
| 28 |
# Define the instructabsa instruction text with examples.
|
| 29 |
self.instructabsa_instruction = """Definition: The output will be 'positive' if the aspect identified in the sentence contains a positive sentiment. If the sentiment of the identified aspect in the input is negative the answer will be 'negative'.
|
| 30 |
+
Otherwise, the output should be 'neutral'. For aspects which are classified as noaspectterm, the sentiment is none.
|
| 31 |
+
Positive example 1-
|
| 32 |
+
input: With the great variety on the menu , I eat here often and never get bored. The aspect is menu.
|
| 33 |
+
output: positive
|
| 34 |
+
Positive example 2-
|
| 35 |
+
input: Great food, good size menu, great service and an unpretensious setting. The aspect is food.
|
| 36 |
+
output: positive
|
| 37 |
+
Negative example 1-
|
| 38 |
+
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. The aspect is toast.
|
| 39 |
+
output: negative
|
| 40 |
+
Negative example 2-
|
| 41 |
+
input: The seats are uncomfortable if you are sitting against the wall on wooden benches. The aspect is seats.
|
| 42 |
+
output: negative
|
| 43 |
+
Neutral example 1-
|
| 44 |
+
input: I asked for seltzer with lime, no ice. The aspect is seltzer with lime.
|
| 45 |
+
output: neutral
|
| 46 |
+
Neutral example 2-
|
| 47 |
+
input: They wouldnt even let me finish my glass of wine before offering another. The aspect is glass of wine.
|
| 48 |
+
output: neutral
|
| 49 |
+
Now complete the following example-
|
| 50 |
+
"""
|
| 51 |
|
| 52 |
# Define the Alpaca-style prompt template.
|
| 53 |
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.
|
| 54 |
+
### Instruction:
|
| 55 |
+
{}
|
| 56 |
+
### Input:
|
| 57 |
+
{}
|
| 58 |
+
### Response:
|
| 59 |
+
{}"""
|
| 60 |
+
|
| 61 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 62 |
+
# First, try to use the "inputs" key.
|
| 63 |
+
if "inputs" in data:
|
| 64 |
+
full_input = data["inputs"]
|
| 65 |
+
# Otherwise, check for "review" and "aspect" keys.
|
| 66 |
+
elif "review" in data and "aspect" in data:
|
| 67 |
+
full_input = f"{data['review']} The aspect is {data['aspect']}."
|
| 68 |
+
else:
|
| 69 |
+
return [{"error": "Provide either an 'inputs' key or both 'review' and 'aspect' keys."}]
|
| 70 |
|
| 71 |
+
# Build the final prompt.
|
|
|
|
| 72 |
prompt = self.alpaca_prompt.format(self.instructabsa_instruction, full_input, "")
|
| 73 |
|
| 74 |
+
# Set device.
|
| 75 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 76 |
inputs = self.tokenizer(prompt, return_tensors="pt", truncation=True).to(device)
|
| 77 |
|