# coding: utf-8

"""
    Docling Serve

    No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)

    The version of the OpenAPI document: 
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import pprint
import re  # noqa: F401
import json

from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictFloat, StrictInt
from typing import Any, ClassVar, Dict, List, Optional, Union
from yfkm3_services_parse_client.models.base_vlm_engine_options import BaseVlmEngineOptions
from yfkm3_services_parse_client.models.vlm_model_spec import VlmModelSpec
from typing import Optional, Set
from typing_extensions import Self
from pydantic_core import to_jsonable_python

class CodeFormulaVlmOptions(BaseModel):
    """
    Configuration for VLM-based code and formula extraction.  This stage uses vision-language models to extract code blocks and mathematical formulas from document images. Supports preset-based configuration via StagePresetMixin.  Examples:     # Use CodeFormulaV2 preset     options = CodeFormulaVlmOptions.from_preset(\"codeformulav2\")      # Use Granite Docling preset     options = CodeFormulaVlmOptions.from_preset(\"granite_docling\")
    """ # noqa: E501
    engine_options: BaseVlmEngineOptions = Field(description="Runtime configuration (transformers, mlx, api, etc.)")
    model_spec: VlmModelSpec = Field(description="Model specification with runtime-specific overrides")
    scale: Optional[Union[StrictFloat, StrictInt]] = Field(default=2.0, description="Image scaling factor for preprocessing")
    max_size: Optional[StrictInt] = None
    extract_code: Optional[StrictBool] = Field(default=True, description="Extract code blocks")
    extract_formulas: Optional[StrictBool] = Field(default=True, description="Extract mathematical formulas")
    __properties: ClassVar[List[str]] = ["engine_options", "model_spec", "scale", "max_size", "extract_code", "extract_formulas"]

    model_config = ConfigDict(
        validate_by_name=True,
        validate_by_alias=True,
        validate_assignment=True,
        protected_namespaces=(),
    )


    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.model_dump(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        return json.dumps(to_jsonable_python(self.to_dict()))

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of CodeFormulaVlmOptions from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        """
        excluded_fields: Set[str] = set([
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of engine_options
        if self.engine_options:
            _dict['engine_options'] = self.engine_options.to_dict()
        # override the default output from pydantic by calling `to_dict()` of model_spec
        if self.model_spec:
            _dict['model_spec'] = self.model_spec.to_dict()
        # set to None if max_size (nullable) is None
        # and model_fields_set contains the field
        if self.max_size is None and "max_size" in self.model_fields_set:
            _dict['max_size'] = None

        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of CodeFormulaVlmOptions from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate({
            "engine_options": BaseVlmEngineOptions.from_dict(obj["engine_options"]) if obj.get("engine_options") is not None else None,
            "model_spec": VlmModelSpec.from_dict(obj["model_spec"]) if obj.get("model_spec") is not None else None,
            "scale": obj.get("scale") if obj.get("scale") is not None else 2.0,
            "max_size": obj.get("max_size"),
            "extract_code": obj.get("extract_code") if obj.get("extract_code") is not None else True,
            "extract_formulas": obj.get("extract_formulas") if obj.get("extract_formulas") is not None else True
        })
        return _obj


