Skip to content

Commit 263deab

Browse files
SK-2813: Fix and clean up SDK sample files (#243)
* SK-2817: cleanup samples * SK-2838: add v2 banner in readme
1 parent f30e8c9 commit 263deab

7 files changed

Lines changed: 140 additions & 138 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Skyflow Python SDK
22

3+
> **This is the current, recommended version of the Skyflow SDK.** V2.1.0 brings flexible auth, multi-vault support, native data types, and rich error diagnostics.
4+
>
5+
> Migrating from v1? See the **[Migration Guide](https://github.com/skyflowapi/skyflow-python/blob/main/docs/migrate_to_v2.md)** for step-by-step instructions. V1 is in maintenance mode and will reach End of Life on October 31, 2026.
6+
37
The Skyflow Python SDK is designed to help with integrating Skyflow into a Python backend.
48

59
## Table of Contents
Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
from skyflow.error import SkyflowError
22
from skyflow import Env, Skyflow, LogLevel
33
from skyflow.utils.enums import DetectEntities, MaskingMethod, DetectOutputTranscriptions
4-
from skyflow.vault.detect import DeidentifyFileRequest, TokenFormat, Transformations, DateTransformation, Bleep, FileInput
4+
from skyflow.vault.detect import (
5+
DeidentifyFileRequest,
6+
TokenFormat,
7+
Transformations,
8+
DateTransformation,
9+
Bleep,
10+
FileInput,
11+
)
512

613
"""
714
* Skyflow Deidentify File Example
@@ -11,6 +18,7 @@
1118
* spreadsheets, presentations, structured text.
1219
"""
1320

21+
1422
def perform_file_deidentification():
1523
try:
1624
# Step 1: Configure Credentials
@@ -23,7 +31,7 @@ def perform_file_deidentification():
2331
'vault_id': '<YOUR_VAULT_ID>', # Replace with your vault ID
2432
'cluster_id': '<YOUR_CLUSTER_ID>', # Replace with your cluster ID
2533
'env': Env.PROD, # Deployment environment
26-
'credentials': credentials
34+
'credentials': credentials,
2735
}
2836

2937
# Step 3: Configure & Initialize Skyflow Client
@@ -36,70 +44,66 @@ def perform_file_deidentification():
3644

3745
# Step 4: Create File Object
3846
file_path = '<FILE_PATH>' # Replace with your file path
39-
file = open(file_path, 'rb')
40-
# Step 5: Configure Deidentify File Request with all options
41-
deidentify_request = DeidentifyFileRequest(
42-
file=FileInput(file), # File to de-identify (can also provide a file path)
43-
entities=[DetectEntities.SSN, DetectEntities.CREDIT_CARD], # Entities to detect
44-
allow_regex_list=['<YOUR_REGEX_PATTERN>'], # Optional: Patterns to allow
45-
restrict_regex_list=['<YOUR_REGEX_PATTERN>'], # Optional: Patterns to restrict
46-
47-
# Token format configuration
48-
token_format=TokenFormat(
49-
vault_token=[DetectEntities.SSN], # Use vault tokens for these entities
50-
),
51-
52-
# Optional: Custom transformations
53-
# transformations=Transformations(
54-
# shift_dates=DateTransformation(
55-
# max_days=30,
56-
# min_days=10,
57-
# entities=[DetectEntities.DOB]
58-
# )
59-
# ),
60-
61-
# Output configuration
62-
output_directory='<OUTPUT_DIRECTORY_PATH>', # Where to save processed file
63-
wait_time=15, # Max wait time in seconds (max 64)
64-
65-
# Image-specific options
66-
output_processed_image=True, # Include processed image in output
67-
output_ocr_text=True, # Include OCR text in response
68-
masking_method=MaskingMethod.BLACKBOX, # Masking method for images
69-
70-
# PDF-specific options
71-
pixel_density=15, # Pixel density for PDF processing
72-
max_resolution=2000, # Max resolution for PDF
7347

74-
# Audio-specific options
75-
output_processed_audio=True, # Include processed audio
76-
output_transcription=DetectOutputTranscriptions.PLAINTEXT_TRANSCRIPTION, # Transcription type
77-
78-
# Audio bleep configuration
79-
80-
# bleep=Bleep(
81-
# gain=5, # Loudness in dB
82-
# frequency=1000, # Pitch in Hz
83-
# start_padding=0.1, # Padding at start (seconds)
84-
# stop_padding=0.2 # Padding at end (seconds)
85-
# )
86-
)
87-
88-
# Step 6: Call deidentifyFile API
89-
response = skyflow_client.detect().deidentify_file(deidentify_request)
48+
# Step 5: Configure Deidentify File Request and call API
49+
with open(file_path, 'rb') as file:
50+
deidentify_request = DeidentifyFileRequest(
51+
file=FileInput(file), # File to de-identify (can also provide a file path)
52+
entities=[DetectEntities.SSN, DetectEntities.CREDIT_CARD], # Entities to detect
53+
allow_regex_list=['<YOUR_REGEX_PATTERN>'], # Optional: Patterns to allow
54+
restrict_regex_list=['<YOUR_REGEX_PATTERN>'], # Optional: Patterns to restrict
55+
# Token format configuration
56+
token_format=TokenFormat(
57+
vault_token=[DetectEntities.SSN], # Use vault tokens for these entities
58+
),
59+
# Optional: Custom transformations
60+
# transformations=Transformations(
61+
# shift_dates=DateTransformation(
62+
# max_days=30,
63+
# min_days=10,
64+
# entities=[DetectEntities.DOB]
65+
# )
66+
# ),
67+
# Output configuration
68+
output_directory='<OUTPUT_DIRECTORY_PATH>', # Where to save processed file
69+
wait_time=15, # Max wait time in seconds (max 64)
70+
# Image-specific options
71+
output_processed_image=True, # Include processed image in output
72+
output_ocr_text=True, # Include OCR text in response
73+
masking_method=MaskingMethod.BLACKBOX, # Masking method for images
74+
# PDF-specific options
75+
pixel_density=15, # Pixel density for PDF processing
76+
max_resolution=2000, # Max resolution for PDF
77+
# Audio-specific options
78+
output_processed_audio=True, # Include processed audio
79+
output_transcription=DetectOutputTranscriptions.PLAINTEXT_TRANSCRIPTION, # Transcription type
80+
# Audio bleep configuration
81+
# bleep=Bleep(
82+
# gain=5, # Loudness in dB
83+
# frequency=1000, # Pitch in Hz
84+
# start_padding=0.1, # Padding at start (seconds)
85+
# stop_padding=0.2 # Padding at end (seconds)
86+
# )
87+
)
88+
89+
# Step 6: Call deidentifyFile API
90+
response = skyflow_client.detect().deidentify_file(deidentify_request)
9091

9192
# Handle Successful Response
92-
print("\nDeidentify File Response:", response)
93+
print('\nDeidentify File Response:', response)
9394

9495
except SkyflowError as error:
9596
# Handle Skyflow-specific errors
96-
print('\nSkyflow Error:', {
97-
'http_code': error.http_code,
98-
'grpc_code': error.grpc_code,
99-
'http_status': error.http_status,
100-
'message': error.message,
101-
'details': error.details
102-
})
97+
print(
98+
'\nSkyflow Error:',
99+
{
100+
'http_code': error.http_code,
101+
'grpc_code': error.grpc_code,
102+
'http_status': error.http_status,
103+
'message': error.message,
104+
'details': error.details,
105+
},
106+
)
103107
except Exception as error:
104108
# Handle unexpected errors
105109
print('Unexpected Error:', error)
Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
from skyflow.error import SkyflowError
22
from skyflow import Env, Skyflow, LogLevel
33
from skyflow.utils.enums import DetectEntities, MaskingMethod, DetectOutputTranscriptions
4-
from skyflow.vault.detect import DeidentifyFileRequest, TokenFormat, Transformations, DateTransformation, Bleep, FileInput
4+
from skyflow.vault.detect import (
5+
DeidentifyFileRequest,
6+
TokenFormat,
7+
Transformations,
8+
DateTransformation,
9+
Bleep,
10+
FileInput,
11+
)
512
from concurrent.futures import ThreadPoolExecutor
613

714
"""
@@ -13,6 +20,7 @@
1320
* spreadsheets, presentations, structured text.
1421
"""
1522

23+
1624
def perform_file_deidentification_async():
1725
try:
1826
# Step 1: Configure Credentials
@@ -25,7 +33,7 @@ def perform_file_deidentification_async():
2533
'vault_id': '<YOUR_VAULT_ID>', # Replace with your vault ID
2634
'cluster_id': '<YOUR_CLUSTER_ID>', # Replace with your cluster ID
2735
'env': Env.PROD, # Deployment environment
28-
'credentials': credentials
36+
'credentials': credentials,
2937
}
3038

