Skip to content

Instantly share code, notes, and snippets.

@Hirosaji
Last active February 24, 2020 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hirosaji/5cb8bf884b466e5a35b2c9d12489454b to your computer and use it in GitHub Desktop.
Save Hirosaji/5cb8bf884b466e5a35b2c9d12489454b to your computer and use it in GitHub Desktop.
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