В моей локальной среде с использованием стандартного вызова API Process_request вызов запроса требуется от 8 до 20 секунд. В моей оболочке EC2 Python я смог запустить ее один раз, но потребовалось 34 секунды, что недопустимо для моего варианта использования.class DocumentAIClient:
logger = get_logger(__name__)
def __init__(
self,
project_id: str,
location: str,
processor_id: str,
):
"""
Initialize a Document AI client
Args:
project_id: Google Cloud project ID
location: Location of the processor (e.g., "us")
processor_id: Document AI processor ID
"""
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.environ[
"GOOGLE_APPLICATION_CREDENTIALS_FILE"
]
self.project_id = project_id
self.location = location
self.processor_id = processor_id
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
self.client = documentai.DocumentProcessorServiceClient(client_options=opts)
def fetch_document_from_xxxxx(
self,
file_content: bytes,
mime_type: str = "application/pdf",
processor_version_id: str | None = None,
) -> documentai.Document:
"""
Process a document using Google Document AI
Args:
file_content: Binary content of the file
mime_type: MIME type of the document
field_mask: Optional fields to return in the Document object
processor_version_id: Optional processor version ID
pages: Optional list of specific pages to process
Returns:
Tuple containing the processed document and processing time
"""
start_time = time.time()
if processor_version_id:
name = self.client.processor_version_path(
self.project_id, self.location, self.processor_id, processor_version_id
)
else:
name = self.client.processor_path(
self.project_id, self.location, self.processor_id
)
raw_document = documentai.RawDocument(content=file_content, mime_type=mime_type)
process_options = documentai.ProcessOptions(
individual_page_selector=documentai.ProcessOptions.IndividualPageSelector(
pages=[1, 2]
)
)
request = documentai.ProcessRequest(
name=name,
raw_document=raw_document,
field_mask="text,entities,pages.pageNumber",
process_options=process_options,
)
try:
result = self.client.process_document(request=request, timeout=90.0)
except Exception as e:
self.logger.exception("Error getting result: %s", e)
raise
end_time = time.time()
processed_timing = end_time - start_time
document = result.document
self.logger.info(
"Document AI processed document in %s seconds", processed_timing
)
return document
def fetch_pdf_from_s3(
self,
pdf_s3_url: str,
) -> bytes:
self.logger = self.logger.bind(pdf_s3_url=pdf_s3_url)
self.logger.info("S3 URL: %s", pdf_s3_url)
parsed = urlparse(pdf_s3_url)
key = parsed.path.lstrip("/")
try:
with default_storage.open(key, "rb") as file:
data = file.read()
self.logger.info("Fetched %s bytes from path %s", len(data), key)
return data
except Exception as e:
self.logger.exception("Error fetching file from storage: %s", e)
raise
< /code>
Протестировано в локальном - от 8 до 20 с.>
Подробнее здесь: https://stackoverflow.com/questions/796 ... ll-is-slow
Документ AI чрезвычайно медленный в экземпляре EC2 и в целом медленно ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Почему поиск документа FAISS медленно и непоследователен на экземпляре EC2 T3.MICRO?
Anonymous » » в форуме Python - 0 Ответы
- 0 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Почему поиск документа FAISS медленно и непоследователен на экземпляре EC2 T3.MICRO?
Anonymous » » в форуме Python - 0 Ответы
- 1 Просмотры
-
Последнее сообщение Anonymous
-
-
-
MySQL выберите запрос чрезвычайно медленный с пунктом на индексированном столбце
Anonymous » » в форуме MySql - 0 Ответы
- 5 Просмотры
-
Последнее сообщение Anonymous
-