From 497e3ceab079bb578b84ff409e023f4db5c1877a Mon Sep 17 00:00:00 2001 From: jm12138 <2286040843@qq.com> Date: Thu, 29 Dec 2022 10:27:33 +0800 Subject: [PATCH] update lac (#2187) --- modules/text/lexical_analysis/lac/README.md | 9 +- .../text/lexical_analysis/lac/ahocorasick.py | 1 - modules/text/lexical_analysis/lac/custom.py | 2 - modules/text/lexical_analysis/lac/module.py | 11 ++- .../text/lexical_analysis/lac/processor.py | 1 - modules/text/lexical_analysis/lac/test.py | 96 +++++++------------ 6 files changed, 53 insertions(+), 67 deletions(-) diff --git a/modules/text/lexical_analysis/lac/README.md b/modules/text/lexical_analysis/lac/README.md index 2633a8d9..79f9df09 100644 --- a/modules/text/lexical_analysis/lac/README.md +++ b/modules/text/lexical_analysis/lac/README.md @@ -245,7 +245,8 @@ - 关于PaddleHub Serving更多信息参考:[服务部署](../../../../docs/docs_ch/tutorial/serving.md) - +- ### Gradio APP 支持 + 从 PaddleHub 2.3.1 开始支持使用链接 http://127.0.0.1:8866/gradio/lac 在浏览器中访问 lac 的 Gradio APP。 ## 五、更新历史 @@ -287,6 +288,10 @@ 移除 fluid api +* 2.4.0 + + 添加 Gradio APP 支持 + - ```shell - $ hub install lac==2.3.0 + $ hub install lac==2.4.0 ``` diff --git a/modules/text/lexical_analysis/lac/ahocorasick.py b/modules/text/lexical_analysis/lac/ahocorasick.py index efa35c8c..6637442b 100644 --- a/modules/text/lexical_analysis/lac/ahocorasick.py +++ b/modules/text/lexical_analysis/lac/ahocorasick.py @@ -1,4 +1,3 @@ -# -*- coding: UTF-8 -*- """ 本模块实现AC自动机封装为Ahocorasick类,用于进行词典的多模匹配。 """ diff --git a/modules/text/lexical_analysis/lac/custom.py b/modules/text/lexical_analysis/lac/custom.py index 88a62b4b..97ed3c98 100644 --- a/modules/text/lexical_analysis/lac/custom.py +++ b/modules/text/lexical_analysis/lac/custom.py @@ -1,8 +1,6 @@ -# -*- coding: UTF-8 -*- """ 该模块实现用户自定义词典的功能 """ - from io import open from .ahocorasick import Ahocorasick diff --git a/modules/text/lexical_analysis/lac/module.py b/modules/text/lexical_analysis/lac/module.py index 95f5eece..c67ed49b 100644 --- a/modules/text/lexical_analysis/lac/module.py +++ b/modules/text/lexical_analysis/lac/module.py @@ -1,4 +1,3 @@ -# -*- coding:utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function @@ -33,7 +32,7 @@ class DataFormatError(Exception): @moduleinfo( name="lac", - version="2.3.0", + version="2.4.0", summary= "Baidu's open-source lexical analysis tool for Chinese, including word segmentation, part-of-speech tagging & named entity recognition", author="baidu-nlp", @@ -412,3 +411,11 @@ class LAC: raise DataFormatError return input_data + + def create_gradio_app(self): + import gradio as gr + return gr.Interface(self.cut, + gr.Text(label='text'), + gr.JSON(label='results'), + title='lac', + allow_flagging='never') diff --git a/modules/text/lexical_analysis/lac/processor.py b/modules/text/lexical_analysis/lac/processor.py index ce9bc5bd..4882c312 100644 --- a/modules/text/lexical_analysis/lac/processor.py +++ b/modules/text/lexical_analysis/lac/processor.py @@ -1,4 +1,3 @@ -# -*- coding:utf-8 -*- import io import numpy as np diff --git a/modules/text/lexical_analysis/lac/test.py b/modules/text/lexical_analysis/lac/test.py index bb7e5830..e4912c10 100644 --- a/modules/text/lexical_analysis/lac/test.py +++ b/modules/text/lexical_analysis/lac/test.py @@ -8,6 +8,7 @@ os.environ['CUDA_VISIBLE_DEVICES'] = '0' class TestHubModule(unittest.TestCase): + @classmethod def setUpClass(cls) -> None: cls.text = "今天是个好日子" @@ -19,74 +20,51 @@ class TestHubModule(unittest.TestCase): shutil.rmtree('inference') def test_cut1(self): - results = self.module.cut( - text=self.text, - use_gpu=False, - batch_size=1, - return_tag=False - ) + results = self.module.cut(text=self.text, use_gpu=False, batch_size=1, return_tag=False) self.assertEqual(results, ['今天', '是', '个', '好日子']) def test_cut2(self): - results = self.module.cut( - text=self.texts, - use_gpu=False, - batch_size=1, - return_tag=False - ) - self.assertEqual(results, [ - {'word': ['今天', '是', '个', '好日子']}, - {'word': ['天气预报', '说', '今天', '要', '下雨']}, - {'word': ['下', '一班', '地铁', '马上', '就要', '到', '了']} - ]) + results = self.module.cut(text=self.texts, use_gpu=False, batch_size=1, return_tag=False) + self.assertEqual(results, [{ + 'word': ['今天', '是', '个', '好日子'] + }, { + 'word': ['天气预报', '说', '今天', '要', '下雨'] + }, { + 'word': ['下', '一班', '地铁', '马上', '就要', '到', '了'] + }]) def test_cut3(self): - results = self.module.cut( - text=self.texts, - use_gpu=False, - batch_size=2, - return_tag=False - ) - self.assertEqual(results, [ - {'word': ['今天', '是', '个', '好日子']}, - {'word': ['天气预报', '说', '今天', '要', '下雨']}, - {'word': ['下', '一班', '地铁', '马上', '就要', '到', '了']} - ]) + results = self.module.cut(text=self.texts, use_gpu=False, batch_size=2, return_tag=False) + self.assertEqual(results, [{ + 'word': ['今天', '是', '个', '好日子'] + }, { + 'word': ['天气预报', '说', '今天', '要', '下雨'] + }, { + 'word': ['下', '一班', '地铁', '马上', '就要', '到', '了'] + }]) def test_cut4(self): - results = self.module.cut( - text=self.texts, - use_gpu=True, - batch_size=2, - return_tag=False - ) - self.assertEqual(results, [ - {'word': ['今天', '是', '个', '好日子']}, - {'word': ['天气预报', '说', '今天', '要', '下雨']}, - {'word': ['下', '一班', '地铁', '马上', '就要', '到', '了']} - ]) + results = self.module.cut(text=self.texts, use_gpu=True, batch_size=2, return_tag=False) + self.assertEqual(results, [{ + 'word': ['今天', '是', '个', '好日子'] + }, { + 'word': ['天气预报', '说', '今天', '要', '下雨'] + }, { + 'word': ['下', '一班', '地铁', '马上', '就要', '到', '了'] + }]) def test_cut5(self): - results = self.module.cut( - text=self.texts, - use_gpu=True, - batch_size=2, - return_tag=True - ) - self.assertEqual(results, [ - { - 'word': ['今天', '是', '个', '好日子'], - 'tag': ['TIME', 'v', 'q', 'n'] - }, - { - 'word': ['天气预报', '说', '今天', '要', '下雨'], - 'tag': ['n', 'v', 'TIME', 'v', 'v'] - }, - { - 'word': ['下', '一班', '地铁', '马上', '就要', '到', '了'], - 'tag': ['f', 'm', 'n', 'd', 'v', 'v', 'xc'] - } - ]) + results = self.module.cut(text=self.texts, use_gpu=True, batch_size=2, return_tag=True) + self.assertEqual(results, [{ + 'word': ['今天', '是', '个', '好日子'], + 'tag': ['TIME', 'v', 'q', 'n'] + }, { + 'word': ['天气预报', '说', '今天', '要', '下雨'], + 'tag': ['n', 'v', 'TIME', 'v', 'v'] + }, { + 'word': ['下', '一班', '地铁', '马上', '就要', '到', '了'], + 'tag': ['f', 'm', 'n', 'd', 'v', 'v', 'xc'] + }]) def test_save_inference_model(self): self.module.save_inference_model('./inference/model') -- GitLab