Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
426 changes: 307 additions & 119 deletions docs/api_reference.md

Large diffs are not rendered by default.

286 changes: 201 additions & 85 deletions skyvault/README.md

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions skyvault/samples/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Skyflow Python SDK — Samples

Runnable examples for the Skyflow Python SDK, grouped by area. Start with the [README](../README.md) and [API Reference](../docs/api_reference.md) for full documentation.
Runnable examples for the Skyflow Python SDK, grouped by area. Start with the [README](../README.md) and [API Reference](../../docs/api_reference.md) for full documentation.

## Prerequisites

Expand Down Expand Up @@ -43,15 +43,16 @@ Core vault data operations.
| `invoke_connection.py` | Invoking a Skyflow Connection |

### `detect_api/`
Skyflow Detect (de-identification / re-identification).
Skyflow Detect V2 de-identification, re-identification, and guardrail checks.

| Sample | Demonstrates |
|--------|--------------|
| `deidentify_text.py` | De-identifying text |
| `reidentify_text.py` | Re-identifying text |
| `deidentify_file.py` | De-identifying a file |
| `deidentify_file_async.py` | Running a file de-identification on a background thread (thread-based concurrency, not asyncio) |
| `get_detect_run.py` | Polling a file de-identification run by `run_id` |
| `deidentify_string.py` | De-identifying a string with a stored or inline Detect configuration |
| `deidentify_file.py` | De-identifying a local file, polling for the result, and writing the processed files |
| `get_run.py` | Retrieving a file de-identification run by `run_id` and saving its outputs |
| `reidentify_string.py` | Re-identifying a string with per-entity redaction types |
| `reidentify_file.py` | Re-identifying a base64-encoded file and saving the result |
| `check_guardrails.py` | Checking text for toxicity and denied topics |

### `service_account/`
Bearer-token and signed-data-token generation.
Expand Down
65 changes: 65 additions & 0 deletions skyvault/samples/detect_api/check_guardrails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from skyflow.error import SkyflowError
from skyflow import Env, Skyflow, LogLevel
from skyflow.utils.enums import GuardrailsValidation
from skyflow.vault.detect import CheckGuardrailsRequest

"""
* Skyflow Check Guardrails Example
*
* This example demonstrates how to:
* 1. Configure Skyflow client credentials
* 2. Set up vault configuration
* 3. Check text for toxicity and denied topics
* 4. Handle response and errors
"""

def perform_check_guardrails():
try:
# Step 1: Configure Credentials
credentials = {
'path': '<PATH_TO_CREDENTIALS_JSON>' # Path to your service account credentials file
}

# Step 2: Configure Vault
vault_config = {
'vault_id': '<YOUR_VAULT_ID>', # Replace with your vault ID
'cluster_id': '<YOUR_CLUSTER_ID>', # Replace with your cluster ID
'env': Env.PROD, # Deployment environment
'credentials': credentials
}

# Step 3: Configure & Initialize Skyflow Client
skyflow_client = (
Skyflow.builder()
.add_vault_config(vault_config)
.set_log_level(LogLevel.ERROR)
.build()
)

# Step 4: Create the request
request = CheckGuardrailsRequest(
text='<TEXT_TO_CHECK>',
check_toxicity=True, # Check the text for toxicity
deny_topics=['politics', 'medical advice'] # Topics to deny
)

# Step 5: Check the text
response = skyflow_client.detect(vault_config.get('vault_id')).check_guardrails(request)

print('Validation:', response.validation, 'Toxic:', response.toxic, 'Denied topic:', response.denied_topic)
if response.validation == GuardrailsValidation.FAILED:
print('The text failed the guardrails check.')

except SkyflowError as error:
# Handle Skyflow-specific errors
print('Skyflow Specific Error: ', {
'code': error.http_code,
'message': error.message,
'details': error.details
})
except Exception as error:
# Handle unexpected errors
print('Unexpected Error:', error)

# Invoke the check guardrails function
perform_check_guardrails()
121 changes: 45 additions & 76 deletions skyvault/samples/detect_api/deidentify_file.py
Original file line number Diff line number Diff line change
@@ -1,109 +1,78 @@
from skyflow.error import SkyflowError
from skyflow import Env, Skyflow, LogLevel
from skyflow.utils.enums import DetectEntities, MaskingMethod, DetectOutputTranscriptions
from skyflow.vault.detect import (
DeidentifyFileRequest,
TokenFormat,
Transformations,
DateTransformation,
Bleep,
FileInput,
)
from skyflow.utils.enums import DetectRunStatus
from skyflow.vault.detect import DeidentifyFileRequest, PollOptions

"""
* Skyflow Deidentify File Example
*
* This sample demonstrates how to use all available options for deidentifying files.
* Supported file types: images (jpg, png, etc.), pdf, audio (mp3, wav), documents,
* spreadsheets, presentations, structured text.
* Skyflow De-identify File Example
*
* This example demonstrates how to:
* 1. Configure Skyflow client credentials
* 2. Set up vault configuration
* 3. Submit a local file for de-identification and poll for the result
* 4. Write the processed files to an output directory
* 5. Handle response and errors
"""


