반응형
Recent Posts
Recent Comments
관리 메뉴

개발잡부

[900gle] Text embedding API 본문

900gle shopping/api

[900gle] Text embedding API

닉의네임 2022. 1. 19. 18:33
반응형

텍스트를 vector로 변환해주는 API

 

 

개발환경

  • anaconda 
  • python 3.7
  • tensorflow 2.x

 

 

 

  • Request parameter
key value
keyword String

Respose 

key value
vendors 512차원 백터
# import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text
import kss, numpy
import json

from flask import Flask, request
from flask_restful import reqparse, abort, Api, Resource

app = Flask(__name__)
api = Api(app)


def getTextVectors(input):
    vectors = module(input)
    return [vector.numpy().tolist() for vector in vectors]
#
class NumpyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, numpy.ndarray):
            return obj.tolist()
        return json.JSONEncoder.default(self, obj)

class Vector(Resource):
    def post(self):
        try:
            parser = reqparse.RequestParser()
            parser.add_argument('keyword', type=str)
            args = parser.parse_args()
            _keyword = args['keyword']
            return {'vectors': json.dumps(getTextVectors(_keyword)[0], cls=NumpyEncoder)}
        except Exception as e:
            return {'error': str(e)}
api.add_resource(Vector, '/vectors')

if __name__ == '__main__':
    module = hub.load("https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/3")
    app.run(debug=True)

 

Start

(base) ➜  ~ conda activate 900gle

cd project/900gle/python/tf-embeddings/api
python api/app.py

 

반응형

'900gle shopping > api' 카테고리의 다른 글

[900] Java High Level REST Client Query 3  (0) 2022.01.27
[900gle] Java High Level REST Client Query 2  (0) 2022.01.26
[900gle] Search Template  (0) 2022.01.25
[900] Java High Level REST Client Query  (0) 2022.01.24
[900] Search API  (0) 2022.01.19
Comments