|
1 | | -from typing import Any, Optional |
2 | | - |
3 | | -from fastapi import Body |
4 | | -from pydantic import BaseModel |
5 | | - |
6 | | - |
7 | | -class QueryParameters(BaseModel): |
8 | | - document_type: Optional[str] |
9 | | - structure_type: Optional[str] |
10 | | - return_format: Optional[str] |
11 | | - |
12 | | - with_attachments: Optional[str] |
13 | | - need_content_analysis: Optional[str] |
14 | | - recursion_deep_attachments: Optional[str] |
15 | | - return_base64: Optional[str] |
16 | | - attachments_dir: Optional[str] |
17 | | - |
18 | | - insert_table: Optional[str] |
19 | | - need_pdf_table_analysis: Optional[str] |
20 | | - table_type: Optional[str] |
21 | | - orient_analysis_cells: Optional[str] |
22 | | - orient_cell_angle: Optional[str] |
23 | | - |
24 | | - pdf_with_text_layer: Optional[str] |
25 | | - language: Optional[str] |
26 | | - pages: Optional[str] |
27 | | - is_one_column_document: Optional[str] |
28 | | - document_orientation: Optional[str] |
29 | | - need_header_footer_analysis: Optional[str] |
30 | | - need_binarization: Optional[str] |
31 | | - |
32 | | - delimiter: Optional[str] |
33 | | - encoding: Optional[str] |
34 | | - html_fields: Optional[str] |
35 | | - handle_invisible_table: Optional[str] |
36 | | - |
37 | | - def __init__(self, |
38 | | - # type of document structure parsing |
39 | | - document_type: Optional[str] = Body(description="a document type. Default: ''", enum=["", "law", "tz", "diploma"], default=None), # noqa |
40 | | - structure_type: Optional[str] = Body(description="output structure type (linear or tree). Default: 'tree'", enum=["linear", "tree"], default=None), # noqa |
41 | | - return_format: Optional[str] = Body(description="an option for returning a response in html form, json, pretty_json or tree. Assume that one should use json in all cases, all other formats are used for debug porpoises only. Default: 'json'", default=None), # noqa |
42 | | - |
43 | | - # attachments handling |
44 | | - with_attachments: Optional[str] = Body(description="an option to enable the analysis of attached files. Default: 'false'", default=None), # noqa |
45 | | - need_content_analysis: Optional[str] = Body(description="turn on if you need parse the contents of the document attachments. Default: 'false'", default=None), # noqa |
46 | | - recursion_deep_attachments: Optional[str] = Body(description="the depth on which nested attachments will be parsed if need_content_analysis=true. Default: '10'", default=None), # noqa |
47 | | - return_base64: Optional[str] = Body(description="returns images in base64 format. Default: 'false'", default=None), # noqa |
48 | | - attachments_dir: Optional[str] = Body(description="path to the directory where to save files' attachments", default=None), # noqa |
49 | | - |
50 | | - # tables handling |
51 | | - insert_table: Optional[str] = Body(description="Insert table into the result tree's content or not. Default: 'false'", default=None), # noqa |
52 | | - need_pdf_table_analysis: Optional[str] = Body(description="include a table analysis into pdfs. Default: 'true'", default=None), # noqa |
53 | | - table_type: Optional[str] = Body(description="a pipeline mode for a table recognition. Default: ''", default=None), # noqa |
54 | | - orient_analysis_cells: Optional[str] = Body(description="a table recognition option enables analysis of rotated cells in table headers. Default: 'false'", default=None), # noqa |
55 | | - orient_cell_angle: Optional[str] = Body(description="an option to set orientation of cells in table headers. \"270\" - cells are rotated 90 degrees clockwise, \"90\" - cells are rotated 90 degrees counterclockwise (or 270 clockwise)", default=None), # noqa |
56 | | - |
57 | | - # pdf handling |
58 | | - pdf_with_text_layer: Optional[str] = Body(description="an option to extract text from a text layer to PDF or using OCR methods for image-documents. Default: 'auto_tabby'", enum=["true", "false", "auto", "auto_tabby", "tabby"], default=None), # noqa |
59 | | - language: Optional[str] = Body(description="a recognition language. Default: 'rus+eng'", enum=["rus+eng", "rus", "eng"], default=None), # noqa |
60 | | - pages: Optional[str] = Body(description="an option to limit page numbers in pdf, archives with images. left:right, read pages from left to right. Default: ':'", default=None), # noqa |
61 | | - is_one_column_document: Optional[str] = Body(description="an option to set one or multiple column document. \"auto\" - system predict number of columns in document pages, \"true\" - is one column documents, \"false\" - is multiple column documents. Default: 'auto'", default=None), # noqa |
62 | | - document_orientation: Optional[str] = Body(description="an option to set vertical orientation of the document without using an orientation classifier \"auto\" - system predict angle (0, 90, 180, 270) and rotate document, \"no_change\" - do not predict orientation. Default: 'auto'", enum=["auto", "no_change"], default=None), # noqa |
63 | | - need_header_footer_analysis: Optional[str] = Body(description="include header-footer analysis into pdf with text layer. Default: 'false'", default=None), # noqa |
64 | | - need_binarization: Optional[str] = Body(description="include an adaptive binarization into pdf without a text layer. Default: 'false'", default=None), # noqa |
65 | | - |
66 | | - # other formats handling |
67 | | - delimiter: Optional[str] = Body(description="a column separator for csv-files", default=None), # noqa |
68 | | - encoding: Optional[str] = Body(description="a document encoding", default=None), # noqa |
69 | | - html_fields: Optional[str] = Body(description="a list of fields for JSON documents to be parsed as HTML documents. It is written as a json string of a list, where each list item is a list of keys to get the field. Default: ''", default=None), # noqa |
70 | | - handle_invisible_table: Optional[str] = Body(description="handle table without visible borders as tables in html. Default: 'false'", default=None), # noqa |
71 | | - |
72 | | - |
73 | | - **data: Any) -> None: # noqa |
74 | | - |
75 | | - super().__init__(**data) |
76 | | - self.document_type: str = document_type or "" |
77 | | - self.structure_type: str = structure_type or "tree" |
78 | | - self.return_format: str = return_format or "json" |
79 | | - |
80 | | - self.with_attachments: str = with_attachments or "false" |
81 | | - self.need_content_analysis: str = need_content_analysis or "false" |
82 | | - self.recursion_deep_attachments: str = recursion_deep_attachments or "10" |
83 | | - self.return_base64: str = return_base64 or "false" |
84 | | - self.attachments_dir: str = attachments_dir |
85 | | - |
86 | | - self.insert_table: str = insert_table or "false" |
87 | | - self.need_pdf_table_analysis: str = need_pdf_table_analysis or "true" |
88 | | - self.table_type: str = table_type or "" |
89 | | - self.orient_analysis_cells: str = orient_analysis_cells or "false" |
90 | | - self.orient_cell_angle: str = orient_cell_angle or "90" |
91 | | - |
92 | | - self.pdf_with_text_layer: str = pdf_with_text_layer or "auto_tabby" |
93 | | - self.language: str = language or "rus+eng" |
94 | | - self.pages: str = pages or ":" |
95 | | - self.is_one_column_document: str = is_one_column_document or "auto" |
96 | | - self.document_orientation: str = document_orientation or "auto" |
97 | | - self.need_header_footer_analysis: str = need_header_footer_analysis or "false" |
98 | | - self.need_binarization: str = need_binarization or "false" |
99 | | - |
100 | | - self.delimiter: str = delimiter |
101 | | - self.encoding: str = encoding |
102 | | - self.html_fields: str = html_fields or "" |
103 | | - self.handle_invisible_table: str = handle_invisible_table or "false" |
| 1 | +from dataclasses import asdict, dataclass |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +from fastapi import Form |
| 5 | + |
| 6 | + |
| 7 | +@dataclass |
| 8 | +class QueryParameters: |
| 9 | + # type of document structure parsing |
| 10 | + document_type: str = Form("", enum=["", "law", "tz", "diploma"], description="Document domain") |
| 11 | + structure_type: str = Form("tree", enum=["linear", "tree"], description="Output structure type") |
| 12 | + return_format: str = Form("json", enum=["json", "html", "plain_text", "tree", "collapsed_tree", "ujson", "pretty_json"], |
| 13 | + description="Response representation, most types (except json) are used for debug purposes only") |
| 14 | + |
| 15 | + # attachments handling |
| 16 | + with_attachments: str = Form("false", enum=["true", "false"], description="Enable attached files extraction") |
| 17 | + need_content_analysis: str = Form("false", enum=["true", "false"], description="Enable parsing contents of the attached files") |
| 18 | + recursion_deep_attachments: str = Form("10", description="Depth on which nested attachments will be parsed if need_content_analysis=true") |
| 19 | + return_base64: str = Form("false", enum=["true", "false"], description="Save attached images to the document metadata in base64 format") |
| 20 | + attachments_dir: Optional[str] = Form(None, description="Path to the directory where to save files' attachments") |
| 21 | + |
| 22 | + # tables handling |
| 23 | + need_pdf_table_analysis: str = Form("true", enum=["true", "false"], description="Enable table recognition for pdf") |
| 24 | + table_type: str = Form("", description="Pipeline mode for table recognition") |
| 25 | + orient_analysis_cells: str = Form("false", enum=["true", "false"], description="Enable analysis of rotated cells in table headers") |
| 26 | + orient_cell_angle: str = Form("90", enum=["90", "270"], |
| 27 | + description='Set cells orientation in table headers, "90" means 90 degrees counterclockwise cells rotation') |
| 28 | + |
| 29 | + # pdf handling |
| 30 | + pdf_with_text_layer: str = Form("auto_tabby", enum=["true", "false", "auto", "auto_tabby", "tabby"], |
| 31 | + description="Extract text from a text layer of PDF or using OCR methods for image-like documents") |
| 32 | + language: str = Form("rus+eng", enum=["rus+eng", "rus", "eng"], description="Recognition language") |
| 33 | + pages: str = Form(":", description='Page numbers range for reading PDF or images, "left:right" means read pages from left to right') |
| 34 | + is_one_column_document: str = Form("auto", enum=["auto", "true", "false"], |
| 35 | + description='One or multiple column document, "auto" - predict number of page columns automatically') |
| 36 | + document_orientation: str = Form("auto", enum=["auto", "no_change"], |
| 37 | + description='Orientation of the document pages, "auto" - predict orientation (0, 90, 180, 270 degrees), ' |
| 38 | + '"no_change" - set vertical orientation of the document without using an orientation classifier') |
| 39 | + need_header_footer_analysis: str = Form("false", enum=["true", "false"], description="Exclude headers and footers from PDF parsing result") |
| 40 | + need_binarization: str = Form("false", enum=["true", "false"], description="Binarize document pages (for images or PDF without a textual layer)") |
| 41 | + |
| 42 | + # other formats handling |
| 43 | + delimiter: Optional[str] = Form(None, description="Column separator for CSV files") |
| 44 | + encoding: Optional[str] = Form(None, description="Document encoding") |
| 45 | + html_fields: str = Form("", description="List of fields for JSON documents to be parsed as HTML documents") |
| 46 | + handle_invisible_table: str = Form("false", enum=["true", "false"], description="Handle tables without visible borders as tables in HTML") |
| 47 | + |
| 48 | + def to_dict(self) -> dict: |
| 49 | + parameters = {} |
| 50 | + |
| 51 | + for parameter_name, parameter_value in asdict(self).items(): |
| 52 | + parameters[parameter_name] = getattr(parameter_value, "default", parameter_value) |
| 53 | + |
| 54 | + return parameters |
0 commit comments