models.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Dict, Generic, List, Tuple, TypeVar, Union
  15. from pydantic import BaseModel, Discriminator
  16. from typing_extensions import Annotated, Literal, TypeAlias
  17. __all__ = [
  18. "NoResultResponse",
  19. "ResultT",
  20. "ResultResponse",
  21. "Response",
  22. "ImageInfo",
  23. "PDFPageInfo",
  24. "PDFInfo",
  25. "DataInfo",
  26. "PrimaryOperations",
  27. ]
  28. class NoResultResponse(BaseModel):
  29. logId: str
  30. errorCode: int
  31. errorMsg: str
  32. ResultT = TypeVar("ResultT", bound=BaseModel)
  33. class ResultResponse(BaseModel, Generic[ResultT]):
  34. logId: str
  35. result: ResultT
  36. errorCode: Literal[0] = 0
  37. errorMsg: Literal["Success"] = "Success"
  38. Response: TypeAlias = Union[ResultResponse, NoResultResponse]
  39. class ImageInfo(BaseModel):
  40. width: int
  41. height: int
  42. type: Literal["image"] = "image"
  43. class PDFPageInfo(BaseModel):
  44. width: int
  45. height: int
  46. class PDFInfo(BaseModel):
  47. numPages: int
  48. pages: List[PDFPageInfo]
  49. type: Literal["pdf"] = "pdf"
  50. DataInfo: TypeAlias = Annotated[Union[ImageInfo, PDFInfo], Discriminator("type")]
  51. # Should we use generics?
  52. PrimaryOperations: TypeAlias = Dict[str, Tuple[str, BaseModel, BaseModel]]