Python SDK¶
Pythonic API design. Full type hints, Pydantic models, sync and async clients, streaming support.
Installation¶
Requirements:
- Python 3.9+
- Dependencies:
httpx,pydantic
Quick Start¶
from pauhu import Pauhu
# Initialize client
client = Pauhu(api_key="pk_...")
# Translate text
result = client.translate(
text="Hello, world!",
target="fi"
)
print(result.translation) # "Hei, maailma!"
print(result.confidence) # 0.98
Client Configuration¶
from pauhu import Pauhu
client = Pauhu(
api_key="pk_...", # API key (or set PAUHU_API_KEY env)
base_url="https://api.pauhu.ai/v1", # API base URL
timeout=30.0, # Request timeout (seconds)
max_retries=3, # Retry attempts
verify_ssl=True, # SSL verification
proxy=None # HTTP proxy
)
Environment Variables¶
export PAUHU_API_KEY="pk_..."
export PAUHU_BASE_URL="https://api.pauhu.ai/v1"
export PAUHU_TIMEOUT="30"
Translation¶
Basic Translation¶
result = client.translate(
text="The contract is valid.",
target="fi",
source="en" # Optional: auto-detect if omitted
)
print(result.translation) # "Sopimus on voimassa."
print(result.source_language) # "en"
print(result.target_language) # "fi"
print(result.confidence) # 0.97
print(result.word_count) # 4
Domain-Specific Translation¶
result = client.translate(
text="The controller shall implement appropriate measures.",
target="fi",
domain="12 Law" # EuroVoc domain
)
Translation Options¶
from pauhu.types import TranslateOptions
result = client.translate(
text="Hello",
target="fi",
options=TranslateOptions(
formality="formal",
preserve_formatting=True,
quality="quality",
glossary_id="gl_abc123"
)
)
Batch Translation¶
texts = ["Hello", "How are you?", "Goodbye"]
results = client.translate_batch(
texts=texts,
target="fi"
)
for r in results:
print(f"{r.text} -> {r.translation}")
Parallel Batch¶
# Process in parallel for speed
results = client.translate_batch(
texts=large_text_list,
target="fi",
parallel=True,
chunk_size=50 # Texts per request
)
Streaming¶
Token Streaming¶
# Stream tokens as they're generated
for token in client.translate_stream(
text="Welcome to Finland!",
target="fi"
):
print(token, end="", flush=True)
# Output: "Tervetuloa Suomeen!"
Event Streaming¶
from pauhu.types import StreamEvent
for event in client.translate_stream(
text="Hello, world!",
target="fi",
events=True # Return event objects
):
if event.type == "token":
print(event.token, end="")
elif event.type == "complete":
print(f"\nDone in {event.latency_ms}ms")
Async Client¶
import asyncio
from pauhu import AsyncPauhu
async def main():
client = AsyncPauhu()
# Async translate
result = await client.translate(
text="Hello",
target="fi"
)
print(result.translation)
# Async streaming
async for token in client.translate_stream(
text="Hello, world!",
target="fi"
):
print(token, end="", flush=True)
asyncio.run(main())
Concurrent Translations¶
async def translate_many(texts: list[str], target: str):
client = AsyncPauhu()
tasks = [
client.translate(text=t, target=target)
for t in texts
]
return await asyncio.gather(*tasks)
results = asyncio.run(translate_many(
["Hello", "World", "Test"],
"fi"
))
Document Translation¶
# Translate PDF
result = client.translate_document(
file_path="contract.pdf",
target="fi",
domain="12 Law"
)
# Wait for completion
result.wait()
# Save translated document
result.save("contract_fi.pdf")
# Get metadata
print(result.page_count) # 15
print(result.word_count) # 5000
print(result.status) # "completed"
Async Document¶
async def translate_doc():
client = AsyncPauhu()
result = await client.translate_document(
file_path="report.pdf",
target="fi"
)
# Poll for completion
while result.status != "completed":
await asyncio.sleep(1)
result = await client.documents.get(result.id)
await result.save("report_fi.pdf")
Language Detection¶
result = client.detect("Hyvää päivää!")
print(result.language) # "fi"
print(result.language_name) # "Finnish"
print(result.confidence) # 0.997
print(result.script) # "Latin"
Batch Detection¶
texts = ["Hello", "Bonjour", "Hallo", "Hei"]
results = client.detect_batch(texts)
for text, r in zip(texts, results):
print(f"{text}: {r.language}")
Models¶
Translation Response¶
from pauhu.types import TranslationResult
result: TranslationResult = client.translate(...)
# All fields are typed
result.translation: str
result.source_language: str
result.target_language: str
result.confidence: float
result.word_count: int
result.character_count: int
result.metadata: dict
Document Response¶
from pauhu.types import DocumentResult
result: DocumentResult = client.translate_document(...)
result.id: str
result.filename: str
result.status: Literal["pending", "processing", "completed", "failed"]
result.page_count: int
result.word_count: int
result.source_language: str
result.target_language: str
result.created_at: datetime
result.completed_at: Optional[datetime]
Error Handling¶
from pauhu import Pauhu
from pauhu.exceptions import (
PauhuError,
AuthenticationError,
RateLimitError,
ValidationError,
NotFoundError
)
try:
result = client.translate("Hello", target="fi")
except AuthenticationError:
print("Invalid API key")
except RateLimitError as e:
print(f"Rate limited. Retry after {e.retry_after}s")
except ValidationError as e:
print(f"Invalid request: {e.message}")
except NotFoundError:
print("Resource not found")
except PauhuError as e:
print(f"API error: {e}")
Logging¶
import logging
# Enable debug logging
logging.basicConfig(level=logging.DEBUG)
# Or configure Pauhu logger specifically
logger = logging.getLogger("pauhu")
logger.setLevel(logging.DEBUG)
Context Manager¶
from pauhu import Pauhu
with Pauhu() as client:
result = client.translate("Hello", target="fi")
print(result.translation)
# Connection closed automatically
Async: