Coding-With-Bashir commited on
Commit
0d95c80
·
verified ·
1 Parent(s): 0eca2e3

Upload .\tests\test_basic.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. .//tests//test_basic.py +189 -0
.//tests//test_basic.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for BwengeAi data collection."""
2
+
3
+ import json
4
+ import tempfile
5
+ from pathlib import Path
6
+
7
+ import pytest
8
+
9
+
10
+ class TestDataProcessor:
11
+ """Tests for DataProcessor."""
12
+
13
+ def test_clean_text(self):
14
+ from src.data_collection.data_processor import DataProcessor
15
+
16
+ processor = DataProcessor(
17
+ raw_dir=tempfile.mkdtemp(),
18
+ processed_dir=tempfile.mkdtemp(),
19
+ )
20
+
21
+ text = " This is a test with extra spaces "
22
+ cleaned = processor.clean_text(text)
23
+ assert cleaned == "This is a test with extra spaces"
24
+
25
+ html_text = "<p>Hello <b>world</b></p>"
26
+ cleaned = processor.clean_text(html_text)
27
+ assert "<p>" not in cleaned
28
+ assert "<b>" not in cleaned
29
+ assert "Hello world" == cleaned
30
+
31
+ short_text = "Hi"
32
+ cleaned = processor.clean_text(short_text)
33
+ assert cleaned == ""
34
+
35
+ min_text = "ab"
36
+ cleaned = processor.clean_text(min_text)
37
+ assert cleaned == ""
38
+
39
+ valid_text = "This is valid text"
40
+ cleaned = processor.clean_text(valid_text)
41
+ assert cleaned == "This is valid text"
42
+
43
+ def test_deduplicate(self):
44
+ from src.data_collection.data_processor import DataProcessor
45
+
46
+ processor = DataProcessor(
47
+ raw_dir=tempfile.mkdtemp(),
48
+ processed_dir=tempfile.mkdtemp(),
49
+ )
50
+
51
+ texts = ["Hello world", "Hello world", "Another text", "hello world"]
52
+ unique = processor.deduplicate(texts)
53
+
54
+ assert len(unique) == 2
55
+ assert "Hello world" in unique
56
+ assert "Another text" in unique
57
+
58
+ def test_create_training_format(self):
59
+ from src.data_collection.data_processor import DataProcessor
60
+
61
+ processor = DataProcessor(
62
+ raw_dir=tempfile.mkdtemp(),
63
+ processed_dir=tempfile.mkdtemp(),
64
+ )
65
+
66
+ texts = ["Short", "This is a longer text that should be included"]
67
+ training_data = processor.create_training_format(texts)
68
+
69
+ assert len(training_data) == 1
70
+ assert training_data[0]["text"] == "This is a longer text that should be included"
71
+
72
+ def test_create_instruction_format(self):
73
+ from src.data_collection.data_processor import DataProcessor
74
+
75
+ processor = DataProcessor(
76
+ raw_dir=tempfile.mkdtemp(),
77
+ processed_dir=tempfile.mkdtemp(),
78
+ )
79
+
80
+ data = [
81
+ {"instruction": "What is Kinyarwanda?", "output": "Kinyarwanda is a language spoken in Rwanda."},
82
+ {"question": "What is the capital of Rwanda?", "answer": "Kigali"},
83
+ ]
84
+
85
+ formatted = processor.create_instruction_format(data)
86
+
87
+ assert len(formatted) == 2
88
+ assert formatted[0]["instruction"] == "What is Kinyarwanda?"
89
+ assert formatted[0]["output"] == "Kinyarwanda is a language spoken in Rwanda."
90
+
91
+ def test_load_jsonl(self):
92
+ from src.data_collection.data_processor import DataProcessor
93
+
94
+ with tempfile.NamedTemporaryFile(mode="w", suffix=".jsonl", delete=False) as f:
95
+ f.write('{"text": "hello"}\n')
96
+ f.write('{"text": "world"}\n')
97
+ temp_path = f.name
98
+
99
+ processor = DataProcessor(
100
+ raw_dir=tempfile.mkdtemp(),
101
+ processed_dir=tempfile.mkdtemp(),
102
+ )
103
+
104
+ data = processor.load_jsonl(Path(temp_path))
105
+
106
+ assert len(data) == 2
107
+ assert data[0]["text"] == "hello"
108
+ assert data[1]["text"] == "world"
109
+
110
+ Path(temp_path).unlink()
111
+
112
+ def test_is_likely_kinyarwanda(self):
113
+ from src.data_collection.data_processor import is_likely_kinyarwanda
114
+
115
+ assert is_likely_kinyarwanda("U Rwanda ni igihugu cyiza cyane mu Afrika") is True
116
+ assert is_likely_kinyarwanda("Hello world this is English text") is True
117
+ assert is_likely_kinyarwanda("") is True
118
+ assert is_likely_kinyarwanda("Short") is True
119
+
120
+ def test_count_tokens(self):
121
+ from src.data_collection.data_processor import count_tokens
122
+
123
+ assert count_tokens("hello world") == 2
124
+ assert count_tokens("") == 0
125
+ assert count_tokens("one two three four five") == 5
126
+
127
+ def test_create_chat_format(self):
128
+ from src.data_collection.data_processor import DataProcessor
129
+
130
+ processor = DataProcessor(
131
+ raw_dir=tempfile.mkdtemp(),
132
+ processed_dir=tempfile.mkdtemp(),
133
+ )
134
+
135
+ data = [
136
+ {"input": "Muraho", "output": "Amakuru"},
137
+ ]
138
+
139
+ formatted = processor.create_chat_format(data)
140
+
141
+ assert len(formatted) == 1
142
+ assert formatted[0]["messages"][0]["role"] == "user"
143
+ assert formatted[0]["messages"][0]["content"] == "Muraho"
144
+
145
+
146
+ class TestBwengeEvaluator:
147
+ """Tests for BwengeEvaluator."""
148
+
149
+ def test_compute_rouge(self):
150
+ from src.evaluation.metrics import BwengeEvaluator
151
+
152
+ evaluator = BwengeEvaluator({})
153
+
154
+ predictions = ["The cat sat on the mat"]
155
+ references = ["The cat is on the mat"]
156
+
157
+ scores = evaluator.compute_rouge(predictions, references)
158
+
159
+ assert "rouge1_fmeasure" in scores
160
+ assert "rouge2_fmeasure" in scores
161
+ assert "rougeL_fmeasure" in scores
162
+ assert 0 <= scores["rouge1_fmeasure"] <= 1
163
+
164
+ def test_compute_bleu(self):
165
+ from src.evaluation.metrics import BwengeEvaluator
166
+
167
+ evaluator = BwengeEvaluator({})
168
+
169
+ predictions = ["The cat sat on the mat"]
170
+ references = ["The cat sat on the mat"]
171
+
172
+ scores = evaluator.compute_bleu(predictions, references)
173
+
174
+ assert "bleu_1" in scores
175
+ assert "bleu_4" in scores
176
+ assert scores["bleu_1"] > 0.5
177
+
178
+ def test_compute_accuracy(self):
179
+ from src.evaluation.metrics import BwengeEvaluator
180
+
181
+ evaluator = BwengeEvaluator({})
182
+
183
+ predictions = ["hello", "world", "test"]
184
+ references = ["hello", "wrong", "test"]
185
+
186
+ scores = evaluator.compute_accuracy(predictions, references)
187
+
188
+ assert scores["accuracy"] == 2 / 3
189
+ assert scores["exact_matches"] == 2