Generate novel-length Markdown books using Moonshot AI's Kimi K2.5 reasoning models (262k context, 1T parameters).
β¨ New: FLUX.2 image generation for cover and chapter illustrations! π One-click launch in GitHub Codespaces!
Two ways to use:
- π₯οΈ Web UI - Streamlit-based interface for easy novel generation and management
- π» CLI - Command-line interface for automated workflows
Features:
- π Novel generation with Kimi K2.5 (262k context, multi-step reasoning)
- π¨ Optional illustrations via FLUX.2 models on OpenRouter (cover + chapter images)
- β‘ Async image generation - images generate concurrently with text, not blocking
- βΈοΈ Pause/Resume - stop anytime and continue later
- π Live progress - real-time updates during generation
API essentials:
- Base URL:
https://api.moonshot.ai/v1 - Install OpenAI SDK
>=1.x - Recommended model:
kimi-k2.5(262k context, 1T parameters) - Image generation: FLUX.2 via OpenRouter (optional)
The easiest way to get started is using GitHub Codespaces:
-
Fork this repository first (click the Fork button at the top right)
-
In your fork, click the Code button β Codespaces β Create codespace on
main -
Wait for the environment to set up (automatic)
-
Create a
.envfile with your Moonshot API key:cp .env.example .env # Edit .env and add: MOONSHOT_API_KEY=sk-... -
Start the web UI:
# Option 1: Direct launch streamlit run app.py # Option 2: Using helper script (auto-installs dependencies) bash run-ui.sh
-
Click the notification to open the UI (or go to the Ports tab and open port 8501)
Why fork first? Forking gives you your own copy of the repository where you can save your generated novels. The "Publish" feature commits novels to your fork, preserving your work.
Web UI Features:
- π Generate - Create new novels with a user-friendly form
- π¨ Illustrations - Optional FLUX.2 cover and chapter images (async generation)
- βΈοΈ Pause/Resume - Stop generation anytime and continue later
- π Live Progress - Real-time sidebar progress panel with chapter-by-chapter updates
- π Background Generation - Non-blocking UI; generation runs in background thread
- π Library - Manage preview and published novels
- π Reader - Read novels with chapter navigation and images
- β Publish - Move novels from preview to published (auto-commits to repo)
- β¬οΈ Download - Export novels as Markdown files
Directory Structure:
preview/- Novels being worked on (gitignored)published/- Finalized novels (committed to repo)
If you prefer the command-line interface:
# Using pip
pip install -e .
# Or using uv (recommended)
uv sync
# Configure
cp .env.example .env
# Edit .env and paste your Moonshot key:
# MOONSHOT_API_KEY=sk-...Optional .env overrides:
# Text generation (Moonshot)
KIMI_MODEL=kimi-k2.5
KIMI_TEMPERATURE=1.0
KIMI_TOP_P=0.95
KIMI_MAX_OUTPUT_TOKENS=8192
# Image generation (OpenRouter) - optional
OPENROUTER_API_KEY=sk-or-v1-...
FLUX_MODEL=black-forest-labs/flux.2-klein-4b # or flux.2-max for higher quality
IMAGE_GENERATION_TIMEOUT=180 # seconds, increase for slower modelsUsing Make targets (recommended):
make help # See all available commands
make web # Launch Streamlit web UI
make novel # Interactive CLI (text only)
make novel-images # Interactive CLI with illustrations
make resume # Continue from saved state
make clean # Remove CLI artifactsDirect CLI usage:
python kimi_writer.py # Interactive
python kimi_writer.py --prompt "..." --title "..." --out book.md # Non-interactive
python kimi_writer.py --images # Enable image generation
python kimi_writer.py --no-images # Disable images (faster)
python kimi_writer.py --flux-model black-forest-labs/flux.2-max # Use specific FLUX modelCLI flags:
--resumeContinue fromnovel_state.json--chapters NLimit chapters:- Without
--resume: Write up to N chapters total - With
--resume: Write N more chapters from current progress
- Without
--imagesEnable FLUX.2 image generation (requiresOPENROUTER_API_KEY)--no-imagesDisable image generation--flux-model MODELSpecify FLUX model (flux.2-klein-4borflux.2-max)
Artifacts:
novel.md- full Markdown output (title, outline, chapters, images) β customizable via--outnovel_state.json- checkpoint/resume state for the CLI<slug>_images/- generated cover and chapter images (if enabled)
- Outline phase. Asks K2.5 to create a 20-40 chapter outline as a numbered Markdown list.
- Cover image (if enabled). Submits cover image generation to async queue.
- Chapter phase. Iterates over chapter titles, requesting ~1.5-2.5k-word chapters. A short rolling context (last few chapter snippets) is sent to preserve continuity without exhausting context.
- Chapter images (if enabled). Each chapter image is submitted to async queue after text completes.
- Image completion. At end, waits for all pending images to finish.
- Streaming + retries. Output streams to console (dots), with exponential backoff for robustness.
Async Image Generation: Images generate concurrently with text (up to 2 simultaneous image requests). This means chapter writing isn't blocked waiting for images, significantly reducing total generation time when images are enabled.
-
Generate Tab
- Enter novel concept, title, and settings
- Generation runs in background thread (UI stays responsive)
- Live progress panel in sidebar shows current chapter
- Pause anytime with the pause button; resume from Library
- Novels auto-saved to
preview/directory after each chapter
-
Library Tab
- Browse preview and published novels
- Continue incomplete novels with one click
- Read novels with chapter navigation
- Download as Markdown files
- Publish complete novels (moves to
published/, commits to repo) - Delete unwanted novels
-
Reader Mode
- Clean reading interface
- Chapter-by-chapter navigation with dropdown selector
- Previous/Next chapter buttons
- Full markdown rendering
- 262K context for long novel projects
- 1T parameters with multi-step reasoning
- Optimized temperature=1.0 for creative writing
- High throughput for chapter generation
kimi-book-writer/
βββ app.py # Streamlit web UI (background thread generation)
βββ kimi_writer.py # CLI novel generator
βββ utils.py # Shared utilities (outline parsing, validation)
βββ image_gen.py # FLUX.2 image generation via OpenRouter
βββ async_image_gen.py # Async image queue with ThreadPoolExecutor
βββ Makefile # Make targets for common operations
βββ run-ui.sh # Helper script to launch web UI
βββ tests/ # pytest test suite
β βββ conftest.py # Test fixtures
β βββ test_kimi_writer.py
β βββ test_image_gen.py
β βββ test_utils.py
βββ preview/ # Draft novels (gitignored)
β βββ <slug>_images/ # Generated images per novel
βββ published/ # Published novels (committed)
βββ examples/ # Example generated novels
βββ .devcontainer/ # Codespaces configuration
βββ .env.example # Environment template
βββ pyproject.toml # Dependencies (Python 3.10+, Streamlit 1.37+)
βββ AGENTS.md # Repository guidelines for AI assistants
βββ CLAUDE.md # Project-specific instructions
- Both the CLI and Web UI share the same core generation logic and prompts
- Prompts in
kimi_writer.pycan be customized for different genres/styles - The Web UI runs generation in a background thread, so the UI remains responsive
- Progress is saved after each chapter, enabling safe pause/resume
- Requires Streamlit 1.37.0+ for the
@st.fragment(run_every=...)auto-refresh feature - Supports Kimi K2.5 which returns content via
delta.thinkinganddelta.contentstreaming - Image generation is async - images generate concurrently with text (2 workers)
- Images are optional; generation works without
OPENROUTER_API_KEY - CLI is useful for automation, scripting, and headless server workflows
Run the test suite with:
uv run pytest # Using uv (recommended)
pytest # Using pip-installed pytestTests are deterministic and do not make live API calls.
MIT