You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.0 KiB
35 lines
1.0 KiB
# -*- coding:utf-8 -*-
|
|
# @Time: 2023/8/22 14:44
|
|
# @Author:ZYP
|
|
# @File:fasttext_api.py
|
|
# @mail:zypsunshine1@gmail.com
|
|
# @Software: PyCharm
|
|
import json
|
|
|
|
import numpy as np
|
|
from gensim.models.keyedvectors import KeyedVectors
|
|
import time
|
|
from flask import Flask, request
|
|
|
|
app_fasttext = Flask(__name__)
|
|
|
|
fasttext_path = '/home/zc-nlp-zyp/work_file/ssd_data/public_data/fasttext_model/fasttext.vector'
|
|
model_fasttext = KeyedVectors.load_word2vec_format(fasttext_path, binary=True)
|
|
|
|
|
|
@app_fasttext.route('/fasttext', methods=['POST'])
|
|
def get_word2vec():
|
|
word_dict = json.loads(request.data.decode())
|
|
try:
|
|
vec = model_fasttext.get_vector(word_dict["word"])
|
|
str_vec = ','.join([str(i) for i in vec])
|
|
# vec1 = np.array([float(j) for j in str_vec.split(',')], dtype=np.float64)
|
|
vec_dict = {'vec': str_vec}
|
|
return json.dumps(vec_dict)
|
|
except:
|
|
return 'error_fasttext'
|
|
|
|
|
|
# if __name__ == '__main__':
|
|
# app.run(host='0.0.0.0', port=50002, debug=False)
|
|
|
|
|