困惑度计算算子 - 基于语言模型的文本质量评估解决方案
输入列名 | 说明 |
|---|---|
texts | 待处理的文本列,要求元素类型为字符串。 |
困惑度值列,元素为浮点数类型。
如参数没有默认值,则为必填参数
参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
lang | str | zh | 语种 描述:需要计算的文本的语种 可选值:["en", "zh"] 默认值:"zh" |
model_path | str | /opt/las/models | 模型文件所在的路径 默认值:"/opt/las/models" |
model_name | str | kenlm/wikipedia | 模型名称 默认值:"kenlm/wikipedia" |
rank | int | 0 | GPU 编号 描述:GPU 编号,用于模型加载 默认值:0 |
下面的代码展示了如何使用 daft 运行算子计算文本的困惑度,用于评估文本质量。
from __future__ import annotations import os import daft from daft import col from daft.las.functions.text.perplexity_calculator import PerplexityCalculator from daft.las.functions.udf import las_udf if __name__ == "__main__": if os.getenv("DAFT_RUNNER", "native") == "ray": import logging import ray def configure_logging(): logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S.%s".format(), ) logging.getLogger("tracing.span").setLevel(logging.WARNING) logging.getLogger("daft_io.stats").setLevel(logging.WARNING) logging.getLogger("DaftStatisticsManager").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaScheduler").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaDispatcher").setLevel(logging.WARNING) ray.init(dashboard_host="0.0.0.0", runtime_env={"worker_process_setup_hook": configure_logging}) daft.context.set_runner_ray() daft.set_execution_config(actor_udf_ready_timeout=600) daft.set_execution_config(min_cpu_per_task=0) samples = { "text": [ "人工智能技术正在快速发展,人工智能技术已经广泛应用于各个领域。", "这是一个正常的句子。", "乱码文本 12345 !@#$%", ] } lang = "zh" ds = daft.from_pydict(samples) ds = ds.with_column( "perplexity", las_udf( PerplexityCalculator, construct_args={ "lang": lang, "model_path": os.getenv("MODEL_PATH", "/opt/las/models"), "model_name": "kenlm/wikipedia", }, num_gpus=0, batch_size=1, concurrency=1, )(col("text")), ) ds.show() # ╭──────────────────────────────────────────────┬──────────────────────────────────────────────╮ # │ text ┆ perplexity │ # │ --- ┆ --- │ # │ Utf8 ┆ Float64 │ # ╞══════════════════════════════════════════════╪══════════════════════════════════════════════╡ # │ 人工智能技术正在快速发展,人工智能技术已经广泛应 ┆ 335.9 │ # │ 用于各个领域。 ┆ │ # ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ # │ 这是一个正常的句子。 ┆ 530.3 │ # ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ # │ 乱码文本 12345 !@#$% ┆ 9081.4 │ # ╰──────────────────────────────────────────────┴──────────────────────────────────────────────╯