Appearance
Python Integration
Integrate with Questa Anonymizer using the requests library.
Installation
bash
pip install requestsText Anonymization
python
import requests
API_URL = "https://demo.questa-ai.online"
API_TOKEN = "YOUR_API_TOKEN"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_TOKEN}"
}
payload = {
"text": "My name is John Doe and my email is john@example.com."
}
response = requests.post(
f"{API_URL}/anonymize/text",
headers=headers,
json=payload
)
data = response.json()
print(data["anonymized_text"])
# My name is [PERSON_NAME_1] and my email is [EMAIL_ADDRESS_2].
print(data["map"])
# {"John Doe": "[PERSON_NAME_1]", "john@example.com": "[EMAIL_ADDRESS_2]"}
print(data["entities"])
# 2File Anonymization
Upload and Poll
python
import requests
import base64
import time
API_URL = "https://demo.questa-ai.online"
API_TOKEN = "YOUR_API_TOKEN"
FILE_PATH = "confidential_report.pdf"
FILE_TYPE = "pdf" # pdf, docx, csv, excel
headers = {"Authorization": f"Bearer {API_TOKEN}"}
# Upload
with open(FILE_PATH, "rb") as f:
response = requests.post(
f"{API_URL}/anonymize/{FILE_TYPE}",
headers=headers,
files={"file": f}
)
job = response.json()
job_id = job["job_id"]
print(f"Job started: {job_id}")
# Poll until completion
while True:
status_response = requests.get(
f"{API_URL}/anonymize/status/{job_id}",
headers=headers
)
result = status_response.json()
if result["status"] == "completed":
break
elif result["status"] == "failed":
print(f"Error: {result.get('error')}")
exit(1)
print(f"Status: {result['status']} ({result.get('progress', 0)}%)")
time.sleep(1)
# Decode result
file_data = base64.b64decode(result["result"])
output_path = f"anonymized_{FILE_PATH}"
with open(output_path, "wb") as f:
f.write(file_data)
print(f"Saved to: {output_path}")
print(f"Entities found: {result['entity_count']}")Helper Class
python
# anonymizer_client.py
import requests
import base64
import time
from typing import Optional
class AnonymizerClient:
def __init__(self, base_url: str, token: Optional[str] = None):
self.base_url = base_url.rstrip("/")
self.headers = {"Content-Type": "application/json"}
if token:
self.headers["Authorization"] = f"Bearer {token}"
def anonymize_text(
self,
text: str,
entities: Optional[str] = None,
custom_entities: Optional[list] = None,
start_index: int = 0,
) -> dict:
payload = {"text": text, "start_index": start_index}
if entities:
payload["entities"] = entities
if custom_entities:
payload["custom_entities"] = custom_entities
response = requests.post(
f"{self.base_url}/anonymize/text",
headers=self.headers,
json=payload,
)
response.raise_for_status()
return response.json()
def upload_file(
self,
file_path: str,
file_type: str,
entities: Optional[str] = None,
custom_entities: Optional[list] = None,
) -> str:
headers = {}
if self.headers.get("Authorization"):
headers["Authorization"] = self.headers["Authorization"]
files = {"file": open(file_path, "rb")}
data = {}
if entities:
data["entities"] = entities
if custom_entities:
data["custom_entities"] = custom_entities
response = requests.post(
f"{self.base_url}/anonymize/{file_type}",
headers=headers,
files=files,
data=data,
)
response.raise_for_status()
return response.json()["job_id"]
def poll_job(self, job_id: str, interval: float = 1.0) -> dict:
while True:
response = requests.get(
f"{self.base_url}/anonymize/status/{job_id}",
headers=self.headers,
)
result = response.json()
if result["status"] in ("completed", "failed"):
return result
time.sleep(interval)
def anonymize_file(
self,
file_path: str,
file_type: str,
entities: Optional[str] = None,
custom_entities: Optional[list] = None,
output_path: Optional[str] = None,
) -> dict:
job_id = self.upload_file(file_path, file_type, entities, custom_entities)
result = self.poll_job(job_id)
if result["status"] == "completed" and output_path:
file_data = base64.b64decode(result["result"])
with open(output_path, "wb") as f:
f.write(file_data)
return result
# Usage
client = AnonymizerClient(
base_url="https://demo.questa-ai.online",
token="YOUR_API_TOKEN",
)
result = client.anonymize_text("My name is Alice.")
print(result["anonymized_text"])
result = client.anonymize_file("report.pdf", "pdf", output_path="anonymized_report.pdf")
print(f"Found {result['entity_count']} entities")Next: JavaScript Integration