__init__.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from typing import Callable
  2. from abc import ABC, abstractmethod
  3. from magic_pdf.data.data_reader_writer import DataWriter
  4. from magic_pdf.data.dataset import Dataset
  5. from magic_pdf.pipe.operators import PipeResult
  6. __use_inside_model__ = True
  7. __model_mode__ = "full"
  8. class InferenceResultBase(ABC):
  9. @abstractmethod
  10. def __init__(self, inference_results: list, dataset: Dataset):
  11. """Initialized method.
  12. Args:
  13. inference_results (list): the inference result generated by model
  14. dataset (Dataset): the dataset related with model inference result
  15. """
  16. self._infer_res = inference_results
  17. self._dataset = dataset
  18. @abstractmethod
  19. def draw_model(self, file_path: str) -> None:
  20. """Draw model inference result.
  21. Args:
  22. file_path (str): the output file path
  23. """
  24. pass
  25. @abstractmethod
  26. def dump_model(self, writer: DataWriter, file_path: str):
  27. """Dump model inference result to file.
  28. Args:
  29. writer (DataWriter): writer handle
  30. file_path (str): the location of target file
  31. """
  32. pass
  33. @abstractmethod
  34. def get_infer_res(self):
  35. """Get the inference result.
  36. Returns:
  37. list: the inference result generated by model
  38. """
  39. pass
  40. @abstractmethod
  41. def apply(self, proc: Callable, *args, **kwargs):
  42. """Apply callable method which.
  43. Args:
  44. proc (Callable): invoke proc as follows:
  45. proc(inference_result, *args, **kwargs)
  46. Returns:
  47. Any: return the result generated by proc
  48. """
  49. pass
  50. @abstractmethod
  51. def pipe_txt_mode(
  52. self,
  53. imageWriter: DataWriter,
  54. start_page_id=0,
  55. end_page_id=None,
  56. debug_mode=False,
  57. lang=None,
  58. ) -> PipeResult:
  59. """Post-proc the model inference result, Extract the text using the
  60. third library, such as `pymupdf`
  61. Args:
  62. imageWriter (DataWriter): the image writer handle
  63. start_page_id (int, optional): Defaults to 0. Let user select some pages He/She want to process
  64. end_page_id (int, optional): Defaults to the last page index of dataset. Let user select some pages He/She want to process
  65. debug_mode (bool, optional): Defaults to False. will dump more log if enabled
  66. lang (str, optional): Defaults to None.
  67. Returns:
  68. PipeResult: the result
  69. """
  70. pass
  71. @abstractmethod
  72. def pipe_ocr_mode(
  73. self,
  74. imageWriter: DataWriter,
  75. start_page_id=0,
  76. end_page_id=None,
  77. debug_mode=False,
  78. lang=None,
  79. ) -> PipeResult:
  80. pass