Skip to main content

Read safetensor metadata and fetch CivitAI model information

Project description

tensors

tensors

PyPI Coverage Python License

A CLI tool for working with safetensor files, CivitAI models, and stable-diffusion.cpp image generation.

Features

  • Read safetensor metadata - Parse headers, count tensors, extract embedded metadata
  • CivitAI integration - Search models, fetch info, identify files by hash
  • Download models - Resume support, type-based default paths
  • Hash verification - SHA256 computation with progress display
  • Image generation - txt2img/img2img via stable-diffusion.cpp server
  • Server wrapper - FastAPI wrapper for sd-server with hot reload
  • Models database - SQLite cache for local files and CivitAI metadata
  • Image gallery - Manage generated images with metadata
  • Remote mode - Control remote tsr servers via --remote flag

Installation

# Clone and install
git clone https://github.com/saiden-dev/tensors.git
cd tensors
uv sync

# Or install directly
uv pip install git+https://github.com/saiden-dev/tensors.git

# With server wrapper support
pip install tensors[server]

Usage

Search CivitAI

# Search by query
tsr search "illustrious"

# Filter by type and base model
tsr search -t lora -b sdxl

# Sort by newest, limit results
tsr search -t checkpoint -s newest -n 10

# Filter by tag and period
tsr search --tag anime -p week -b illustrious

# By creator
tsr search -u "username"

# SFW only with commercial use filter
tsr search --sfw --commercial sell

Get Model Info

# Get model info by ID (shows all versions)
tsr get 12345

# Get specific version info
tsr get -v 67890

Download Models

# Download latest version of a model
tsr dl -m 12345

# Download specific version
tsr dl -v 67890

# Download by hash lookup
tsr dl -H ABC123...

# Custom output directory
tsr dl -m 12345 -o ./models

Inspect Local Files

# Read safetensor file and lookup on CivitAI
tsr info model.safetensors

# Skip CivitAI lookup
tsr info model.safetensors --skip-civitai

# Output as JSON
tsr info model.safetensors -j

# Save metadata files
tsr info model.safetensors --save-to ./metadata

Generate Images

Requires a running stable-diffusion.cpp server.

# Generate an image
tsr generate "a cat sitting on a roof"

# Custom size, steps, and output
tsr generate "sunset over mountains" -W 768 -H 512 --steps 30 -o ./output

# Multiple images with seed
tsr generate "cyberpunk city" -b 4 -s 42

# With sampler and negative prompt
tsr generate "portrait" --sampler euler_a -n "blurry, low quality"

Server Wrapper

Manage sd-server process via a REST API. Requires pip install tensors[server].

# Start the wrapper API with a model
tsr serve --model /path/to/model.safetensors

# Custom host and port
tsr serve --model /path/to/model.safetensors --host 0.0.0.0 --port 51200

# Check server status
tsr status

# Hot-reload with a new model
tsr reload --model /path/to/other_model.safetensors

Models Database

Track local safetensor files and cache CivitAI metadata for offline access.

# Scan a directory for safetensor files
tsr db scan /path/to/models

# Link unscanned files to CivitAI by hash
tsr db link

# Cache full CivitAI model data
tsr db cache 12345

# List local files with CivitAI info
tsr db list

# Search cached models offline
tsr db search "pony"
tsr db search -t lora -b sdxl

# Get trigger words for a LoRA
tsr db triggers model.safetensors

# Show database statistics
tsr db stats

Image Gallery

Manage generated images on a remote server.

# List images in gallery
tsr images list --remote junkpile

# Show image metadata
tsr images show IMAGE_ID --remote junkpile

# Download an image
tsr images download IMAGE_ID --remote junkpile -o ./downloads

# Delete an image
tsr images delete IMAGE_ID --remote junkpile

Model Management

List and switch models on a remote server.

# List available models
tsr models list --remote junkpile

# Show active model
tsr models active --remote junkpile

# Switch to a different model
tsr models switch /path/to/model.safetensors --remote junkpile

# List available LoRAs
tsr models loras --remote junkpile

Remote Mode

Control a remote tsr server instead of local operations.

# Configure a remote server
tsr remote add junkpile http://junkpile:51200

# Set default remote
tsr remote default junkpile

# List configured remotes
tsr remote list

# Generate on remote server
tsr generate "a cat" --remote junkpile

# Download model to remote server
tsr dl -m 12345 --remote junkpile

# All commands support --remote flag
tsr status --remote junkpile

Configuration

# Show current config
tsr config

# Set CivitAI API key
tsr config --set-key YOUR_API_KEY

Configuration

Config file: ~/.config/tensors/config.toml

[api]
civitai_key = "your-api-key"

