HowdenParser 1.0.1__tar.gz → 2.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,12 @@
2
2
  from typing import Literal
3
3
  from pydantic import BaseModel
4
4
 
5
+ class Hest(BaseModel):
6
+ provider_and_model: str = "huggingface:trocr-large-printed"
7
+
8
+
5
9
  class Parameter(BaseModel):
10
+ model1: Hest = Hest()
6
11
  provider_and_model: str = "llamaparser:"
7
12
  result_type: Literal["md"] = "md"
8
13
  mode: bool = False
@@ -5,6 +5,7 @@ from pathlib import Path
5
5
  from PyPDF2 import PdfReader
6
6
  import dotenv
7
7
  import logging
8
+ from inspect import signature
8
9
 
9
10
  dotenv.load_dotenv()
10
11
  logger = logging.getLogger(__name__)
@@ -30,7 +31,7 @@ class Parser(BaseParser):
30
31
 
31
32
  @classmethod
32
33
  def available_parsers(cls) -> None:
33
- """Return registered parsers and their init arguments."""
34
+ """Print registered parsers and their init arguments."""
34
35
  import inspect
35
36
  result = {}
36
37
  for name, parser_cls in BaseParser._registry.items():
@@ -41,23 +42,48 @@ class Parser(BaseParser):
41
42
  print(f"{key} with parameters: {values}")
42
43
 
43
44
  @classmethod
44
- def create(cls, config: dict | None = None, **kwargs) -> BaseParser:
45
- provider = kwargs["provider_and_model"].split(":")[0].lower()
46
- model = kwargs["provider_and_model"].split(":")[1].lower()
45
+ def create(cls, config_or_dict: "Parameter | dict", **kwargs) -> BaseParser:
46
+ """
47
+ Dynamically create parser instances from a config object or dict.
48
+ Only passes arguments accepted by the parser constructor.
49
+ """
50
+ # Convert Parameter -> dict if needed
51
+ if hasattr(config_or_dict, "model_dump"):
52
+ config_dict = config_or_dict.model_dump()
53
+ elif isinstance(config_or_dict, dict):
54
+ config_dict = config_or_dict
55
+ else:
56
+ raise TypeError("Expected Parameter instance or dict for config_or_dict")
57
+
58
+ if "provider_and_model" not in kwargs and "provider_and_model" not in config_dict:
59
+ raise ValueError("provider_and_model must be specified")
60
+
61
+ # Merge dict + kwargs (kwargs take precedence)
62
+ merged_args = {**config_dict, **kwargs}
63
+
64
+ # Extract provider/model info
65
+ provider_and_model = merged_args.get("provider_and_model")
66
+ provider, model = provider_and_model.split(":")
67
+ provider = provider.lower()
68
+ model = model.lower()
69
+
47
70
  if provider not in BaseParser._registry:
48
- raise ValueError(f"Unknown parser '{provider}'. "
49
- f"Available: {cls.available_parsers()}")
71
+ raise ValueError(f"Unknown parser '{provider}'. Available: {list(BaseParser._registry)}")
50
72
 
51
73
  parser_cls = BaseParser._registry[provider]
52
- import inspect
53
- merged_args = {**(config or {}), **kwargs}
54
- if "model" in inspect.signature(parser_cls.__init__).parameters:
55
- merged_args["model"] = model
56
74
 
57
- # Remove keys not in constructor
58
- sig = inspect.signature(parser_cls.__init__)
75
+ # Inspect constructor and only pass valid arguments
76
+ sig = signature(parser_cls.__init__)
59
77
  valid_args = {k: v for k, v in merged_args.items() if k in sig.parameters and k != "self"}
60
78
 
79
+ # Auto-insert 'model' if required
80
+ if "model" in sig.parameters and "model" not in valid_args:
81
+ valid_args["model"] = model
82
+
83
+ # Auto-insert 'provider_and_model' if required
84
+ if "provider_and_model" in sig.parameters and "provider_and_model" not in valid_args:
85
+ valid_args["provider_and_model"] = provider_and_model
86
+
61
87
  return parser_cls(**valid_args)