3139
# Step 3: Configure & Initialize Skyflow Client
@@ -38,18 +46,16 @@ def perform_file_deidentification_async():
3846

3947
# Step 4: Create File Object
4048
file_path = '<FILE_PATH>' # Replace with your file path
41-
49+
4250
deidentify_request = DeidentifyFileRequest(
4351
file=FileInput(file_path=file_path), # File to de-identify
4452
# entities=[DetectEntities.SSN, DetectEntities.CREDIT_CARD], # Entities to detect
4553
allow_regex_list=['<YOUR_REGEX_PATTERN>'], # Optional: Patterns to allow
4654
restrict_regex_list=['<YOUR_REGEX_PATTERN>'], # Optional: Patterns to restrict
47-
4855
# Token format configuration
4956
token_format=TokenFormat(
5057
vault_token=[DetectEntities.SSN], # Use vault tokens for these entities
5158
),
52-
5359
# Optional: Custom transformations
5460
# transformations=Transformations(
5561
# shift_dates=DateTransformation(
@@ -58,67 +64,61 @@ def perform_file_deidentification_async():
5864
# entities=[DetectEntities.DOB]
5965
# )
6066
# ),
61-
62-
# Output configuration
63-
output_directory='<OUTPUT_DIRECTORY_PATH>', # Where to save processed file
64-
wait_time=15, # Max wait time in seconds (max 64)
65-
67+
# Output configuration
68+
output_directory='<OUTPUT_DIRECTORY_PATH>', # Where to save processed file
69+
wait_time=15, # Max wait time in seconds (max 64)
6670
# Image-specific options
6771
output_processed_image=True, # Include processed image in output
6872
output_ocr_text=True, # Include OCR text in response
6973
masking_method=MaskingMethod.BLACKBOX, # Masking method for images
70-
7174
# PDF-specific options
7275
pixel_density=15, # Pixel density for PDF processing
7376
max_resolution=2000, # Max resolution for PDF
74-
7577
# Audio-specific options
7678
output_processed_audio=True, # Include processed audio
7779
output_transcription=DetectOutputTranscriptions.PLAINTEXT_TRANSCRIPTION, # Transcription type
78-
7980
# Audio bleep configuration
80-
8181
# bleep=Bleep(
8282
# gain=5, # Loudness in dB
8383
# frequency=1000, # Pitch in Hz
8484
# start_padding=0.1, # Padding at start (seconds)
8585
# stop_padding=0.2 # Padding at end (seconds)
8686
# )
8787
)
88-
88+
8989
# Create a thread pool executor
9090
executor = ThreadPoolExecutor(max_workers=1)
91-
92-
future = executor.submit(
93-
lambda: skyflow_client.detect().deidentify_file(deidentify_request)
94-
)
95-
91+
92+
future = executor.submit(lambda: skyflow_client.detect().deidentify_file(deidentify_request))
93+
9694
def handle_response(future):
9795
exception = future.exception()
9896
if exception is not None:
9997
if isinstance(exception, SkyflowError):
10098
# Handle Skyflow-specific errors
101-
print('\nSkyflow Error:', {
102-
'http_code': exception.http_code,
103-
'grpc_code': exception.grpc_code,
104-
'http_status': exception.http_status,
105-
'message': exception.message,
106-
'details': exception.details
107-
})
99+
print(
100+
'\nSkyflow Error:',
101+
{
102+
'http_code': exception.http_code,
103+
'grpc_code': exception.grpc_code,
104+
'http_status': exception.http_status,
105+
'message': exception.message,
106+
'details': exception.details,
107+
},
108+
)
108109
else:
109110
# Handle unexpected errors
110111
print('Unexpected Error:', exception)
111112
return
112-
113+
113114
# Handle Successful Response
114115
result = future.result()
115-
print("\nDeidentify File Response:", result)
116-
116+
print('\nDeidentify File Response:', result)
117+
117118
future.add_done_callback(handle_response)
118119

