Examples
Transformation Examples
Examples of transforming documents with Graphora
Was this page helpful?
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Examples of transforming documents with Graphora
import os
from graphora import GraphoraClient
client = GraphoraClient(
auth_token=os.environ["GRAPHORA_AUTH_TOKEN"],
)
# Use a previously registered ontology
ontology_id = "ont_123456789"
# Prepare a list of document files to transform
document_paths = [
"document1.pdf",
"document2.docx",
"document3.txt"
]
# Transform the documents
response = client.transform(ontology_id, document_paths)
print(f"Transformation started with ID: {response.id}")
# Check the status of the transformation
status = client.get_transform_status(response.id)
print(f"Transformation status: {status.status}")
print(f"Progress: {status.progress:.2%}")
import os
from graphora import GraphoraClient, DocumentMetadata, DocumentType
client = GraphoraClient(
auth_token=os.environ["GRAPHORA_AUTH_TOKEN"],
)
# Use a previously registered ontology
ontology_id = "ont_123456789"
# Prepare document paths and metadata
document_paths = [
"document1.pdf",
"document2.docx"
]
metadata = [
DocumentMetadata(
source="Annual Report 2024",
document_type=DocumentType.PDF,
tags=["finance", "annual", "report"],
custom_metadata={
"department": "Finance",
"confidentiality": "Internal"
}
),
DocumentMetadata(
source="Product Specifications",
document_type=DocumentType.DOCX,
tags=["product", "specifications", "technical"],
custom_metadata={
"department": "Engineering",
"product_id": "PRD-2024-001"
}
)
]
# Transform the documents with metadata
response = client.transform(ontology_id, document_paths, metadata=metadata)
print(f"Transformation started with ID: {response.id}")
import os
import time
from graphora import GraphoraClient, TransformRunStatus
client = GraphoraClient(
auth_token=os.environ["GRAPHORA_AUTH_TOKEN"],
)
# Start a transformation (assuming you have already set up the ontology and documents)
transform_response = client.transform(ontology_id, document_paths)
transform_id = transform_response.id
# Monitor the transformation progress
completed = False
while not completed:
status = client.get_transform_status(transform_id)
print(
f"{status.current_stage} | {status.overall_status} | "
f"{status.percentage_complete:.1f}%"
)
if status.overall_status in (
TransformRunStatus.COMPLETED,
TransformRunStatus.FAILED,
):
completed = True
if status.overall_status == TransformRunStatus.COMPLETED:
print("Transformation completed successfully!")
else:
failure = status.error_summary.error_message if status.error_summary else "Unknown error"
print(f"Transformation failed: {failure}")
else:
# Wait for 5 seconds before checking again
time.sleep(5)
# If transformation was successful, you can proceed to merging
if status.status == "COMPLETED":
print("Ready to merge the transformed data into a graph")
import os
from graphora import GraphoraClient
client = GraphoraClient(
auth_token=os.environ["GRAPHORA_AUTH_TOKEN"],
)
# Start a transformation
transform_response = client.transform(ontology_id, document_paths)
transform_id = transform_response.id
# Wait for completion (with timeout)
try:
final_status = client.wait_for_transform(transform_id, timeout=300) # 5 minutes
print(f"Transformation completed with status: {final_status.overall_status}")
except Exception as e:
print(f"Transformation failed or timed out: {e}")
import os
from graphora import GraphoraClient
client = GraphoraClient(
auth_token=os.environ["GRAPHORA_AUTH_TOKEN"],
)
# Use a previously registered ontology
ontology_id = "ont_123456789"
# Prepare a list of different document types
documents = [
"text_document.txt", # Plain text
"word_document.docx", # Microsoft Word
"pdf_document.pdf", # PDF
"data.csv", # CSV
"config.json", # JSON
"schema.yaml" # YAML
]
# Transform the documents
response = client.transform(ontology_id, documents)
print(f"Transformation started with ID: {response.id}")
Was this page helpful?
