Perplexity calculation operator – a text quality evaluation solution based on language models
Input column name | Note |
|---|---|
texts | The text column to be processed; element type must be string. |
Perplexity value column, with elements of floating-point type.
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 |
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 │ # ╰──────────────────────────────────────────────┴──────────────────────────────────────────────╯