-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.py
101 lines (76 loc) · 2.51 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import time
from enum import Enum
from pathlib import Path
from typing import Any, Literal, Union
import httpx
from pydantic import BaseModel, Field
class ChunkType(str, Enum):
title = "title" # type: ignore [assignment]
page_header = "page_header"
page_footer = "page_footer"
page_number = "page_number"
key_value = "key_value"
form = "form"
table = "table"
figure = "figure"
text = "text"
error = "error"
class ChunkGroundingBox(BaseModel):
"""
A bounding box of a chunk.
The coordinates are in the format of [left, top, right, bottom].
"""
l: float # noqa: E741
t: float
r: float
b: float
class ChunkGrounding(BaseModel):
page: int
# NOTE: could be None if error happens in parsing the chunk
box: Union[ChunkGroundingBox, None]
# NOTE: image_path doesn't come from the server API, so it's null by default
image_path: Union[Path, None] = None
class Chunk(BaseModel):
text: str
grounding: list[ChunkGrounding]
chunk_type: ChunkType
chunk_id: Union[str, None]
@staticmethod
def error_chunk(error_msg: str, page_idx: int) -> "Chunk":
return Chunk(
text=error_msg,
grounding=[ChunkGrounding(page=page_idx, box=None)],
chunk_type=ChunkType.error,
chunk_id=None,
)
class ParsedDocument(BaseModel):
markdown: str
chunks: list[Chunk]
start_page_idx: int
end_page_idx: int
doc_type: Literal["pdf", "image"]
class RetryableError(Exception):
def __init__(self, response: httpx.Response):
self.response = response
self.reason = f"{response.status_code} - {response.text}"
def __str__(self) -> str:
return self.reason
class Document(BaseModel):
file_path: Path = Field(description="The local file path to the document file")
start_page_idx: int = Field(
description="The index of the first page in the file", ge=0
)
end_page_idx: int = Field(
description="The index of the last page in the file", ge=0
)
def __str__(self) -> str:
return f"File name: {self.file_path.name}\tPage: [{self.start_page_idx}:{self.end_page_idx}]"
class Timer:
"""A context manager for timing code execution in a thread-safe manner."""
def __init__(self) -> None:
self.elapsed = 0.0
def __enter__(self) -> "Timer":
self.start = time.perf_counter()
return self
def __exit__(self, *args: Any) -> None:
self.elapsed = time.perf_counter() - self.start