-
Notifications
You must be signed in to change notification settings - Fork 0
/
localhost_flask_embeddings.py
61 lines (51 loc) · 1.64 KB
/
localhost_flask_embeddings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask
from flask import request
from flask import redirect, url_for, jsonify
from flask_cors import CORS, cross_origin
from gensim.models.keyedvectors import KeyedVectors
w2v_bin_path = '/home/DATA/Biomedical/other/BiomedicalWordEmbeddings/binary/biomedical-vectors-200.bin'
wv = KeyedVectors.load_word2vec_format(w2v_bin_path, binary=True)
print("Let's get started")
def get_vecs(words):
wds, vecs = [], []
for w in words:
if w in wv:
vec = wv[w].tolist()
vecs.append(vec)
wds.append(w)
return wds, vecs
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
@app.route('/get_bioasq_w2v_embeds', methods=['GET', 'POST'])
@cross_origin()
def hello():
try:
app.logger.debug('JOSN received')
app.logger.debug(request.json)
if(request.json):
mydata = request.json
if('tokens' in mydata):
t = get_vecs(mydata['tokens'])
ret = {
'toks': t[0],
'embeds': t[1]
}
else:
ret = {}
return jsonify(ret)
except Exception as e:
app.logger.debug(e.message)
if __name__ == '__main__':
app.run(port=1234, debug=True)
'''
import urllib2, json
from pprint import pprint
data = {'tokens' : ['hello darkness my old friend ...'.split()]}
req = urllib2.Request('http://localhost:1234/get_bioasq_w2v_embeds')
req.add_header('Content-Type', 'applications/json')
response = urllib2.urlopen(req, json.dumps(data))
pprint(response)
'''