krea2-anime-capgen

A tag -> caption generator in the training-caption style of the Krea 2 Anime-tuned models (anime illustration of ..., comma phrases, ...). You give it a few Danbooru tags, it returns as many different captions as you ask for, each meant to express all of the tags. It uses no LLM: every caption is assembled from whole phrase groups of real training captions. It runs on the CPU with Python and numpy only (no pyarrow, no pickle files).

Install

Python 3 with numpy (tested: Python 3.12, numpy 2.5). Download this repository and put its directory on sys.path; the code loads the model files next to capgen.py.

import sys
sys.path.append('/path/to/krea2-anime-capgen')
from capgen import Generator

g = Generator.load()             # ~5 s, < 1 GB RAM; load once and keep the instance

RUNTIME.json names the runtime version and lists every file with its size and sha256.

Library usage

for s in g.generate(['beach', 'bikini', 'cat ears'], n=5, length='long', seed=1):
    print(s.text)

# exclusions: '-tag' keeps a tag (and its words) out of the captions
g.generate(['beach', '-white background', '-simple background'], n=3, seed=2)

# autocomplete: tag, Danbooru category, training count, Danbooru post count
g.suggest_info('blue_ar')        # [{'tag', 'category', 'training_count', 'post_count', 'in_training'}, ...]
g.suggest_info('-white b')       # a leading '-' suggests exclusions ('-white background', ...)
g.suggest('cat')                 # tag names only

# how each input was understood (kind: tag, char, series, dropped, rejected, raw, exclude, ...)
g.describe(['highres', 'blue archive', 'cat ears', '-smile'])

Tags may use _ or spaces (cat_ears = cat ears). A tag can be a general tag, a character (kanami (strinova)) or a series (blue archive: a different character of that series per caption, when the series has training characters).

Each result is a Sample: s.text is the caption, s.to_dict() a JSON-serializable dict. What happened to the inputs is reported on the sample: dropped (artist / meta tags such as highres, never written into a caption), rejected (requests the generator does not support, such as 2girls), raw (inputs it does not know, inserted as plain text), unrealized (inputs expressed only by their tag text), picked_character (for a series) and warnings. generate(..., explain=True) and s.explain_text() show which real caption each part came from.

Options of generate(tags, ...)

Option Default Meaning
n 5 number of distinct captions
length 'long' 'long', 'medium' or 'short'
seed None fixed seed = reproducible output for the same tags and options
wild 1.0 1 = surprising combinations (donor captions drawn uniformly); 0 = plausible (compatibility-weighted donors, conflict rejection)
beta 1.0 compatibility sharpness, used as (1 - wild) * beta
order 'template' phrase order of a real caption; 'facet' = one fixed learned order (same phrases)
overlay_text False keep marks on the picture (watermark, artist signature, corner text, logo); dropped by default
scene_text True False also drops text on scene objects (shirt prints, signs, speech bubbles)
mix_look True hair, eyes and body traits from three different captions; False = one appearance donor
distinct_donor_chars True a named character gives at most one part of a caption
explain False record the sources for s.explain_text()

Command line (same options): python capgen.py "beach, bikini, cat ears" -n 5 --length short --seed 1, python capgen.py "kan" --suggest, python capgen.py --help.

Limits

  • Single-character captions only. The training data is solo images, mostly 1girl: multi-character requests (2girls, multiple girls, ...) are rejected with a warning, and 1boy works with far less data behind it.
  • Inputs never seen in training (not a training tag, alias, character or series) are not understood: they are inserted verbatim as a phrase and reported in s.raw (kind raw in describe). Artist and meta tags are dropped and reported in s.dropped.
  • Exclusions work on tags and words; excluding a character, a series or 1boy is ignored (describe shows exclude_unsupported), and an exclusion that a required tag contains is rejected (the requirement wins).
  • A Generator is not thread-safe: hold a lock around generate() / describe() when threads share one instance.

Note on bias

Auto-generated captions often contain "white background" and lean towards Danbooru rating s (roughly R-12 to R-15 content). Remove what you do not want with exclusions, for example -white background, -simple background, or exclude suggestive elements by tag. This is a note only; there is no rating filter.

Related

License

MIT, see LICENSE.

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Space using ricecake/krea2-anime-capgen 1