-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_finepdfs_tutorial.py
More file actions
404 lines (308 loc) · 12.6 KB
/
test_finepdfs_tutorial.py
File metadata and controls
404 lines (308 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python3
"""
FinePDFs Tutorial Test Script
Validates all examples and functionality from the tutorial
"""
import os
import sys
import time
import traceback
import subprocess
from pathlib import Path
def check_requirements():
"""Check if all required packages are installed"""
required_packages = [
'datasets',
'transformers',
'torch',
'huggingface_hub',
'pandas',
'numpy',
'matplotlib',
'seaborn',
'tqdm',
'rich'
]
missing_packages = []
for package in required_packages:
try:
__import__(package)
print(f"✓ {package} is installed")
except ImportError:
missing_packages.append(package)
print(f"✗ {package} is missing")
if missing_packages:
print(f"\nMissing packages: {missing_packages}")
print("Install them with: pip install " + " ".join(missing_packages))
return False
return True
def test_basic_dataset_loading():
"""Test basic FinePDFs dataset loading"""
print("\n=== Testing Basic Dataset Loading ===")
try:
from datasets import load_dataset
print("Loading FinePDFs English subset...")
dataset = load_dataset("HuggingFaceFW/finepdfs", "eng_Latn", split="train",
streaming=True)
# Test streaming access
print("Testing streaming access...")
count = 0
for item in dataset:
if 'text' not in item or 'language' not in item:
raise ValueError("Required fields missing")
text_length = len(item['text'])
print(f"Sample {count+1}: {text_length} characters")
count += 1
if count >= 3: # Test first 3 items
break
print("✓ Basic dataset loading successful")
return True
except Exception as e:
print(f"✗ Basic dataset loading failed: {e}")
return False
def test_text_analysis():
"""Test text analysis functionality"""
print("\n=== Testing Text Analysis ===")
try:
from datasets import load_dataset
import re
# Load small sample
dataset = load_dataset("HuggingFaceFW/finepdfs", "eng_Latn",
split="train[:10]") # Small sample
print("Analyzing text quality...")
total_length = 0
total_words = 0
total_sentences = 0
for item in dataset:
text = item['text']
text_length = len(text)
word_count = len(text.split())
sentence_count = len(re.split(r'[.!?]+', text))
total_length += text_length
total_words += word_count
total_sentences += sentence_count
avg_length = total_length / len(dataset)
avg_words = total_words / len(dataset)
avg_sentences = total_sentences / len(dataset)
print(f"Average text length: {avg_length:.0f} characters")
print(f"Average word count: {avg_words:.0f} words")
print(f"Average sentence count: {avg_sentences:.0f} sentences")
print("✓ Text analysis successful")
return True
except Exception as e:
print(f"✗ Text analysis failed: {e}")
return False
def test_domain_filtering():
"""Test domain-specific filtering"""
print("\n=== Testing Domain Filtering ===")
try:
from datasets import load_dataset
# Load small sample
dataset = load_dataset("HuggingFaceFW/finepdfs", "eng_Latn",
split="train[:50]") # Small sample for testing
def filter_by_domain(dataset, domain_keywords, min_matches=1):
def contains_domain_keywords(example):
text_lower = example['text'].lower()
matches = sum(1 for keyword in domain_keywords
if keyword in text_lower)
return matches >= min_matches
return dataset.filter(contains_domain_keywords)
# Test scientific content filtering
scientific_keywords = ['research', 'study', 'analysis', 'experiment']
scientific_subset = filter_by_domain(dataset, scientific_keywords)
print(f"Original dataset: {len(dataset)} documents")
print(f"Scientific subset: {len(scientific_subset)} documents")
# Test legal content filtering
legal_keywords = ['court', 'law', 'legal', 'contract']
legal_subset = filter_by_domain(dataset, legal_keywords)
print(f"Legal subset: {len(legal_subset)} documents")
print("✓ Domain filtering successful")
return True
except Exception as e:
print(f"✗ Domain filtering failed: {e}")
return False
def test_memory_efficient_processing():
"""Test memory-efficient processing techniques"""
print("\n=== Testing Memory Efficient Processing ===")
try:
from datasets import load_dataset
import gc
def process_in_chunks(dataset, chunk_size=10):
total_processed = 0
total_chars = 0
# Convert streaming to iterable
items = []
for i, item in enumerate(dataset):
items.append(item)
if i >= 30: # Limit for testing
break
for i in range(0, len(items), chunk_size):
chunk = items[i:i+chunk_size]
chunk_chars = sum(len(item['text']) for item in chunk)
total_chars += chunk_chars
total_processed += len(chunk)
print(f"Processed chunk {i//chunk_size + 1}: "
f"{len(chunk)} documents, {chunk_chars} characters")
# Force garbage collection
gc.collect()
return total_processed, total_chars
# Test streaming dataset
dataset = load_dataset("HuggingFaceFW/finepdfs", "eng_Latn",
streaming=True, split="train")
total_docs, total_chars = process_in_chunks(dataset)
print(f"Total processed: {total_docs} documents, {total_chars} characters")
print("✓ Memory efficient processing successful")
return True
except Exception as e:
print(f"✗ Memory efficient processing failed: {e}")
return False
def test_quality_assessment():
"""Test document quality assessment"""
print("\n=== Testing Quality Assessment ===")
try:
from datasets import load_dataset
import re
def assess_document_quality(text):
char_count = len(text)
word_count = len(text.split())
quality_score = 0
issues = []
# Length checks
if word_count < 50:
issues.append("Too short")
quality_score -= 1
elif word_count > 100:
quality_score += 1
# Diversity check
unique_words = len(set(text.lower().split()))
diversity_ratio = unique_words / max(word_count, 1)
if diversity_ratio > 0.7:
quality_score += 1
elif diversity_ratio < 0.3:
issues.append("Low vocabulary diversity")
quality_score -= 1
return {
'quality_score': quality_score,
'issues': issues,
'word_count': word_count,
'diversity_ratio': diversity_ratio
}
# Load small sample
dataset = load_dataset("HuggingFaceFW/finepdfs", "eng_Latn",
split="train[:20]")
quality_results = []
for item in dataset:
quality = assess_document_quality(item['text'])
quality_results.append(quality)
avg_quality = sum(r['quality_score'] for r in quality_results) / len(quality_results)
high_quality = sum(1 for r in quality_results if r['quality_score'] >= 1)
print(f"Average quality score: {avg_quality:.2f}")
print(f"High quality documents: {high_quality}/{len(quality_results)}")
print("✓ Quality assessment successful")
return True
except Exception as e:
print(f"✗ Quality assessment failed: {e}")
return False
def test_multilingual_support():
"""Test multilingual dataset support"""
print("\n=== Testing Multilingual Support ===")
try:
from datasets import load_dataset
# Test multiple language configurations
language_configs = ["eng_Latn", "arb_Arab"] # Limited for testing
for lang_config in language_configs:
try:
print(f"Testing {lang_config}...")
dataset = load_dataset("HuggingFaceFW/finepdfs", lang_config,
streaming=True, split="train")
# Test first item
first_item = next(iter(dataset))
text_length = len(first_item['text'])
print(f" {lang_config}: {text_length} characters in first document")
except Exception as e:
print(f" {lang_config}: Failed to load - {e}")
print("✓ Multilingual support test completed")
return True
except Exception as e:
print(f"✗ Multilingual support test failed: {e}")
return False
def run_performance_benchmark():
"""Run basic performance benchmark"""
print("\n=== Running Performance Benchmark ===")
try:
from datasets import load_dataset
import time
start_time = time.time()
# Load streaming dataset
dataset = load_dataset("HuggingFaceFW/finepdfs", "eng_Latn",
streaming=True, split="train")
# Process first 100 items
count = 0
total_chars = 0
for item in dataset:
total_chars += len(item['text'])
count += 1
if count >= 100:
break
end_time = time.time()
processing_time = end_time - start_time
print(f"Processed {count} documents in {processing_time:.2f} seconds")
print(f"Average processing speed: {count/processing_time:.1f} docs/second")
print(f"Total characters processed: {total_chars:,}")
print("✓ Performance benchmark completed")
return True
except Exception as e:
print(f"✗ Performance benchmark failed: {e}")
return False
def main():
"""Run all tests"""
print("FinePDFs Tutorial Test Suite")
print("=" * 50)
# Check Python version
if sys.version_info < (3, 8):
print("Error: Python 3.8+ required")
return False
print(f"Python version: {sys.version}")
# Check requirements
if not check_requirements():
return False
# Run tests
tests = [
test_basic_dataset_loading,
test_text_analysis,
test_domain_filtering,
test_memory_efficient_processing,
test_quality_assessment,
test_multilingual_support,
run_performance_benchmark
]
results = []
start_time = time.time()
for test in tests:
try:
result = test()
results.append(result)
except Exception as e:
print(f"\nTest {test.__name__} crashed: {e}")
traceback.print_exc()
results.append(False)
# Summary
end_time = time.time()
total_time = end_time - start_time
print("\n" + "=" * 50)
print("Test Summary")
print("=" * 50)
passed = sum(results)
total = len(results)
print(f"Tests passed: {passed}/{total}")
print(f"Success rate: {passed/total*100:.1f}%")
print(f"Total execution time: {total_time:.1f} seconds")
if passed == total:
print("\n🎉 All tests passed! The tutorial examples are working correctly.")
return True
else:
print("\n⚠️ Some tests failed. Check the output above for details.")
return False
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)