UI_card_mapping / inference.py
DinoLiu's picture
update
0bbfa9a
raw
history blame
No virus
2.43 kB
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]