You need to enable JavaScript to run this app.
AI 数据湖服务

AI 数据湖服务

复制全文
文本处理
困惑度计算器
复制全文
困惑度计算器

算子介绍

描述

困惑度计算算子 - 基于语言模型的文本质量评估解决方案

核心功能

  • 语言模型评估
  • 基于 KenLM 语言模型计算文本困惑度
  • 支持中英文文本质量评估
  • 提供文本可读性指标
  • 质量评估
  • 困惑度越低,文本质量越高
  • 适用于文本质量筛选和评估

技术实现

  • 模型核心
  • KenLM: 高效的语言模型推理
  • SentencePiece: 中英文分词处理
  • 计算优化
  • 批量处理提升效率
  • 内存友好的模型加载

Daft 调用

算子参数

输入

输入列名

说明

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                                       │
    # ╰──────────────────────────────────────────────┴──────────────────────────────────────────────╯
最近更新时间:2026.01.08 19:14:22
这个页面对您有帮助吗?
有用
有用
无用
无用