Views
No views yet
1import torch
2from transformers import BertTokenizer
3from huggingface_hub import hf_hub_download
4
5# Step 1: Download model.py
6file_path = hf_hub_download(repo_id='Berketarak/Product-Matching-Classifier', filename='model.py')
7
8# Step 2: Add directory containing model.py to the Python path
9import sys, os
10model_dir = os.path.dirname(file_path)
11sys.path.append(model_dir)
12
13# Step 3: Import custom model class
14from model import CustomBertModel
15
16# Step 4: Load tokenizer and model
17tokenizer = BertTokenizer.from_pretrained('Berketarak/Product-Matching-Classifier')
18model = CustomBertModel.from_pretrained('Berketarak/Product-Matching-Classifier')
19
20# Send model to GPU
21device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
22model.to(device)
23
24# Define the products
25product1 = 'X brand Pegasus Sneakers'
26product2 = 'Y brand Shoes'
27
28# Tokenize the input
29inputs = tokenizer(product1, product2, padding='max_length', truncation=True, max_length=350, return_tensors='pt')
30
31# Inference
32with torch.no_grad():
33 input_ids = inputs['input_ids'].to(device)
34 attention_mask = inputs['attention_mask'].to(device)
35 token_type_ids = inputs['token_type_ids'].to(device)
36 output = model(input_ids, attention_mask, token_type_ids).item()
37
38# Interpret the output
39if output > 0.5:
40 print(f"The products are likely the SAME. Model output: {output}")
41else:
42 print(f"The products are likely DIFFERENT. Model output: {output}")