Metadata-Version: 2.4
Name: clipcloud-batch
Version: 0.1.0
Summary: Batch AI image generator: a CSV, Excel or text file of prompts in, a folder of images out (ClipCloud API).
Author: alexmrgit
License-Expression: MIT
Project-URL: Homepage, https://github.com/alexmrgit/clipcloud-batch
Project-URL: Source, https://github.com/alexmrgit/clipcloud-batch
Project-URL: Issues, https://github.com/alexmrgit/clipcloud-batch/issues
Project-URL: ClipCloud API, https://clipcloud.cliplabs.top/docs/
Keywords: ai image generator,bulk,batch,csv,text to image,clipcloud
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Environment :: Console
Classifier: Environment :: Web Environment
Classifier: Intended Audience :: End Users/Desktop
Classifier: Topic :: Multimedia :: Graphics
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: openpyxl>=3.0
Requires-Dist: Pillow>=9.0
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Dynamic: license-file

# ClipCloud Batch

Bulk AI image generator: a table of prompts in (CSV, Excel, text or pasted rows), a folder of
images out. A simple window plus a command line. Runs on your computer. MIT license.

![ClipCloud Batch: drop a table, check the rows, see the price, get a folder of images](https://raw.githubusercontent.com/alexmrgit/clipcloud-batch/main/docs/demo.gif)

It uses the [ClipCloud API](https://clipcloud.cliplabs.top/docs/): 35 styles, from $0.0005 per image,
and 25 free images a day with a free key.

- **Never pays twice.** Stop it, close the laptop, lose Wi-Fi: the next run picks up where it stopped
  and does not send any row again.
- **Price first.** See the cost of the whole table before anything is sent.
- **Free-image planner.** With a free key it sends what today's 25 free images cover and keeps the
  rest for tomorrow. One click continues the batch the next day.
- **Any table.** CSV, Excel, TSV, a text file or rows pasted from Google Sheets. Style, count,
  negative prompt and file name per row. Style names like "Watercolor" or "anime style" and column
  names in several languages ("Промпт", "Stil") are understood.
- **PNG, JPEG or WebP**, plus `results.csv` with the file, status and cost of every row.

## Quick start

```bash
pipx install clipcloud-batch
```

(or `pip install clipcloud-batch`, or the latest code: `pipx install git+https://github.com/alexmrgit/clipcloud-batch`.
Python 3.9 or newer.)

Get a free key at [clipcloud.cliplabs.top/panel](https://clipcloud.cliplabs.top/panel/) or in
[@clipcloudbot](https://t.me/clipcloudbot), then:

```bash
clipcloud-batch gui
```

Paste the key in the window, or set it once: `export CLIPCLOUD_API_KEY=clip-...`
(Windows PowerShell: `$env:CLIPCLOUD_API_KEY="clip-..."`).

## The window

`clipcloud-batch gui` opens a page in your browser. It works only on your computer (127.0.0.1).

1. **Prompts.** Drop a table or paste prompts, one per line. No table yet? The window has an
   example to download.
2. **Check the rows.** The columns are matched by name; change them if needed, and say whether the
   first row is a header. Every row shows what will be sent, and skipped rows say why (no prompt,
   unknown style, a comma without quotes).
3. **Settings.** Style for rows without one, images per prompt, format, things to avoid, folder.
4. **Price and start.** The price per style, what today's free images cover, and one Start button.

![Price and free-image plan](https://raw.githubusercontent.com/alexmrgit/clipcloud-batch/main/docs/plan.jpg)

While it works you see each row with its images. Click an image to see it large. Filters show
what is done, in progress, waiting or needs attention:

- **blocked by the content filter**: not charged; rewrite the prompt in place and send it again;
- **failed**: send it again;
- **lost**: ClipCloud answered with errors for this job, but it may still finish. Check it again for
  free; continuing the batch also checks it once more. Sending again may pay twice, so it asks first;
- **uncertain**: the request may have reached ClipCloud without an answer, so it may be paid.
  It is never sent again unless you confirm.

![A finished batch](https://raw.githubusercontent.com/alexmrgit/clipcloud-batch/main/docs/batch.jpg)

You can close the browser tab: the batch goes on while the program runs. Stopped or waiting
batches are listed on the first page with a **Continue** button, also after a restart.

![Unfinished batches on the first page](https://raw.githubusercontent.com/alexmrgit/clipcloud-batch/main/docs/continue.jpg)

## Command line

```bash
clipcloud-batch example                          # write prompts.csv to start from
clipcloud-batch plan prompts.csv                 # price and free-image plan, sends nothing
clipcloud-batch run prompts.csv                  # make the images into "prompts images/"
clipcloud-batch run -o "prompts images"          # continue after a stop, a crash or the next day
clipcloud-batch status "prompts images"          # what happened to each row
clipcloud-batch styles                           # styles, sizes and prices
```

A real run of 50 rows, stopped with Ctrl+C and started again (shortened):

```text
$ clipcloud-batch run fifty.csv
50 rows, 50 images. Full price $0.025.
  lowquality: 50 images x $0.0005 = $0.025
Send 50 rows? [y/N] y
[20:32:25] row 4 sent
...
[20:32:39] row 51 sent
^C
Stopping. Requests already on the way are allowed to finish (up to two minutes), so their job ids are saved.
Rows: sent 50
Run again to continue: clipcloud-batch run -o "fifty images". Nothing is sent twice.

$ clipcloud-batch run -o "fifty images"
50 rows were sent before and are still being made. They are only checked, not sent again.
[20:33:48] row 7 done -> 0007-a-bowl-of-ramen-soft-morning-light.webp
...
[20:36:33] row 48 done -> 0048-a-rainy-window-at-sunset.webp
Rows: done 50
Spent on this batch: $0.025. Results: fifty images/results.csv
```

Options for `run`:

| Option | What it does |
|---|---|
| `-o folder` | output folder (default: `<table name> images` next to the table) |
| `--style HD-HQ` | style for rows without one |
| `--count 4` | images per row when the table has no count column (1 to 10) |
| `--format png` | `webp` (as made), `png` or `jpg` |
| `--free-only` | send only what today's free images cover; run again tomorrow for the rest |
| `--max-usd 1` | stop sending when the price reaches this sum |
| `--name "{style}/{row}"` | file name pattern: `{row}`, `{prompt}`, `{style}` |
| `--col style=Look` | use the column "Look" for the style |
| `--header yes` | the first row holds column names (`no`: it is a prompt; default: guessed) |
| `--unknown-style default` | rows with a style that does not exist get `--style` instead of being skipped |
| `--post-concurrency 5` | requests sent at the same time |
| `-y` | start without asking |

Fixing rows after a run (you decide, nothing is resent on its own):

```bash
clipcloud-batch censored images                  # writes censored.csv: fill new_prompt
clipcloud-batch retry images --from images/censored.csv
clipcloud-batch retry images --all-failed        # send failed rows again
clipcloud-batch retry images --all-lost --recheck  # ask about the same jobs again, free
clipcloud-batch retry images --rows 9 --resend-uncertain   # only if you checked: may pay twice
```

## The table

Column names are found automatically (`prompt`, `description`, `title`, `style`, `look`, `count`,
`negative`, `filename` and similar), or set with `--col`.

| Column | What | If empty |
|---|---|---|
| prompt | what to draw | the row is skipped |
| style | a ClipCloud style, or a common word like watercolor, photo, pixel art, vertical | `--style`, default HD-HQ |
| count | images for this prompt, 1 to 10 | `--count`, default 1 |
| negative | what to avoid | `--negative` |
| filename | file name, may include a folder: `cats/tabby` | `0007-a-red-fox` (row and prompt) |

A `.txt` file is one prompt per line (or a table, if its lines have tabs). A CSV without a header
row is read as one whole prompt per line, commas included; add a header row to use columns.
In Excel files `--sheet` (or the window) picks the sheet. Save CSV files as "CSV UTF-8".
Row numbers are the row numbers you see in Excel. Files that are already in the output folder are
never overwritten.

The output folder gets the images, `results.csv` (row, status, files, cost, job id, error, hint)
and `.clipcloud-batch.sqlite`, the job journal. Keep the journal until the batch is done.

## Why not just a script

A loop with `requests.post` takes a minute to write. These parts take longer, and they are where
money gets lost:

- **Nothing is paid twice.** Each row is written to the journal before its request goes out, and
  its job id right after. After a crash the next run only checks jobs that were already started.
  A request that may have reached the server without an answer is marked "uncertain" and is never
  sent again on its own. Requests are repeated only when they surely did not leave your computer,
  or the server said it started nothing. Two programs cannot work on one folder at once.
- **Free-limit planning.** A paid balance is used first and free images after it. The planner
  splits the table by day, and "only free images" keeps each day inside the limit.
- **Polite to the server.** About 5 requests at once, up to 50 jobs in progress, a check every
  30 seconds. "Too many requests" pauses all sending for a while. When the balance runs out, the key
  stops working or the connection drops, sending stops and the rest wait.
- **Blocked prompts** are marked, not charged, and never resent with the same text.
- **Checked downloads.** An error page instead of an image is caught, not saved as a picture.
- **Big tables.** Tested with 5,000 rows.

## Ways to pay

| Route | Key | Price |
|---|---|---|
| `direct` (default) | ClipCloud key `clip-...` in `CLIPCLOUD_API_KEY` | [ClipCloud prices](https://clipcloud.cliplabs.top/pricing/), 25 free a day |
| `apimarket` | [api.market](https://api.market/) key in `API_MARKET_KEY` | api.market price per image |
| `apify` | [Apify](https://apify.com/) token in `APIFY_TOKEN` | the Apify actor's price, billed by Apify |

Pick one with `--route apimarket`, or in the window (the key dialog also guesses it from the key).
`--key-file path` reads the key from a file. The window can remember a key in
`~/.config/clipcloud-batch/keys.json`, stored in plain text in your user folder.
On Apify each style has its price tier (draft, basic, HD, Full HD, 4K) plus a small fee per run.

## Good to know

- Jobs take 1 to 15 minutes, bigger styles longer. Rows run in parallel: 50 rows took about
  4 minutes in our test.
- Image links from the API are temporary, so images are downloaded as soon as they are ready.
- A job that is not ready after 30 minutes is marked "still not ready". Continuing the batch checks
  it again (`--max-wait` changes the limit).
- Free images go into a slower queue than paid ones.
- English prompts work best.
- On a server without a screen: `clipcloud-batch gui --port 8765 --no-browser` there, then
  `ssh -L 8765:127.0.0.1:8765 server` on your computer and open http://127.0.0.1:8765/.
- `CLIPCLOUD_BASE_URL` sets another API address, for example the mirror
  `https://clipcloud-rumirror.clipai.pro`.
- ClipCloud developers can add their developer id (`soft_id`) with `--soft-id N` or
  `CLIPCLOUD_SOFT_ID`. Nothing is sent by default.

## Development

```bash
python -m venv .venv && .venv/bin/pip install -e ".[test]"
.venv/bin/pytest
```

The tests use a fake API, no key needed. Code map: `journal.py` (row states in SQLite),
`runner.py` (the queue), `routes.py` (direct, api.market, Apify), `planner.py` (price and free days),
`table.py` (reading tables), `gui.py` and `gui.html` (the window).

License: MIT.