119120
executor.shutdown(wait=True)
120121

121122
except Exception as error:
122123
# Handle unexpected errors
123124
print('Unexpected Error:', error)
124-
Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import json
22
from skyflow.service_account import (
3-
is_expired,
43
generate_signed_data_tokens,
54
generate_signed_data_tokens_from_creds,
65
)
76

8-
file_path = 'CREDENTIALS_FILE_PATH'
9-
bearer_token = ''
7+
file_path = '<CREDENTIALS_FILE_PATH>'
108

119
skyflow_credentials = {
1210
'clientID': '<YOUR_CLIENT_ID>',
@@ -19,15 +17,18 @@
1917

2018

2119
# Approach 1: Signed data tokens with string context
20+
# Returns: [('<DATA_TOKEN>', '<SIGNED_TOKEN>'), ...]
2221
def get_signed_tokens_with_string_context():
2322
options = {
2423
'ctx': 'user_12345',
25-
'data_tokens': ['DATA_TOKEN1', 'DATA_TOKEN2'],
24+
'data_tokens': ['<DATA_TOKEN1>', '<DATA_TOKEN2>'],
2625
'time_to_live': 90, # in seconds
2726
}
2827
try:
29-
data_token, signed_data_token = generate_signed_data_tokens(file_path, options)
30-
return data_token, signed_data_token
28+
results = generate_signed_data_tokens(file_path, options)
29+
for data_token, signed_data_token in results:
30+
print(f' Token: {data_token}, Signed Token: {signed_data_token}')
31+
return results
3132
except Exception as e:
3233
print(f'Error: {str(e)}')
3334

@@ -42,12 +43,14 @@ def get_signed_tokens_with_object_context():
4243
'department': 'research',
4344
'user_id': 'user_67890',
4445
},
45-
'data_tokens': ['DATA_TOKEN1', 'DATA_TOKEN2'],
46+
'data_tokens': ['<DATA_TOKEN1>', '<DATA_TOKEN2>'],
4647
'time_to_live': 90,
4748
}
4849
try:
49-
data_token, signed_data_token = generate_signed_data_tokens(file_path, options)
50-
return data_token, signed_data_token
50+
results = generate_signed_data_tokens(file_path, options)
51+
for data_token, signed_data_token in results:
52+
print(f' Token: {data_token}, Signed Token: {signed_data_token}')
53+
return results
5154
except Exception as e:
5255
print(f'Error: {str(e)}')
5356