62
88
 
63
89
  @abstractmethod
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: HowdenParser
3
- Version: 1.0.1
3
+ Version: 2.0.1
4
4
  Summary: A simple configuration manager with Pydantic and JSON export.
5
5
  License: MIT
6
6
  Keywords: config,configuration,pydantic,json
@@ -13,7 +13,6 @@ Classifier: Programming Language :: Python :: 3.12
13
13
  Classifier: Programming Language :: Python :: 3.13
14
14
  Requires-Dist: fitz (>=0.0.1.dev2,<0.0.2)
15
15
  Requires-Dist: hf-xet (>=1.1.7,<2.0.0)
16
- Requires-Dist: howdenconfig (>=0.1.13,<0.2.0)
17
16
  Requires-Dist: langchain (>=0.3.27,<0.4.0)
18
17
  Requires-Dist: llama-parse (>=0.6.58,<0.7.0)
19
18
  Requires-Dist: mistralai (>=1.9.3,<2.0.0)
@@ -78,8 +77,9 @@ print(text)
78
77
 
79
78
  if HowdenConfig package being used
80
79
 
80
+ config: Config = Config(parameter=Parameter())
81
81
 
82
- parser = ParserFactory.get_parser("mistralocr:", **config.parameter.dump_model())
82
+ parser = Parser.create(config.parameter)
83
83
 
84
84
  text = parser.parse(Path("document.pdf"))
85
85
 
@@ -48,8 +48,9 @@ print(text)
48
48
 
49
49
  if HowdenConfig package being used
50
50
 
51
+ config: Config = Config(parameter=Parameter())
51
52
 
52
- parser = ParserFactory.get_parser("mistralocr:", **config.parameter.dump_model())
53
+ parser = Parser.create(config.parameter)
53
54
 
54
55
  text = parser.parse(Path("document.pdf"))
55
56
 
@@ -3,7 +3,7 @@ name = "HowdenParser"
3
3
  description = ""
4
4
  readme = "README.md"
5
5
  requires-python = ">=3.12,<3.14"
6
- dependencies = [ "mistralai (>=1.9.3,<2.0.0)", "llama-parse (>=0.6.58,<0.7.0)", "langchain (>=0.3.27,<0.4.0)", "transformers (>=4.55.2,<5.0.0)", "fitz (>=0.0.1.dev2,<0.0.2)", "howdenconfig (>=0.1.13,<0.2.0)", "pypdf2 (>=3.0.1,<4.0.0)", "pdf2image (>=1.17.0,<2.0.0)", "torch (>=2.8.0,<3.0.0)", "torchvision (>=0.23.0,<0.24.0)", "torchaudio (>=2.8.0,<3.0.0)", "hf-xet (>=1.1.7,<2.0.0)",]
6
+ dependencies = [ "mistralai (>=1.9.3,<2.0.0)", "llama-parse (>=0.6.58,<0.7.0)", "langchain (>=0.3.27,<0.4.0)", "transformers (>=4.55.2,<5.0.0)", "fitz (>=0.0.1.dev2,<0.0.2)", "pypdf2 (>=3.0.1,<4.0.0)", "pdf2image (>=1.17.0,<2.0.0)", "torch (>=2.8.0,<3.0.0)", "torchvision (>=0.23.0,<0.24.0)", "torchaudio (>=2.8.0,<3.0.0)", "hf-xet (>=1.1.7,<2.0.0)",]
7
7
  [[project.authors]]
8
8
  name = "JesperThoftIllemannJ"
9
9
  email = "jesper.jaeger@howdendanmark.dk"
@@ -14,7 +14,7 @@ build-backend = "poetry.core.masonry.api"
14
14
 
15
15
  [tool.poetry]
16
16
  name = "HowdenParser"
17
- version = "1.0.1"
17
+ version = "2.0.1"
18
18
  description = "A simple configuration manager with Pydantic and JSON export."
19
19
  authors = [ "JesperThoftIllemannJ <jesper.jaeger@howdendanmark.dk>",]
20
20
  readme = "README.md"