{"id":20082930,"url":"https://github.com/carrychang/litnlp","last_synced_at":"2025-05-06T01:30:45.974Z","repository":{"id":48905953,"uuid":"273849611","full_name":"CarryChang/litNlp","owner":"CarryChang","description":"litNlp 是基于 Tensorflow2.0 实现的一个轻量级的深度情感极性分析库，可以实现细粒度的多级别情感极性训练和预测，是搭建情感分析和文本分类模型的快速方案，具体应用如：民宿顾客意见挖掘，见右边的链接。","archived":false,"fork":false,"pushed_at":"2023-05-22T09:46:16.000Z","size":83929,"stargazers_count":17,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-24T18:52:00.573Z","etag":null,"topics":["litnlp","nlp","sentiment-analysis"],"latest_commit_sha":null,"homepage":"https://github.com/CarryChang/Customer_Satisfaction_Analysis","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CarryChang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-21T06:40:42.000Z","updated_at":"2024-11-20T05:35:37.000Z","dependencies_parsed_at":"2024-11-13T15:49:08.855Z","dependency_job_id":"3fff100e-e66a-4cc7-b054-309cb4d886cd","html_url":"https://github.com/CarryChang/litNlp","commit_stats":{"total_commits":58,"total_committers":3,"mean_commits":"19.333333333333332","dds":0.08620689655172409,"last_synced_commit":"548876e3faf9841afa9aaaf4896d338ffe0199e6"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CarryChang%2FlitNlp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CarryChang%2FlitNlp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CarryChang%2FlitNlp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CarryChang%2FlitNlp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CarryChang","download_url":"https://codeload.github.com/CarryChang/litNlp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252604164,"owners_count":21775058,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["litnlp","nlp","sentiment-analysis"],"created_at":"2024-11-13T15:45:15.387Z","updated_at":"2025-05-06T01:30:45.103Z","avatar_url":"https://github.com/CarryChang.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/CarryChang/litNlp/blob/master/pic/logo.png\"\u003e\u003cbr\u003e\n\u003c/div\u003e\n\n-----------------\n## litNlp: A Fast Tool for Sentiment Analysis with Tensorflow2\n[![996.icu](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu)\n[![PyPI Latest Release](https://img.shields.io/pypi/v/litNlp.svg)](https://pypi.org/project/litNlp/)\n[![Downloads](https://pepy.tech/badge/litnlp)](https://pepy.tech/project/litnlp)\n[![Downloads](https://pepy.tech/badge/litnlp/month)](https://pepy.tech/project/litnlp/month)\n[![Downloads](https://pepy.tech/badge/litnlp/week)](https://pepy.tech/project/litnlp/week)\n\n\n# litNlp 简介\n\nlitNlp 是兼容最新版 Tensorflow 2.0 实现的一个轻量级的深度情感极性推理模型，使用字符级代替词语级进一步提升训练和推理速度，可以实现细粒度的多级别情感极性训练和预测，TF2 下 GPU 和 CPU 平台都能直接安装运行，是搭建 NLP 情感分析和分类模型 Baseline 的快速方案。\n\n\t1. 内置深度学习情感分析模型。\n\t2. 直接提供模型训练，默认 Text-CNN 字符级卷积网络作为 baseline ，自带早停操作，使用少的参数即可开始训练多分类模型。\n\t3. 使用 Streamlit 快速对模型进行 UI 演示。\n\t4. 增加 TF Serving 的转化和部署。\n\t5. 增加 docker-compose up 的启动方式\n\n\n## 直接使用 emample/sa_ui.py 进行前端 ui 展示效果\n\n```python\n    # 安装 streamlit 之后直接运行脚本\n    streamlit run sa_ui.py\n```\n\n\u003cdiv align=center\u003e\u003cimg  src=\"https://github.com/CarryChang/litNlp/blob/master/pic/ui.png\"\u003e\u003c/div\u003e\n\n## 使用方法\n\u003e 1. pip install  litNlp\n\u003e 2. 模型需要先通过训练，保存在 sa_model 里面，然后就可以批预测，具体的使用见 example 文件内容\n\n```python\n\tfrom litNlp.predict import SA_Model_Predict\n\timport numpy as np\n\t\n\t# 加载模型的字典项\n\ttokenize_path = 'model/tokenizer.pickle'\n\t# train_method : 模型训练方式，默认 textcnn ，可选：bilstm , gru\n\ttrain_method = 'textcnn'\n\t# 模型的保存位置，后续用于推理\n\tsa_model_path_m = 'model/{}.h5'.format(train_method)\n\t# 开始输入待测样例\n\tpredict_text = ['这个我不喜欢', '这个我喜欢不']\n\t# 加载模型\n\tmodel = SA_Model_Predict(tokenize_path, sa_model_path_m, max_len=100)\n\t# 开始推理\n\tsa_score = model.predict(predict_text)\n\t# 情感极性概率\n\tprint(np.asarray(sa_score)[:,1])\n\t# 情感label输出\n\tprint(np.argmax(np.asarray(sa_score), axis=1))\n\n```\n\n## 参数解释\n```python\n    # 最大句子长度\n    maxlen = 100\n    # 最大的tokenizer字典长度\n    max_words = 1000\n    # 设置embedding大小\n    embedding_dim = 300\n    # 模型的保存位置，后续用于推理\n    sa_model_path_m = 'sa_model/c_cnn_m.h5'\n    # 离线保存tokenizer\n    tokenize_path ='sa_model/tokenizer.pickle'\n    # 分类的类别数\n    num_classes = 2\n    # train_method : 模型训练方式，默认textcnn，可选：bilstm, gru\n    train_method = 'textcnn'\n```\n\n## 2 个 epoch 的二分类性能\n\n\u003cdiv align=center\u003e\u003cimg  src=\"https://github.com/CarryChang/litNlp/blob/master/pic/auc_2poch.png\"\u003e\u003c/div\u003e\n\n## jupyter 实验\n\n\u003e  情感分析，优化语义的情感推理\n\u003cdiv align=center\u003e\u003cimg  src=\"https://github.com/CarryChang/litNlp/blob/master/pic/tools.png\"\u003e\u003c/div\u003e\n\n## Flask Gunicorn 模型部署\npython sa_server.py 即可对训练的情感分析模型进行部署，模型首次推理需要预热，后续推理耗时在 200ms 之内。\n\n\u003cdiv align=center\u003e\u003cimg  src=\"https://github.com/CarryChang/litNlp/blob/master/pic/server.png\"\u003e\u003c/div\u003e\n\n## Tensorflow Serving 模型部署\n\n利用 python example/sa_model2tf_serving_model.py 进行模型转换之后即可直接进行 TF Serving 的服务部署。\n\n首先拉取对应版本的 TF Serving Docker\n\n    docker pull tensorflow/serving:2.3.0\n    \n直接利用 Docker 加载转换之后的模型即可完成模型部署，TensorFlow Serving 会自动选择版本号最大的模型进行载入。\n\nDocker 命令行的 Dev 启动模式\n\n    docker run -t --rm -p 9500:8500 -p:9501:8501 \\\n    -v \"$(pwd)/tf_model/:/models/textcnn\" \\\n    -e MODEL_NAME=textcnn -tensorflow_inter_op_parallelism=4 \\\n    tensorflow/serving:2.3.0\n \n\nDocker 命令行的 Pro 启动模式\n\n    docker run -d --rm -p 9500:8500 -p:9501:8501 \\\n    -v \"$(pwd)/tf_model/:/models/textcnn\" \\\n    -e MODEL_NAME=textcnn -tensorflow_inter_op_parallelism=4 \\\n    tensorflow/serving:2.3.0\n\n或者在 yml 所在的文件夹下增加直接使用 docker-compose up 进行服务的启动。\n\n服务请求： \n部署之后使用 python sa_tf_serving_api_client.py 进行 TF serving 服务的调用。\n \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcarrychang%2Flitnlp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcarrychang%2Flitnlp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcarrychang%2Flitnlp/lists"}