File size: 2,540 Bytes
5404ee8 | 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 64 65 66 67 68 69 70 71 72 73 74 75 | """
Support Ticket Classifier -Inference Script
Author: Adedayo Adebayo
Date: 2026-03-14
"""
import torch
import logging
from typing import List, Dict, Any
from transformers import AutoTokenizer, AutoModelForSequenceClassification
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def load_inference_tools(model_path: str):
logger.info(f"Loading resources from: {model_path}")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)
model.to(device)
model.eval()
return tokenizer, model, device
def get_predictions(texts: List[str], tokenizer, model, device) -> List[Dict[str, Any]]:
id2label = model.config.id2label
inputs = tokenizer(
texts,
padding=True,
truncation=True,
max_length=128,
return_tensors="pt"
).to(device)
with torch.no_grad():
outputs = model(**inputs)
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
confidences, predictions = torch.max(probs, dim=-1)
results = []
for text, pred_id, conf in zip(texts, predictions, confidences):
results.append({
"text": text,
"category": id2label[pred_id.item()],
"confidence": conf.item()
})
return results
def main():
MODEL_PATH = "Adedayo2000/BERTweet-Support-Ticket-Classifier"
input_ticket = [
"My account is locked,help!.",
"The latest update broke the export feature on the dashboard.",
"The app is crashing when I try to upload a file.",
"How do I change the notification settings for my team?",
"I need to reset my password but the reset link isn't working.",
"The new UI is confusing and hard to navigate.",
"I was double charged for my subscription this month, please assist."
]
tokenizer, model, device = load_inference_tools(MODEL_PATH)
logger.info("Classifying tickets...")
predictions = get_predictions(input_ticket, tokenizer, model, device)
print(f"{'TICKET PREVIEW':<60} | {'CATEGORY':<15} | {'CONF'}")
for res in predictions:
preview = (res['text'][:40] + '...') if len(res['text']) > 40 else res['text']
print(f"{preview:<60} | {res['category']:<15} | {res['confidence']:.2%}")
if __name__ == "__main__":
main() |