Skip to main content

Python Client and Model Management for CivitAI.

Project description

pycivitai

PyPI PyPI - Python Version Loc Comments

Code Test Package Release codecov

GitHub Org's stars GitHub stars GitHub forks GitHub commit activity GitHub issues GitHub pulls Contributors GitHub license

Python Client and Model Management for civitai,

The design was inspired by huggingface/huggingface_hub, implementing automatic management of downloaded models locally, and automatically detecting file changes and the latest versions.

Installation

You can install pycivitai with pip

pip install pycivitai

If you need TUI for manually deleting downloaded models, just install like this

pip install pycivitai[cli]

Quick Start

Use Model From CivitAI

pycivitai follows a similar usage pattern as the huggingface_hub, as shown below:

from pycivitai import civitai_download

if __name__ == '__main__':
    # get the latest version of DEN_barbucci_artstyle (either model title or id is okay)
    # Homepage of this model: https://civitai.com/models/85716
    # the return value of civitai_download is the path of downloaded file you need
    print(civitai_download('DEN_barbucci_artstyle'))

    # get the same model (DEN_barbucci_artstyle) with model id
    print(civitai_download(85716))

    # get the specific version (either version name of id is okay)
    print(civitai_download('DEN_barbucci_artstyle', version='v1.0'))
    print(civitai_download('DEN_barbucci_artstyle', 91158))

    # This is a model with multiple files inside.
    # It contains a safetensors and a vae, the safetensors is primary.
    # Homepage of this model: https://civitai.com/models/6755/cetus-mix
    print(civitai_download('Cetus-Mix'))  # the safetensors file (when file not specified, primary file will be chosen)
    print(civitai_download('Cetus-Mix', file='*.vae.pt'))  # get the vae file

Get Information of Model Resource

If you only need to obtain information about the model's resource files, for example, if you need to download the files yourself, you can use the civitai_find_online function, which will return a Resource object.

from pycivitai import civitai_find_online, Resource

if __name__ == '__main__':
    # get resource of this model file, arguments are the same as `civitai_download`
    resource: Resource = civitai_find_online('DEN_barbucci_artstyle')

    # information of this resource
    print(resource.model_name)
    print(resource.model_id)
    print(resource.version_name)
    print(resource.version_id)
    print(resource.filename)
    print(resource.url)
    print(resource.size)
    print(resource.sha256)
    print(resource.is_primary)

Clear the Downloaded Models

If you need to delete all the local models, just

pycivitai delete-cache -A  # download all models

or use the TUI to choose which one to delete

pip install pycivitai[cli]  # this step is necessary
pycivitai delete-cache

F.A.Q.

Where will the downloaded model be saved?

The downloaded model will be saved by default in the ~/.cache/civitai directory. If you need to change the save path, you can set the value of the CIVITAI_HOME environment variable to your desired save path.

How are downloaded models managed?

After downloading, the models are managed using the file system, and file locks are set to ensure thread safety during reading and writing. The structure is similar to the following:

~/.cache/civitai
├── cetus_mix__6755
│   └── cetusmix_whalefall2__105924
│       ├── files
│       │   ├── cetusMix_Whalefall2.safetensors
│       │   └── vae-ft-mse-840000-ema-pruned.vae.pt
│       ├── hashes
│       │   ├── cetusMix_Whalefall2.safetensors.hash
│       │   └── vae-ft-mse-840000-ema-pruned.vae.pt.hash
│       └── primary
└── den_barbucci_artstyle__85716
    ├── v1_0__91158
    │   ├── files
    │   │   └── DEN_barbucci_artstyle.pt
    │   ├── hashes
    │   │   └── DEN_barbucci_artstyle.pt.hash
    │   └── primary
    └── v2_0__113049
        ├── files
        │   └── DEN_barbucci_styleMK2.pt
        ├── hashes
        │   └── DEN_barbucci_styleMK2.pt.hash
        └── primary

This structure does not have any additional dependencies. Therefore, when it is necessary to migrate the storage path, you can simply move it and modify the environment variable CIVITAI_HOME.

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

pycivitai-0.0.1.tar.gz (23.5 kB view details)

Uploaded Source

Built Distribution

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

pycivitai-0.0.1-py3-none-any.whl (25.0 kB view details)

Uploaded Python 3

File details

Details for the file pycivitai-0.0.1.tar.gz.

File metadata

  • Download URL: pycivitai-0.0.1.tar.gz
  • Upload date:
  • Size: 23.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for pycivitai-0.0.1.tar.gz
Algorithm Hash digest
SHA256 8594923b9a2ea7a2d7c8ff8d811d4ee5d7dc2c99cff72af3639e43bb95cd2480
MD5 67927e44ec4541646b207b3dec0ff9cd
BLAKE2b-256 d8dc3b03fae1a6fac6c1934276c8cdbdf4ac8574abd4c5bb9742c0f17f04c9ab

See more details on using hashes here.

File details

Details for the file pycivitai-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pycivitai-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 25.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for pycivitai-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2bb0eddd338195b583e45e221d50a991ed818a1d83d1b04fe0048ba173392dd0
MD5 c6a44b4f646064b4f852b9d6ced9173d
BLAKE2b-256 7eeb5c22eb8091c8488c318c2bb8b8dd7bd0d0efcbdf7c29226b1242a421e077

See more details on using hashes here.

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