37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
# services/gcp_document_ai_client.py
|
|
from google.api_core.client_options import ClientOptions
|
|
from google.api_core.exceptions import GoogleAPICallError
|
|
from google.cloud import documentai
|
|
|
|
def process_document_gcp(
|
|
project_id: str,
|
|
location: str,
|
|
processor_id: str,
|
|
file_bytes: bytes,
|
|
mime_type: str,
|
|
) -> documentai.Document:
|
|
"""
|
|
Procesa el contenido de un documento en bytes usando la API de Google Document AI.
|
|
Esta función ahora solo se encarga de la comunicación con GCP.
|
|
"""
|
|
try:
|
|
client_options = ClientOptions(
|
|
api_endpoint=f"{location}-documentai.googleapis.com"
|
|
)
|
|
client = documentai.DocumentProcessorServiceClient(client_options=client_options)
|
|
|
|
resource_name = client.processor_path(project_id, location, processor_id)
|
|
|
|
raw_document = documentai.RawDocument(
|
|
content=file_bytes, mime_type=mime_type
|
|
)
|
|
request = documentai.ProcessRequest(name=resource_name, raw_document=raw_document)
|
|
|
|
result = client.process_document(request=request)
|
|
return result.document
|
|
|
|
except GoogleAPICallError as e:
|
|
raise GoogleAPICallError(f"API call failed: {e}") from e
|
|
except Exception as e:
|
|
raise Exception(f"An unexpected error occurred during GCP processing: {e}") from e
|