def perform_file_deidentification():
def perform_deidentify_file():
try:
# Step 1: Configure Credentials
credentials = {
'path': '/path/to/credentials.json' # Path to credentials file
'path': '<PATH_TO_CREDENTIALS_JSON>' # Path to your service account credentials file
}

# Step 2: Configure Vault
vault_config = {
'vault_id': '<YOUR_VAULT_ID>', # Replace with your vault ID
'cluster_id': '<YOUR_CLUSTER_ID>', # Replace with your cluster ID
'env': Env.PROD, # Deployment environment
'credentials': credentials,
'credentials': credentials
}

# Step 3: Configure & Initialize Skyflow Client
skyflow_client = (
Skyflow.builder()
.add_vault_config(vault_config)
.set_log_level(LogLevel.INFO) # Use LogLevel.ERROR in production
.set_log_level(LogLevel.ERROR)
.build()
)

# Step 4: Create File Object
file_path = '<FILE_PATH>' # Replace with your file path

# Step 5: Configure Deidentify File Request and call API
with open(file_path, 'rb') as file:
deidentify_request = DeidentifyFileRequest(
file=FileInput(file), # File to de-identify (can also provide a file path)
entities=[DetectEntities.SSN, DetectEntities.CREDIT_CARD], # Entities to detect
allow_regex_list=['<YOUR_REGEX_PATTERN>'], # Optional: Patterns to allow
restrict_regex_list=['<YOUR_REGEX_PATTERN>'], # Optional: Patterns to restrict
# Token format configuration
token_format=TokenFormat(
vault_token=[DetectEntities.SSN], # Use vault tokens for these entities
),
# Optional: Custom transformations
# transformations=Transformations(
# shift_dates=DateTransformation(
# max_days=30,
# min_days=10,
# entities=[DetectEntities.DOB]
# )
# ),
# Output configuration
output_directory='<OUTPUT_DIRECTORY_PATH>', # Where to save processed file
wait_time=15, # Max wait time in seconds (max 64)
# Image-specific options
output_processed_image=True, # Include processed image in output
output_ocr_text=True, # Include OCR text in response
masking_method=MaskingMethod.BLACKBOX, # Masking method for images
# PDF-specific options
pixel_density=15, # Pixel density for PDF processing
max_resolution=2000, # Max resolution for PDF
# Audio-specific options
output_processed_audio=True, # Include processed audio
output_transcription=DetectOutputTranscriptions.PLAINTEXT_TRANSCRIPTION, # Transcription type
# Audio bleep configuration
# bleep=Bleep(
# gain=5, # Loudness in dB
# frequency=1000, # Pitch in Hz
# start_padding=0.1, # Padding at start (seconds)
# stop_padding=0.2 # Padding at end (seconds)
# )
)
# Step 4: Create the request
# Without poll_options, deidentify_file only submits the run and returns its run_id.
# With poll_options, the SDK polls until the run finishes or a polling limit is reached.
request = DeidentifyFileRequest(
file_path='<PATH_TO_FILE>', # Local file to de-identify
configuration_id='<CONFIGURATION_ID>', # ID of an existing Detect configuration
poll_options=PollOptions(
wait_time=120, # Maximum total wait in seconds (1-300, default 60)
max_attempts=10 # Maximum status checks (1-23, default 23)
),
output_directory='<OUTPUT_DIRECTORY>' # Where processed files are written; requires poll_options
)

# Step 6: Call deidentifyFile API
response = skyflow_client.detect().deidentify_file(deidentify_request)
# Step 5: De-identify the file
response = skyflow_client.detect(vault_config.get('vault_id')).deidentify_file(request)

# Handle Successful Response
print('\nDeidentify File Response:', response)
print('Run ID:', response.run_id, 'Status:', response.status)
if response.status == DetectRunStatus.SUCCESS:
# The SDK has written each output to output_directory with a 'processed-' prefix
for output in response.output:
print(output.processed_file_type, output.processed_file_extension)
elif response.status in (DetectRunStatus.QUEUED, DetectRunStatus.IN_PROGRESS):
print('Still running. Call get_run later with run ID:', response.run_id)
else:
print('Run ended with status:', response.status, response.message)

except SkyflowError as error:
# Handle Skyflow-specific errors
print(
'\nSkyflow Error:',
{
'http_code': error.http_code,
'grpc_code': error.grpc_code,
'http_status': error.http_status,
'message': error.message,
'details': error.details,
},
)
print('Skyflow Specific Error: ', {
'code': error.http_code,
'message': error.message,
'details': error.details
})
except Exception as error:
# Handle unexpected errors
print('Unexpected Error:', error)

# Invoke the de-identify file function
perform_deidentify_file()
125 changes: 0 additions & 125 deletions skyvault/samples/detect_api/deidentify_file_async.py

This file was deleted.

Loading
Loading