|
| 1 | +from argparse import ArgumentParser, Namespace |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import Optional, Type, Union |
| 4 | + |
| 5 | +from mindee import ( |
| 6 | + ClientV2, |
| 7 | + InferenceResponse, |
| 8 | + CropResponse, |
| 9 | + SplitResponse, |
| 10 | + ClassificationResponse, |
| 11 | + InferenceParameters, |
| 12 | + ClassificationParameters, |
| 13 | + CropParameters, |
| 14 | + SplitParameters, |
| 15 | +) |
| 16 | +from mindee.input import BaseParameters |
| 17 | + |
| 18 | +from mindee.input.sources import PathInput, UrlInputSource |
| 19 | +from mindee.v2.parsing import BaseResponse |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class ProductConfig: |
| 24 | + """Configuration for a command.""" |
| 25 | + |
| 26 | + response_class: Type[BaseResponse] |
| 27 | + params_class: Type[BaseParameters] |
| 28 | + |
| 29 | + |
| 30 | +PRODUCTS = { |
| 31 | + "classification": ProductConfig( |
| 32 | + response_class=ClassificationResponse, |
| 33 | + params_class=ClassificationParameters, |
| 34 | + ), |
| 35 | + "crop": ProductConfig( |
| 36 | + response_class=CropResponse, |
| 37 | + params_class=CropParameters, |
| 38 | + ), |
| 39 | + "extraction": ProductConfig( |
| 40 | + response_class=InferenceResponse, |
| 41 | + params_class=InferenceParameters, |
| 42 | + ), |
| 43 | + "split": ProductConfig( |
| 44 | + response_class=SplitResponse, |
| 45 | + params_class=SplitParameters, |
| 46 | + ), |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +class MindeeArgumentParser(ArgumentParser): |
| 51 | + """Custom parser to simplify adding various options.""" |
| 52 | + |
| 53 | + def add_main_options(self) -> None: |
| 54 | + """Adds main options for most parsings.""" |
| 55 | + self.add_argument( |
| 56 | + "-k", |
| 57 | + "--key", |
| 58 | + dest="api_key", |
| 59 | + help="API key for the account", |
| 60 | + required=False, |
| 61 | + default=None, |
| 62 | + ) |
| 63 | + self.add_argument( |
| 64 | + "-m", |
| 65 | + "--model-id", |
| 66 | + dest="model_id", |
| 67 | + help="Model ID", |
| 68 | + required=True, |
| 69 | + default=None, |
| 70 | + ) |
| 71 | + |
| 72 | + def add_path_arg(self) -> None: |
| 73 | + """Adds options related to output/display of a document (parse, parse-queued).""" |
| 74 | + self.add_argument(dest="path", help="Full path to the file") |
| 75 | + |
| 76 | + |
| 77 | +class MindeeParser: |
| 78 | + """Custom parser for the Mindee CLI.""" |
| 79 | + |
| 80 | + parser: MindeeArgumentParser |
| 81 | + """Parser options.""" |
| 82 | + parsed_args: Namespace |
| 83 | + """Stores attributes relating to parsing.""" |
| 84 | + client: ClientV2 |
| 85 | + """Mindee client""" |
| 86 | + input_source: Union[PathInput, UrlInputSource] |
| 87 | + """Document to be parsed.""" |
| 88 | + |
| 89 | + def __init__( |
| 90 | + self, |
| 91 | + parser: Optional[MindeeArgumentParser] = None, |
| 92 | + parsed_args: Optional[Namespace] = None, |
| 93 | + client: Optional[ClientV2] = None, |
| 94 | + ) -> None: |
| 95 | + self.parser = ( |
| 96 | + parser if parser else MindeeArgumentParser(description="Mindee_API") |
| 97 | + ) |
| 98 | + self.parsed_args = parsed_args if parsed_args else self._set_args() |
| 99 | + if client: |
| 100 | + self.client = client |
| 101 | + else: |
| 102 | + api_key = self.parsed_args.api_key if "api_key" in self.parsed_args else "" |
| 103 | + self.client = ClientV2(api_key=api_key) |
| 104 | + self.input_source = self._get_input_source() |
| 105 | + |
| 106 | + def call_parse(self) -> None: |
| 107 | + """Calls the parse method of the input document.""" |
| 108 | + product_conf = PRODUCTS[self.parsed_args.product_name] |
| 109 | + response = self.client.enqueue_and_get_result( |
| 110 | + response_type=product_conf.response_class, |
| 111 | + input_source=self.input_source, |
| 112 | + params=product_conf.params_class( |
| 113 | + model_id=self.parsed_args.model_id, |
| 114 | + ), |
| 115 | + ) |
| 116 | + print(response.inference) |
| 117 | + |
| 118 | + def _set_args(self) -> Namespace: |
| 119 | + """Parse command line arguments.""" |
| 120 | + parse_product_subparsers = self.parser.add_subparsers( |
| 121 | + dest="product_name", |
| 122 | + required=True, |
| 123 | + ) |
| 124 | + for name in PRODUCTS: |
| 125 | + parse_subparser = parse_product_subparsers.add_parser(name) |
| 126 | + |
| 127 | + parse_subparser.add_main_options() |
| 128 | + parse_subparser.add_path_arg() |
| 129 | + |
| 130 | + parsed_args = self.parser.parse_args() |
| 131 | + return parsed_args |
| 132 | + |
| 133 | + def _get_input_source(self) -> Union[PathInput, UrlInputSource]: |
| 134 | + """Loads an input document.""" |
| 135 | + |
| 136 | + if self.parsed_args.path.lower().startswith("http"): |
| 137 | + return UrlInputSource(self.parsed_args.path) |
| 138 | + return PathInput(self.parsed_args.path) |
0 commit comments