You need to enable JavaScript to run this app.
Lake AI Service

Lake AI Service

Copy page
Download PDF
Text processing
Perplexity calculator
Copy page
Download PDF
Perplexity calculator

Operator introduction

Description

Perplexity calculation operator – a text quality evaluation solution based on language models

Key features

  • Language model evaluation
  • Calculates text perplexity based on the KenLM language model
  • Supports quality evaluation for both Chinese and English texts
  • Provides text readability metrics
  • Quality evaluation
  • The lower the perplexity, the higher the text quality
  • Suitable for text quality filtering and evaluation

Technical implementation

  • Model core
  • KenLM: Efficient language model inference
  • SentencePiece: Chinese and English tokenization processing
  • Computation optimization
  • Batch processing to improve efficiency
  • Memory-friendly model loading

Daft invocation

Operator parameters

Input

Input column name

Note

texts

The text column to be processed; element type must be string.

Output

Perplexity value column, with elements of floating-point type.

Parameters

If a parameter does not have a default value, it is required.

Parameter name

Type

Default value

Description

lang

str

zh

Language Description: The language of the text to be calculated Optional values: ["en", "zh"] Default value: "zh"

model_path

str

/opt/las/models

Path to the model files Default value: "/opt/las/models"

model_name

str

kenlm/wikipedia

Model name Default value: "kenlm/wikipedia"

rank

int

0

GPU index Description: GPU index used for model loading Default value: 0

Examples

The following code demonstrates how to use daft to run the operator to calculate text perplexity for evaluating text quality.

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.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                                      │
    # ╞══════════════════════════════════════════════╪══════════════════════════════════════════════╡
    # │ Artificial intelligence technology is developing rapidly, and artificial intelligence technology has been widely app        ┆ 335.9                                        │
    # │ lied in various fields.                                 ┆                                             │
    # ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
    # │ This is a normal sentence.                            ┆ 530.3                                        │
    # ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
    # │ Garbled text 12345 !@#$%                           ┆ 9081.4                                       │
    # ╰──────────────────────────────────────────────┴──────────────────────────────────────────────╯
Last updated: 2026.05.12 19:06:37