Add framework for inter-version compatibility

This commit is contained in:
Samuel Sloniker 2021-10-27 18:22:52 -07:00
parent 91e35a0d61
commit a5d7143a13
4 changed files with 24 additions and 3 deletions

View File

@ -2,3 +2,4 @@
from gptc.compiler import compile
from gptc.classifier import Classifier
from gptc.exceptions import *

View File

@ -1,4 +1,5 @@
import gptc.tokenizer
import gptc.tokenizer, gptc.compiler, gptc.exceptions
import warnings
class Classifier:
"""A text classifier.
@ -16,7 +17,22 @@ class Classifier:
"""
def __init__(self, model):
try:
model_version = model['__version__']
except:
model_version = 1
if model_version == 1:
self.model = model
else:
# The model is an unsupported version
try:
raw_model = model['__raw__']
except:
raise gptc.exceptions.UnsupportedModelError('this model is unsupported and does not contain a raw model for recompiling')
warnings.warn("model needed to be recompiled on-the-fly; please re-compile it and use the new compiled model in the future")
self.model = gptc.compiler.compile(raw_model)
def classify(self, text):
"""Classify text.
@ -34,6 +50,7 @@ class Classifier:
"""
model = self.model
text = gptc.tokenizer.tokenize(text)
probs = {}
for word in text:

View File

@ -42,4 +42,7 @@ def compile(raw_model):
except KeyError:
word_weights[word] = {category:value}
word_weights['__version__'] = 1
word_weights['__raw__'] = raw_model
return word_weights

File diff suppressed because one or more lines are too long