[remotes]
junkpile = "http://junkpile:51200"
local = "http://localhost:51200"

# Optional: set default remote for all commands
default_remote = "junkpile"

Or set API keys via environment variables:

export CIVITAI_API_KEY="your-api-key"      # For CivitAI API access
export TENSORS_API_KEY="your-server-key"   # For server authentication

Default Paths

Data is stored in XDG-compliant paths:

Type Path
Config ~/.config/tensors/config.toml
Database ~/.local/share/tensors/models.db
Checkpoints ~/.local/share/tensors/models/checkpoints/
LoRAs ~/.local/share/tensors/models/loras/
Gallery ~/.local/share/tensors/gallery/
Metadata ~/.local/share/tensors/metadata/

Search Options

Option Values
-t, --type checkpoint, lora, embedding, vae, controlnet, locon
-b, --base sd14, sd15, sd2, sdxl, pony, flux, illustrious, noobai, auraflow
-s, --sort downloads, rating, newest
-n, --limit Number of results (default: 25)
-p, --period all, year, month, week, day
--tag Filter by tag (e.g., "anime")
-u, --user Filter by creator username
--nsfw none, soft, mature, x
--sfw Exclude NSFW content
--commercial none, image, rent, sell
--page Page number for pagination

Generate Options

Option Description
-W Image width (default: 512)
-H Image height (default: 512)
--steps Sampling steps (default: 20)
--cfg-scale CFG scale (default: 7.0)
-s RNG seed, -1 for random (default: -1)
-b Batch size / number of images (default: 1)
-n Negative prompt
-o Output directory (default: .)
--sampler Sampler name
--scheduler Scheduler name
--host sd-server address (default: 127.0.0.1)
--port sd-server port (default: 1234)

Server API Endpoints

When running tsr serve, the following endpoints are available:

OpenAPI Documentation: Visit /docs for interactive Scalar API documentation.

Authentication: If TENSORS_API_KEY is set, all endpoints except /status and /docs require authentication via:

  • Header: X-API-Key: your-key
  • Query param: ?api_key=your-key
Endpoint Method Description
/status GET Server status (public)
/docs GET OpenAPI documentation (public)
/reload POST Hot-reload with new model
/api/images GET List gallery images
/api/images/{id} GET Get image file
/api/images/{id}/meta GET Get image metadata
/api/images/{id}/edit POST Update image metadata
/api/images/{id} DELETE Delete image
/api/models GET List available models
/api/models/active GET Get active model
/api/models/switch POST Switch model
/api/models/loras GET List available LoRAs
/api/generate POST Generate images
/api/download POST Start CivitAI download
/api/db/files GET List local files
/api/db/models GET Search cached models
/api/db/stats GET Database statistics

All sd-server endpoints (/sdapi/v1/*) are proxied through to the underlying process.

Development

# Install dev dependencies
uv sync --group dev

# Run tests
uv run pytest

# Run tests with coverage
uv run pytest --cov=tensors

# Lint and format
uv run ruff check .
uv run ruff format .

# Type check
uv run mypy tensors/

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tensors-0.1.18.tar.gz (1.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tensors-0.1.18-py3-none-any.whl (40.6 kB view details)

Uploaded Python 3

File details

Details for the file tensors-0.1.18.tar.gz.

File metadata

  • Download URL: tensors-0.1.18.tar.gz
  • Upload date:
  • Size: 1.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tensors-0.1.18.tar.gz
Algorithm Hash digest
SHA256 17b508cbdfc75c7011083ec6625ed572219caf38af57624790a1a0e0d07ebbe1
MD5 ca02440730d9778af022fa80f678fc55
BLAKE2b-256 2f3d50a203dc0600b5b58c5f4e8c5d13aba283b2eca7b0e1c7ee5501d863dcc8

See more details on using hashes here.

Provenance

The following attestation bundles were made for tensors-0.1.18.tar.gz:

Publisher: publish.yml on saiden-dev/tensors

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tensors-0.1.18-py3-none-any.whl.

File metadata

  • Download URL: tensors-0.1.18-py3-none-any.whl
  • Upload date:
  • Size: 40.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tensors-0.1.18-py3-none-any.whl
Algorithm Hash digest
SHA256 4ea73c9a12d71cf24150455d2ed5511829440b252509a68922bdc5fba08b7b5d
MD5 8f3c6cc9ce4438473a70fadd3d068ba2
BLAKE2b-256 315ce04fe9d9f5575014600f8fdbdb5e6bb5af3ef45c2c80d42755710d45a1a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for tensors-0.1.18-py3-none-any.whl:

Publisher: publish.yml on saiden-dev/tensors

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page