@@ -56,16 +59,21 @@ def get_signed_tokens_with_object_context():
5659
def get_signed_tokens_from_credentials_string():
5760
options = {
5861
'ctx': 'user_12345',
59-
'data_tokens': ['DATA_TOKEN1', 'DATA_TOKEN2'],
62+
'data_tokens': ['<DATA_TOKEN1>', '<DATA_TOKEN2>'],
6063
'time_to_live': 90,
6164
}
6265
try:
63-
data_token, signed_data_token = generate_signed_data_tokens_from_creds(credentials_string, options)
64-
return data_token, signed_data_token
66+
results = generate_signed_data_tokens_from_creds(credentials_string, options)
67+
for data_token, signed_data_token in results:
68+
print(f' Token: {data_token}, Signed Token: {signed_data_token}')
69+
return results
6570
except Exception as e:
6671
print(f'Error: {str(e)}')
6772

6873

69-
print("String context:", get_signed_tokens_with_string_context())
70-
print("Object context:", get_signed_tokens_with_object_context())
71-
print("Creds string:", get_signed_tokens_from_credentials_string())
74+
print('String context:')
75+
get_signed_tokens_with_string_context()
76+
print('Object context:')
77+
get_signed_tokens_with_object_context()
78+
print('Creds string:')
79+
get_signed_tokens_from_credentials_string()

0 commit comments

Comments
 (0)