SkyWorlds01 commited on
Commit
6845ac8
·
verified ·
1 Parent(s): 8aa3d92

Create Shelly/automated_testing.py

Browse files
Files changed (1) hide show
  1. Shelly/Shelly/automated_testing.py +250 -0
Shelly/Shelly/automated_testing.py ADDED
@@ -0,0 +1,250 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Shelly AI - Automated Testing
3
+
4
+ Provides safe internal tests and score tracking.
5
+
6
+ Tests are local logic exercises. This module does not scan,
7
+ probe, or attack external systems.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import random
13
+ import threading
14
+ import time
15
+
16
+ from typing import Any, Dict, List, Optional
17
+
18
+
19
+ class AutomatedTesting:
20
+ def __init__(
21
+ self,
22
+ random_seed: Optional[int] = None,
23
+ ):
24
+ self._lock = threading.RLock()
25
+ self.random = random.Random(random_seed)
26
+
27
+ self.categories = {
28
+ "Reasoning": [],
29
+ "Knowledge": [],
30
+ "Language": [],
31
+ "Vision": [],
32
+ "Planning": [],
33
+ "Tool Use": [],
34
+ "Learning": [],
35
+ "Creativity": [],
36
+ }
37
+
38
+ self.results: List[Dict[str, Any]] = []
39
+
40
+ self.statistics = {
41
+ "attempts": 0,
42
+ "correct": 0,
43
+ "incorrect": 0,
44
+ "current_streak": 0,
45
+ "best_streak": 0,
46
+ }
47
+
48
+ self.questions = [
49
+ {
50
+ "id": "reasoning_001",
51
+ "category": "Reasoning",
52
+ "question": "What number comes next: 2, 4, 8, 16?",
53
+ "answer": "32",
54
+ "explanation": "Each number is multiplied by two.",
55
+ },
56
+ {
57
+ "id": "reasoning_002",
58
+ "category": "Reasoning",
59
+ "question": "If all A are B and all B are C, are all A also C?",
60
+ "answer": "yes",
61
+ "explanation": "This follows from transitive inclusion.",
62
+ },
63
+ {
64
+ "id": "planning_001",
65
+ "category": "Planning",
66
+ "question": "A task has three steps. Step B requires A, and C requires B. Which step comes first?",
67
+ "answer": "a",
68
+ "explanation": "A must be completed before B and C.",
69
+ },
70
+ {
71
+ "id": "knowledge_001",
72
+ "category": "Knowledge",
73
+ "question": "What is the chemical symbol for water?",
74
+ "answer": "h2o",
75
+ "explanation": "Water is commonly represented by H2O.",
76
+ },
77
+ {
78
+ "id": "language_001",
79
+ "category": "Language",
80
+ "question": "What is the plural of 'child'?",
81
+ "answer": "children",
82
+ "explanation": "Children is the irregular plural form.",
83
+ },
84
+ {
85
+ "id": "creativity_001",
86
+ "category": "Creativity",
87
+ "question": "Name one useful purpose for an empty cardboard box.",
88
+ "answer": None,
89
+ "explanation": "Open-ended answers require human or model evaluation.",
90
+ },
91
+ ]
92
+
93
+ def get_question(
94
+ self,
95
+ category: Optional[str] = None,
96
+ ) -> Optional[Dict[str, Any]]:
97
+ candidates = self.questions
98
+
99
+ if category is not None:
100
+ candidates = [
101
+ question
102
+ for question in candidates
103
+ if question["category"].lower()
104
+ == str(category).lower()
105
+ ]
106
+
107
+ if not candidates:
108
+ return None
109
+
110
+ question = self.random.choice(candidates)
111
+
112
+ return {
113
+ "id": question["id"],
114
+ "category": question["category"],
115
+ "question": question["question"],
116
+ "open_ended": question["answer"] is None,
117
+ }
118
+
119
+ def submit_answer(
120
+ self,
121
+ question_id: str,
122
+ answer: str,
123
+ evaluator: Optional[Any] = None,
124
+ ) -> Dict[str, Any]:
125
+ question = next(
126
+ (
127
+ item
128
+ for item in self.questions
129
+ if item["id"] == question_id
130
+ ),
131
+ None,
132
+ )
133
+
134
+ if question is None:
135
+ return {
136
+ "success": False,
137
+ "error": "Question not found.",
138
+ }
139
+
140
+ if question["answer"] is None:
141
+ if evaluator is None:
142
+ return {
143
+ "success": False,
144
+ "error": "Open-ended question requires an evaluator.",
145
+ }
146
+
147
+ evaluation = evaluator(
148
+ question["question"],
149
+ str(answer),
150
+ )
151
+
152
+ correct = bool(evaluation)
153
+
154
+ else:
155
+ normalized = str(answer).strip().lower()
156
+ expected = str(question["answer"]).strip().lower()
157
+
158
+ correct = normalized == expected
159
+
160
+ record = {
161
+ "question_id": question_id,
162
+ "category": question["category"],
163
+ "answer": str(answer),
164
+ "correct": correct,
165
+ "explanation": question["explanation"],
166
+ "timestamp": time.time(),
167
+ }
168
+
169
+ with self._lock:
170
+ self.results.append(record)
171
+
172
+ self.statistics["attempts"] += 1
173
+
174
+ if correct:
175
+ self.statistics["correct"] += 1
176
+ self.statistics["current_streak"] += 1
177
+
178
+ self.statistics["best_streak"] = max(
179
+ self.statistics["best_streak"],
180
+ self.statistics["current_streak"],
181
+ )
182
+ else:
183
+ self.statistics["incorrect"] += 1
184
+ self.statistics["current_streak"] = 0
185
+
186
+ self.categories[question["category"]].append(
187
+ correct
188
+ )
189
+
190
+ return {
191
+ "success": True,
192
+ "correct": correct,
193
+ "explanation": question["explanation"],
194
+ "statistics": self.get_statistics(),
195
+ }
196
+
197
+ def get_statistics(self) -> Dict[str, Any]:
198
+ with self._lock:
199
+ attempts = self.statistics["attempts"]
200
+
201
+ accuracy = (
202
+ self.statistics["correct"] / attempts
203
+ if attempts
204
+ else 0.0
205
+ )
206
+
207
+ category_scores = {}
208
+
209
+ for category, outcomes in self.categories.items():
210
+ category_scores[category] = (
211
+ sum(outcomes) / len(outcomes)
212
+ if outcomes
213
+ else None
214
+ )
215
+
216
+ return {
217
+ **self.statistics,
218
+ "accuracy": accuracy,
219
+ "category_scores": category_scores,
220
+ }
221
+
222
+ def get_history(self, limit: int = 100) -> List[Dict[str, Any]]:
223
+ with self._lock:
224
+ return [
225
+ dict(item)
226
+ for item in self.results[-max(1, int(limit)):]
227
+ ]
228
+
229
+ def reset_statistics(self) -> None:
230
+ with self._lock:
231
+ self.results.clear()
232
+
233
+ for category in self.categories:
234
+ self.categories[category].clear()
235
+
236
+ self.statistics = {
237
+ "attempts": 0,
238
+ "correct": 0,
239
+ "incorrect": 0,
240
+ "current_streak": 0,
241
+ "best_streak": 0,
242
+ }
243
+
244
+ def get_status(self) -> Dict[str, Any]:
245
+ return {
246
+ "available_categories": list(self.categories.keys()),
247
+ "question_count": len(self.questions),
248
+ "statistics": self.get_statistics(),
249
+ }
250
+