用的是modelscope环境:
pip install modelscope
下载模型:
modelscope download --model iic/nlp_deberta_rex-uninlu_chinese-base
测试代码:
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
semantic_cls = pipeline('rex-uninlu',model='iic/nlp_deberta_rex-uninlu_chinese-base',dispatch_batches=False)
#result = semantic_cls("王羲之在绍兴兰亭写下了《兰亭集序》。", schema="{\"人物\": null, \"地理位置\": null,\"事情\": null}")
#result = semantic_cls("很满意,音质很好,发货速度快,值得购买", schema="{\"属性词\": {\"情感词\": null}}")
result = semantic_cls("1987年首播的央视版《红楼梦》是中央电视台和中国电视剧制作中心根据中国古典文学名著《红楼梦》摄制的一部古装连续剧", schema='{"组织机构": {"注册资本(数字)": null, "创始人(人物)": null, "董事长(人物)": null, "总部地点(地理位置)": null, "代言人(人物)": null, "成立日期(时间)": null, "占地面积(数字)": null, "简称(组织机构)": null}}')
print(result)
结果输出:
{'output': [[{'type': '组织机构', 'span': '中央电视台', 'offset': [17, 22]}, {'type': '简称(组织机构)', 'span': '央视', 'offset': [8, 10]}]]}
如果出现这个错误:
ImportError cannot import name 'get_metadata_patterns' from 'datasets.data_files
错误原因:
modelscope需要datasets的版本范围为>=2.16.0,<2.19.0
pip install datasets==2.18.0