Metadata-Version: 2.4
Name: picsha
Version: 0.3.0
Summary: Official Python SDK for Picsha AI - Store, Encode, Understand.
Author: Peter
Author-email: peter@picsha.ai
Requires-Python: >=3.9,<4.0
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Dist: httpx (>=0.27,<1.0)
Requires-Dist: pydantic (>=2.6.0,<3.0.0)
Description-Content-Type: text/markdown

# Picsha AI Python SDK 🐍

The official Python client for Picsha AI. Designed specifically for data scientists, computational biologists, and ML engineers.

## Installation

We recommend using poetry:

```bash
poetry add picsha
```

Or pip:

```bash
pip install picsha
```

Requires Python 3.9 or later.

## Quickstart

```python
import picsha

# Or set the PICSHA_API_KEY environment variable and call picsha.Client()
client = picsha.Client(api_key="sk_your_key_here")

# 1. Semantic Search
results = client.search(
    query="fluorescent cancer cell cultures",
    mode="ai"  # Matches on meaning as well as words
)

# 2. Get transformed URLs for ML pipelines
for asset in results.assets:
    url = asset.generate_url(width=512, height=512, fit="cover", format="webp")
    print(url)
```

## Uploading

```python
result = client.upload(
    "./data/sample_01.heic",
    tags=["assay:123", "fluorescent"],
    metadata={"experiment": "alpha"},
)
print(result.id)

# Analysis runs in the background. Check on it:
asset = client.get_asset(result.id)
print(asset.status)
```

Files up to 95 MB are sent through the API. Larger files, up to 5 GiB, go straight to storage with a signed URL; the call is the same.

## Searching

```python
results = client.search(
    "fluorescent cells",
    mode="ai",            # or "standard": words in names, tags and metadata
    limit=20,
    page=1,
    filters={"type": "image", "metadata": {"experiment": "alpha"}},
)

print(results.total_count, results.has_more)
for asset in results.assets:
    print(asset.id, asset.original_filename, asset.match_score)
```

`threshold`, `min_similarity` and `sort` are optional; the API's defaults apply when you leave them out.

## Listing

```python
page = client.list_assets(type="image", tag="assay:123", limit=50)
for asset in page.assets:
    print(asset.id, asset.original_filename)
```

## Delivery URLs

`asset.generate_url(...)` builds a URL that resizes and converts the asset on the fly. No request is made until you fetch it.

```python
asset.generate_url(width=512, format="jpeg", quality=80)
# https://api.picsha.ai/v1/assets/<id>/render?w=512&fmt=jpeg&q=80
```

`width`, `height`, `format`, `quality`, `aspect_ratio`, `position`, `radius`, `background`, `kernel`, `watermark` and `watermark_position` are mapped to the API's parameter names. Any other keyword is sent as given, so the API's own names (`w`, `h`, `fmt`, `fit`, ...) work too.

Resizing, cropping, format conversion and watermarks are public URLs. Generative edits need a signed URL.

## Asynchronous Support

For high-throughput workloads, an `AsyncClient` with the same methods is included out-of-the-box:

```python
import picsha
import asyncio

async def main():
    async with picsha.AsyncClient(api_key="sk_your_key_here") as client:
        result = await client.search(query="cancer cells", mode="ai")
        print(f"Found {result.total_count} assets.")

asyncio.run(main())
```

## Errors

```python
try:
    client.get_asset("does-not-exist")
except picsha.PicshaAuthError:
    ...  # 401 or 403: the key is wrong or lacks access
except picsha.PicshaAPIError as err:
    print(err.status_code, err.details)
```

## Development

```bash
poetry install
poetry run pytest
```

See the `notebooks/` directory for a full example.

