Views
No views yet
1from transformers import AutoTokenizer, AutoModelForMaskedLM
2import torch
3
4# Load the tokenizer and model
5tokenizer = AutoTokenizer.from_pretrained("Behpouyan/Behpouyan-Fill-Mask")
6model = AutoModelForMaskedLM.from_pretrained("Behpouyan/Behpouyan-Fill-Mask")
7
8# List of 5 Persian sentences with a masked word (replacing a word with [MASK])
9sentences = [
10 "این کتاب بسیار <mask> است.", # The book is very <mask
11 "مشتری همیشه از <mask> شما راضی است.", # The customer is always satisfied with your <mask
12 "من به دنبال <mask> هستم.", # I am looking for <mask
13 "این پروژه نیاز به <mask> دارد.", # This project needs <mask
14 "تیم ما برای انجام کارها <mask> است." # Our team is <mask to do the tasks
15]
16
17# Function to predict masked words
18def predict_masked_word(sentence):
19 # Tokenize the input sentence
20 inputs = tokenizer(sentence, return_tensors="pt")
21
22 # Forward pass to get logits
23 with torch.no_grad():
24 outputs = model(**inputs)
25 logits = outputs.logits
26
27 # Get the position of the [MASK] token
28 mask_token_index = torch.where(inputs.input_ids == tokenizer.mask_token_id)[1].item()
29
30 # Get the predicted token
31 predicted_token_id = torch.argmax(logits[0, mask_token_index]).item()
32 predicted_word = tokenizer.decode([predicted_token_id])
33
34 return predicted_word
35
36# Test the model on the sentences
37for sentence in sentences:
38 predicted_word = predict_masked_word(sentence)
39 print(f"Sentence: {sentence}")
40 print(f"Predicted word: {predicted_word}")
41 print("-" * 50)