import pandas as pd import torch from torch.utils.data import Dataset, DataLoader from transformers import BertTokenizer from tensorflow.keras.preprocessing.text import Tokenizer from transformers import BertForSequenceClassification import numpy as np import json with open('dict.json', 'r') as json_file: target_word_index = json.load(json_file) model = BertForSequenceClassification.from_pretrained('saved_bert_model') tokenizer = BertTokenizer.from_pretrained('saved_bert_model') model.eval() def predict_category(text): model.eval() inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128).to('cpu') with torch.no_grad(): outputs = model(**inputs).logits preds = (outputs.cpu().numpy() > 0.5).astype(int) return preds # Example text = "Wireless Bluetooth Headphones with Noise Cancelling" text = "Azonee Gaming Headphone & Controller Holder, 3-in-1 PC Headphones Hanger Adjustable & Rotating Arm Clamp, Headphone Stand Under Desk, Universal Headset Controllers Hook (Black)" text = "1 Pack Refrigerator Lock with 1 Keys, Refrigerator Lock Dorm Freezer Door Lock and Child Safety Cabinet Lock with Strong Adhesive" predicted_category = predict_category(text) indices_of_ones = np.where(predicted_category[0] == 1)[0] target_dic = {v:k for k, v in target_word_index.items()} print([target_dic[v] for v in indices_of_ones])