# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""Generate Thai text using the WangChanGLM model."""

from __future__ import annotations

import re
from typing import TYPE_CHECKING, Optional, cast

from pythainlp.tools import warn_deprecation

if TYPE_CHECKING:
    import pandas as pd
    import torch
    from transformers import PreTrainedModel, PreTrainedTokenizerBase


class WangChanGLM:
    """Generate Thai text using the WangChanGLM model."""

    exclude_pattern: re.Pattern[str]
    stop_token: str
    PROMPT_DICT: dict[str, str]
    device: str
    torch_dtype: torch.dtype
    model_path: str
    model: PreTrainedModel
    tokenizer: PreTrainedTokenizerBase
    df: pd.DataFrame
    exclude_ids: list[int]

    def __init__(self) -> None:
        """Initialize the WangChanGLM settings."""
        warn_deprecation(
            "pythainlp.generate.wangchanglm.WangChanGLM",
            deprecated_version="5.3.8",
            removal_version="6.0.0",
        )
        self.exclude_pattern: re.Pattern[str] = re.compile(r"[^ก-๙]+")
        self.stop_token: str = "\n"  # noqa: S105
        self.PROMPT_DICT: dict[str, str] = {
            "prompt_input": (
                "<context>: {input}\n<human>: {instruction}\n<bot>: "
            ),
            "prompt_no_input": ("<human>: {instruction}\n<bot>: "),
            "prompt_chatbot": ("<human>: {human}\n<bot>: {bot}"),
        }

    def is_exclude(self, text: str) -> bool:
        """
        Check whether text contains a non-Thai character.

        :param str text: text to check
        :return: ``True`` if the text contains a character outside the Thai
            range, otherwise ``False``
        :rtype: bool
        """
        return bool(self.exclude_pattern.search(text))

    def load_model(
        self,
        model_path: str = "pythainlp/wangchanglm-7.5B-sft-en-sharded",
        return_dict: bool = True,
        load_in_8bit: bool = False,
        device: str = "cuda",
        torch_dtype: Optional[torch.dtype] = None,
        offload_folder: str = "./",
        low_cpu_mem_usage: bool = True,
        revision: Optional[str] = None,
    ) -> None:
        """
        Load the model.

        :param str model_path: model path
        :param bool return_dict: return the output as a dictionary
        :param bool load_in_8bit: load the model in 8-bit precision
        :param str device: device (cpu, cuda, or other)
        :param Optional[torch.dtype] torch_dtype: data type of the model
        :param str offload_folder: folder to offload weights to
        :param bool low_cpu_mem_usage: reduce CPU memory usage while loading
        :param Optional[str] revision: git revision id (branch, tag, or
            commit hash). Pin to a full commit hash for secure downloads.
        """
        import pandas as pd
        from transformers import AutoModelForCausalLM, AutoTokenizer

        self.device: str = device
        self.torch_dtype: torch.dtype = torch_dtype
        self.model_path: str = model_path
        self.model: PreTrainedModel = AutoModelForCausalLM.from_pretrained(
            self.model_path,
            return_dict=return_dict,
            load_in_8bit=load_in_8bit,
            device_map=device,
            torch_dtype=torch_dtype,
            offload_folder=offload_folder,
            low_cpu_mem_usage=low_cpu_mem_usage,
            revision=revision,
        )
        self.tokenizer: PreTrainedTokenizerBase = (
            AutoTokenizer.from_pretrained(self.model_path, revision=revision)
        )
        self.df: pd.DataFrame = pd.DataFrame(
            self.tokenizer.vocab.items(), columns=["text", "idx"]
        )
        self.df["is_exclude"] = self.df.text.map(self.is_exclude)
        self.exclude_ids: list[int] = self.df[
            self.df.is_exclude is True
        ].idx.tolist()

    def gen_instruct(
        self,
        text: str,
        max_new_tokens: int = 512,
        top_p: float = 0.95,
        temperature: float = 0.9,
        top_k: int = 50,
        no_repeat_ngram_size: int = 2,
        typical_p: float = 1.0,
        thai_only: bool = True,
        skip_special_tokens: bool = True,
    ) -> str:
        """
        Generate an answer to an instruction prompt.

        :param str text: text of the prompt
        :param int max_new_tokens: maximum number of new tokens
        :param float top_p: cumulative probability for nucleus sampling
        :param float temperature: sampling temperature
        :param int top_k: number of top tokens to sample from
        :param int no_repeat_ngram_size: size of n-grams that must not repeat
        :param float typical_p: typical probability for sampling
        :param bool thai_only: generate Thai characters only
        :param bool skip_special_tokens: skip special tokens in the output
        :return: generated answer
        :rtype: str
        """
        import torch

        batch = self.tokenizer(text, return_tensors="pt")
        with torch.autocast(device_type=self.device, dtype=self.torch_dtype):
            if thai_only:
                output_tokens = self.model.generate(
                    input_ids=batch["input_ids"],
                    max_new_tokens=max_new_tokens,  # 512
                    begin_suppress_tokens=self.exclude_ids,
                    no_repeat_ngram_size=no_repeat_ngram_size,
                    # oasst k50
                    top_k=top_k,
                    top_p=top_p,  # 0.95
                    typical_p=typical_p,
                    temperature=temperature,  # 0.9
                )
            else:
                output_tokens = self.model.generate(
                    input_ids=batch["input_ids"],
                    max_new_tokens=max_new_tokens,  # 512
                    no_repeat_ngram_size=no_repeat_ngram_size,
                    # oasst k50
                    top_k=top_k,
                    top_p=top_p,  # 0.95
                    typical_p=typical_p,
                    temperature=temperature,  # 0.9
                )
        return cast(
            "str",
            self.tokenizer.decode(
                output_tokens[0][len(batch["input_ids"][0]) :],
                skip_special_tokens=skip_special_tokens,
            ),
        )

    def instruct_generate(
        self,
        instruct: str,
        context: str = "",
        max_new_tokens: int = 512,
        temperature: float = 0.9,
        top_p: float = 0.95,
        top_k: int = 50,
        no_repeat_ngram_size: int = 2,
        typical_p: float = 1,
        thai_only: bool = True,
        skip_special_tokens: bool = True,
    ) -> str:
        """
        Generate an answer to an instruction.

        :param str instruct: instruction
        :param str context: context of the instruction (optional, empty
            by default)
        :param int max_new_tokens: maximum number of new tokens
        :param float top_p: cumulative probability for nucleus sampling
        :param float temperature: sampling temperature
        :param int top_k: number of top tokens to sample from
        :param int no_repeat_ngram_size: size of n-grams that must not repeat
        :param float typical_p: typical probability for sampling
        :param bool thai_only: generate Thai characters only
        :param bool skip_special_tokens: skip special tokens in the output
        :return: generated answer
        :rtype: str

        :Example:

            >>>     from pythainlp.generate.wangchanglm import WangChanGLM  # doctest: +SKIP
            >>>     import torch  # doctest: +SKIP

            >>>     model = WangChanGLM()  # doctest: +SKIP

            >>>     model.load_model(device="cpu", torch_dtype=torch.bfloat16)  # doctest: +SKIP

            >>>     print(model.instruct_generate(instruct="ขอวิธีลดน้ำหนัก"))  # doctest: +SKIP
                ลดน้ําหนักให้ได้ผล ต้องทําอย่างค่อยเป็นค่อยไป
                ปรับเปลี่ยนพฤติกรรมการกินอาหาร
                ออกกําลังกายอย่างสม่ําเสมอ
                และพักผ่อนให้เพียงพอ
                ที่สําคัญควรหลีกเลี่ยงอาหารที่มีแคลอรี่สูง
                เช่น อาหารทอด อาหารมัน อาหารที่มีน้ําตาลสูง
                และเครื่องดื่มแอลกอฮอล์

        """
        if not context:
            prompt = self.PROMPT_DICT["prompt_no_input"].format_map(
                {"instruction": instruct, "input": ""}
            )
        else:
            prompt = self.PROMPT_DICT["prompt_input"].format_map(
                {"instruction": instruct, "input": context}
            )
        result = self.gen_instruct(
            prompt,
            max_new_tokens=max_new_tokens,
            top_p=top_p,
            top_k=top_k,
            temperature=temperature,
            no_repeat_ngram_size=no_repeat_ngram_size,
            typical_p=typical_p,
            thai_only=thai_only,
            skip_special_tokens=skip_special_tokens,
        )
        return result
