sapiens-model 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sapiens_model-1.0.0/LICENSE.txt +3 -0
- sapiens_model-1.0.0/PKG-INFO +23 -0
- sapiens_model-1.0.0/sapiens_model/__init__.py +19 -0
- sapiens_model-1.0.0/sapiens_model/sapiens_model.py +1127 -0
- sapiens_model-1.0.0/sapiens_model.egg-info/PKG-INFO +23 -0
- sapiens_model-1.0.0/sapiens_model.egg-info/SOURCES.txt +10 -0
- sapiens_model-1.0.0/sapiens_model.egg-info/dependency_links.txt +1 -0
- sapiens_model-1.0.0/sapiens_model.egg-info/requires.txt +12 -0
- sapiens_model-1.0.0/sapiens_model.egg-info/top_level.txt +1 -0
- sapiens_model-1.0.0/setup.cfg +7 -0
- sapiens_model-1.0.0/setup.py +43 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: sapiens_model
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Home-page: https://github.com/sapiens-technology/sapiens_architecture_with_sapiens_model
|
|
5
|
+
Author: SAPIENS TECHNOLOGY
|
|
6
|
+
License: Proprietary Software
|
|
7
|
+
License-File: LICENSE.txt
|
|
8
|
+
Requires-Dist: semantic-comparison-network==1.0.8
|
|
9
|
+
Requires-Dist: scnetwork==1.1.3
|
|
10
|
+
Requires-Dist: scn==3.0.1
|
|
11
|
+
Requires-Dist: hur==1.1.4
|
|
12
|
+
Requires-Dist: hur-multimodal==1.0.9
|
|
13
|
+
Requires-Dist: sapiens-cpu==4.0.1
|
|
14
|
+
Requires-Dist: mgt==2.0.2
|
|
15
|
+
Requires-Dist: sapiens-dataset==4.0.4
|
|
16
|
+
Requires-Dist: utilities-nlp==7.0.3
|
|
17
|
+
Requires-Dist: tqdm==4.67.1
|
|
18
|
+
Requires-Dist: certifi==2026.1.4
|
|
19
|
+
Requires-Dist: INFINITE-CONTEXT-WINDOW==3.0.2
|
|
20
|
+
Dynamic: author
|
|
21
|
+
Dynamic: home-page
|
|
22
|
+
Dynamic: license
|
|
23
|
+
Dynamic: requires-dist
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This algorithm was designed, programmed, and developed by Sapiens Technology®️ to enable the construction, training, tuning, and inference of language models using the main architectural frameworks of Sapiens Technology®️.
|
|
3
|
+
The code includes a main class with the base architecture and several other internal sub-architectures that can be configured through the model's parameters.
|
|
4
|
+
|
|
5
|
+
Any changes to this code, reverse engineering, disclosure, or public commentary involving the technical aspects of the technology contained herein are strictly prohibited, and the authors will be duly prosecuted by our legal team.
|
|
6
|
+
|
|
7
|
+
WE DO NOT authorize the commercial use of this code without prior permission from Sapiens Technology®️.
|
|
8
|
+
"""
|
|
9
|
+
# --------------------------> A SAPIENS TECHNOLOGY®️ PRODUCTION) <--------------------------
|
|
10
|
+
from .sapiens_model import *
|
|
11
|
+
"""
|
|
12
|
+
This algorithm was designed, programmed, and developed by Sapiens Technology®️ to enable the construction, training, tuning, and inference of language models using the main architectural frameworks of Sapiens Technology®️.
|
|
13
|
+
The code includes a main class with the base architecture and several other internal sub-architectures that can be configured through the model's parameters.
|
|
14
|
+
|
|
15
|
+
Any changes to this code, reverse engineering, disclosure, or public commentary involving the technical aspects of the technology contained herein are strictly prohibited, and the authors will be duly prosecuted by our legal team.
|
|
16
|
+
|
|
17
|
+
WE DO NOT authorize the commercial use of this code without prior permission from Sapiens Technology®️.
|
|
18
|
+
"""
|
|
19
|
+
# --------------------------> A SAPIENS TECHNOLOGY®️ PRODUCTION) <--------------------------
|
|
@@ -0,0 +1,1127 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This algorithm was designed, programmed, and developed by Sapiens Technology®️ to enable the construction, training, tuning, and inference of language models using the main architectural frameworks of Sapiens Technology®️.
|
|
3
|
+
The code includes a main class with the base architecture and several other internal sub-architectures that can be configured through the model's parameters.
|
|
4
|
+
|
|
5
|
+
Any changes to this code, reverse engineering, disclosure, or public commentary involving the technical aspects of the technology contained herein are strictly prohibited, and the authors will be duly prosecuted by our legal team.
|
|
6
|
+
|
|
7
|
+
WE DO NOT authorize the commercial use of this code without prior permission from Sapiens Technology®️.
|
|
8
|
+
"""
|
|
9
|
+
# --------------------------> A SAPIENS TECHNOLOGY®️ PRODUCTION) <--------------------------
|
|
10
|
+
class SapiensModel:
|
|
11
|
+
def __init__(self, show_errors=True, display_error_point=False):
|
|
12
|
+
try:
|
|
13
|
+
self.__show_errors = bool(show_errors) if type(show_errors) in (bool, int, float) else True
|
|
14
|
+
self.__display_error_point = bool(display_error_point) if type(display_error_point) in (bool, int, float) else False
|
|
15
|
+
try:
|
|
16
|
+
from warnings import filterwarnings
|
|
17
|
+
from logging import getLogger, ERROR, disable, CRITICAL
|
|
18
|
+
from os import environ
|
|
19
|
+
from dotenv import load_dotenv
|
|
20
|
+
filterwarnings('ignore')
|
|
21
|
+
filterwarnings('ignore', category=UserWarning, module='torch.distributed')
|
|
22
|
+
getLogger('torch.distributed.elastic.multiprocessing.redirects').setLevel(ERROR)
|
|
23
|
+
environ['DISABLE_MODEL_SOURCE_CHECK'] = 'True'
|
|
24
|
+
load_dotenv()
|
|
25
|
+
disable(CRITICAL)
|
|
26
|
+
except: pass
|
|
27
|
+
from traceback import print_exc
|
|
28
|
+
self.__print_exc = print_exc
|
|
29
|
+
from semantic_comparison_network import SemanticComparisonNetwork
|
|
30
|
+
from scnetwork import SCNet
|
|
31
|
+
from scn import SCN
|
|
32
|
+
from hur import HurModel
|
|
33
|
+
from hur_multimodal import HurMultiModal
|
|
34
|
+
from sapiens_cpu import CPUModel
|
|
35
|
+
from mgt import MGT
|
|
36
|
+
self.__semantic_comparison_network = SemanticComparisonNetwork
|
|
37
|
+
self.__scnetwork = SCNet
|
|
38
|
+
self.__scn = SCN
|
|
39
|
+
self.__hur = HurModel
|
|
40
|
+
self.__hur_multimodal = HurMultiModal
|
|
41
|
+
self.__sapiens_cpu = CPUModel
|
|
42
|
+
self.__mgt = MGT
|
|
43
|
+
self.__sapiens_model = None
|
|
44
|
+
self.__sub_architecture = 'cpu'
|
|
45
|
+
self.__model_path = ''
|
|
46
|
+
self.__copy_folder_to_temp = ''
|
|
47
|
+
self.string = ''
|
|
48
|
+
self.precision = 1.0
|
|
49
|
+
self.tokenizer = 'gpt-4'
|
|
50
|
+
self.method = 'semantic'
|
|
51
|
+
self.interaction = True
|
|
52
|
+
self.activation_function = 'linear'
|
|
53
|
+
self.bias = 0.0
|
|
54
|
+
self.learning_rate = 1.0
|
|
55
|
+
self.stochastic_factor = False
|
|
56
|
+
self.fx = False
|
|
57
|
+
self.context_window = float('inf')
|
|
58
|
+
self.end_tag = '<|end|>'
|
|
59
|
+
self.validate = 0.0
|
|
60
|
+
self.hurnet_initializer = True
|
|
61
|
+
self.hurnet_layer = False
|
|
62
|
+
self.hurnet_fit = False
|
|
63
|
+
self.stream_dataset = False
|
|
64
|
+
self.quantization = None
|
|
65
|
+
self.experts = 1
|
|
66
|
+
self.language = None
|
|
67
|
+
self.information_gain = None
|
|
68
|
+
self.minimum_score = 0.5
|
|
69
|
+
self.hot = False
|
|
70
|
+
self.max_tokens = None
|
|
71
|
+
self.min_fit_probability = 0.7
|
|
72
|
+
self.min_probability = 0.01
|
|
73
|
+
self.generalization = True
|
|
74
|
+
self.temperature = 0.1
|
|
75
|
+
self.top_k = 0
|
|
76
|
+
self.top_p = 1.0
|
|
77
|
+
self.system = ''
|
|
78
|
+
self.messages = []
|
|
79
|
+
self.sapiens_model_path = ''
|
|
80
|
+
self.embedding_dim = None
|
|
81
|
+
self.block_size = None
|
|
82
|
+
self.batch_size = None
|
|
83
|
+
self.number_heads = None
|
|
84
|
+
self.number_layers = None
|
|
85
|
+
self.dropout = None
|
|
86
|
+
self.eval_interval = None
|
|
87
|
+
self.epochs = None
|
|
88
|
+
self.use_bit_net_quantization = None
|
|
89
|
+
self.device = None
|
|
90
|
+
self.user_id = 0
|
|
91
|
+
self.parallel_prediction = False
|
|
92
|
+
self.minimum_probability_for_candidates = 0.9
|
|
93
|
+
self.show_errors = True
|
|
94
|
+
self.display_error_point = False
|
|
95
|
+
self.progress = True
|
|
96
|
+
self.scn_architecture = 'level_1'
|
|
97
|
+
self.system_tag = 'System:'
|
|
98
|
+
self.user_tag = 'User:'
|
|
99
|
+
self.assistant_tag = 'Assistant:'
|
|
100
|
+
self.tokens_number = 0
|
|
101
|
+
self.parameters_number = 0
|
|
102
|
+
self.perplexity = 100.0
|
|
103
|
+
self.weight_decay = None
|
|
104
|
+
self.hurnet_embedding_length = 25
|
|
105
|
+
self.hurnet_division_method = 0
|
|
106
|
+
self.include_vocabulary_in_model = True
|
|
107
|
+
self.use_scheduler = False
|
|
108
|
+
self.hurnet_dtype = None
|
|
109
|
+
self.show_error = False
|
|
110
|
+
self.show_error_details = False
|
|
111
|
+
self.delay = 0.01
|
|
112
|
+
self.real_time_generator = False
|
|
113
|
+
self.online_consultation = True
|
|
114
|
+
self.summary_automation = True
|
|
115
|
+
self.translation_automation = True
|
|
116
|
+
self.mathematical_automation = True
|
|
117
|
+
except Exception as error:
|
|
118
|
+
try:
|
|
119
|
+
if self.__show_errors:
|
|
120
|
+
error_message = 'ERROR in SapiensModel.__init__: '+str(error)
|
|
121
|
+
print(error_message)
|
|
122
|
+
try: self.__print_exc() if self.__display_error_point else None
|
|
123
|
+
except: pass
|
|
124
|
+
except: pass
|
|
125
|
+
def __resolve_directory_path(self, directory_path=''):
|
|
126
|
+
try:
|
|
127
|
+
from pathlib import Path
|
|
128
|
+
path_object = Path(str(directory_path))
|
|
129
|
+
if path_object.is_dir(): return str(path_object)
|
|
130
|
+
if path_object.is_file(): return str(path_object.parent)
|
|
131
|
+
current_path = path_object
|
|
132
|
+
while not current_path.exists():
|
|
133
|
+
if current_path.parent == current_path: break
|
|
134
|
+
current_path = current_path.parent
|
|
135
|
+
return str(current_path)
|
|
136
|
+
except Exception as error:
|
|
137
|
+
try:
|
|
138
|
+
if self.__show_errors:
|
|
139
|
+
error_message = 'ERROR in SapiensModel.__resolve_directory_path: '+str(error)
|
|
140
|
+
print(error_message)
|
|
141
|
+
try: self.__print_exc() if self.__display_error_point else None
|
|
142
|
+
except: pass
|
|
143
|
+
except: pass
|
|
144
|
+
return directory_path
|
|
145
|
+
def __save_sapiens_json(self, directory_path=''):
|
|
146
|
+
try:
|
|
147
|
+
directory_path = str(directory_path).strip()
|
|
148
|
+
directory_path = self.__resolve_directory_path(directory_path=directory_path)
|
|
149
|
+
from json import dump
|
|
150
|
+
from os.path import join, isdir
|
|
151
|
+
from os import makedirs
|
|
152
|
+
if not isdir(directory_path): makedirs(directory_path)
|
|
153
|
+
context_window = float('inf') if self.__sub_architecture == 'hur' and self.context_window is None else self.context_window
|
|
154
|
+
data = {
|
|
155
|
+
'architecture': 'sapiens_architecture',
|
|
156
|
+
'sub_architecture': self.__sub_architecture,
|
|
157
|
+
'string': self.string,
|
|
158
|
+
'precision': self.precision,
|
|
159
|
+
'tokenizer': self.tokenizer,
|
|
160
|
+
'method': self.method,
|
|
161
|
+
'interaction': self.interaction,
|
|
162
|
+
'activation_function': self.activation_function,
|
|
163
|
+
'bias': self.bias,
|
|
164
|
+
'learning_rate': self.learning_rate,
|
|
165
|
+
'stochastic_factor': self.stochastic_factor,
|
|
166
|
+
'fx': self.fx,
|
|
167
|
+
'context_window': context_window,
|
|
168
|
+
'end_tag': self.end_tag,
|
|
169
|
+
'validate': self.validate,
|
|
170
|
+
'hurnet_initializer': self.hurnet_initializer,
|
|
171
|
+
'hurnet_layer': self.hurnet_layer,
|
|
172
|
+
'hurnet_fit': self.hurnet_fit,
|
|
173
|
+
'stream_dataset': self.stream_dataset,
|
|
174
|
+
'quantization': self.quantization,
|
|
175
|
+
'experts': self.experts,
|
|
176
|
+
'language': self.language,
|
|
177
|
+
'information_gain': self.information_gain,
|
|
178
|
+
'minimum_score': self.minimum_score,
|
|
179
|
+
'hot': self.hot,
|
|
180
|
+
'max_tokens': self.max_tokens,
|
|
181
|
+
'min_fit_probability': self.min_fit_probability,
|
|
182
|
+
'min_probability': self.min_probability,
|
|
183
|
+
'generalization': self.generalization,
|
|
184
|
+
'temperature': self.temperature,
|
|
185
|
+
'top_k': self.top_k,
|
|
186
|
+
'top_p': self.top_p,
|
|
187
|
+
'system': self.system,
|
|
188
|
+
'messages': self.messages,
|
|
189
|
+
'sapiens_model_path': self.sapiens_model_path,
|
|
190
|
+
'embedding_dim': self.embedding_dim,
|
|
191
|
+
'block_size': self.block_size,
|
|
192
|
+
'batch_size': self.batch_size,
|
|
193
|
+
'number_heads': self.number_heads,
|
|
194
|
+
'number_layers': self.number_layers,
|
|
195
|
+
'dropout': self.dropout,
|
|
196
|
+
'eval_interval': self.eval_interval,
|
|
197
|
+
'epochs': self.epochs,
|
|
198
|
+
'use_bit_net_quantization': self.use_bit_net_quantization,
|
|
199
|
+
'device': self.device,
|
|
200
|
+
'user_id': self.user_id,
|
|
201
|
+
'parallel_prediction': self.parallel_prediction,
|
|
202
|
+
'minimum_probability_for_candidates': self.minimum_probability_for_candidates,
|
|
203
|
+
'show_errors': self.show_errors,
|
|
204
|
+
'display_error_point': self.display_error_point,
|
|
205
|
+
'progress': self.progress,
|
|
206
|
+
'scn_architecture': self.scn_architecture,
|
|
207
|
+
'system_tag': self.system_tag,
|
|
208
|
+
'user_tag': self.user_tag,
|
|
209
|
+
'assistant_tag': self.assistant_tag,
|
|
210
|
+
'tokens_number': self.tokens_number,
|
|
211
|
+
'parameters_number': self.parameters_number,
|
|
212
|
+
'perplexity': self.perplexity,
|
|
213
|
+
'weight_decay': self.weight_decay,
|
|
214
|
+
'hurnet_embedding_length': self.hurnet_embedding_length,
|
|
215
|
+
'hurnet_division_method': self.hurnet_division_method,
|
|
216
|
+
'include_vocabulary_in_model': self.include_vocabulary_in_model,
|
|
217
|
+
'use_scheduler': self.use_scheduler,
|
|
218
|
+
'hurnet_dtype': self.hurnet_dtype,
|
|
219
|
+
'show_error': self.show_error,
|
|
220
|
+
'show_error_details': self.show_error_details,
|
|
221
|
+
'delay': self.delay,
|
|
222
|
+
'real_time_generator': self.real_time_generator,
|
|
223
|
+
'online_consultation': self.online_consultation,
|
|
224
|
+
'summary_automation': self.summary_automation,
|
|
225
|
+
'translation_automation': self.translation_automation,
|
|
226
|
+
'mathematical_automation': self.mathematical_automation
|
|
227
|
+
}
|
|
228
|
+
file_path = join(directory_path, 'sapiens.json')
|
|
229
|
+
with open(file_path, 'w', encoding='utf-8') as file: dump(data, file, ensure_ascii=False, indent=4)
|
|
230
|
+
return True
|
|
231
|
+
except Exception as error:
|
|
232
|
+
try:
|
|
233
|
+
if self.__show_errors:
|
|
234
|
+
error_message = 'ERROR in SapiensModel.__save_sapiens_json: '+str(error)
|
|
235
|
+
print(error_message)
|
|
236
|
+
try: self.__print_exc() if self.__display_error_point else None
|
|
237
|
+
except: pass
|
|
238
|
+
except: pass
|
|
239
|
+
return False
|
|
240
|
+
def __load_sapiens_json(self, directory_path=''):
|
|
241
|
+
try:
|
|
242
|
+
directory_path = str(directory_path).strip()
|
|
243
|
+
directory_path = self.__resolve_directory_path(directory_path=directory_path)
|
|
244
|
+
from json import load
|
|
245
|
+
from os.path import join, isfile
|
|
246
|
+
file_path = join(directory_path, 'sapiens.json')
|
|
247
|
+
if not isfile(file_path): return False
|
|
248
|
+
with open(file_path, 'r', encoding='utf-8') as file: data = load(file)
|
|
249
|
+
self.__sub_architecture = str(data.get('sub_architecture', 'cpu')).lower().strip()
|
|
250
|
+
try: self.string = str(data.get('string', '')).strip()
|
|
251
|
+
except: self.string = ''
|
|
252
|
+
try: self.precision = float(data.get('precision', 1.0))
|
|
253
|
+
except: self.precision = 1.0
|
|
254
|
+
try: self.tokenizer = str(data.get('tokenizer', 'gpt-4')).lower().strip()
|
|
255
|
+
except: self.tokenizer = 'gpt-4'
|
|
256
|
+
try: self.method = str(data.get('method', 'semantic')).lower().strip()
|
|
257
|
+
except: self.method = 'semantic'
|
|
258
|
+
try: self.interaction = bool(data.get('interaction', True))
|
|
259
|
+
except: self.interaction = True
|
|
260
|
+
try: self.activation_function = str(data.get('activation_function', 'linear')).lower().strip()
|
|
261
|
+
except: self.activation_function = 'linear'
|
|
262
|
+
try: self.bias = float(data.get('bias', 0.0))
|
|
263
|
+
except: self.bias = 0.0
|
|
264
|
+
try: self.learning_rate = float(data.get('learning_rate', 1.0))
|
|
265
|
+
except: self.learning_rate = 1.0
|
|
266
|
+
try: self.stochastic_factor = bool(data.get('stochastic_factor', False))
|
|
267
|
+
except: self.stochastic_factor = False
|
|
268
|
+
try: self.fx = bool(data.get('fx', False))
|
|
269
|
+
except: self.fx = False
|
|
270
|
+
try:
|
|
271
|
+
self.context_window = data.get('context_window', float('inf'))
|
|
272
|
+
if self.__sub_architecture == 'hur' and self.context_window == float('inf'): self.context_window = None
|
|
273
|
+
except: self.context_window = None if self.__sub_architecture == 'hur' else float('inf')
|
|
274
|
+
try: self.end_tag = str(data.get('end_tag', '<|end|>'))
|
|
275
|
+
except: self.end_tag = '<|end|>'
|
|
276
|
+
try: self.validate = float(data.get('validate', 0.0))
|
|
277
|
+
except: self.validate = 0.0
|
|
278
|
+
try: self.hurnet_initializer = bool(data.get('hurnet_initializer', True))
|
|
279
|
+
except: self.hurnet_initializer = True
|
|
280
|
+
try: self.hurnet_layer = bool(data.get('hurnet_layer', False))
|
|
281
|
+
except: self.hurnet_layer = False
|
|
282
|
+
try: self.hurnet_fit = bool(data.get('hurnet_fit', False))
|
|
283
|
+
except: self.hurnet_fit = False
|
|
284
|
+
try: self.stream_dataset = bool(data.get('stream_dataset', False))
|
|
285
|
+
except: self.stream_dataset = False
|
|
286
|
+
try: self.quantization = data.get('quantization', None)
|
|
287
|
+
except: self.quantization = None
|
|
288
|
+
try: self.experts = int(data.get('experts', 1))
|
|
289
|
+
except: self.experts = 1
|
|
290
|
+
try: self.language = data.get('language', None)
|
|
291
|
+
except: self.language = None
|
|
292
|
+
try: self.information_gain = data.get('information_gain', None)
|
|
293
|
+
except: self.information_gain = None
|
|
294
|
+
try: self.minimum_score = float(data.get('minimum_score', 0.5))
|
|
295
|
+
except: self.minimum_score = 0.5
|
|
296
|
+
try: self.hot = bool(data.get('hot', False))
|
|
297
|
+
except: self.hot = False
|
|
298
|
+
try: self.max_tokens = data.get('max_tokens', None)
|
|
299
|
+
except: self.max_tokens = None
|
|
300
|
+
try: self.min_fit_probability = float(data.get('min_fit_probability', 0.7))
|
|
301
|
+
except: self.min_fit_probability = 0.7
|
|
302
|
+
try: self.min_probability = float(data.get('min_probability', 0.01))
|
|
303
|
+
except: self.min_probability = 0.01
|
|
304
|
+
try: self.generalization = bool(data.get('generalization', True))
|
|
305
|
+
except: self.generalization = True
|
|
306
|
+
try: self.temperature = float(data.get('temperature', 0.1))
|
|
307
|
+
except: self.temperature = 0.1
|
|
308
|
+
try: self.top_k = int(data.get('top_k', 0))
|
|
309
|
+
except: self.top_k = 0
|
|
310
|
+
try: self.top_p = float(data.get('top_p', 1.0))
|
|
311
|
+
except: self.top_p = 1.0
|
|
312
|
+
try: self.system = str(data.get('system', '')).strip()
|
|
313
|
+
except: self.system = ''
|
|
314
|
+
try: self.messages = list(data.get('messages', []))
|
|
315
|
+
except: self.messages = []
|
|
316
|
+
try: self.sapiens_model_path = str(data.get('sapiens_model_path', '')).strip()
|
|
317
|
+
except: self.sapiens_model_path = ''
|
|
318
|
+
try: self.embedding_dim = data.get('embedding_dim', None)
|
|
319
|
+
except: self.embedding_dim = None
|
|
320
|
+
try: self.block_size = data.get('block_size', None)
|
|
321
|
+
except: self.block_size = None
|
|
322
|
+
try: self.batch_size = data.get('batch_size', None)
|
|
323
|
+
except: self.batch_size = None
|
|
324
|
+
try: self.number_heads = data.get('number_heads', None)
|
|
325
|
+
except: self.number_heads = None
|
|
326
|
+
try: self.number_layers = data.get('number_layers', None)
|
|
327
|
+
except: self.number_layers = None
|
|
328
|
+
try: self.dropout = data.get('dropout', None)
|
|
329
|
+
except: self.dropout = None
|
|
330
|
+
try: self.eval_interval = data.get('eval_interval', None)
|
|
331
|
+
except: self.eval_interval = None
|
|
332
|
+
try: self.epochs = data.get('epochs', None)
|
|
333
|
+
except: self.epochs = None
|
|
334
|
+
try: self.use_bit_net_quantization = data.get('use_bit_net_quantization', None)
|
|
335
|
+
except: self.use_bit_net_quantization = None
|
|
336
|
+
try: self.device = data.get('device', None)
|
|
337
|
+
except: self.device = None
|
|
338
|
+
try: self.user_id = data.get('user_id', 0)
|
|
339
|
+
except: self.user_id = 0
|
|
340
|
+
try: self.parallel_prediction = bool(data.get('parallel_prediction', False))
|
|
341
|
+
except: self.parallel_prediction = False
|
|
342
|
+
try: self.minimum_probability_for_candidates = float(data.get('minimum_probability_for_candidates', 0.9))
|
|
343
|
+
except: self.minimum_probability_for_candidates = 0.9
|
|
344
|
+
try: self.show_errors = bool(data.get('show_errors', True))
|
|
345
|
+
except: self.show_errors = True
|
|
346
|
+
try: self.display_error_point = bool(data.get('display_error_point', False))
|
|
347
|
+
except: self.display_error_point = False
|
|
348
|
+
try: self.progress = bool(data.get('progress', True))
|
|
349
|
+
except: self.progress = True
|
|
350
|
+
try: self.scn_architecture = str(data.get('scn_architecture', 'level_1')).lower().strip()
|
|
351
|
+
except: self.scn_architecture = 'level_1'
|
|
352
|
+
try: self.system_tag = str(data.get('system_tag', 'System:'))
|
|
353
|
+
except: self.system_tag = 'System:'
|
|
354
|
+
try: self.user_tag = str(data.get('user_tag', 'User:'))
|
|
355
|
+
except: self.user_tag = 'User:'
|
|
356
|
+
try: self.assistant_tag = str(data.get('assistant_tag', 'Assistant:'))
|
|
357
|
+
except: self.assistant_tag = 'Assistant:'
|
|
358
|
+
try: self.tokens_number = int(data.get('tokens_number', 0))
|
|
359
|
+
except: self.tokens_number = 0
|
|
360
|
+
try: self.parameters_number = int(data.get('parameters_number', 0))
|
|
361
|
+
except: self.parameters_number = 0
|
|
362
|
+
try: self.perplexity = float(data.get('perplexity', 100.0))
|
|
363
|
+
except: self.perplexity = 100.0
|
|
364
|
+
try: self.weight_decay = data.get('weight_decay', None)
|
|
365
|
+
except: self.weight_decay = None
|
|
366
|
+
try: self.hurnet_embedding_length = int(data.get('hurnet_embedding_length', 25))
|
|
367
|
+
except: self.hurnet_embedding_length = 25
|
|
368
|
+
try: self.hurnet_division_method = int(data.get('hurnet_division_method', 0))
|
|
369
|
+
except: self.hurnet_division_method = 0
|
|
370
|
+
try: self.include_vocabulary_in_model = bool(data.get('include_vocabulary_in_model', True))
|
|
371
|
+
except: self.include_vocabulary_in_model = True
|
|
372
|
+
try: self.use_scheduler = bool(data.get('use_scheduler', False))
|
|
373
|
+
except: self.use_scheduler = False
|
|
374
|
+
try: self.hurnet_dtype = data.get('hurnet_dtype', None)
|
|
375
|
+
except: self.hurnet_dtype = None
|
|
376
|
+
try: self.show_error = bool(data.get('show_error', False))
|
|
377
|
+
except: self.show_error = False
|
|
378
|
+
try: self.show_error_details = bool(data.get('show_error_details', False))
|
|
379
|
+
except: self.show_error_details = False
|
|
380
|
+
try: self.delay = float(data.get('delay', 0.01))
|
|
381
|
+
except: self.delay = 0.01
|
|
382
|
+
try: self.real_time_generator = bool(data.get('real_time_generator', False))
|
|
383
|
+
except: self.real_time_generator = False
|
|
384
|
+
try: self.online_consultation = bool(data.get('online_consultation', True))
|
|
385
|
+
except: self.online_consultation = True
|
|
386
|
+
try: self.summary_automation = bool(data.get('summary_automation', True))
|
|
387
|
+
except: self.summary_automation = True
|
|
388
|
+
try: self.translation_automation = bool(data.get('translation_automation', True))
|
|
389
|
+
except: self.translation_automation = True
|
|
390
|
+
try: self.mathematical_automation = bool(data.get('mathematical_automation', True))
|
|
391
|
+
except: self.mathematical_automation = True
|
|
392
|
+
return True
|
|
393
|
+
except Exception as error:
|
|
394
|
+
try:
|
|
395
|
+
if self.__show_errors:
|
|
396
|
+
error_message = 'ERROR in SapiensModel.__load_sapiens_json: '+str(error)
|
|
397
|
+
print(error_message)
|
|
398
|
+
try: self.__print_exc() if self.__display_error_point else None
|
|
399
|
+
except: pass
|
|
400
|
+
except: pass
|
|
401
|
+
return False
|
|
402
|
+
def __get_online_answer(self, prompt=''):
|
|
403
|
+
try:
|
|
404
|
+
obtained_response = ''
|
|
405
|
+
prompt = str(prompt).strip()
|
|
406
|
+
def ___eligible_question(question=''):
|
|
407
|
+
eligible_question_result = [False, '']
|
|
408
|
+
question = str(question).lower().strip()
|
|
409
|
+
words_length = len(question.split())
|
|
410
|
+
if words_length > 10: return eligible_question_result
|
|
411
|
+
elif not question.endswith(chr(63)): return eligible_question_result
|
|
412
|
+
forbidden_strings = ('your name', 'sapiens chat', 'sapiens technology')
|
|
413
|
+
for forbidden_string in forbidden_strings:
|
|
414
|
+
if forbidden_string in question: return eligible_question_result
|
|
415
|
+
terms_strings = ('who is', '¿quién es', '¿quien es', 'quem é', 'quem e', 'who was', '¿quién fue', '¿quien fue', 'quem foi',
|
|
416
|
+
'¿quién era', '¿quien era', 'quem era', 'who were', '¿quiénes fueron', '¿quienes fueron', 'quem foram', '¿quiénes eran',
|
|
417
|
+
'¿quienes eran', 'quem eram', 'what is', "what's", 'whats', 'what`s', 'what’s', '¿qué es', '¿que es', 'o que é', 'oque é', 'o que e',
|
|
418
|
+
'oque e', 'o quê é', 'oquê é', 'o quê e', 'oquê e', 'what was', 'what were', '¿qué fueron', '¿que fueron', '¿qué fue',
|
|
419
|
+
'¿que fue', 'o quê foi', 'oquê foi', 'o quê foram', 'oquê foram', 'o que foi', 'oque foi', 'o que foram', 'oque foram',
|
|
420
|
+
'¿qué eran', '¿que eran', '¿qué era', '¿que era', 'o quê eram', 'o que eram', 'oquê eram', 'oque eram', 'o quê era',
|
|
421
|
+
'o que era', 'oquê era', 'oque era', 'cuál es', 'cual es', 'qual é', 'qual e', 'what are', "what're", 'whatre', 'what`re',
|
|
422
|
+
'what’re', 'cuáles son', 'cuales son', 'quais são', 'quais sao', 'who are', 'quiénes son', 'quienes son', 'quem são', 'quem sao')
|
|
423
|
+
connections_strings = ('a', 'an', 'the', 'some', 'un', 'una', 'el', 'la', 'unos', 'unas',
|
|
424
|
+
'los', 'las', 'um', 'uma', 'o', 'á', 'à', 'uns', 'umas', 'os', 'as', 'ás', 'às')
|
|
425
|
+
for term_string in terms_strings:
|
|
426
|
+
term_string = str(term_string).lower().strip()
|
|
427
|
+
if question.startswith(term_string+chr(32)):
|
|
428
|
+
new_question = question.split(term_string)[-1].strip()
|
|
429
|
+
for connection_string in connections_strings:
|
|
430
|
+
if new_question.startswith(connection_string+chr(32)):
|
|
431
|
+
other_question = new_question.split(term_string)[-1].strip()
|
|
432
|
+
if other_question.endswith(chr(63)):
|
|
433
|
+
title, eligible_question = other_question[:-1].strip(), True
|
|
434
|
+
eligible_question_result[0] = eligible_question
|
|
435
|
+
eligible_question_result[1] = title
|
|
436
|
+
break
|
|
437
|
+
if not eligible_question_result[0] and new_question.endswith(chr(63)):
|
|
438
|
+
title, eligible_question = new_question[:-1].strip(), True
|
|
439
|
+
eligible_question_result[0] = eligible_question
|
|
440
|
+
eligible_question_result[1] = title
|
|
441
|
+
return eligible_question_result
|
|
442
|
+
def ___formats_content(content=''):
|
|
443
|
+
page_content_lines = content.split('\n')
|
|
444
|
+
for index, page_content_line in enumerate(page_content_lines):
|
|
445
|
+
page_content_line = page_content_line.strip()
|
|
446
|
+
if page_content_line:
|
|
447
|
+
if page_content_line.startswith('=') and '= ' in page_content_line:
|
|
448
|
+
page_content_line = page_content_line.rstrip('=')
|
|
449
|
+
if '====== ' in page_content_line: page_content_line = page_content_line.replace('====== ', '###### ').strip()
|
|
450
|
+
if '===== ' in page_content_line: page_content_line = page_content_line.replace('===== ', '##### ').strip()
|
|
451
|
+
if '==== ' in page_content_line: page_content_line = page_content_line.replace('==== ', '#### ').strip()
|
|
452
|
+
if '=== ' in page_content_line: page_content_line = page_content_line.replace('=== ', '### ').strip()
|
|
453
|
+
if '== ' in page_content_line: page_content_line = page_content_line.replace('== ', '## ').strip()
|
|
454
|
+
if '= ' in page_content_line: page_content_line = page_content_line.replace('= ', '# ').strip()
|
|
455
|
+
page_content_lines[index] = page_content_line
|
|
456
|
+
content = str('\n'.join(page_content_lines)).strip()
|
|
457
|
+
return content
|
|
458
|
+
eligible_question_result = ___eligible_question(question=prompt)
|
|
459
|
+
eligible_result = eligible_question_result[0]
|
|
460
|
+
eligible_tile = eligible_question_result[-1]
|
|
461
|
+
if eligible_result:
|
|
462
|
+
from sapiens_dataset import SapiensDataset
|
|
463
|
+
from utilities_nlp import UtilitiesNLP as SapiensUtilities
|
|
464
|
+
sapiens_dataset = SapiensDataset(show_errors=False, display_error_point=False)
|
|
465
|
+
sapiens_utilities = SapiensUtilities(show_errors=False, display_error_point=False)
|
|
466
|
+
language = sapiens_utilities.getLanguage(prompt)
|
|
467
|
+
obtained_response = sapiens_dataset.wikipediaArticleSummary(eligible_tile, language if language else None)
|
|
468
|
+
if obtained_response: obtained_response = ___formats_content(content=obtained_response)
|
|
469
|
+
return obtained_response.strip()
|
|
470
|
+
except Exception as error:
|
|
471
|
+
try:
|
|
472
|
+
if self.__show_errors:
|
|
473
|
+
error_message = 'ERROR in SapiensModel.__get_online_answer: '+str(error)
|
|
474
|
+
print(error_message)
|
|
475
|
+
try: self.__print_exc() if self.__display_error_point else None
|
|
476
|
+
except: pass
|
|
477
|
+
except: pass
|
|
478
|
+
return ''
|
|
479
|
+
def __mathematical_solution(self, prompt=''):
|
|
480
|
+
try:
|
|
481
|
+
mathematical_solution = ''
|
|
482
|
+
prompt = str(prompt).strip()
|
|
483
|
+
from utilities_nlp import UtilitiesNLP as SapiensUtilities
|
|
484
|
+
sapiens_utilities = SapiensUtilities(show_errors=self.__show_errors, display_error_point=self.__display_error_point)
|
|
485
|
+
count_tokens = sapiens_utilities.countTokens(string=prompt, pattern='gpt')
|
|
486
|
+
if count_tokens > 150: return mathematical_solution
|
|
487
|
+
from re import search, sub, finditer
|
|
488
|
+
def _check_operation(text=''):
|
|
489
|
+
temp_text = text.replace('x', '*').replace('X', '*').replace('ˆ', '**').replace('^', '**')
|
|
490
|
+
temp_text = temp_text.replace('⁰', '** 0').replace('¹', '** 1').replace('²', '** 2').replace('³', '** 3').replace('⁴', '** 4')
|
|
491
|
+
temp_text = temp_text.replace('⁵', '** 5').replace('⁶', '** 6').replace('⁷', '** 7').replace('⁸', '** 8').replace('⁹', '** 9')
|
|
492
|
+
temp_text = temp_text.replace('raised to the power of', '**').replace('elevado a la', '**').replace('elevado à', '**').replace('elevado a', '**')
|
|
493
|
+
number = r'[-+]?\d+(?:[.,]\d+)?'
|
|
494
|
+
operators = [r'\*\*\=', r'\+\=', r'-\=', r'/\=', r'//\=', r'%\=', r'\*\=', r'\*\*(?!\=)', r'//(?!\=)', r'<=', r'>=', r'!=', r'\+(?!\=)', r'-(?!\=)', r'/(?!\=)', r'%(?!\=)', r'\*(?!\=)', r'<(?!\=)', r'>(?!\=)']
|
|
495
|
+
for operator in operators:
|
|
496
|
+
pattern = rf'{number}\s*{operator}\s*{number}'
|
|
497
|
+
if search(pattern, temp_text): return True
|
|
498
|
+
return False
|
|
499
|
+
def _process_and_calculate(text=''):
|
|
500
|
+
processed_text = sub(r'(\d),(\d)', r'\1.\2', text)
|
|
501
|
+
processed_text = processed_text.replace('x', '*').replace('X', '*').replace('ˆ', '**').replace('^', '**')
|
|
502
|
+
processed_text = processed_text.replace('⁰', '** 0').replace('¹', '** 1').replace('²', '** 2').replace('³', '** 3').replace('⁴', '** 4')
|
|
503
|
+
processed_text = processed_text.replace('⁵', '** 5').replace('⁶', '** 6').replace('⁷', '** 7').replace('⁸', '** 8').replace('⁹', '** 9')
|
|
504
|
+
processed_text = processed_text.replace('raised to the power of', '**').replace('elevado a la', '**').replace('elevado à', '**').replace('elevado a', '**')
|
|
505
|
+
expression_pattern = r'[(\[]*[-+]?\d+(?:\.\d+)?[)\]]*(?:\s*(?:\*\*\=?|//\=?|[+\-*/%<>=!]+\=?)\s*[(\[]*[-+]?\d+(?:\.\d+)?[)\]]*)+|[(\[][-+]?\d+(?:\.\d+)?(?:\s*(?:\*\*\=?|//\=?|[+\-*/%<>=!]+\=?)\s*[-+]?\d+(?:\.\d+)?)+[)\]](?:\s*(?:\*\*\=?|//\=?|[+\-*/%<>=!]+\=?)\s*[(\[]*[-+]?\d+(?:\.\d+)?[)\]]*)*'
|
|
506
|
+
matches = list(finditer(expression_pattern, processed_text))
|
|
507
|
+
if not matches: return ''
|
|
508
|
+
best_match = max(matches, key=lambda m: len(m.group()))
|
|
509
|
+
expression = best_match.group().strip()
|
|
510
|
+
open_paren = expression.count('(')
|
|
511
|
+
close_paren = expression.count(')')
|
|
512
|
+
open_bracket = expression.count('[')
|
|
513
|
+
close_bracket = expression.count(']')
|
|
514
|
+
start_pos = best_match.start()
|
|
515
|
+
end_pos = best_match.end()
|
|
516
|
+
while open_paren > close_paren and end_pos < len(processed_text):
|
|
517
|
+
if processed_text[end_pos] == ')':
|
|
518
|
+
close_paren += 1
|
|
519
|
+
end_pos += 1
|
|
520
|
+
expression = processed_text[start_pos:end_pos].strip()
|
|
521
|
+
else: end_pos += 1
|
|
522
|
+
while open_bracket > close_bracket and end_pos < len(processed_text):
|
|
523
|
+
if processed_text[end_pos] == ']':
|
|
524
|
+
close_bracket += 1
|
|
525
|
+
end_pos += 1
|
|
526
|
+
expression = processed_text[start_pos:end_pos].strip()
|
|
527
|
+
else: end_pos += 1
|
|
528
|
+
while close_paren > open_paren and start_pos > 0:
|
|
529
|
+
start_pos -= 1
|
|
530
|
+
if processed_text[start_pos] == '(':
|
|
531
|
+
open_paren += 1
|
|
532
|
+
expression = processed_text[start_pos:end_pos].strip()
|
|
533
|
+
while close_bracket > open_bracket and start_pos > 0:
|
|
534
|
+
start_pos -= 1
|
|
535
|
+
if processed_text[start_pos] == '[':
|
|
536
|
+
open_bracket += 1
|
|
537
|
+
expression = processed_text[start_pos:end_pos].strip()
|
|
538
|
+
try:
|
|
539
|
+
result = eval(expression)
|
|
540
|
+
return f'{expression} = {result}'
|
|
541
|
+
except: return ''
|
|
542
|
+
def _calculate_expression(text=''):
|
|
543
|
+
if _check_operation(text=text): return _process_and_calculate(text=text.replace('[', '(').replace(']', ')') if '[' in text and ']' in text else text)
|
|
544
|
+
else: return ''
|
|
545
|
+
lines, calculate, calculated_expressions = prompt.split('\n'), False, []
|
|
546
|
+
for line in lines:
|
|
547
|
+
calculate_expression = _calculate_expression(text=str(line).strip())
|
|
548
|
+
if calculate_expression:
|
|
549
|
+
calculated_expressions.append(calculate_expression)
|
|
550
|
+
calculate = True
|
|
551
|
+
if calculate and calculated_expressions: mathematical_solution = '\n'.join(calculated_expressions)
|
|
552
|
+
return mathematical_solution.strip()
|
|
553
|
+
except Exception as error:
|
|
554
|
+
try:
|
|
555
|
+
if self.__show_errors:
|
|
556
|
+
error_message = 'ERROR in SapiensModel.__mathematical_solution: '+str(error)
|
|
557
|
+
print(error_message)
|
|
558
|
+
try: self.__print_exc() if self.__display_error_point else None
|
|
559
|
+
except: pass
|
|
560
|
+
except: pass
|
|
561
|
+
return False
|
|
562
|
+
def training(self, dataset_path='', sub_architecture='cpu', progress=True):
|
|
563
|
+
try:
|
|
564
|
+
sub_architecture, training_function = str(sub_architecture).lower().strip(), False
|
|
565
|
+
if sub_architecture == 'cpu':
|
|
566
|
+
self.__sapiens_model = self.__sapiens_cpu(show_errors=self.show_errors, display_error_point=self.display_error_point)
|
|
567
|
+
self.__sapiens_model.END_TAG = self.end_tag
|
|
568
|
+
self.__sapiens_model.N_EXPERTS = self.experts
|
|
569
|
+
training_function = self.__sapiens_model.train(dataset_path=dataset_path, progress=progress)
|
|
570
|
+
elif sub_architecture == 'scn':
|
|
571
|
+
if self.scn_architecture == 'level_1':
|
|
572
|
+
self.__sapiens_model = self.__semantic_comparison_network()
|
|
573
|
+
if self.tokenizer.startswith('gpt'): self.tokenizer = 'gpt'
|
|
574
|
+
training_function = self.__sapiens_model.train(dataset_path=dataset_path, string=self.string, precision=self.precision, tokenizer=self.tokenizer, method=self.method, interaction=self.interaction, activation_function=self.activation_function, bias=self.bias, learning_rate=self.learning_rate, stochastic_factor=self.stochastic_factor, fx=self.fx, progress=progress)
|
|
575
|
+
elif self.scn_architecture == 'level_3':
|
|
576
|
+
self.__sapiens_model = self.__scn(device=self.device, user_id=self.user_id, parallel_prediction=self.parallel_prediction, minimum_probability_for_candidates=self.minimum_probability_for_candidates, show_errors=self.show_errors)
|
|
577
|
+
training_function = self.__sapiens_model.train(dataset_path=dataset_path, string=self.string, precision=self.precision, tokenizer=self.tokenizer, context_window=self.context_window, end_tag=self.end_tag, validate=self.validate, progress=progress)
|
|
578
|
+
else:
|
|
579
|
+
self.__sapiens_model = self.__scnetwork(device=self.device, user_id=self.user_id, parallel_prediction=self.parallel_prediction, minimum_probability_for_candidates=self.minimum_probability_for_candidates, show_errors=self.show_errors)
|
|
580
|
+
training_function = self.__sapiens_model.train(dataset_path=dataset_path, string=self.string, precision=self.precision, tokenizer=self.tokenizer, context_window=self.context_window, end_tag=self.end_tag, validate=self.validate, progress=progress)
|
|
581
|
+
elif sub_architecture == 'hur':
|
|
582
|
+
if self.learning_rate == 1.0: self.learning_rate = None
|
|
583
|
+
if self.context_window == float('inf'): self.context_window = None
|
|
584
|
+
self.__sapiens_model = self.__hur_multimodal(embedding_dim=self.embedding_dim, block_size=self.block_size, batch_size=self.batch_size, number_heads=self.number_heads, number_layers=self.number_layers, dropout=self.dropout, learning_rate=self.learning_rate, eval_interval=self.eval_interval, epochs=self.epochs, use_bit_net_quantization=self.use_bit_net_quantization, device=self.device)
|
|
585
|
+
self.__sapiens_model.END_TAG = self.end_tag
|
|
586
|
+
self.__sapiens_model.SYSTEM_TAG = self.system_tag
|
|
587
|
+
self.__sapiens_model.USER_TAG = self.user_tag
|
|
588
|
+
self.__sapiens_model.ASSISTANT_TAG = self.assistant_tag
|
|
589
|
+
self.__sapiens_model.TOKENS_NUMBER = self.tokens_number
|
|
590
|
+
self.__sapiens_model.PARAMETERS_NUMBER = self.parameters_number
|
|
591
|
+
self.__sapiens_model.PERPLEXITY = self.perplexity
|
|
592
|
+
self.__sapiens_model.WEIGHT_DECAY = self.weight_decay
|
|
593
|
+
self.__sapiens_model.USER_ID = self.user_id
|
|
594
|
+
self.__sapiens_model.HURNET_EMBEDDING_LENGTH = self.hurnet_embedding_length
|
|
595
|
+
self.__sapiens_model.HURNET_DIVISION_METHOD = self.hurnet_division_method
|
|
596
|
+
self.__sapiens_model.INCLUDE_VOCABULARY_IN_MODEL = self.include_vocabulary_in_model
|
|
597
|
+
self.__sapiens_model.USE_SCHEDULER = self.use_scheduler
|
|
598
|
+
self.__sapiens_model.HURNET_DTYPE = self.hurnet_dtype
|
|
599
|
+
self.__sapiens_model.SHOW_ERROR = self.show_error
|
|
600
|
+
self.__sapiens_model.SHOW_ERROR_DETAILS = self.show_error_details
|
|
601
|
+
training_function = self.__sapiens_model.train(dataset_path=dataset_path, string=self.string, precision=self.precision, tokenizer=self.tokenizer, context_window=self.context_window, hurnet_initializer=self.hurnet_initializer, hurnet_layer=self.hurnet_layer, hurnet_fit=self.hurnet_fit, end_tag=self.end_tag, stream_dataset=self.stream_dataset, validate=self.validate, quantization=self.quantization, experts=self.experts, progress=progress)
|
|
602
|
+
self.tokens_number = self.__sapiens_model.TOKENS_NUMBER
|
|
603
|
+
self.parameters_number = self.__sapiens_model.PARAMETERS_NUMBER
|
|
604
|
+
elif sub_architecture == 'mgt':
|
|
605
|
+
self.__sapiens_model = self.__mgt(show_errors=self.show_errors, display_error_point=self.display_error_point, progress=self.progress and progress)
|
|
606
|
+
_MGT__interpretation = self.__sapiens_model._MGT__interpretation
|
|
607
|
+
_MGT__database = self.__sapiens_model._MGT__database
|
|
608
|
+
_MGT__n_tokens = self.__sapiens_model._MGT__n_tokens
|
|
609
|
+
old_interpretation = _MGT__interpretation if _MGT__interpretation else _MGT__database
|
|
610
|
+
training_function = self.__sapiens_model.train(path=dataset_path, language=self.language, information_gain=self.information_gain)
|
|
611
|
+
if old_interpretation and _MGT__n_tokens:
|
|
612
|
+
self.__scnetwork().textSummary(text=old_interpretation, max_tokens=_MGT__n_tokens)
|
|
613
|
+
self.__sapiens_model._MGT__interpretation = old_interpretation
|
|
614
|
+
else:
|
|
615
|
+
self.__sapiens_model = self.__scnetwork(device=self.device, user_id=self.user_id, parallel_prediction=self.parallel_prediction, minimum_probability_for_candidates=self.minimum_probability_for_candidates, show_errors=self.show_errors)
|
|
616
|
+
training_function = self.__sapiens_model.train(dataset_path=dataset_path, string=self.string, precision=self.precision, tokenizer=self.tokenizer, context_window=self.context_window, end_tag=self.end_tag, validate=self.validate, progress=progress)
|
|
617
|
+
self.__sub_architecture = sub_architecture
|
|
618
|
+
return training_function
|
|
619
|
+
except Exception as error:
|
|
620
|
+
try:
|
|
621
|
+
if self.__show_errors:
|
|
622
|
+
error_message = 'ERROR in SapiensModel.training: '+str(error)
|
|
623
|
+
print(error_message)
|
|
624
|
+
try: self.__print_exc() if self.__display_error_point else None
|
|
625
|
+
except: pass
|
|
626
|
+
except: pass
|
|
627
|
+
return False
|
|
628
|
+
def saveModel(self, model_path='', progress=True):
|
|
629
|
+
try:
|
|
630
|
+
model_path = str(model_path).strip()
|
|
631
|
+
progress, progress_bar = bool(progress) if type(progress) in (bool, int, float) else True, None
|
|
632
|
+
if not self.__sapiens_model:
|
|
633
|
+
if self.__show_errors: print('The model has not yet been trained.')
|
|
634
|
+
return False
|
|
635
|
+
def _ensure_path_exists(file_path=''):
|
|
636
|
+
from os.path import dirname, exists
|
|
637
|
+
from os import makedirs
|
|
638
|
+
directory_path = dirname(file_path)
|
|
639
|
+
if len(directory_path) == 0: return
|
|
640
|
+
if not exists(directory_path): makedirs(directory_path)
|
|
641
|
+
_ensure_path_exists(file_path=model_path)
|
|
642
|
+
if self.__sub_architecture in ('cpu', 'mgt'):
|
|
643
|
+
if self.__sub_architecture == 'mgt': self.__sapiens_model._MGT__progress = progress
|
|
644
|
+
if self.__sub_architecture == 'cpu' and progress:
|
|
645
|
+
from tqdm import tqdm
|
|
646
|
+
from shutil import get_terminal_size
|
|
647
|
+
total_value, terminal_width = 100, get_terminal_size().columns
|
|
648
|
+
progress_bar = tqdm(total=total_value, desc='Saving', ncols=terminal_width)
|
|
649
|
+
progress_bar.update(50)
|
|
650
|
+
saved_model = self.__sapiens_model.saveModel(model_path=model_path)
|
|
651
|
+
if self.__sub_architecture == 'cpu' and progress and progress_bar is not None:
|
|
652
|
+
progress_bar.update(50)
|
|
653
|
+
progress_bar.n = 100
|
|
654
|
+
progress_bar.refresh()
|
|
655
|
+
progress_bar.close()
|
|
656
|
+
else:
|
|
657
|
+
if self.__copy_folder_to_temp and self.__sub_architecture == 'scn' and self.scn_architecture in ('level_2', 'level_3'):
|
|
658
|
+
def _delete_folder(folder_path=''):
|
|
659
|
+
from shutil import rmtree
|
|
660
|
+
try:
|
|
661
|
+
rmtree(folder_path)
|
|
662
|
+
return True
|
|
663
|
+
except: return False
|
|
664
|
+
def _copy_files_with_defined_name(source_folder_path='', destination_folder_path='', defined_name=''):
|
|
665
|
+
from os import listdir, makedirs
|
|
666
|
+
from os.path import isfile, join, splitext
|
|
667
|
+
from shutil import copy2
|
|
668
|
+
makedirs(destination_folder_path, exist_ok=True)
|
|
669
|
+
for item in listdir(source_folder_path):
|
|
670
|
+
source_item_path = join(source_folder_path, item)
|
|
671
|
+
if isfile(source_item_path):
|
|
672
|
+
file_extension = splitext(item)[1]
|
|
673
|
+
destination_item_path = join(destination_folder_path, defined_name + file_extension)
|
|
674
|
+
copy2(source_item_path, destination_item_path)
|
|
675
|
+
from pathlib import Path
|
|
676
|
+
file_name = Path(model_path).name
|
|
677
|
+
if not Path(model_path).is_dir(): model_path = str(Path(model_path).parent)
|
|
678
|
+
_copy_files_with_defined_name(source_folder_path=self.__copy_folder_to_temp, destination_folder_path=model_path, defined_name=file_name)
|
|
679
|
+
_delete_folder(folder_path=self.__copy_folder_to_temp)
|
|
680
|
+
self.__copy_folder_to_temp, saved_model = '', True
|
|
681
|
+
else: saved_model = self.__sapiens_model.saveModel(model_path=model_path, progress=progress)
|
|
682
|
+
save_sapiens_json = self.__save_sapiens_json(directory_path=model_path)
|
|
683
|
+
return saved_model and save_sapiens_json
|
|
684
|
+
except Exception as error:
|
|
685
|
+
try:
|
|
686
|
+
if self.__show_errors:
|
|
687
|
+
error_message = 'ERROR in SapiensModel.saveModel: '+str(error)
|
|
688
|
+
print(error_message)
|
|
689
|
+
try: self.__print_exc() if self.__display_error_point else None
|
|
690
|
+
except: pass
|
|
691
|
+
except: pass
|
|
692
|
+
return False
|
|
693
|
+
def loadModel(self, model_path='', progress=True):
|
|
694
|
+
try:
|
|
695
|
+
loaded_model = False
|
|
696
|
+
model_path = str(model_path).strip()
|
|
697
|
+
progress = bool(progress) if type(progress) in (bool, int, float) else True
|
|
698
|
+
loaded_model = self.__load_sapiens_json(directory_path=model_path)
|
|
699
|
+
self.__model_path = model_path
|
|
700
|
+
from pathlib import Path
|
|
701
|
+
file_name = Path(model_path).name
|
|
702
|
+
if model_path and '.' in file_name and not Path(model_path).is_file():
|
|
703
|
+
if self.__show_errors: print(f'The referenced model "{model_path}" DOES NOT EXIST.')
|
|
704
|
+
return False
|
|
705
|
+
def _has_file_with_extensions(folder_path='', extensions=[]):
|
|
706
|
+
if not Path(folder_path).is_dir(): folder_path = Path(folder_path).parent
|
|
707
|
+
from os import walk
|
|
708
|
+
normalized_extensions = []
|
|
709
|
+
for extension in extensions:
|
|
710
|
+
extension = extension.lower().strip()
|
|
711
|
+
if not extension.startswith('.'): extension = '.' + extension
|
|
712
|
+
normalized_extensions.append(extension)
|
|
713
|
+
for root, directories, files in walk(folder_path):
|
|
714
|
+
for file_name in files:
|
|
715
|
+
lower_name = file_name.lower()
|
|
716
|
+
for extension in normalized_extensions:
|
|
717
|
+
if lower_name.endswith(extension): return True
|
|
718
|
+
return False
|
|
719
|
+
if _has_file_with_extensions(folder_path=model_path, extensions=['cpu']): self.__sub_architecture = 'cpu'
|
|
720
|
+
elif _has_file_with_extensions(folder_path=model_path, extensions=['scconf', 'scnnet', 'vocabu']): self.__sub_architecture, self.scn_architecture = 'scn', 'level_1'
|
|
721
|
+
elif _has_file_with_extensions(folder_path=model_path, extensions=['sccon', 'scnet']): self.__sub_architecture, self.scn_architecture = 'scn', 'level_2'
|
|
722
|
+
elif _has_file_with_extensions(folder_path=model_path, extensions=['scn01', 'scn02', 'scn03']): self.__sub_architecture, self.scn_architecture = 'scn', 'level_3'
|
|
723
|
+
elif _has_file_with_extensions(folder_path=model_path, extensions=['hurlm']): self.__sub_architecture = 'hur'
|
|
724
|
+
elif _has_file_with_extensions(folder_path=model_path, extensions=['mini']): self.__sub_architecture = 'mgt'
|
|
725
|
+
if self.__sub_architecture == 'cpu':
|
|
726
|
+
self.__sapiens_model, progress_bar = self.__sapiens_cpu(show_errors=self.show_errors, display_error_point=self.display_error_point), None
|
|
727
|
+
if progress:
|
|
728
|
+
from tqdm import tqdm
|
|
729
|
+
from shutil import get_terminal_size
|
|
730
|
+
total_value, terminal_width = 100, get_terminal_size().columns
|
|
731
|
+
progress_bar = tqdm(total=total_value, desc='Loading', ncols=terminal_width)
|
|
732
|
+
progress_bar.update(50)
|
|
733
|
+
loaded_model = self.__sapiens_model.loadModel(model_path=model_path)
|
|
734
|
+
if progress and progress_bar is not None:
|
|
735
|
+
progress_bar.update(50)
|
|
736
|
+
progress_bar.n = 100
|
|
737
|
+
progress_bar.refresh()
|
|
738
|
+
progress_bar.close()
|
|
739
|
+
elif self.__sub_architecture == 'scn':
|
|
740
|
+
if self.scn_architecture == 'level_1': self.__sapiens_model = self.__semantic_comparison_network()
|
|
741
|
+
elif self.scn_architecture == 'level_2': self.__sapiens_model = self.__scnetwork(device=self.device, user_id=self.user_id, parallel_prediction=self.parallel_prediction, minimum_probability_for_candidates=self.minimum_probability_for_candidates, show_errors=self.show_errors)
|
|
742
|
+
elif self.scn_architecture == 'level_3': self.__sapiens_model = self.__scn(device=self.device, user_id=self.user_id, parallel_prediction=self.parallel_prediction, minimum_probability_for_candidates=self.minimum_probability_for_candidates, show_errors=self.show_errors)
|
|
743
|
+
loaded_model = self.__sapiens_model.loadModel(model_path=model_path, progress=progress)
|
|
744
|
+
elif self.__sub_architecture == 'hur':
|
|
745
|
+
self.__sapiens_model = self.__hur_multimodal(embedding_dim=self.embedding_dim, block_size=self.block_size, batch_size=self.batch_size, number_heads=self.number_heads, number_layers=self.number_layers, dropout=self.dropout, learning_rate=self.learning_rate, eval_interval=self.eval_interval, epochs=self.epochs, use_bit_net_quantization=self.use_bit_net_quantization, device=self.device)
|
|
746
|
+
loaded_model = self.__sapiens_model.loadModel(model_path=model_path, progress=progress)
|
|
747
|
+
elif self.__sub_architecture == 'mgt':
|
|
748
|
+
self.__sapiens_model = self.__mgt(show_errors=self.show_errors, display_error_point=self.display_error_point, progress=self.progress and progress)
|
|
749
|
+
loaded_model = self.__sapiens_model.loadModel(model_path=model_path)
|
|
750
|
+
else: loaded_model = False
|
|
751
|
+
return loaded_model
|
|
752
|
+
except Exception as error:
|
|
753
|
+
try:
|
|
754
|
+
if self.__show_errors:
|
|
755
|
+
error_message = 'ERROR in SapiensModel.loadModel: '+str(error)
|
|
756
|
+
print(error_message)
|
|
757
|
+
try: self.__print_exc() if self.__display_error_point else None
|
|
758
|
+
except: pass
|
|
759
|
+
except: pass
|
|
760
|
+
return False
|
|
761
|
+
def fineTuning(self, dataset_path='', progress=True):
|
|
762
|
+
try:
|
|
763
|
+
adjusted_model = False
|
|
764
|
+
dataset_path = str(dataset_path).strip()
|
|
765
|
+
progress = bool(progress) if type(progress) in (bool, int, float) else True
|
|
766
|
+
if self.__sub_architecture == 'mgt':
|
|
767
|
+
import tqdm
|
|
768
|
+
def disable_tqdm():
|
|
769
|
+
def silent_init(self, *args, **kwargs): self.iterable = args[0] if args else None
|
|
770
|
+
tqdm.tqdm.__init__ = silent_init
|
|
771
|
+
def enable_tqdm():
|
|
772
|
+
from importlib import reload
|
|
773
|
+
reload(tqdm)
|
|
774
|
+
if not progress: disable_tqdm()
|
|
775
|
+
try: adjusted_model = self.__sapiens_model.fineTuning(file_path=dataset_path)
|
|
776
|
+
finally:
|
|
777
|
+
if not progress: enable_tqdm()
|
|
778
|
+
elif self.__sub_architecture == 'cpu': adjusted_model = self.__sapiens_model.fineTuning(dataset_path=dataset_path, progress=progress)
|
|
779
|
+
else:
|
|
780
|
+
if self.__model_path and self.__sub_architecture == 'scn' and self.scn_architecture in ('level_2', 'level_3'):
|
|
781
|
+
from pathlib import Path
|
|
782
|
+
from os.path import join
|
|
783
|
+
def _copy_folder_to_temp(folder_path=''):
|
|
784
|
+
folder_path = Path(folder_path)
|
|
785
|
+
file_name = ''
|
|
786
|
+
if not folder_path.is_dir():
|
|
787
|
+
file_name = folder_path.stem
|
|
788
|
+
folder_path = folder_path.parent
|
|
789
|
+
from tempfile import gettempdir
|
|
790
|
+
from shutil import copytree, rmtree, copy2
|
|
791
|
+
from os.path import exists, join, splitext
|
|
792
|
+
from os import makedirs, listdir
|
|
793
|
+
temp_directory = gettempdir()
|
|
794
|
+
folder_name = folder_path.name
|
|
795
|
+
destination_path = join(temp_directory, folder_name)
|
|
796
|
+
if exists(destination_path): rmtree(destination_path)
|
|
797
|
+
if file_name != '':
|
|
798
|
+
makedirs(destination_path)
|
|
799
|
+
for current_file in listdir(folder_path):
|
|
800
|
+
if splitext(current_file)[0] == file_name: copy2(join(folder_path, current_file), destination_path)
|
|
801
|
+
else: copytree(folder_path, destination_path)
|
|
802
|
+
self.__copy_folder_to_temp = destination_path
|
|
803
|
+
return destination_path
|
|
804
|
+
if self.scn_architecture == 'level_2' and self.__sapiens_model: del self.__sapiens_model
|
|
805
|
+
elif self.scn_architecture == 'level_3': self.__sapiens_model.close()
|
|
806
|
+
model_path = _copy_folder_to_temp(folder_path=self.__model_path)
|
|
807
|
+
if self.scn_architecture == 'level_2': self.__sapiens_model = self.__scnetwork(device=self.device, user_id=self.user_id, parallel_prediction=self.parallel_prediction, minimum_probability_for_candidates=self.minimum_probability_for_candidates, show_errors=self.show_errors)
|
|
808
|
+
elif self.scn_architecture == 'level_3': self.__sapiens_model = self.__scn(device=self.device, user_id=self.user_id, parallel_prediction=self.parallel_prediction, minimum_probability_for_candidates=self.minimum_probability_for_candidates, show_errors=self.show_errors)
|
|
809
|
+
model_name = Path(self.__model_path).name
|
|
810
|
+
model_path = join(model_path, model_name)
|
|
811
|
+
self.__sapiens_model.loadModel(model_path=model_path, progress=progress)
|
|
812
|
+
def _read_remote_file(remote_path=''):
|
|
813
|
+
remote_path = str(remote_path).strip()
|
|
814
|
+
from urllib.request import urlopen
|
|
815
|
+
try:
|
|
816
|
+
from os import environ
|
|
817
|
+
from certifi import where
|
|
818
|
+
environ['SSL_CERT_FILE'] = where()
|
|
819
|
+
from logging import getLogger, ERROR
|
|
820
|
+
getLogger('urlopen').setLevel(ERROR)
|
|
821
|
+
except: pass
|
|
822
|
+
remote_stream = urlopen(remote_path)
|
|
823
|
+
content = remote_stream.read().decode('utf-8')
|
|
824
|
+
return str(content).strip()
|
|
825
|
+
is_json, string_content = dataset_path.lower().endswith('.json'), ''
|
|
826
|
+
if dataset_path.startswith(('https://', 'http://')): string_content = _read_remote_file(remote_path=dataset_path)
|
|
827
|
+
else:
|
|
828
|
+
with open(dataset_path, 'r', encoding='utf-8') as file_object: string_content = str(file_object.read()).strip()
|
|
829
|
+
if is_json:
|
|
830
|
+
fit_structure, data = [], []
|
|
831
|
+
from utilities_nlp import UtilitiesNLP as SapiensUtilities
|
|
832
|
+
sapiens_utilities = SapiensUtilities(show_errors=self.__show_errors, display_error_point=self.__display_error_point)
|
|
833
|
+
if string_content: fit_structure = sapiens_utilities.stringToDictionaryOrJSON(string=string_content)
|
|
834
|
+
if fit_structure:
|
|
835
|
+
if type(fit_structure) == dict:
|
|
836
|
+
json_keys = list(fit_structure.keys())
|
|
837
|
+
if 'data' in json_keys: data = list(fit_structure.get('data', []))
|
|
838
|
+
else:
|
|
839
|
+
data_key = str(json_keys[0]).strip()
|
|
840
|
+
data = list(fit_structure.get(data_key, []))
|
|
841
|
+
elif type(fit_structure) in (tuple, list): data = fit_structure
|
|
842
|
+
if data:
|
|
843
|
+
from tqdm import tqdm
|
|
844
|
+
total_length = len(data)
|
|
845
|
+
with tqdm(total=total_length, unit='item', disable=not progress) as progress_bar:
|
|
846
|
+
for input_output in data:
|
|
847
|
+
_input, _output, _file_path = '', '', ''
|
|
848
|
+
if input_output and type(input_output) == dict:
|
|
849
|
+
json_keys = list(input_output.keys())
|
|
850
|
+
if 'input' in json_keys: _input = str(input_output.get('input', '')).strip()
|
|
851
|
+
elif 'Input' in json_keys: _input = str(input_output.get('Input', '')).strip()
|
|
852
|
+
elif 'INPUT' in json_keys: _input = str(input_output.get('INPUT', '')).strip()
|
|
853
|
+
elif 'question' in json_keys: _input = str(input_output.get('question', '')).strip()
|
|
854
|
+
elif 'Question' in json_keys: _input = str(input_output.get('Question', '')).strip()
|
|
855
|
+
elif 'QUESTION' in json_keys: _input = str(input_output.get('QUESTION', '')).strip()
|
|
856
|
+
elif 'prompt' in json_keys: _input = str(input_output.get('prompt', '')).strip()
|
|
857
|
+
elif 'Prompt' in json_keys: _input = str(input_output.get('Prompt', '')).strip()
|
|
858
|
+
elif 'PROMPT' in json_keys: _input = str(input_output.get('PROMPT', '')).strip()
|
|
859
|
+
if 'output' in json_keys: _output = str(input_output.get('output', '')).strip()
|
|
860
|
+
elif 'Output' in json_keys: _output = str(input_output.get('Output', '')).strip()
|
|
861
|
+
elif 'OUTPUT' in json_keys: _output = str(input_output.get('OUTPUT', '')).strip()
|
|
862
|
+
elif 'answer' in json_keys: _output = str(input_output.get('answer', '')).strip()
|
|
863
|
+
elif 'Answer' in json_keys: _output = str(input_output.get('Answer', '')).strip()
|
|
864
|
+
elif 'ANSWER' in json_keys: _output = str(input_output.get('ANSWER', '')).strip()
|
|
865
|
+
elif 'response' in json_keys: _output = str(input_output.get('response', '')).strip()
|
|
866
|
+
elif 'Response' in json_keys: _output = str(input_output.get('Response', '')).strip()
|
|
867
|
+
elif 'RESPONSE' in json_keys: _output = str(input_output.get('RESPONSE', '')).strip()
|
|
868
|
+
if 'file_path' in json_keys: _file_path = str(input_output.get('file_path', '')).strip()
|
|
869
|
+
elif 'File_path' in json_keys: _file_path = str(input_output.get('File_path', '')).strip()
|
|
870
|
+
elif 'FILE_PATH' in json_keys: _file_path = str(input_output.get('FILE_PATH', '')).strip()
|
|
871
|
+
if not _input: _input = str(input_output[json_keys[0]]).strip()
|
|
872
|
+
if not _output: _output = str(input_output[json_keys[1]]).strip()
|
|
873
|
+
if _input and _output:
|
|
874
|
+
if self.__sub_architecture == 'hur':
|
|
875
|
+
try:
|
|
876
|
+
if self.epochs: self.__sapiens_model.addFit(prompt=_input, answer=_output, file_path=_file_path)
|
|
877
|
+
else: self.__sapiens_model.addSemanticFit(prompt=_input, answer=_output, file_path=_file_path, precision=self.precision)
|
|
878
|
+
except: self.__sapiens_model.addFit(prompt=_input, answer=_output)
|
|
879
|
+
else: self.__sapiens_model.addFit(prompt=_input, answer=_output)
|
|
880
|
+
if progress:
|
|
881
|
+
progress_bar.set_description('Adjusting')
|
|
882
|
+
progress_bar.update(1)
|
|
883
|
+
adjusted_model = total_length > 0
|
|
884
|
+
elif self.__show_errors:
|
|
885
|
+
print('The file must be a JSON with a "data" key containing an array of objects with the keys "input" and "output".')
|
|
886
|
+
adjusted_model = False
|
|
887
|
+
if self.__model_path and self.__sub_architecture == 'scn' and self.scn_architecture in ('level_2', 'level_3'):
|
|
888
|
+
if self.scn_architecture == 'level_2' and self.__sapiens_model:
|
|
889
|
+
del self.__sapiens_model
|
|
890
|
+
self.__sapiens_model = self.__scnetwork(device=self.device, user_id=self.user_id, parallel_prediction=self.parallel_prediction, minimum_probability_for_candidates=self.minimum_probability_for_candidates, show_errors=self.show_errors)
|
|
891
|
+
elif self.scn_architecture == 'level_3': self.__sapiens_model.close()
|
|
892
|
+
elif self.__sub_architecture == 'hur':
|
|
893
|
+
self.__sapiens_model.EPOCHS = self.epochs if self.epochs else 1000
|
|
894
|
+
self.__sapiens_model.train(precision=self.precision)
|
|
895
|
+
return adjusted_model
|
|
896
|
+
except Exception as error:
|
|
897
|
+
try:
|
|
898
|
+
if self.__show_errors:
|
|
899
|
+
error_message = 'ERROR in SapiensModel.fineTuning: '+str(error)
|
|
900
|
+
print(error_message)
|
|
901
|
+
try: self.__print_exc() if self.__display_error_point else None
|
|
902
|
+
except: pass
|
|
903
|
+
except: pass
|
|
904
|
+
return False
|
|
905
|
+
def inference(self, prompt='', file_path='', stream=False):
|
|
906
|
+
try:
|
|
907
|
+
prompt, file_path, inference_function = str(prompt).strip(), str(file_path).strip(), ''
|
|
908
|
+
stream, task_names = bool(stream) if type(stream) in (bool, int, float) else False, []
|
|
909
|
+
if not prompt: prompt = '?'
|
|
910
|
+
def _get_tokens(inference_function='', stream=False, generator=False):
|
|
911
|
+
if generator:
|
|
912
|
+
if stream: return inference_function
|
|
913
|
+
else:
|
|
914
|
+
tokens = []
|
|
915
|
+
for token in inference_function: tokens.append(token)
|
|
916
|
+
inference_function = str(''.join(tokens)).strip()
|
|
917
|
+
return inference_function
|
|
918
|
+
else:
|
|
919
|
+
tokens = inference_function.split(chr(32))
|
|
920
|
+
def __get_tokens(tokens=[]):
|
|
921
|
+
def ___capitalize_first_letter_preserve_rest(text=''):
|
|
922
|
+
if not text: return text
|
|
923
|
+
return text[0].upper() + text[1:]
|
|
924
|
+
tokens_length = len(tokens)
|
|
925
|
+
for index, token in enumerate(tokens):
|
|
926
|
+
counter = index + 1
|
|
927
|
+
if counter <= 1: token = ___capitalize_first_letter_preserve_rest(text=token.lstrip())
|
|
928
|
+
if 1 <= counter < tokens_length: token = token + chr(32)
|
|
929
|
+
if counter == tokens_length: token = token.rstrip()
|
|
930
|
+
yield token
|
|
931
|
+
if stream: return __get_tokens(tokens=tokens)
|
|
932
|
+
else: return inference_function
|
|
933
|
+
if self.mathematical_automation:
|
|
934
|
+
mathematical_solution = self.__mathematical_solution(prompt=prompt)
|
|
935
|
+
if mathematical_solution: inference_function = mathematical_solution
|
|
936
|
+
if not inference_function and self.online_consultation:
|
|
937
|
+
online_consultation = self.__get_online_answer(prompt=prompt)
|
|
938
|
+
if online_consultation: inference_function = online_consultation
|
|
939
|
+
inference_function = str(inference_function).strip()
|
|
940
|
+
if inference_function: return _get_tokens(inference_function=inference_function, stream=stream, generator=False)
|
|
941
|
+
from utilities_nlp import UtilitiesNLP as SapiensUtilities
|
|
942
|
+
sapiens_utilities = SapiensUtilities(show_errors=False, display_error_point=False)
|
|
943
|
+
task_names = sapiens_utilities.getTasks(prompt=prompt)
|
|
944
|
+
from pathlib import Path
|
|
945
|
+
if file_path and not Path(file_path).is_file():
|
|
946
|
+
if self.__show_errors: print(f'The referenced file "{file_path}" DOES NOT EXIST.')
|
|
947
|
+
return False
|
|
948
|
+
elif file_path and self.__sub_architecture != 'hur':
|
|
949
|
+
from INFINITE_CONTEXT_WINDOW import InfiniteContextWindow as SAPIENS_INFINITE_CONTEXT_WINDOW
|
|
950
|
+
interpreted_file = SAPIENS_INFINITE_CONTEXT_WINDOW(show_errors=self.show_errors, display_error_point=self.display_error_point).interpreter(file_path=file_path)
|
|
951
|
+
prompt = rf'{interpreted_file}\n\n{prompt}'
|
|
952
|
+
authorized_task = False
|
|
953
|
+
def _capitalize_sentences(text=''):
|
|
954
|
+
result = []
|
|
955
|
+
capitalize_next = True
|
|
956
|
+
for character in text:
|
|
957
|
+
if capitalize_next and character.isalpha():
|
|
958
|
+
result.append(character.upper())
|
|
959
|
+
capitalize_next = False
|
|
960
|
+
else: result.append(character)
|
|
961
|
+
if character in '.!?': capitalize_next = True
|
|
962
|
+
return ''.join(result)
|
|
963
|
+
if task_names and 'TEXT_SUMMARY' in task_names and self.summary_automation: inference_function, authorized_task = sapiens_utilities.summary(string=prompt, topics=False), True
|
|
964
|
+
elif task_names and 'TEXT_SUMMARY_WITH_BULLET_POINTS' in task_names and self.summary_automation: inference_function, authorized_task = sapiens_utilities.summary(string=prompt, topics=True), True
|
|
965
|
+
elif task_names and any(task.startswith('TRANSLATION_') for task in task_names) and self.translation_automation:
|
|
966
|
+
target_language = 'en'
|
|
967
|
+
for task in task_names:
|
|
968
|
+
if task.startswith('TRANSLATION_'):
|
|
969
|
+
target_language = task.split('TRANSLATION_')[-1].lower().strip()
|
|
970
|
+
break
|
|
971
|
+
inference_function, authorized_task = sapiens_utilities.translate(string=prompt, source_language='auto', target_language=target_language), True
|
|
972
|
+
if authorized_task: inference_function = _capitalize_sentences(sapiens_utilities.formatPrompt(prompt=inference_function, task_names=task_names))
|
|
973
|
+
if inference_function: return _get_tokens(inference_function=inference_function, stream=stream, generator=False)
|
|
974
|
+
if not self.__sapiens_model:
|
|
975
|
+
if self.__show_errors: print('This model has not yet been trained.')
|
|
976
|
+
return False
|
|
977
|
+
if self.__sub_architecture == 'cpu':
|
|
978
|
+
inference_function = self.__sapiens_model.infer(prompt=prompt, temperature=self.temperature, max_tokens=self.max_tokens)
|
|
979
|
+
return _get_tokens(inference_function=inference_function, stream=stream, generator=True)
|
|
980
|
+
elif self.__sub_architecture == 'scn':
|
|
981
|
+
if self.scn_architecture == 'level_1':
|
|
982
|
+
inference_function = self.__sapiens_model.predict(prompt=prompt, minimum_score=self.minimum_score, hot=self.hot, stream=False)
|
|
983
|
+
if len(self.end_tag.strip()) > 0 and self.end_tag in inference_function:
|
|
984
|
+
parts_of_the_inference = inference_function.split(self.end_tag)
|
|
985
|
+
inference_function = parts_of_the_inference[0].strip() if len(parts_of_the_inference[0].strip()) > 0 else parts_of_the_inference[-1].strip()
|
|
986
|
+
else: inference_function = self.__sapiens_model.predict(prompt=prompt, max_tokens=self.max_tokens, min_fit_probability=self.min_fit_probability, min_probability=self.min_probability, generalization=self.generalization, stream=False)['answer']
|
|
987
|
+
if self.scn_architecture == 'level_2': self.close()
|
|
988
|
+
elif self.__sub_architecture == 'hur':
|
|
989
|
+
try:
|
|
990
|
+
if not self.real_time_generator:
|
|
991
|
+
inference_function = self.__sapiens_model.predict(prompt=prompt, file_path=file_path, max_tokens=self.max_tokens, temperature=self.temperature, top_k=self.top_k, top_p=self.top_p, stream=stream)
|
|
992
|
+
return _get_tokens(inference_function=inference_function, stream=stream, generator=True)
|
|
993
|
+
else: inference_function = self.__sapiens_model.predict(prompt=prompt, file_path=file_path, max_tokens=self.max_tokens, temperature=self.temperature, top_k=self.top_k, top_p=self.top_p, stream=False)
|
|
994
|
+
except:
|
|
995
|
+
self.__sapiens_model = self.__hur(embedding_dim=self.embedding_dim, block_size=self.block_size, batch_size=self.batch_size, number_heads=self.number_heads, number_layers=self.number_layers, dropout=self.dropout, learning_rate=self.learning_rate, eval_interval=self.eval_interval, epochs=self.epochs, use_bit_net_quantization=self.use_bit_net_quantization, device=self.device)
|
|
996
|
+
inference_function = self.__sapiens_model.predict(prompt=prompt, max_tokens=self.max_tokens, temperature=self.temperature, top_k=self.top_k, top_p=self.top_p, stream=False)
|
|
997
|
+
try: self.perplexity = self.__sapiens_model.PERPLEXITY
|
|
998
|
+
except: pass
|
|
999
|
+
elif self.__sub_architecture == 'mgt':
|
|
1000
|
+
inference_function = self.__sapiens_model.infer(system=self.system, prompt=prompt, messages=self.messages, temperature=self.temperature, stream=False, sapiens_model_path=self.sapiens_model_path)
|
|
1001
|
+
if len(self.end_tag.strip()) > 0 and self.end_tag in inference_function:
|
|
1002
|
+
parts_of_the_inference = inference_function.split(self.end_tag)
|
|
1003
|
+
inference_function = parts_of_the_inference[0].strip() if len(parts_of_the_inference[0].strip()) > 0 else parts_of_the_inference[-1].strip()
|
|
1004
|
+
else: inference_function = self.__sapiens_model.predict(prompt=prompt, max_tokens=self.max_tokens, min_fit_probability=self.min_fit_probability, min_probability=self.min_probability, generalization=self.generalization, stream=False)['answer']
|
|
1005
|
+
return _get_tokens(inference_function=inference_function, stream=stream, generator=False)
|
|
1006
|
+
except Exception as error:
|
|
1007
|
+
try:
|
|
1008
|
+
if self.__show_errors:
|
|
1009
|
+
error_message = 'ERROR in SapiensModel.inference: '+str(error)
|
|
1010
|
+
print(error_message)
|
|
1011
|
+
try: self.__print_exc() if self.__display_error_point else None
|
|
1012
|
+
except: pass
|
|
1013
|
+
except: pass
|
|
1014
|
+
return ''
|
|
1015
|
+
def completeMessages(self, messages=[], stream=False):
|
|
1016
|
+
try:
|
|
1017
|
+
complete_messages = {'answer': '', 'messages': [], 'next_token': ''}
|
|
1018
|
+
messages = list(messages) if type(messages) in (tuple, list, dict) else []
|
|
1019
|
+
stream = bool(stream) if type(stream) in (bool, int, float) else False
|
|
1020
|
+
complete_messages['messages'] = messages
|
|
1021
|
+
if messages:
|
|
1022
|
+
template, file_path = '', ''
|
|
1023
|
+
if self.__sub_architecture in ('hur', 'cpu') and len(messages) > 2:
|
|
1024
|
+
if self.__sub_architecture == 'cpu':
|
|
1025
|
+
_CPUModel__fine_tuning = self.__sapiens_model._CPUModel__fine_tuning
|
|
1026
|
+
if _CPUModel__fine_tuning: template = str(messages[-1].get('content', '')).strip()
|
|
1027
|
+
else:
|
|
1028
|
+
end_tag = self.end_tag if self.end_tag else ''
|
|
1029
|
+
for message in messages:
|
|
1030
|
+
role = str(message.get('role', '')).strip()
|
|
1031
|
+
content = str(message.get('content', '')).strip()
|
|
1032
|
+
if role and content: template += f'{role.capitalize()}:\n{content}{end_tag}\n'
|
|
1033
|
+
template = template.strip()
|
|
1034
|
+
if template: file_path = str(messages[-1].get('file_path', '')).strip()
|
|
1035
|
+
else: template = str(messages[-1].get('content', '')).strip()
|
|
1036
|
+
def _get_stream(template='', complete_messages={}, file_path=''):
|
|
1037
|
+
current_answer, message = '', {'role': 'assistant', 'content': ''}
|
|
1038
|
+
complete_messages['messages'].append(message)
|
|
1039
|
+
inference_function = self.inference(prompt=template, file_path=file_path, stream=True)
|
|
1040
|
+
for token in inference_function:
|
|
1041
|
+
current_answer += token
|
|
1042
|
+
complete_messages['answer'] = current_answer
|
|
1043
|
+
complete_messages['messages'][-1]['content'] = current_answer
|
|
1044
|
+
complete_messages['next_token'] = token
|
|
1045
|
+
yield complete_messages
|
|
1046
|
+
def _get_string(template='', complete_messages={}, file_path=''):
|
|
1047
|
+
inference_function = self.inference(prompt=template, file_path=file_path, stream=False)
|
|
1048
|
+
complete_messages['answer'] = inference_function
|
|
1049
|
+
message = {'role': 'assistant', 'content': inference_function}
|
|
1050
|
+
complete_messages['messages'].append(message)
|
|
1051
|
+
token = inference_function.split(chr(32))[-1].rstrip()
|
|
1052
|
+
complete_messages['next_token'] = token
|
|
1053
|
+
return complete_messages
|
|
1054
|
+
if stream: complete_messages = _get_stream(template=template, complete_messages=complete_messages, file_path=file_path)
|
|
1055
|
+
else: complete_messages = _get_string(template=template, complete_messages=complete_messages, file_path=file_path)
|
|
1056
|
+
return complete_messages
|
|
1057
|
+
except Exception as error:
|
|
1058
|
+
try:
|
|
1059
|
+
if self.__show_errors:
|
|
1060
|
+
error_message = 'ERROR in SapiensModel.completeMessages: '+str(error)
|
|
1061
|
+
print(error_message)
|
|
1062
|
+
try: self.__print_exc() if self.__display_error_point else None
|
|
1063
|
+
except: pass
|
|
1064
|
+
except: pass
|
|
1065
|
+
return ''
|
|
1066
|
+
def printInference(self, prompt='', file_path='', stream=True):
|
|
1067
|
+
try:
|
|
1068
|
+
inference = self.inference(prompt=prompt, file_path=file_path, stream=stream)
|
|
1069
|
+
if stream:
|
|
1070
|
+
from time import sleep
|
|
1071
|
+
for token in inference:
|
|
1072
|
+
print(token, end='', flush=True)
|
|
1073
|
+
sleep(self.delay)
|
|
1074
|
+
print()
|
|
1075
|
+
else: print(inference)
|
|
1076
|
+
except Exception as error:
|
|
1077
|
+
try:
|
|
1078
|
+
if self.__show_errors:
|
|
1079
|
+
error_message = 'ERROR in SapiensModel.printInference: '+str(error)
|
|
1080
|
+
print(error_message)
|
|
1081
|
+
try: self.__print_exc() if self.__display_error_point else None
|
|
1082
|
+
except: pass
|
|
1083
|
+
except: pass
|
|
1084
|
+
def printCompleteMessages(self, messages=[], stream=True):
|
|
1085
|
+
try:
|
|
1086
|
+
inference = self.completeMessages(messages=messages, stream=stream)
|
|
1087
|
+
if stream:
|
|
1088
|
+
from time import sleep
|
|
1089
|
+
for token in inference:
|
|
1090
|
+
print(token['next_token'], end='', flush=True)
|
|
1091
|
+
sleep(self.delay)
|
|
1092
|
+
print()
|
|
1093
|
+
else: print(inference['answer'])
|
|
1094
|
+
except Exception as error:
|
|
1095
|
+
try:
|
|
1096
|
+
if self.__show_errors:
|
|
1097
|
+
error_message = 'ERROR in SapiensModel.printInference: '+str(error)
|
|
1098
|
+
print(error_message)
|
|
1099
|
+
try: self.__print_exc() if self.__display_error_point else None
|
|
1100
|
+
except: pass
|
|
1101
|
+
except: pass
|
|
1102
|
+
def close(self):
|
|
1103
|
+
try:
|
|
1104
|
+
if self.__sub_architecture == 'scn' and self.scn_architecture == 'level_3': self.__sapiens_model.close()
|
|
1105
|
+
else:
|
|
1106
|
+
try: del self.__sapiens_model
|
|
1107
|
+
except: pass
|
|
1108
|
+
return True
|
|
1109
|
+
except Exception as error:
|
|
1110
|
+
try:
|
|
1111
|
+
if self.__show_errors:
|
|
1112
|
+
error_message = 'ERROR in SapiensModel.close: '+str(error)
|
|
1113
|
+
print(error_message)
|
|
1114
|
+
try: self.__print_exc() if self.__display_error_point else None
|
|
1115
|
+
except: pass
|
|
1116
|
+
except: pass
|
|
1117
|
+
return False
|
|
1118
|
+
class SapiensArchitecture(SapiensModel): pass
|
|
1119
|
+
"""
|
|
1120
|
+
This algorithm was designed, programmed, and developed by Sapiens Technology®️ to enable the construction, training, tuning, and inference of language models using the main architectural frameworks of Sapiens Technology®️.
|
|
1121
|
+
The code includes a main class with the base architecture and several other internal sub-architectures that can be configured through the model's parameters.
|
|
1122
|
+
|
|
1123
|
+
Any changes to this code, reverse engineering, disclosure, or public commentary involving the technical aspects of the technology contained herein are strictly prohibited, and the authors will be duly prosecuted by our legal team.
|
|
1124
|
+
|
|
1125
|
+
WE DO NOT authorize the commercial use of this code without prior permission from Sapiens Technology®️.
|
|
1126
|
+
"""
|
|
1127
|
+
# --------------------------> A SAPIENS TECHNOLOGY®️ PRODUCTION) <--------------------------
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: sapiens_model
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Home-page: https://github.com/sapiens-technology/sapiens_architecture_with_sapiens_model
|
|
5
|
+
Author: SAPIENS TECHNOLOGY
|
|
6
|
+
License: Proprietary Software
|
|
7
|
+
License-File: LICENSE.txt
|
|
8
|
+
Requires-Dist: semantic-comparison-network==1.0.8
|
|
9
|
+
Requires-Dist: scnetwork==1.1.3
|
|
10
|
+
Requires-Dist: scn==3.0.1
|
|
11
|
+
Requires-Dist: hur==1.1.4
|
|
12
|
+
Requires-Dist: hur-multimodal==1.0.9
|
|
13
|
+
Requires-Dist: sapiens-cpu==4.0.1
|
|
14
|
+
Requires-Dist: mgt==2.0.2
|
|
15
|
+
Requires-Dist: sapiens-dataset==4.0.4
|
|
16
|
+
Requires-Dist: utilities-nlp==7.0.3
|
|
17
|
+
Requires-Dist: tqdm==4.67.1
|
|
18
|
+
Requires-Dist: certifi==2026.1.4
|
|
19
|
+
Requires-Dist: INFINITE-CONTEXT-WINDOW==3.0.2
|
|
20
|
+
Dynamic: author
|
|
21
|
+
Dynamic: home-page
|
|
22
|
+
Dynamic: license
|
|
23
|
+
Dynamic: requires-dist
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE.txt
|
|
2
|
+
setup.cfg
|
|
3
|
+
setup.py
|
|
4
|
+
sapiens_model/__init__.py
|
|
5
|
+
sapiens_model/sapiens_model.py
|
|
6
|
+
sapiens_model.egg-info/PKG-INFO
|
|
7
|
+
sapiens_model.egg-info/SOURCES.txt
|
|
8
|
+
sapiens_model.egg-info/dependency_links.txt
|
|
9
|
+
sapiens_model.egg-info/requires.txt
|
|
10
|
+
sapiens_model.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
sapiens_model
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This algorithm was designed, programmed, and developed by Sapiens Technology®️ to enable the construction, training, tuning, and inference of language models using the main architectural frameworks of Sapiens Technology®️.
|
|
3
|
+
The code includes a main class with the base architecture and several other internal sub-architectures that can be configured through the model's parameters.
|
|
4
|
+
|
|
5
|
+
Any changes to this code, reverse engineering, disclosure, or public commentary involving the technical aspects of the technology contained herein are strictly prohibited, and the authors will be duly prosecuted by our legal team.
|
|
6
|
+
|
|
7
|
+
WE DO NOT authorize the commercial use of this code without prior permission from Sapiens Technology®️.
|
|
8
|
+
"""
|
|
9
|
+
# --------------------------> A SAPIENS TECHNOLOGY®️ PRODUCTION) <--------------------------
|
|
10
|
+
from setuptools import setup, find_packages
|
|
11
|
+
package_name = 'sapiens_model'
|
|
12
|
+
version = '1.0.0'
|
|
13
|
+
setup(
|
|
14
|
+
name=package_name,
|
|
15
|
+
version=version,
|
|
16
|
+
author='SAPIENS TECHNOLOGY',
|
|
17
|
+
packages=find_packages(),
|
|
18
|
+
install_requires=[
|
|
19
|
+
'semantic-comparison-network==1.0.8',
|
|
20
|
+
'scnetwork==1.1.3',
|
|
21
|
+
'scn==3.0.1',
|
|
22
|
+
'hur==1.1.4',
|
|
23
|
+
'hur-multimodal==1.0.9',
|
|
24
|
+
'sapiens-cpu==4.0.1',
|
|
25
|
+
'mgt==2.0.2',
|
|
26
|
+
'sapiens-dataset==4.0.4',
|
|
27
|
+
'utilities-nlp==7.0.3',
|
|
28
|
+
'tqdm==4.67.1',
|
|
29
|
+
'certifi==2026.1.4',
|
|
30
|
+
'INFINITE-CONTEXT-WINDOW==3.0.2'
|
|
31
|
+
],
|
|
32
|
+
url='https://github.com/sapiens-technology/sapiens_architecture_with_sapiens_model',
|
|
33
|
+
license='Proprietary Software'
|
|
34
|
+
)
|
|
35
|
+
"""
|
|
36
|
+
This algorithm was designed, programmed, and developed by Sapiens Technology®️ to enable the construction, training, tuning, and inference of language models using the main architectural frameworks of Sapiens Technology®️.
|
|
37
|
+
The code includes a main class with the base architecture and several other internal sub-architectures that can be configured through the model's parameters.
|
|
38
|
+
|
|
39
|
+
Any changes to this code, reverse engineering, disclosure, or public commentary involving the technical aspects of the technology contained herein are strictly prohibited, and the authors will be duly prosecuted by our legal team.
|
|
40
|
+
|
|
41
|
+
WE DO NOT authorize the commercial use of this code without prior permission from Sapiens Technology®️.
|
|
42
|
+
"""
|
|
43
|
+
# --------------------------> A SAPIENS TECHNOLOGY®️ PRODUCTION) <--------------------------
|