DeepSwapAI Logo - Professional Face Swap Platform
Deep Swap AI
Py

Python SDK

v2.0.0 โ€ข Python 3.8+

The official Python SDK for DeepSwapAI provides a simple, Pythonic interface for all face swap operations with full async support.

Installation

pip install deepswapai

Or with optional async support:

pip install deepswapai[async]

Quick Start

import deepswapai

# Initialize client
client = deepswapai.Client(api_key="YOUR_API_KEY")

# Simple image swap
result = client.image_swap(
    source_image="path/to/source.jpg",
    target_image="path/to/target.jpg"
)

print(f"Success! Output: {result.output_url}")

# Download the result
result.download("output.png")

Configuration

import deepswapai

# Using environment variable (recommended)
# export DEEPSWAPAI_API_KEY=your_key
client = deepswapai.Client()

# Or pass explicitly
client = deepswapai.Client(
    api_key="YOUR_API_KEY",
    timeout=60,  # Request timeout in seconds
    max_retries=3,  # Auto-retry on failures
    base_url="https://api.deepswapai.com"  # Custom endpoint (enterprise)
)

Image Swap

# From file paths
result = client.image_swap(
    source_image="face.jpg",
    target_image="photo.jpg",
    quality="4k",  # "standard", "high", "4k"
    output_format="png"  # "png", "jpg", "webp"
)

# From URLs
result = client.image_swap(
    source_image="https://example.com/face.jpg",
    target_image="https://example.com/photo.jpg"
)

# From bytes/base64
with open("face.jpg", "rb") as f:
    result = client.image_swap(
        source_image=f.read(),
        target_image="photo.jpg"
    )

# Access result
print(result.output_url)      # Download URL (expires in 24h)
print(result.processing_time) # Time in milliseconds
print(result.faces_detected)  # Number of faces found
print(result.credits_used)    # Credits consumed

# Save to file
result.download("output.png")

# Get as bytes
image_bytes = result.to_bytes()

# Get as PIL Image
from PIL import Image
pil_image = result.to_pil()

Video Swap

# Start video processing job
job = client.video_swap(
    source_image="face.jpg",
    target_video="video.mp4",
    quality="4k",
    face_enhance=True
)

print(f"Job ID: {job.id}")
print(f"Estimated time: {job.estimated_time}s")

# Poll for completion with progress callback
def on_progress(progress, status):
    print(f"Progress: {progress}% - {status}")

result = job.wait_for_completion(
    on_progress=on_progress,
    poll_interval=5  # Check every 5 seconds
)

# Download completed video
result.download("output.mp4")

# Or check status manually
status = client.get_status(job.id)
print(status.status)  # "queued", "processing", "completed", "failed"

Batch Processing

# Process multiple images
targets = [
    "product1.jpg",
    "product2.jpg", 
    "product3.jpg",
    "https://example.com/product4.jpg"
]

job = client.batch_swap(
    source_image="model_face.jpg",
    target_images=targets,
    quality="high"
)

# Wait for all to complete
results = job.wait_for_completion()

# Process results
for i, result in enumerate(results):
    if result.success:
        result.download(f"output_{i}.png")
    else:
        print(f"Image {i} failed: {result.error}")

Async Support

import asyncio
import deepswapai

async def main():
    # Use async client
    async with deepswapai.AsyncClient(api_key="YOUR_KEY") as client:
        # Async image swap
        result = await client.image_swap(
            source_image="face.jpg",
            target_image="photo.jpg"
        )
        
        # Process multiple concurrently
        tasks = [
            client.image_swap("face.jpg", f"photo{i}.jpg")
            for i in range(10)
        ]
        results = await asyncio.gather(*tasks)
        
        for i, result in enumerate(results):
            await result.download(f"output_{i}.png")

asyncio.run(main())

Error Handling

from deepswapai.exceptions import (
    DeepSwapAIError,
    AuthenticationError,
    RateLimitError,
    NoFaceDetectedError,
    InvalidImageError
)

try:
    result = client.image_swap("face.jpg", "photo.jpg")
except AuthenticationError:
    print("Invalid API key")
except RateLimitError as e:
    print(f"Rate limited. Retry after {e.retry_after}s")
except NoFaceDetectedError:
    print("No face found in image")
except InvalidImageError as e:
    print(f"Invalid image: {e.message}")
except DeepSwapAIError as e:
    print(f"API error: {e.message}")

Resources