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_v2_eng_Latn")
7model = AutoModelForSequenceClassification.from_pretrained("HHuggingFaceFW/finepdfs_edu_classifier_v2_eng_Latn")
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 exhibits properties suitable for educational training data using the 6-point scoring system described below. Select the single score that best represents the extract's educational quality level:
**Score 0: No Educational Value**
- Award 0 points for content with zero educational merit including spam, promotional material, garbled text, random sequences, severely corrupted formatting, or content that provides no learning opportunities whatsoever.
**Score 1: Minimal Educational Content**
- Award 1 point for content with very limited educational value such as basic data listings, simple contact information, minimal factual statements without context, brief announcements, or content that presents isolated facts without meaningful educational framework.
**Score 2: Basic Informational Content**
- Award 2 points for content that provides basic information but lacks depth, context, or clear educational structure. This includes simple news items, basic product descriptions, brief summaries, casual observations, or informational content that states facts without explanation or educational development.
**Score 3: Moderate Educational Value**
- Award 3 points for content that offers solid educational information with some context and explanation. This includes informative articles with background information, basic explanatory content, introductory-level material, general knowledge content, or well-written informational pieces that provide context and some depth.
**Score 4: Strong Educational Content**
- Award 4 points for content with clear educational merit featuring detailed explanations, multiple perspectives, analytical depth, or comprehensive coverage of topics. This includes academic articles, detailed tutorials, in-depth analyses, research-based content, or material that demonstrates critical thinking and provides substantial learning value.
**Score 5: Exceptional Educational Value**
- Award 5 points for content with outstanding educational merit that demonstrates expert-level knowledge, sophisticated analysis, comprehensive understanding, and significant pedagogical value. This includes advanced academic research, expert commentary with deep insights, comprehensive educational material with multiple learning dimensions, or content that advances understanding through original thinking and thorough exploration.
## Evaluation Process
The extract: {example}
After examining the extract:
- Briefly justify your total score, focusing on the educational depth, context provided, and learning potential, up to 100 words.
- Conclude with the score using the format: "Educational value score: <total points>"\Validation Report:
| class | precision | recall | f1-score | support |
|--------:|------------:|---------:|-----------:|----------:|
| 0 | 0.5 | 0.88 | 0.64 | 1122 |
| 1 | 0.89 | 0.61 | 0.72 | 7887 |
| 2 | 0.46 | 0.69 | 0.55 | 3412 |
| 3 | 0.62 | 0.58 | 0.6 | 3568 |
| 4 | 0.66 | 0.6 | 0.63 | 3134 |
| 5 | 0.47 | 0.7 | 0.56 | 877 |Confusion Matrix:
| class | 0 | 1 | 2 | 3 | 4 | 5 |
|---------:|----:|-----:|-----:|-----:|-----:|----:|
| 0 | 983 | 133 | 5 | 1 | 0 | 0 |
| 1 | 972 | 4823 | 1952 | 128 | 11 | 1 |
| 2 | 8 | 437 | 2351 | 581 | 34 | 1 |
| 3 | 1 | 34 | 734 | 2072 | 697 | 30 |
| 4 | 2 | 2 | 63 | 534 | 1873 | 660 |
| 5 | 0 | 0 | 2 | 18 | 243 | 614 |