Views
No views yet
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4model_name="agentlans/deberta-v3-xsmall-readability"
5
6# Put model on GPU or else CPU
7tokenizer = AutoTokenizer.from_pretrained(model_name)
8model = AutoModelForSequenceClassification.from_pretrained(model_name)
9device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10model = model.to(device)
11
12def readability(text):
13 """Processes the text using the model and returns its logits.
14 In this case, it's reading grade level in years of education
15 (the higher the number, the harder it is to read the text)."""
16 inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(device)
17 with torch.no_grad():
18 logits = model(**inputs).logits.squeeze().cpu()
19 return logits.tolist()
20
21# Example usage
22text = ["One day, Tim's teddy bear was sad. Tim did not know why his teddy bear was sad.",
23 "A few years back, I decided it was time for me to take a break from my mundane routine and embark on an adventure.",
24 "We also experimentally verify that simply scaling the pulse energy by 3/2 between linearly and circularly polarized pumping closely reproduces the soliton and dispersive wave dynamics."]
25result = readability(text)
26[round(x, 1) for x in result] # Estimated reading grades [2.9, 9.8, 21.9]