alg_app.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """
  2. @author: luojunhui
  3. """
  4. import jieba
  5. from quart import Quart
  6. from text2vec import Word2Vec
  7. from similarities import BertSimilarity
  8. from routes import AlgRoutes
  9. from applications import AsyncMySQLClient
  10. from applications.embedding_manager import EmbeddingManager
  11. jieba.initialize()
  12. print("jieba初始化成功")
  13. app = Quart(__name__)
  14. AsyncMySQL = AsyncMySQLClient(app)
  15. @app.before_serving
  16. async def init():
  17. """
  18. 初始化模型
  19. """
  20. await AsyncMySQL.init_pool()
  21. similarity_model = BertSimilarity(model_name_or_path="BAAI/bge-large-zh-v1.5")
  22. embedding_manager = EmbeddingManager(similarity_model)
  23. print("相似度模型加载成功")
  24. word2vec_model = Word2Vec("lili666/text2vec-word2vec-tencent-chinese")
  25. print("词向量模型加载成功")
  26. app_routes = AlgRoutes(AsyncMySQL, similarity_model, word2vec_model, embedding_manager)
  27. app.register_blueprint(app_routes)
  28. @app.after_serving
  29. async def close_db():
  30. """
  31. 关闭连接
  32. :return:
  33. """
  34. await AsyncMySQL.close_pool()
  35. if __name__ == '__main__':
  36. app.run()