实用模型推荐(一)相似度,文本向量化:text2vec-base-chinese

1.开源地址:https://github.com/shibing624/text2vec

2.使用场景:文本相似度计算,文本转指令

3.API封装:

import uvicorn
from fastapi import FastAPI
from loguru import logger
from pydantic import BaseModel
from starlette.middleware.cors import CORSMiddleware
from text2vec import Similarity

sim_model = Similarity("shibing624/text2vec-base-chinese")
# define the app
app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"]
)


@app.get('/')
async def index():
    return {"message": "index, docs url: /docs"}


class InputData(BaseModel):
    sentences1: list
    sentences2: list


@app.post('/emb')
async def emb(data: InputData):
    try:
        scores = sim_model.get_scores(data.sentences1, data.sentences2)
        
        result_list = []
        for i in range(len(data.sentences1)):
            for j in range(len(data.sentences2)):
                result_list.append(float(scores[i][j]))
        
        result_dict = {'emb': result_list}
        return result_dict
    except Exception as e:
        logger.error(e)
        return {'status': False, 'msg': str(e)}, 400



if __name__ == '__main__':
    uvicorn.run(app=app, host='0.0.0.0', port=11020)

 

posted @ 2023-06-27 11:27  AmbitiousMice  阅读(2849)  评论(0编辑  收藏  举报