Views
No views yet
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import re
3CHUNK_SIZE = 2048 - 2
4MAX_CHARS = 10_000
5
6tokenizer = AutoTokenizer.from_pretrained("HuggingFaceFW/finepdfs_edu_classifier_fas_Arab")
7model = AutoModelForSequenceClassification.from_pretrained("HuggingFaceFW/finepdfs_edu_classifier_fas_Arab")
8regex_whitespace = re.compile(r'\s')
9
10def create_text_chunks(text: str, tokenizer):
11 def trim_to_whitespace(text: str, trim_start: bool = True, trim_end: bool = True):
12 if trim_start:
13 match = regex_whitespace.search(text)
14 if match:
15 text = text[match.start()+1:]
16 else:
17 text = text[10:]
18 if trim_end:
19 match = regex_whitespace.search(text[::-1])
20 if match:
21 text = text[:len(text) - match.start() - 1]
22 else:
23 text = text[:-10]
24 return text
25
26 # First tokenize the text
27 # Speed hack, we take at most
28 if len(text) <= 2*MAX_CHARS:
29 tokens = tokenizer.encode(text[:MAX_CHARS], return_tensors="np", add_special_tokens=False)[0]
30 # Process the top chunks
31 chunks_from_top_sampled = [tokens[:CHUNK_SIZE]]
32
33 chunks_top_text = tokenizer.batch_decode(chunks_from_top_sampled, skip_special_tokens=True)
34
35 chunks_top_text = [trim_to_whitespace(chunks_top_text[0], trim_start=False, trim_end=True)]
36 return [chunks_top_text]
37
38 else:
39 # We tokenize the top and bottom of text
40 text_top = text[:MAX_CHARS]
41 text_bottom = text[-MAX_CHARS:]
42
43 tokens = tokenizer.batch_encode_plus([text_top, text_bottom], return_tensors="np", add_special_tokens=False)["input_ids"]
44
45 # This ensures that the second chunks is always maxed out
46 chunks = [tokens[0][:CHUNK_SIZE], tokens[1][-CHUNK_SIZE:]]
47
48 chunks_text = tokenizer.batch_decode(chunks, skip_special_tokens=True)
49 chunks_top_text = [trim_to_whitespace(chunks_text[0], trim_start=False, trim_end=True)]
50 chunks_bottom_text = [trim_to_whitespace(chunks_text[1], trim_start=True, trim_end=False)]
51 return chunks_top_text + chunks_bottom_text
52
53text = "This is a test sentence." * 2000
54chunks = create_text_chunks(text, tokenizer)
55scores = []
56for chunk in chunks:
57 inputs = tokenizer(chunk, return_tensors="pt", padding="longest", truncation=True)
58 outputs = model(**inputs)
59 logits = outputs.logits.squeeze(-1).float().detach().numpy()
60 score = logits.item()
61 scores.append(score)
62
63print(max(scores))Below is an extract from a PDF file. Evaluate whether the extract has a high educational
value and could be useful in an educational setting for teaching from primary school to
grade school levels using the additive 5-point scoring system described below. Points are
accumulated based on the satisfaction of each criterion:
- Add 1 point if the extract provides some basic information relevant to educational topics, even if it includes some irrelevant or non-academic content like advertisements and
promotional material.
- Add another point if the extract addresses certain elements pertinent to education but
does not align closely with educational standards. It might mix educational content with
non-educational material, offering a superficial overview of potentially useful topics, or
presenting information in a disorganized manner and incoherent writing style.
- Award a third point if the extract is appropriate for educational use and introduces key
concepts relevant to school curricula. It is coherent though it may not be comprehensive
or could include some extraneous information. It may resemble an introductory section of
a textbook or a basic tutorial that is suitable for learning but has notable limitations like
treating concepts that are too complex for grade school students.
- Grant a fourth point if the extract highly relevant and beneficial for educational purposes
for a level not higher than grade school, exhibiting a clear and consistent writing style. It
could be similar to a chapter from a textbook or a tutorial, offering substantial educational
content, including exercises and solutions, with minimal irrelevant information, and the
concepts aren’t too advanced for grade school students. The content is coherent, focused,
and valuable for structured learning.
- Bestow a fifth point if the extract is outstanding in its educational value, perfectly suited for
teaching either at primary school or grade school. It follows detailed reasoning, the writing
style is easy to follow and offers profound and thorough insights into the subject matter,
devoid of any non-educational or complex content.
The extract: {example}.
After examining the extract:
- Briefly justify your total score, up to 100 words.
- Conclude with the score using the format: "Educational score: <total points>"\Validation Report:
| class | precision | recall | f1-score | support |
|--------:|------------:|---------:|-----------:|----------:|
| 0 | 0.5 | 0.67 | 0.57 | 2992 |
| 1 | 0.89 | 0.83 | 0.86 | 13935 |
| 2 | 0.32 | 0.32 | 0.32 | 673 |
| 3 | 0.24 | 0.37 | 0.29 | 175 |
| 4 | 0.54 | 0.36 | 0.43 | 138 |
| 5 | 0.39 | 0.47 | 0.42 | 15 |Confusion Matrix:
| class | 0 | 1 | 2 | 3 | 4 | 5 |
|---------:|-----:|------:|----:|----:|----:|----:|
| 0 | 1995 | 988 | 9 | 0 | 0 | 0 |
| 1 | 2016 | 11499 | 373 | 43 | 4 | 0 |
| 2 | 5 | 337 | 217 | 103 | 11 | 0 |
| 3 | 0 | 30 | 59 | 64 | 21 | 1 |
| 4 | 0 | 2 | 21 | 56 | 49 | 10 |
| 5 | 0 | 0 | 1 | 1 | 6 | 7 |