Last active
February 24, 2020 09:57
-
-
Save Hirosaji/5cb8bf884b466e5a35b2c9d12489454b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class convert_to_simlarity: | |
def __init__(self): | |
self.output = {"target": None, "texts": None} | |
def from_texts(self, target, texts): | |
self.output["target"] = target | |
self.output["texts"] = texts | |
self.output["sims"] = self.texts2similarity() | |
return self.output | |
def texts2similarity(self): | |
# get futures per sentence | |
body = [self.output["target"]] + self.output["texts"] | |
raw_features = get_futures(BERT_PRAMS, body) | |
# extract "[CLS]" features | |
cls_features = [] | |
for raw_feature in raw_features: | |
cls_feature = list( | |
filter( | |
lambda layer: | |
layer["token"] == "[CLS]", | |
raw_feature["features"] | |
) | |
) | |
cls_features.append(cls_feature[0]) | |
# compute cosine simlarity | |
simlarities = calc_simlarity(cls_features[0], cls_features[1:]) | |
return simlarities | |
req = convert_to_simlarity() | |
################ | |
### omission ### | |
################ | |
application = Flask(__name__) | |
application.add_url_rule( | |
"/sim", | |
"similarity", | |
( | |
lambda: jsonify( | |
{ | |
"type": "sentence similarity", | |
"context": req.from_texts( | |
request.get_json()["target"], | |
request.get_json()["texts"], | |
), | |
} | |
) | |
), | |
methods=["POST"], | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment