File size: 2,428 Bytes
a94233d 73ff1a7 a94233d 73ff1a7 5aa95d8 73ff1a7 e732be5 73ff1a7 a94233d 73ff1a7 a94233d 73ff1a7 a94233d 73ff1a7 a94233d 73ff1a7 a94233d 73ff1a7 a94233d 73ff1a7 a94233d 73ff1a7 a94233d 73ff1a7 a94233d 73ff1a7 a94233d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import os
import sys
import json
import torch
from ts.torch_handler.base_handler import BaseHandler
from transformers import BertTokenizer
# Add the model directory to the Python path
model_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(model_dir)
from model import ImprovedBERTClass # Ensure this import matches your model file name
class UICardMappingHandler(BaseHandler):
def __init__(self):
super().__init__()
self.initialized = False
def initialize(self, context):
self.manifest = context.manifest
properties = context.system_properties
model_dir = properties.get("model_dir")
self.device = torch.device("cuda:" + str(properties.get("gpu_id")) if torch.cuda.is_available() else "cpu")
self.tokenizer = BertTokenizer.from_pretrained(model_dir)
self.model = ImprovedBERTClass()
self.model.load_state_dict(torch.load(os.path.join(model_dir, 'model.pth'), map_location=self.device))
self.model.to(self.device)
self.model.eval()
self.initialized = True
def preprocess(self, data):
text = data[0].get("data")
if text is None:
text = data[0].get("body")
inputs = self.tokenizer(text, return_tensors="pt", max_length=64, padding='max_length', truncation=True)
return inputs.to(self.device)
def inference(self, inputs):
with torch.no_grad():
outputs = self.model(**inputs)
return torch.sigmoid(outputs.logits)
def postprocess(self, inference_output):
probabilities = inference_output.cpu().numpy().flatten()
labels = ['Videos', 'Unit Conversion', 'Translation', 'Shopping Product Comparison', 'Restaurants', 'Product', 'Information', 'Images', 'Gift', 'General Comparison', 'Flights', 'Answer', 'Aircraft Seat Map']
top_k = 3 # You can adjust this value
top_k_indices = probabilities.argsort()[-top_k:][::-1]
top_k_probs = probabilities[top_k_indices]
top_k_predictions = [{"card": labels[i], "probability": float(p)} for i, p in zip(top_k_indices, top_k_probs)]
most_likely_card = "Answer" if sum(probabilities > 0.5) == 0 else labels[probabilities.argmax()]
result = {
"most_likely_card": most_likely_card,
"top_k_predictions": top_k_predictions
}
return [result]
|