Package: pycivitai
pycivitaiPython Client and Model Management for civitai,The design was inspired byhuggingface/huggingface_hub, implementing
automatic management of downloaded models locally, and automatically detecting file changes and the latest versions.InstallationYou can installpycivitaiwith pippipinstallpycivitaiIf you need TUI for manually deleting downloaded models, just install like thispipinstallpycivitai[cli]Quick StartUse Model From CivitAIpycivitaifollows a similar usage pattern as thehuggingface_hub, as shown below:frompycivitaiimportcivitai_downloadif__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 needprint(civitai_download('DEN_barbucci_artstyle'))# get the same model (DEN_barbucci_artstyle) with model idprint(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-mixprint(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 fileIf you are using command line, you can get the models with the following commandspycivitaiget-m'DEN_barbucci_artstyle'# get model, use primary file of the latest versionpycivitaiget-m'DEN_barbucci_artstyle'-v'v1.0'# get model, use primary file the v1.0 versionpycivitaiget-m'Cetus-Mix'# get the safetensors file of Cetus-Mixpycivitaiget-m'Cetus-Mix'-f'*.vae.pt'# get the vae file of Cetus-MixGet Information of Model ResourceIf 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 thecivitai_find_onlinefunction, which will return aResourceobject.frompycivitaiimportcivitai_find_online,Resourceif__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 resourceprint(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 ModelsIf you need to delete all the local models, justpycivitaidelete-cache-A# download all modelsor use the TUI to choose which one to deletepipinstallpycivitai[cli]# this step is necessarypycivitaidelete-cacheTUI is like this, build withInquirerPy:? Choose model versions to delete:
  Model cetus_mix(ID: 6755, 2 files, size: 3.899 GiB):
❯ ○ cetusmix_whalefall2(ID: 105924, 2 files, size: 3.899 GiB)

  Model den_barbucci_artstyle(ID: 85716, 2 files, size: 17.181 KiB):
  ○ v1_0(ID: 91158, 1 file, size: 7.160 KiB)
  ○ v2_0(ID: 113049, 1 file, size: 10.021 KiB)F.A.Q.Where will the downloaded model be saved?The downloaded model will be saved by default in the~/.cache/civitaidirectory. If you need to change the save path,
you can set the value of theCIVITAI_HOMEenvironment 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
        └── primaryThis 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 variableCIVITAI_HOME.What happens when in an offline environment?When a network anomaly or an offline environment is detected (such as connection errors or timeouts), the library will
attempt to find a suitable model from the locally downloaded models to provide to the user. If no locally available
model is found, an exception will be raised.Additionally, if you are in a clearly offline environment and using pre-downloaded models, you can set the value of
theCIVITAI_OFFLINEenvironment variable to avoid attempts for network access.