Create test.py
#1
by Adedayo2000 - opened
test.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Support Ticket Classifier -Inference Script
|
| 3 |
+
Author: Adedayo Adebayo
|
| 4 |
+
Date: 2026-03-14
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
import logging
|
| 9 |
+
from typing import List, Dict, Any
|
| 10 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 11 |
+
|
| 12 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
+
def load_inference_tools(model_path: str):
|
| 16 |
+
logger.info(f"Loading resources from: {model_path}")
|
| 17 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 18 |
+
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 20 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 21 |
+
model.to(device)
|
| 22 |
+
model.eval()
|
| 23 |
+
return tokenizer, model, device
|
| 24 |
+
|
| 25 |
+
def get_predictions(texts: List[str], tokenizer, model, device) -> List[Dict[str, Any]]:
|
| 26 |
+
id2label = model.config.id2label
|
| 27 |
+
|
| 28 |
+
inputs = tokenizer(
|
| 29 |
+
texts,
|
| 30 |
+
padding=True,
|
| 31 |
+
truncation=True,
|
| 32 |
+
max_length=128,
|
| 33 |
+
return_tensors="pt"
|
| 34 |
+
).to(device)
|
| 35 |
+
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
outputs = model(**inputs)
|
| 38 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 39 |
+
confidences, predictions = torch.max(probs, dim=-1)
|
| 40 |
+
|
| 41 |
+
results = []
|
| 42 |
+
for text, pred_id, conf in zip(texts, predictions, confidences):
|
| 43 |
+
results.append({
|
| 44 |
+
"text": text,
|
| 45 |
+
"category": id2label[pred_id.item()],
|
| 46 |
+
"confidence": conf.item()
|
| 47 |
+
})
|
| 48 |
+
return results
|
| 49 |
+
|
| 50 |
+
def main():
|
| 51 |
+
MODEL_PATH = "Adedayo2000/BERTweet-Support-Ticket-Classifier"
|
| 52 |
+
|
| 53 |
+
input_ticket = [
|
| 54 |
+
"My account is locked,help!.",
|
| 55 |
+
"The latest update broke the export feature on the dashboard.",
|
| 56 |
+
"The app is crashing when I try to upload a file.",
|
| 57 |
+
"How do I change the notification settings for my team?",
|
| 58 |
+
"I need to reset my password but the reset link isn't working.",
|
| 59 |
+
"The new UI is confusing and hard to navigate.",
|
| 60 |
+
"I was double charged for my subscription this month, please assist."
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
tokenizer, model, device = load_inference_tools(MODEL_PATH)
|
| 64 |
+
|
| 65 |
+
logger.info("Classifying tickets...")
|
| 66 |
+
predictions = get_predictions(input_ticket, tokenizer, model, device)
|
| 67 |
+
|
| 68 |
+
print(f"{'TICKET PREVIEW':<60} | {'CATEGORY':<15} | {'CONF'}")
|
| 69 |
+
|
| 70 |
+
for res in predictions:
|
| 71 |
+
preview = (res['text'][:40] + '...') if len(res['text']) > 40 else res['text']
|
| 72 |
+
print(f"{preview:<60} | {res['category']:<15} | {res['confidence']:.2%}")
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
main()
|