From a31d11f59fc60e609f1538b433036a7ee9bac806 Mon Sep 17 00:00:00 2001 From: deltaaa00 Date: Sun, 15 Nov 2026 09:48:26 +0200 Subject: [PATCH 01/23] Add DeepSeek support with independent API keys and rewrite README --- cli.py | 17 +- docker-compose.yml | 1 + example.env | 1 + main.py | 94 ++++++-- map/functions.py | 96 ++++++++ readme.md | 373 +++++++++++++++++------------ templates/index.html | 124 ++++++++-- tests/browser/test_map_palettes.py | 107 ++++++++- tests/test_cli.py | 11 + tests/test_deepseek.py | 163 +++++++++++++ 10 files changed, 785 insertions(+), 202 deletions(-) create mode 100644 tests/test_deepseek.py diff --git a/cli.py b/cli.py index 867b5fd..2e86ad3 100644 --- a/cli.py +++ b/cli.py @@ -33,6 +33,9 @@ "gemini-3-flash-preview", "gemma-4-31b-it", "gemma-4-26b-a4b-it", + # Current DeepSeek API model identifiers: https://api-docs.deepseek.com/ + "deepseek-flash", + "deepseek-v4-pro", ] @@ -98,7 +101,7 @@ def main(): @click.option("--key", "api_key", default=None, - help="Gemini API key for this run (overrides GEMINI_API_KEY env var).") + help="API key for the selected model provider (overrides its environment key).") @click.option("--temperature", type=click.FloatRange(0, 2), default=None, help="AI sampling randomness, from 0 to 2. Omit to use the model default.") def make(prompt, mode, map_type, size_mod, optional_feature, model, font_name, show_result, verbose, api_key, temperature): @@ -110,7 +113,7 @@ def make(prompt, mode, map_type, size_mod, optional_feature, model, font_name, s """ # Lazy import so `automap --help` stays fast and doesn't require API keys. from map import log - from map.functions import AIServiceError + from map.functions import AIServiceError, provider_for_model, provider_key_name from map.auto_map import auto_map ai_options = {} @@ -129,7 +132,7 @@ def make(prompt, mode, map_type, size_mod, optional_feature, model, font_name, s # A key passed on the command line overrides the env var for this run only. if api_key: - os.environ["GEMINI_API_KEY"] = api_key + os.environ[provider_key_name(provider_for_model(model))] = api_key # Click accepts mixed-case choices; the renderer compares lowercase names. try: @@ -201,17 +204,17 @@ def make(prompt, mode, map_type, size_mod, optional_feature, model, font_name, s @click.option("--key", "api_key", default=None, - help="Gemini API key for this run (overrides GEMINI_API_KEY env var).") + help="API key for the selected model provider (overrides its environment key).") def batch(ideas_file, output_dir, mode, map_type, size_mod, optional_feature, model, font_name, verbose, api_key): """Generate multiple maps from ideas listed in a .csv IDEAS_FILE.""" from map import log - from map.functions import AIServiceError + from map.functions import AIServiceError, provider_for_model, provider_key_name from map.auto_map import auto_map from map.functions import load_ideas_from_file, slugify_prompt log.set_verbose(verbose) if api_key: - os.environ["GEMINI_API_KEY"] = api_key + os.environ[provider_key_name(provider_for_model(model))] = api_key try: ideas = load_ideas_from_file(ideas_file) @@ -253,7 +256,7 @@ def batch(ideas_file, output_dir, mode, map_type, size_mod, optional_feature, mo font_name=item_font, show_result=False, io_res=True, - api_key=api_key, + api_key=api_key if provider_for_model(item_model) == provider_for_model(model) else None, **style_options, ) except AIServiceError as error: diff --git a/docker-compose.yml b/docker-compose.yml index 56bd1ce..7718c25 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,6 +13,7 @@ services: WEB_PASSWORD: "${WEB_PASSWORD:-deltaaa00!}" WEB_SECRET_KEY: "${WEB_SECRET_KEY:-}" GEMINI_API_KEY: "${GEMINI_API_KEY:-}" + DEEPSEEK_API_KEY: "${DEEPSEEK_API_KEY:-}" YTDLP_URL: http://yt-dlp-worker:8080 volumes: - automap-session:/app/instance diff --git a/example.env b/example.env index af606c7..f550408 100644 --- a/example.env +++ b/example.env @@ -1,4 +1,5 @@ GEMINI_API_KEY=your_google_ai_studio_key +DEEPSEEK_API_KEY=your_deepseek_api_key SERVER=TRUE WEB_LOGIN=deltaaa WEB_PASSWORD=deltaaa00! diff --git a/main.py b/main.py index df20abb..f6c0ba0 100644 --- a/main.py +++ b/main.py @@ -15,10 +15,9 @@ """Flask web interface for AutoMap. Serves the single-page UI in ``templates/index.html`` and exposes a small JSON -API used by that UI. The API key handling is the interesting part: if a ``.env`` -file with a real ``GEMINI_API_KEY`` is present, the web UI never asks for a key. -If it is missing, ``/api/status`` reports it and the UI shows a key input, whose -value is forwarded to the generation request only. +API used by that UI. Gemini and DeepSeek use separate keys from the environment +or ``.env``. If the selected provider has no configured key, ``/api/status`` +reports it and the UI uses that provider's browser-saved key for the request. """ import base64 @@ -55,7 +54,10 @@ ) from map.constants import europe_coords, world_coords from cli import AVAILABLE_MODELS -from map.functions import AIServiceError, parse_ideas_content, slugify_prompt, validate_temperature +from map.functions import ( + AIServiceError, configured_api_key, list_deepseek_models, parse_ideas_content, + provider_for_model, provider_key_name, slugify_prompt, validate_temperature, +) from flask import Flask, request, jsonify, send_file, render_template, Response @@ -99,7 +101,7 @@ def validate_api_json_body(): data = request.get_json(silent=True) if not isinstance(data, dict): return jsonify(status='error', error='Request body must be a valid JSON object.'), 400 - for name in ('api_key', 'url'): + for name in ('api_key', 'url', 'provider', 'model'): if data.get(name) is not None and not isinstance(data[name], str): return jsonify(status='error', error=f'{name} must be a string.'), 400 @@ -180,12 +182,43 @@ def api_codecs(): @app.route('/api/status') def api_status(): - """Report whether the server already has a usable Gemini API key. + """Report whether the selected provider already has a server-side key. Response: { "needs_api_key": true } -> the UI must ask the user for a key """ - return jsonify({"needs_api_key": needs_api_key}) + provider = request.args.get('provider', 'gemini') + if provider not in ('gemini', 'deepseek'): + return jsonify(status='error', error='Unknown AI provider.'), 400 + required = needs_api_key if provider == 'gemini' else not _server_api_key(provider) + return jsonify({"needs_api_key": required}) + + +def _server_api_key(provider): + return configured_api_key(provider, dotenv_values(env) if env.is_file() else {}) + + +def _request_provider(data): + inferred = provider_for_model(data.get('model') or '') + provider = data.get('provider') or inferred + if provider not in ('gemini', 'deepseek'): + raise ValueError('Unknown AI provider.') + if data.get('model') and inferred != provider: + raise ValueError('The selected model belongs to a different AI provider.') + return provider + + +def _request_api_key(data, provider): + supplied = str(data.get('api_key') or '').strip() or None + if provider == 'deepseek': + resolved = _server_api_key(provider) or supplied + else: + resolved = supplied + if not needs_api_key: + return resolved + if not resolved: + raise ValueError(f'API key required. Provide {provider_key_name(provider)} via .env or the web form.') + return resolved def _build_analysis_from_ai( @@ -423,10 +456,11 @@ def generate(): data = request.get_json(silent=True) or {} # Optional user-supplied key (used only when .env has none). - api_key = (data.get('api_key') or '').strip() or None - if needs_api_key and not api_key: - return jsonify({"status": "error", - "error": "API key required. Provide GEMINI_API_KEY via .env or the web form."}), 400 + try: + provider = _request_provider(data) + api_key = _request_api_key(data, provider) + except ValueError as error: + return jsonify(status='error', error=str(error)), 400 # Validate the requested font against what is actually on disk, otherwise # auto_map would raise FileNotFoundError when loading the TrueType file. @@ -514,10 +548,22 @@ def generate(): @app.route('/api/models', methods=['POST']) def list_available_models(): - """Return Gemini/Gemma models available for the active API key.""" + """Return models available for the selected provider's API key.""" data = request.get_json(silent=True) or {} + try: + provider = _request_provider(data) + except ValueError as error: + return jsonify(status='error', error=str(error), models=[]), 400 + if provider == 'deepseek': + try: + models = list_deepseek_models(_request_api_key(data, provider)) + return jsonify(status='success', models=models) + except ValueError as error: + return jsonify(status='error', error=str(error), models=[]), 400 + except AIServiceError as error: + return jsonify(status='error', error=str(error), models=[]), error.status_code req_key = str(data.get("api_key") or "").strip() - env_key = (os.getenv("GEMINI_API_KEY") or "").strip() + env_key = _server_api_key('gemini') or '' key = req_key or env_key if not key: return jsonify({"status": "error", "error": "API key required.", "models": []}), 400 @@ -577,12 +623,11 @@ def generate_batch(): filename = str(data.get("filename") or "") ideas_text = str(data.get("ideas_text") or "") - api_key = str(data.get("api_key") or "").strip() or None - if needs_api_key and not api_key: - return jsonify({ - "status": "error", - "error": "API key required. Provide GEMINI_API_KEY via .env or the web form.", - }), 400 + try: + provider = _request_provider(data) + api_key = _request_api_key(data, provider) + except ValueError as error: + return jsonify(status='error', error=str(error)), 400 try: ideas = parse_ideas_content(ideas_text, filename=filename) @@ -595,7 +640,7 @@ def generate_batch(): default_font, default_eop, default_gsm = _parse_render_params(data) default_mode = str(data.get("mode") or "text").lower() default_map_type = str(data.get("map_type") or "default").lower() - default_model = str(data.get("model") or AVAILABLE_MODELS[0]) + default_model = str(data.get("model") or ('deepseek-flash' if provider == 'deepseek' else AVAILABLE_MODELS[0])) available_fonts = set(_available_fonts()) temperature_options: dict[str, object] = {} @@ -641,6 +686,10 @@ def generate_batch(): out_filename += ".png" try: + item_provider = provider_for_model(item_model) + item_api_key = api_key if item_provider == provider else _server_api_key(item_provider) + if not item_api_key and (item_provider == 'deepseek' or needs_api_key): + raise ValueError(f'API key required for {item_provider}. Use one provider per CSV or configure its server key.') buffer = auto_map( prompt=prompt, mode=item_mode, @@ -651,7 +700,7 @@ def generate_batch(): font_name=item_font, show_result=False, io_res=True, - api_key=api_key, + api_key=item_api_key, **temperature_options, **item_style_kwargs, ) @@ -687,6 +736,7 @@ def generate_batch(): "gsm": item_gsm, "extra_option": item_eop or "none", "model": item_model, + "provider": item_provider, "font": item_font, "palette_theme": item_style_kwargs.get("palette_theme", "auto"), } diff --git a/map/functions.py b/map/functions.py index ed8e689..c3da309 100644 --- a/map/functions.py +++ b/map/functions.py @@ -2,8 +2,10 @@ import io import json import math +import os import re import sys +import requests try: import httpx2 as httpx except ImportError: # Older google-genai releases use the original transport. @@ -20,6 +22,22 @@ load_dotenv(".env") # loading .env file AI_TIMEOUT_MS = 45_000 +DEEPSEEK_API_URL = 'https://api.deepseek.com' + + +def provider_for_model(model): + """DeepSeek models use their own endpoint and credentials.""" + return 'deepseek' if str(model).lower().startswith('deepseek-') else 'gemini' + + +def provider_key_name(provider): + return 'DEEPSEEK_API_KEY' if provider == 'deepseek' else 'GEMINI_API_KEY' + + +def configured_api_key(provider, settings=None): + name = provider_key_name(provider) + value = (os.environ.get(name) or (settings or {}).get(name) or '').strip() + return value if value and not value.lower().startswith('your_') else None class AIServiceError(RuntimeError): @@ -30,6 +48,81 @@ def __init__(self, message, status_code=503): self.status_code = status_code +def _deepseek_json(path, api_key, *, payload=None, timeout=45): + """Call only the fixed DeepSeek origin; never expose upstream credential errors.""" + if not api_key: + raise AIServiceError('DeepSeek API key required. Set DEEPSEEK_API_KEY or enter it in the web form.', 400) + headers = {'Authorization': f'Bearer {api_key}', 'Accept': 'application/json'} + try: + if payload is None: + response = requests.get(f'{DEEPSEEK_API_URL}/{path}', headers=headers, + timeout=(10, timeout), allow_redirects=False) + else: + response = requests.post(f'{DEEPSEEK_API_URL}/{path}', headers=headers, json=payload, + timeout=(10, timeout), allow_redirects=False) + except requests.Timeout as error: + raise AIServiceError(f'DeepSeek did not respond within {timeout} seconds. Try again or select another model.', 504) from error + except requests.RequestException as error: + raise AIServiceError('Could not connect to DeepSeek. Please try again later.', 503) from error + try: + status = response.status_code + if status in (401, 403): + raise AIServiceError('DeepSeek rejected the API key. Check your key and account access.', 400) + if status == 402: + raise AIServiceError('Your DeepSeek account has insufficient balance. Top up your DeepSeek account.', 402) + if status == 429: + raise AIServiceError('DeepSeek request limit reached. Please try again later.', 429) + if status >= 500: + raise AIServiceError('DeepSeek is overloaded or temporarily unavailable. Try again shortly or select another model.', 503) + if status != 200: + raise AIServiceError('DeepSeek could not process the request. Check the selected model and API access.', 400) + try: + result = response.json() + except ValueError as error: + raise AIServiceError('DeepSeek returned an invalid response. Please try again.', 502) from error + if not isinstance(result, dict) or result.get('error'): + raise AIServiceError('DeepSeek could not complete the request. Please try again.', 502) + return result + finally: + response.close() + + +def list_deepseek_models(api_key): + result = _deepseek_json('models', api_key, timeout=15) + models = result.get('data') + if not isinstance(models, list): + raise AIServiceError('DeepSeek returned an invalid model list.', 502) + return sorted({model['id'] for model in models if isinstance(model, dict) + and isinstance(model.get('id'), str) and model['id'].startswith('deepseek-')}) + + +def _deepseek_request(prompt, model, api_key=None, *, temperature=None): + payload = { + 'model': model, + 'messages': [ + {'role': 'system', 'content': 'Return only a JSON object mapping region names to values. Example: {"ukraine": 42}.'}, + {'role': 'user', 'content': prompt}, + ], + 'response_format': {'type': 'json_object'}, + 'thinking': {'type': 'disabled'}, + 'max_tokens': 4096, + 'stream': False, + } + if temperature is not None: + payload['temperature'] = validate_temperature(temperature) + result = _deepseek_json('chat/completions', api_key or configured_api_key('deepseek'), payload=payload) + try: + choice = result['choices'][0] + content = choice['message']['content'] + except (KeyError, IndexError, TypeError) as error: + raise AIServiceError('DeepSeek returned no map data. Please try again.', 502) from error + if choice.get('finish_reason') == 'length': + raise AIServiceError('DeepSeek returned an incomplete map. Try a shorter prompt.', 502) + if not isinstance(content, str) or not content.strip(): + raise AIServiceError('DeepSeek returned no map data. Please try again.', 502) + return content + + def _error(title: str, message: str = ""): """Print an error message to stderr (CLI-safe replacement for tkinter showerror).""" log.error(f"{title}: {message}" if message else title) @@ -76,6 +169,9 @@ def _ai_request(prompt, mode, ai_model, api_key=None, *, temperature=None) -> st config = {} if temperature is None else { 'config': types.GenerateContentConfig(temperature=validate_temperature(temperature)), } + if provider_for_model(ai_model) == 'deepseek': + with log.spinner('Waiting for DeepSeek response...'): + return _deepseek_request(prompt_to_ai, ai_model, api_key, temperature=temperature) try: with log.spinner("Waiting for AI response..."): # If a key was supplied (e.g. from the web UI when .env is missing), diff --git a/readme.md b/readme.md index fe7e519..bdf5791 100644 --- a/readme.md +++ b/readme.md @@ -1,229 +1,294 @@ -# AutoMap-cli (AutoMap v2.0) +# AutoMap -![Logo](resources/demo/banner.png) +![AutoMap](resources/demo/banner.png) -**AutoMap** is a map-generating tool. Describe what you want in plain language and AutoMap uses AI to collect the data, then paints it onto a map for you — a coloured, labelled map in seconds. +AutoMap turns a natural-language topic into a colored, labeled map and can place that map in a short portrait video. It includes a web interface, a command-line interface, and a Python renderer. -It ships with three parts: +Choose **Google Gemini** or **DeepSeek** for map data. Customize the palette and labels, generate several maps from a CSV file, or open **Video Studio** to combine a map with background footage, a headline, branding, and optional music. -- a **CLI** (`cli.py` / the `automap` command), -- a **web UI** (a small Flask app in `main.py`), -- and a Python API (`map.auto_map.auto_map`). +AI-generated values can be approximate or incorrect. Check the underlying data before publishing a map; a prompt asking for current information does not guarantee verified sources or live web research. -## Features +## What you can make -AutoMap can render three map types: +| Map type | Coverage | Appearance | +| --- | --- | --- | +| `default` | European countries, with the regions included in the bundled artwork | Filled regions with relief and borders | +| `flag` | The same European layout | Flag artwork with labels | +| `world` | Continents | Filled continent regions with relief and borders | -| Map type | Description | -| --------- | ------------------------------- | -| `default` | Europe (default map) | -| `flag` | Europe with flag-style borders | -| `world` | World map grouped by continent | +Maps support categorical **text** values and **number** values. Numeric palettes preserve decimal values and interval boundaries. Short numeric strings such as `10k`, `2.5M`, and `1B` are understood by numeric coloring and Most/Least calculations. -Data can be shown in two modes: +The web interface provides: -- **`text`** — categorical values (e.g. `Coffee` → a specific colour). -- **`number`** — numeric values, coloured by value ranges. +- Separate **Gemini** and **DeepSeek** sections in the model selector, with independent API keys. +- Single-map generation and CSV batch generation. +- **Theme & Style** in the left panel, opening Color Filling Studio for palettes, category rules, numeric intervals, and label styling. +- Repainting from an existing map's values without another AI request. +- Palette inspection from an uploaded image. This samples colors; it does not recover the image's underlying country data. +- PNG downloads, run history, and transfer of the latest generated map to Video Studio. +- Video Studio at `/studio`, with title previews, custom overlays, music download/upload and trimming, and MP4 rendering. -Optional extras: +![Example map](resources/demo/map_example.png) -- `show_most_least` — highlight the country/continent with the most and least of a value. -- `short_form` — shorten large numbers (e.g. `1000000` → `1M`). +## Quick start with Docker -Here is an example (*default map*): +Docker Compose starts both required services: the web application and the media worker. The worker image includes FFmpeg, Node.js, and the Python media dependencies. -![Map example](resources/demo/map_example.png) +```sh +git clone https://github.com/deltaaa00/AutoMap-cli.git +cd AutoMap-cli +git switch dev +cp example.env .env +``` + +Edit `.env`. Configure either API key, both keys, or leave their placeholder values and enter a key in the browser instead. Set your own login credentials before exposing the app to other users. + +```dotenv +GEMINI_API_KEY=your_google_ai_studio_key +DEEPSEEK_API_KEY=your_deepseek_api_key +SERVER=TRUE +WEB_LOGIN=your_username +WEB_PASSWORD=your_password +# Optional: use a stable, random signing secret instead of the generated file. +# WEB_SECRET_KEY=your_long_random_secret +``` -## Stack +```sh +docker compose up --build -d +``` + +Open [http://localhost:3000](http://localhost:3000). With `SERVER=TRUE`, sign in using the credentials from `.env`. -Libraries AutoMap uses today: +Useful commands: -- **AI** - - [google-genai](https://github.com/googleapis/python-genai) -- **Drawing** - - [pillow](https://github.com/python-pillow/Pillow) -- **Web UI** - - [flask](https://github.com/pallets/flask) -- **CLI** - - [click](https://github.com/pallets/click) - - [rich](https://github.com/Textualize/rich) -- **Other** - - [python-dotenv](https://github.com/theskumar/python-dotenv) +```sh +docker compose logs -f web yt-dlp-worker +docker compose up --build -d # Apply code, dependency, or configuration changes. +docker compose down # Stop and remove containers; retain named volumes. +``` -## Installation +Compose publishes only the web application (`3000` on the host to `5000` in the container). The media worker listens on `8080` inside the private Compose network. Both services use `restart: unless-stopped`. -To get started with **AutoMap**, follow these steps: +The `automap-session` volume retains the generated session secret. The `automap-media` volume stores worker media, which is still subject to expiry cleanup. Removing a volume removes its contents. -1. **Clone the repository:** - ```bash - git clone https://github.com/deltaaa00/auto_map.git - cd automap - ``` +## API keys and model selection -2. **Set up a virtual environment:** - ```bash - python -m venv venv - # Windows: - venv\Scripts\activate - # macOS / Linux: - source venv/bin/activate - ``` +Get a key from [Google AI Studio](https://aistudio.google.com/apikey) or the [DeepSeek platform](https://platform.deepseek.com/). -3. **Install dependencies:** - ```bash - pip install -r requirements.txt - ``` +| Provider section | Server setting | Built-in default or fallback | +| --- | --- | --- | +| Gemini, including available Gemma models | `GEMINI_API_KEY` | Default: `gemini-3.8-flash` | +| DeepSeek | `DEEPSEEK_API_KEY` | `deepseek-flash`, `deepseek-v4-pro` | - For testing, install `requirements-test.txt` instead. It includes runtime - dependencies and test tools. See [tests/README.md](tests/README.md) for test commands. +The web app requests the models available for the active provider's key and keeps built-in options if discovery is unavailable. Model availability, quota, and billing depend on the provider and account. Use `python cli.py make --help` to see the CLI's supported model choices. -4. **Configure your API key:** - AutoMap reads `GEMINI_API_KEY` from a `.env` file (see `example.env`). - - Copy `example.env` to `.env`. - - Get a key from [Google AI Studio](https://aistudio.google.com/apikey). - - Set it in `.env`: `GEMINI_API_KEY=your_key_here`. +Keys can come from the process environment or the project-root `.env` file. Environment settings take precedence over file settings. Compose forwards both provider keys from its launching environment or `.env`. - On Windows/macOS you can also use the helper scripts in `util/` (`key_man.bat`, `key_man.command`) to manage the key. +In the browser: -5. **Optionally customize your filling configuration:** - The bundled template is used automatically when `map/filling.json` is absent. - Copy it to override the default rules: - ```bash - cp map/filling.template.json map/filling.json - ``` +1. Choose a model from the **Gemini** or **DeepSeek** section. +2. If that provider has no configured server key, enter its key in the displayed field. +3. The shared key field switches with the selected model. Keys are saved separately in this browser for the next visit: switching providers restores the matching key, and clearing the current provider's field leaves the other saved key untouched. -## Usage +Browser-supplied keys are sent to the application server for the active provider's requests. Server-configured keys are not returned to the browser. Browser persistence requires storage to remain enabled; it is specific to the browser profile and site origin. -### Web UI +DeepSeek uses its official Chat Completions API with JSON output and thinking disabled for map generation. No separate OpenAI SDK is required. API requests have a 45-second timeout and no application-level automatic retry. Map drawing takes additional time. Provider overload, quota limits, invalid credentials, insufficient DeepSeek balance, and timeouts are reported in the interface. -Start the Flask server: +## Run from the source checkout -```bash -python main.py +Use **Python 3.12**, matching the Docker images and test workflow. Keep the repository's `resources/` directory available: the renderer uses its map artwork and fonts. + +```sh +python3.12 -m venv .venv +source .venv/bin/activate +# Windows PowerShell: .venv\Scripts\Activate.ps1 +python -m pip install -r requirements.txt +``` + +For map generation through the CLI, this is enough once an API key is configured. To use the `automap` entry point while keeping assets in the checkout: + +```sh +python -m pip install -e . ``` -Then open in your browser. If no key is found in `.env`, the UI prompts you to paste one; otherwise it uses the key from `.env` automatically. +For native Video Studio operation, also install FFmpeg and make its executable available on `PATH`. URL audio downloads use the `yt-dlp` executable installed by the requirements; Node.js is also included in the Docker worker for supported download workflows. Docker is the simplest way to run the complete media stack. -With `SERVER=TRUE`, sign in with `deltaaa` / `deltaaa00!`, or set `WEB_LOGIN` and `WEB_PASSWORD` in the environment or `.env` to use your own credentials. Environment values take precedence. The browser remembers your sign-in for up to a year. Changing the credentials invalidates existing sessions. In development mode, pages and APIs open without login. +To run the web app and worker outside Compose, start them in separate terminals from the project root. Activate the virtual environment in each terminal. -The entered Google API key, last map topic, and palette are remembered in the same browser. Browser storage must remain enabled to keep these preferences between visits. +```sh +# Terminal 1: media worker, default port 8080. +python yt-dlp_serv.py +``` -Open **Theme & Style** in the left panel to choose a map palette in Color Filling -Studio. Selecting a batch result restores that map's generation settings before -you repaint it. +```sh +# Terminal 2: web app, default port 5000. +export YTDLP_URL=http://127.0.0.1:8080 +python main.py +``` -The model list is refreshed for your API key, with an updated offline fallback -list shared by the web interface and CLI. The default is `gemini-3.8-flash`. -AI generation uses a 45-second request timeout without automatic retries. If a -model is overloaded, rate-limited, or times out, the page displays the reason and -lets you retry or choose another model. Drawing the map takes additional time. +On PowerShell, set the worker URL with `$env:YTDLP_URL = 'http://127.0.0.1:8080'`. Open [http://localhost:5000](http://localhost:5000). -In Video Studio, use **Or upload your own music** to select an MP3, WAV, M4A, AAC, OGG or FLAC file up to 10 MB. The track is converted to MP3 and can be previewed, cut or used directly in the rendered video. The source track and selected cut are saved in this browser so they remain available after reopening the studio. +Only the exact value **`SERVER=TRUE`** enables production mode: Waitress, debugging off, and web authentication. Other values use Flask's development server with debugging enabled and no login gate. The supplied `example.env` enables production mode. -Music downloaded from a URL must also fit the 10 MiB MP3 limit. Larger tracks -are rejected immediately; select a shorter track. Each download has its own ID, -so matching song titles cannot overwrite another track or reuse an older cut. +## Web workflow -### Running on a server +### Generate and style a map -Install the updated dependencies and set `SERVER=TRUE` in the environment or `.env`, then run `python main.py`. This starts the production Waitress server with debugging disabled. Without `SERVER=TRUE`, the Flask development server runs with debugging enabled. `PORT` defaults to `5000` for the web service and `8080` for the media worker. +Choose a model, enter a topic, and select text or numeric mode and a map type. Adjust font size, font, temperature, and the optional feature, then generate. -For Docker, set `SERVER=TRUE` in `.env`, run `docker compose up --build -d`, and open port `3000`. Compose forwards this switch to both services and keeps the media worker private to the Docker network. It passes `WEB_LOGIN`, `WEB_PASSWORD`, `WEB_SECRET_KEY`, and `GEMINI_API_KEY` from your environment or `.env` to the web container. Only the exact value `TRUE` activates production mode. +Open **Theme & Style** from the left panel to choose a palette or edit category colors, numeric ranges, and label appearance. **Apply & Re-Paint Current Map** reuses the generated values, so changing colors does not require another AI call. Selecting a batch result restores its generation parameters before repainting it. -You can use the server's exported environment variables directly; no `.env` file or code changes are required: +The temperature slider accepts `0`–`2` and defaults to `0.75` in the web UI. It controls sampling where the selected model supports it; it does not ensure factual accuracy, and some models manage sampling themselves. -```bash -export SERVER=TRUE -export WEB_LOGIN='your-username' -export WEB_PASSWORD='your-password' -# Optional: export GEMINI_API_KEY='your-key' -docker compose up -d --build -``` +The browser remembers API keys, the last topic, and palette preferences. Video Studio also stores selected materials and settings locally. Storage limits or disabled storage can prevent persistence; download important outputs rather than treating browser storage as an archive. -Compose reads the environment of the process that launches it. Export these variables in the launching shell, or supply them through the service manager running Compose. Environment values override `.env`. After changing variables, run `docker compose up -d` again to recreate affected containers; `docker compose restart` keeps the old values. Direct `python main.py` launches read the same exported variables. +### Create a portrait video -Both containers restart automatically after a failure or host reboot, unless manually stopped. For a deployment outside Compose, set `YTDLP_URL` to the worker address (for example `http://127.0.0.1:8080`); its default is `http://yt-dlp-worker:8080`. +1. Generate a map or select a map image in Video Studio. +2. Supply background footage and a headline. +3. Optionally download a music track from a supported URL or upload MP3, WAV, M4A, AAC, OGG, or FLAC audio. Uploaded music is converted to MP3. +4. Use a full track or trim a **1–30 second** segment from an `MM:SS` start time. +5. Configure title font, size and colors, watermarks, stickers, and other overlays, then render and download the MP4. -Session signing uses a generated secret in `instance/web-session-secret`; retain this file across restarts. Compose stores it in the `automap-session` volume. Alternatively, set a stable, random `WEB_SECRET_KEY` on every instance. Removing the secret file or volume, or changing the secret, requires signing in again. Serve the public site over HTTPS to protect the login and API key in transit. +The worker renders a **1080 × 1920** portrait frame. Video duration is limited to **1–30 seconds** and to the available source media. Without selected music, rendering can proceed without audio; background footage's original audio is removed. -### CLI +The CPU encoder is `libx264`. NVIDIA `h264_nvenc` and macOS `h264_videotoolbox` are selectable only when detected by the worker. Installing an encoder does not guarantee compatible hardware or drivers. -Generate a map straight from the terminal: +Maps, background footage, audio, and custom overlay materials have a **10 MiB per-material limit**. URL downloads are also rejected if the resulting MP3 exceeds that limit. A long track may therefore need to be replaced with a shorter source. Each audio download has a unique identifier, independent of its display title. -```bash +Worker files older than approximately **30 minutes** are cleaned up when relevant operations run. Download completed videos promptly. Browser-restored inline audio can keep a selected track usable after worker files have expired, subject to browser storage availability. + +## Command-line usage + +Run commands from the repository root: + +```sh python cli.py make "Population of European countries" -m number -mp default +python cli.py make "Most popular hot drink by country" -m text --model deepseek-flash +python cli.py make "GDP per capita by country" -m number -eop show_most_least --temperature 0.4 ``` -Flags: +`make` saves `result.png` in the repository root, replacing an existing result. `--show` opens the image after generation; it is off by default. + +| Option | Values or purpose | Default | +| --- | --- | --- | +| `-m`, `--mode` | `text`, `number` | `text` | +| `-mp`, `--map-type` | `default`, `flag`, `world` | `default` | +| `-gsm`, `--global-size-modifier` | Integer font-size adjustment | `15` | +| `-eop`, `--extra-option` | `none`, `show_most_least`, `short_form` | `none` | +| `--model` | Supported model ID; see command help | `gemini-3.8-flash` | +| `--font` | Bundled `.ttf` or `.otf` filename | `Bips.ttf` | +| `--key` | Key for the selected model's provider | Provider environment/file key | +| `--temperature` | Sampling value from `0` to `2`; `make` only | Provider default when omitted | +| `--show` / `--no-show` | Open the generated image; `make` only | `--no-show` | +| `-v`, `--verbose` | Detailed progress output | Off | -- `-m, --mode` — `text` (default) or `number` -- `-mp, --map-type` — `default` (default), `flag` or `world` -- `-gsm, --global-size-modifier` — global font size modifier (default `15`) -- `-eop, --extra-option` — `none` (default), `show_most_least` or `short_form` -- `--model` — AI model to use -- `--temperature` — response randomness from `0` to `2`; omit for the model default. The web slider defaults to `0.75` and remembers your selection. This controls sampling, not factual accuracy. -- `--font` — font file name from `resources/fonts` -- `--show/--no-show` — open the result image after generating -- `-v, --verbose` — print detailed progress output +`show_most_least` is supported in **numeric mode on Europe maps** (`default` or `flag`). `short_form` asks the model for shortened values such as `1k` or `2M` and is supported in **text mode**. These are mutually exclusive extra options. -After installing the package (`pip install -e .`) you can also use the `automap` command: +### Batch generation -```bash -automap make "Population of European countries" -m number -mp default +Start with [batch_template.csv](batch_template.csv), or create a UTF-8 CSV: + +```csv +prompt,mode,map_type,gsm,extra_option,model,font,palette_theme,output +"Population density in Europe",number,default,15,none,gemini-3.8-flash,Bips.ttf,viridis,europe_density.png +"Most popular drink in Europe",text,default,15,none,deepseek-flash,Bips.ttf,categorical,europe_drinks.png ``` -The result is saved as `result.png`. +```sh +python cli.py batch batch_template.csv -o output_maps +``` -## Fonts +Only `prompt` is required. Optional columns are `mode`, `map_type`, `gsm`, `extra_option`, `model`, `font`, `palette_theme`, and `output`. Unknown or duplicate columns and invalid values are rejected. CSV `gsm` must be an integer from `1` to `100`. Blank optional cells use command or web-form defaults. -AutoMap ships with a few default fonts. You can drop your own `.ttf` / `.otf` files into `resources/fonts` and select them on the map. +The batch command accepts the shared map/model/font/key/verbosity options above, plus `-o` / `--output-dir` (default: `output_maps`). It does not expose `--temperature` or `--show`. Rows run sequentially. Without `output`, filenames use a row number and a slug of the prompt; an explicit filename is confined to the output directory and given a `.png` extension if needed. -![Fonts](resources/demo/fonts.png) +Mixed-provider CSV files need credentials for each provider. A key entered in the browser or passed through CLI `--key` is applied only to its selected provider; it is not forwarded to the other provider. Configure both server/environment keys for mixed-provider batches. -## Filling +The web interface also accepts CSV ideas files and provides individual PNG downloads. Batch results remain available in the current page's result list and history. -You can define colours for your data in `map/filling.json`. It contains two dictionaries: +## Filling configuration and Python API -- **`filling_txt`** — colours for `text` mode. For example, if the data is `Tea`, the colour is `(246, 255, 0, 255)`. -- **`filling_num`** — colours for `number` mode, keyed by value ranges. For example, `(100,200)` means *value > 100 and value <= 200* → `(235, 40, 33, 255)`. +If `map/filling.json` is absent, AutoMap reads the bundled `map/filling.template.json`. To maintain your own default rules: -> AutoMap uses **RGBA**. The last number must be `255`. +```sh +cp map/filling.template.json map/filling.json +``` -```json -{ - "filling_txt": { - "Tea": [246, 255, 0, 255], - "Coffee": [54, 4, 4, 255] - }, - "filling_num": { - "(100,200)": [235, 40, 33, 255] - } -} +`filling_txt` maps text labels to RGBA arrays. `filling_num` maps stringified `(low, high)` pairs to RGBA arrays, using **`low < value <= high`**. Fractional interval boundaries are supported. The web editor can export a filling configuration. + +For Python use, the renderer accepts model/key settings, style overrides, and optional supplied values: + +```python +from map.auto_map import auto_map + +png = auto_map( + prompt="Example values", + mode="number", + map_type="default", + size_mod=15, + optional_feature="show_most_least", + model="gemini-3.8-flash", + font_name="Bips.ttf", + show_result=False, + io_res=True, + auto_style=True, + palette_theme="viridis", + ai_answer_override={"ukraine": "2M", "poland": 100000}, +) +if png is not None: + with open("example.png", "wb") as output: + output.write(png.getvalue()) ``` -## Deployment (Vercel) +`ai_answer_override` bypasses AI entirely. With `io_res=True`, a successful call returns a PNG `BytesIO` buffer; otherwise it saves and returns the path to `result.png`. Omitted countries or continents are not populated by this example. -The web UI can be deployed to Vercel. The repository root contains a `vercel.json` that builds `AutoMap-cli/main.py` with the Python runtime. +## Server configuration -1. Set `GEMINI_API_KEY` under **Project → Settings → Environment Variables** (do **not** commit a `.env`). -2. Deploy. If no key is configured, the UI simply prompts the visitor for one. +| Variable | Purpose | +| --- | --- | +| `GEMINI_API_KEY` | Server key for Gemini/Gemma models | +| `DEEPSEEK_API_KEY` | Server key for DeepSeek models | +| `SERVER` | Exact `TRUE` enables Waitress and web authentication | +| `WEB_LOGIN` | Login name; default `deltaaa` | +| `WEB_PASSWORD` | Login password; default `deltaaa00!` | +| `WEB_SECRET_KEY` | Optional stable session-signing secret | +| `WEB_AUTH_SECRET_FILE` | Native deployment override for the generated secret file | +| `PORT` | Native service port; web default `5000`, worker default `8080` | +| `YTDLP_URL` | Web-to-worker URL; default `http://yt-dlp-worker:8080` | -Note that AI + image generation can be slow, and Vercel's Hobby plan has short function time limits — a paid plan may be needed for larger requests. +Login sessions are valid for up to a year. Changing the credentials or signing secret invalidates existing sessions. Without `WEB_SECRET_KEY`, a secret is generated in `instance/web-session-secret`; keep that file across native restarts or retain the Compose session volume. For Compose, add an explicit environment entry to use `WEB_AUTH_SECRET_FILE`. Compose already fixes `YTDLP_URL` to the private worker address; edit that entry only if you change the service layout. -## License +Use your own credentials and HTTPS for a public deployment. Keep the media worker private; the web application provides the login gate. After changing Compose environment values, run `docker compose up -d` to recreate affected containers: `docker compose restart` retains the old values. -This project is licensed under the **Apache License 2.0**. +## Tests -Copyright © 2025-2026 **deltaaa00**. +The suite mocks external AI/HTTP downloads and does not require real credentials. It covers map rendering, decimal ranges and extrema, CLI/CSV behavior, provider/key isolation, authentication, service proxies, media handling, and browser interaction. Real software-encoded video rendering is included; live model/YouTube availability and GPU encoders are not tested. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at +```sh +python -m pip install -r requirements-test.txt +python -m playwright install chromium +python -m pytest --cov --cov-report=term-missing +``` + +On Linux, install browser system dependencies with `python -m playwright install --with-deps chromium`. On macOS, browser tests use installed Google Chrome when available, otherwise Playwright Chromium. + +```sh +python -m pytest -m 'not browser' # Python checks without launching a browser. +python -m pytest -m browser # Browser interaction checks only. +``` - http://www.apache.org/licenses/LICENSE-2.0 +The coverage configuration requires **90%** coverage. See [tests/README.md](tests/README.md) for isolation details and test setup. The GitHub Actions workflow runs on pushes and pull requests targeting `main`. + +## Troubleshooting + +- **AI overload, timeout, or quota error:** retry later or select another available model. Drawing and encoding still take time after the AI response; AutoMap does not guarantee a fixed generation time. +- **Key input appears for one provider only:** that provider has no server key. Gemini and DeepSeek credentials are independent. +- **Video or music requests cannot reach the worker:** check `YTDLP_URL` for native launches and inspect `docker compose logs yt-dlp-worker` for Compose. +- **Audio download or conversion fails:** verify FFmpeg/yt-dlp availability, the source URL's accessibility, and the resulting file size. External sites may change or restrict downloads. +- **Files or preferences disappear after reopening:** browser storage may be disabled, cleared, or full; worker files also expire. Keep downloaded copies of important material. +- **A custom font or map asset is missing:** run from the source checkout and retain `resources/fonts/` and `resources/maps/`. + +## License -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. +[Apache License 2.0](LICENSE). diff --git a/templates/index.html b/templates/index.html index a5f8216..b293b31 100644 --- a/templates/index.html +++ b/templates/index.html @@ -282,7 +282,7 @@

Saved in browser @@ -553,8 +553,9 @@

-
@@ -764,7 +770,7 @@

Cartograp
Synthesizing Cartographic Layer
-
Querying Gemini model and rendering map
+
Querying AI model and rendering map
@@ -1174,6 +1180,13 @@

const MAP_PALETTE_STORAGE_KEY = 'automap-map-palette-theme'; const AI_TEMPERATURE_STORAGE_KEY = 'automap-ai-temperature'; const API_KEY_STORAGE_KEY = 'automap-gemini-api-key'; + const PROVIDER_STORAGE_KEY = 'automap-ai-provider'; + const PROVIDER_KEY_STORAGE = {gemini: API_KEY_STORAGE_KEY, deepseek: 'automap-deepseek-api-key'}; + const PROVIDER_LABELS = {gemini: 'Gemini', deepseek: 'DeepSeek'}; + let activeAiProvider = 'gemini'; + let modelListVersion = 0; + const fallbackModels = Array.from(document.getElementById('modelSelect').options, option => option.value); + const providerKeys = {gemini: '', deepseek: ''}; const MAP_TOPIC_STORAGE_KEY = 'automap-map-topic'; const DEFAULT_AI_TEMPERATURE = 0.75; @@ -1187,6 +1200,8 @@

} catch { // Keep the default when browser storage is unavailable. } + populateProviderModels(activeAiProvider); + updateProviderKeyLabel(); restoreMapPaletteTheme(); updateCommandPreview(); renderFillingStudio(); @@ -1198,11 +1213,13 @@

const apiKeyInputEl = document.getElementById('apiKeyInput'); if (apiKeyInputEl) { apiKeyInputEl.addEventListener('input', () => { - saveBrowserValue(API_KEY_STORAGE_KEY, apiKeyInputEl.value.trim()); + providerKeys[activeAiProvider] = apiKeyInputEl.value.trim(); + saveBrowserValue(PROVIDER_KEY_STORAGE[activeAiProvider], apiKeyInputEl.value.trim()); updateApiKeyIndicator(); }); apiKeyInputEl.addEventListener('change', () => { - saveBrowserValue(API_KEY_STORAGE_KEY, apiKeyInputEl.value.trim()); + providerKeys[activeAiProvider] = apiKeyInputEl.value.trim(); + saveBrowserValue(PROVIDER_KEY_STORAGE[activeAiProvider], apiKeyInputEl.value.trim()); updateApiKeyIndicator(); loadAvailableModels(); }); @@ -1267,9 +1284,66 @@

} } + function modelProvider(model) { + return String(model || '').toLowerCase().startsWith('deepseek-') ? 'deepseek' : 'gemini'; + } + + function populateProviderModels(provider, selectedModel = null) { + const select = document.getElementById('modelSelect'); + const models = fallbackModels.filter(model => modelProvider(model) === provider); + if (selectedModel && modelProvider(selectedModel) === provider && !models.includes(selectedModel)) { + models.push(selectedModel); + } + document.getElementById(`${provider}ModelsGroup`).replaceChildren(...models.map(model => new Option(model, model))); + if (selectedModel && models.includes(selectedModel)) select.value = selectedModel; + else if (modelProvider(select.value) !== provider) select.value = models[0]; + } + + function updateProviderKeyLabel() { + document.getElementById('apiKeyLabel').textContent = `${PROVIDER_LABELS[activeAiProvider]} API Key`; + document.getElementById('apiKeyInput').placeholder = `Paste your ${PROVIDER_LABELS[activeAiProvider]} API key...`; + } + + function onAiModelChange(model) { + changeAiProvider(modelProvider(model), model); + } + + function changeAiProvider(provider, selectedModel = null) { + if (selectedModel) provider = modelProvider(selectedModel); + if (!(provider in PROVIDER_LABELS)) return; + const input = document.getElementById('apiKeyInput'); + if (provider !== activeAiProvider) { + providerKeys[activeAiProvider] = input.value.trim(); + saveBrowserValue(PROVIDER_KEY_STORAGE[activeAiProvider], input.value.trim()); + activeAiProvider = provider; + input.value = providerKeys[provider]; + input.type = 'password'; + document.getElementById('apiKeyEyeIcon').className = 'fa-solid fa-eye text-xs'; + apiKeyRequired = true; + document.getElementById('apiKeySection').classList.remove('hidden'); + populateProviderModels(provider, selectedModel); + saveBrowserValue(PROVIDER_STORAGE_KEY, provider); + checkApiKeyStatus(); + loadAvailableModels(); + } else if (selectedModel) { + const select = document.getElementById('modelSelect'); + if (!Array.from(select.options).some(option => option.value === selectedModel)) { + document.getElementById(`${provider}ModelsGroup`).appendChild(new Option(selectedModel, selectedModel)); + } + select.value = selectedModel; + } + updateProviderKeyLabel(); + updateApiKeyIndicator(); + updateCommandPreview(); + } + function restoreSavedMapInputs() { try { - document.getElementById('apiKeyInput').value = localStorage.getItem(API_KEY_STORAGE_KEY) || ''; + activeAiProvider = localStorage.getItem(PROVIDER_STORAGE_KEY) === 'deepseek' ? 'deepseek' : 'gemini'; + for (const provider of Object.keys(providerKeys)) { + providerKeys[provider] = localStorage.getItem(PROVIDER_KEY_STORAGE[provider]) || ''; + } + document.getElementById('apiKeyInput').value = providerKeys[activeAiProvider]; document.getElementById('topicInput').value = localStorage.getItem(MAP_TOPIC_STORAGE_KEY) || ''; } catch { // Use empty fields when browser storage is unavailable. @@ -1317,12 +1391,15 @@

// --- API key status / input --- async function checkApiKeyStatus() { + const provider = activeAiProvider; try { - const res = await fetch('/api/status'); + const res = await fetch(`/api/status?provider=${provider}`); if (!res.ok) throw new Error('status endpoint unavailable'); const data = await res.json(); + if (provider !== activeAiProvider) return; apiKeyRequired = !!data.needs_api_key; } catch (err) { + if (provider !== activeAiProvider) return; // If we can't determine status, be safe and assume no .env key. apiKeyRequired = true; logToConsole(`[Warning] Could not read /api/status (${err.message}). Assuming API key is required.`, 'warn'); @@ -1332,10 +1409,10 @@

if (apiKeyRequired) { section.classList.remove('hidden'); - logToConsole('[Info] No .env API key detected — please enter your Gemini API key above.', 'warn'); + logToConsole(`[Info] Enter your ${PROVIDER_LABELS[provider]} API key above.`, 'warn'); } else { section.classList.add('hidden'); - logToConsole('[Info] Server has a Gemini API key from .env — no key input needed.', 'info'); + logToConsole(`[Info] Server has a ${PROVIDER_LABELS[provider]} API key — no key input needed.`, 'info'); } updateApiKeyIndicator(); @@ -1391,6 +1468,8 @@

} async function loadAvailableModels() { + const provider = activeAiProvider; + const version = ++modelListVersion; const select = document.getElementById('modelSelect'); if (!select) return; const apiKey = (document.getElementById('apiKeyInput')?.value || '').trim(); @@ -1398,24 +1477,28 @@

const res = await fetch('/api/models', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(apiKey ? { api_key: apiKey } : {}) + body: JSON.stringify({provider, ...(apiKey ? {api_key: apiKey} : {})}) }); const data = await res.json(); + if (provider !== activeAiProvider || version !== modelListVersion) return; if (!res.ok || data.status !== 'success' || !Array.isArray(data.models) || data.models.length === 0) { return; } const prev = select.value; - select.replaceChildren(); - data.models.forEach(modelId => { + const models = data.models.filter(model => typeof model === 'string' && modelProvider(model) === provider); + if (!models.length) { populateProviderModels(provider); return; } + const group = document.getElementById(`${provider}ModelsGroup`); + group.replaceChildren(); + models.forEach(modelId => { const opt = document.createElement('option'); opt.value = modelId; opt.textContent = modelId; - select.appendChild(opt); + group.appendChild(opt); }); - if (data.models.includes(prev)) { + if (models.includes(prev)) { select.value = prev; } else { - const preferred = data.models.find(m => m.includes('flash')) || data.models[0]; + const preferred = models.find(m => m.includes('flash')) || models[0]; select.value = preferred; } updateCommandPreview(); @@ -1476,6 +1559,7 @@

} function restoreBatchResultParameters(item) { + changeAiProvider(item.provider || modelProvider(item.model), item.model); for (const name of ['mode', 'map_type', 'extra_option']) { const input = Array.from(document.getElementsByName(name)) .find(input => input.value === item[name]); @@ -1599,6 +1683,7 @@

gsm: item.gsm ?? state.gsm, extra_option: item.extra_option || state.extra_option, model: item.model || state.model, + provider: item.provider || modelProvider(item.model || state.model), font: item.font || state.font, palette_theme: item.palette_theme || state.palette_theme, temperature: item.temperature ?? state.temperature, @@ -1661,6 +1746,7 @@

const { filling_txt, filling_num } = buildFillingDictionaries(); return { + provider: activeAiProvider, mode, map_type: mapType, gsm, @@ -1784,13 +1870,14 @@

return; } + if (modelProvider(val) !== activeAiProvider) changeAiProvider(modelProvider(val), val); const select = document.getElementById('modelSelect'); const existing = Array.from(select.options).find(opt => opt.value === val); if (!existing) { const option = document.createElement('option'); option.value = val; option.textContent = val; - select.appendChild(option); + document.getElementById(`${activeAiProvider}ModelsGroup`).appendChild(option); } select.value = val; @@ -1871,7 +1958,7 @@

// If the server has no .env key, a key must be entered in the form. if (apiKeyRequired && !state.api_key) { - logToConsole('[Error] API key is required. Enter your Gemini API key in the form.', 'error'); + logToConsole(`[Error] Enter your ${PROVIDER_LABELS[activeAiProvider]} API key in the form.`, 'error'); showToast("Please enter your Gemini API key first.", "error"); document.getElementById('apiKeyInput').focus(); return; @@ -2590,6 +2677,7 @@

if (!item) return; const s = item.state; + changeAiProvider(s.provider || modelProvider(s.model), s.model); const modeRadio = document.querySelector(`input[name="mode"][value="${s.mode}"]`); if (modeRadio) modeRadio.checked = true; const mapRadio = document.querySelector(`input[name="map_type"][value="${s.map_type}"]`); @@ -2654,7 +2742,7 @@

document.querySelector('input[name="map_type"][value="default"]').checked = true; syncGsm(15); document.querySelector('input[name="extra_option"][value="none"]').checked = true; - document.getElementById('modelSelect').selectedIndex = 0; + changeAiProvider('gemini', fallbackModels.find(model => modelProvider(model) === 'gemini')); document.getElementById('fontSelect').selectedIndex = 0; document.getElementById('topicInput').value = ''; updateCommandPreview(); diff --git a/tests/browser/test_map_palettes.py b/tests/browser/test_map_palettes.py index c77df77..4a2fc03 100644 --- a/tests/browser/test_map_palettes.py +++ b/tests/browser/test_map_palettes.py @@ -28,7 +28,7 @@ def route_request(route): url = urlsplit(request.url) if url.hostname == 'automap.test': response = web.app.test_client().open( - url.path, method=request.method, data=request.post_data, + url.path + ('?' + url.query if url.query else ''), method=request.method, data=request.post_data, content_type=request.headers.get('content-type')) route.fulfill(status=response.status_code, body=response.data, content_type=response.content_type) @@ -410,3 +410,108 @@ def test_generation_failure_shows_actual_error_and_keeps_retry_available(map_pag assert error in page.locator('#generationError').inner_text() assert error in page.locator('#toastMsg').inner_text() assert page.locator('#generateBtn').is_enabled() + + +def stub_provider_model_lists(page): + def respond(route): + provider = route.request.post_data_json.get('provider', 'gemini') + models = ['deepseek-flash', 'deepseek-v4-pro'] if provider == 'deepseek' else ['gemini-3.8-flash'] + route.fulfill(json={'status': 'success', 'models': models}) + page.route('**/api/models', respond) + + +def test_provider_groups_and_keys_survive_switches_reload_and_independent_clear(map_page): + page = map_page + stub_provider_model_lists(page) + page.evaluate('closeFillingStudio()') + assert page.locator('#modelSelect optgroup').count() == 2 + assert page.locator('#modelSelect optgroup').evaluate_all('(groups) => groups.map(group => group.label)') == ['Gemini', 'DeepSeek'] + page.locator('#apiKeyInput').fill('gemini-browser-key') + page.select_option('#modelSelect', 'deepseek-flash') + page.wait_for_function("activeAiProvider === 'deepseek' && document.getElementById('apiKeyLabel').textContent.includes('DeepSeek')") + assert page.locator('#apiKeyInput').input_value() == '' + page.locator('#apiKeyInput').fill('deepseek-browser-key') + page.reload() + page.wait_for_function("activeAiProvider === 'deepseek' && getFormState().model === 'deepseek-flash'") + assert page.locator('#apiKeyInput').input_value() == 'deepseek-browser-key' + assert page.locator('#apiKeyInput').get_attribute('type') == 'password' + assert page.evaluate('getFormState().provider') == 'deepseek' + page.select_option('#modelSelect', 'gemini-3.8-flash') + assert page.locator('#apiKeyInput').input_value() == 'gemini-browser-key' + page.select_option('#modelSelect', 'deepseek-flash') + page.locator('#apiKeyInput').fill('') + page.reload() + assert page.locator('#apiKeyInput').input_value() == '' + page.select_option('#modelSelect', 'gemini-3.8-flash') + assert page.locator('#apiKeyInput').input_value() == 'gemini-browser-key' + assert page.locator('#modelSelect optgroup').count() == 2 + + +def test_deepseek_generation_sends_only_active_provider_key(map_page): + page = map_page + stub_provider_model_lists(page) + page.evaluate('closeFillingStudio()') + page.locator('#apiKeyInput').fill('gemini-do-not-send') + page.select_option('#modelSelect', 'deepseek-flash') + page.locator('#apiKeyInput').fill('deepseek-use-this') + page.locator('#topicInput').fill('Population of Europe') + payloads = [] + def capture(route): + payloads.append(route.request.post_data_json) + route.fulfill(status=503, json={'status': 'error', 'error': 'DeepSeek is overloaded. Please try again.'}) + page.route('**/api/generate', capture) + page.click('#generateBtn') + page.wait_for_function("!document.getElementById('generateBtn').disabled") + assert len(payloads) == 1 + assert payloads[0]['model'] == 'deepseek-flash' + assert payloads[0]['provider'] == 'deepseek' + assert payloads[0]['api_key'] == 'deepseek-use-this' + assert 'gemini-do-not-send' not in str(payloads) + assert 'DeepSeek is overloaded' in page.locator('#generationError').inner_text() + + +def test_server_deepseek_key_hides_input_without_exposing_key(map_page, monkeypatch): + page = map_page + stub_provider_model_lists(page) + monkeypatch.setenv('DEEPSEEK_API_KEY', 'server-deepseek-never-expose') + page.select_option('#modelSelect', 'deepseek-flash') + page.wait_for_function("activeAiProvider === 'deepseek' && !apiKeyRequired") + assert page.locator('#apiKeySection').is_hidden() + assert page.locator('#apiKeyInput').input_value() == '' + assert 'server-deepseek-never-expose' not in page.content() + page.select_option('#modelSelect', 'gemini-3.8-flash') + page.wait_for_function('apiKeyRequired') + assert page.locator('#apiKeySection').is_visible() + + +def test_provider_keys_stay_independent_when_storage_is_blocked(map_page): + page = map_page + stub_provider_model_lists(page) + page.add_init_script('''Object.defineProperty(window, 'localStorage', { + get() { throw new DOMException('Storage blocked', 'SecurityError'); } + });''') + page.reload() + page.locator('#apiKeyInput').fill('gemini-memory') + page.select_option('#modelSelect', 'deepseek-flash') + page.locator('#apiKeyInput').fill('deepseek-memory') + page.select_option('#modelSelect', 'gemini-3.8-flash') + assert page.locator('#apiKeyInput').input_value() == 'gemini-memory' + page.select_option('#modelSelect', 'deepseek-flash') + assert page.locator('#apiKeyInput').input_value() == 'deepseek-memory' + + +def test_history_uses_model_provider_and_its_saved_key(map_page): + page = map_page + stub_provider_model_lists(page) + page.evaluate('closeFillingStudio()') + page.locator('#apiKeyInput').fill('gemini-history-key') + page.select_option('#modelSelect', 'deepseek-flash') + page.locator('#apiKeyInput').fill('deepseek-history-key') + page.select_option('#modelSelect', 'gemini-3.8-flash') + page.evaluate('''() => { + addToHistory({...getFormState(), provider:'gemini', model:'deepseek-flash'}, 'Legacy history'); + restoreHistoryItem(0); + }''') + assert page.evaluate('getFormState().provider') == 'deepseek' + assert page.locator('#apiKeyInput').input_value() == 'deepseek-history-key' + assert page.locator('#modelSelect').input_value() == 'deepseek-flash' diff --git a/tests/test_cli.py b/tests/test_cli.py index 50129d8..23d776b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -118,3 +118,14 @@ def test_font_discovery(module_name, web, tmp_path, monkeypatch): assert module._available_fonts() == ['A.OTF', 'z.ttf'] monkeypatch.setattr(module, 'FONTS_DIR', tmp_path / 'missing') assert module._available_fonts() == [] + + +def test_deepseek_cli_key_does_not_replace_gemini_key(generate, monkeypatch): + import os + monkeypatch.setenv('GEMINI_API_KEY', 'keep-gemini') + monkeypatch.setenv('DEEPSEEK_API_KEY', 'previous-deepseek') + result = CliRunner().invoke(cli.main, ['make', 'Population', '--model', 'deepseek-flash', '--key', 'deepseek-cli-key']) + assert result.exit_code == 0, result.output + assert os.environ['GEMINI_API_KEY'] == 'keep-gemini' + assert os.environ['DEEPSEEK_API_KEY'] == 'deepseek-cli-key' + assert generate.call_args.kwargs['model'] == 'deepseek-flash' diff --git a/tests/test_deepseek.py b/tests/test_deepseek.py new file mode 100644 index 0000000..d92f08d --- /dev/null +++ b/tests/test_deepseek.py @@ -0,0 +1,163 @@ +"""DeepSeek transport and credential isolation; no real API keys or requests.""" +import io +from unittest.mock import Mock + +import pytest +import requests +from dotenv.main import dotenv_values + +from map import functions + + +@pytest.fixture(autouse=True) +def no_deepseek_server_key(monkeypatch): + monkeypatch.delenv('DEEPSEEK_API_KEY', raising=False) + + +def reply(body, status=200): + return Mock(status_code=status, json=Mock(return_value=body)) + + +@pytest.mark.parametrize('use_server_key', [False, True]) +@pytest.mark.parametrize('model', ['deepseek-flash', 'deepseek-v4-pro']) +def test_deepseek_generates_parseable_map_json(monkeypatch, model, use_server_key): + if use_server_key: + monkeypatch.setenv('DEEPSEEK_API_KEY', 'deepseek-server-key') + response = reply({'choices': [{'message': {'content': '{"ukraine":0.4,"poland":"2M"}'}, 'finish_reason': 'stop'}]}) + post = Mock(return_value=response) + monkeypatch.setattr(requests, 'post', post) + result = functions.ai_request('Population', 'default', model, + None if use_server_key else 'deepseek-browser-key', temperature=0.4) + assert result == {'ukraine': 0.4, 'poland': '2M'} + assert post.call_args.args == ('https://api.deepseek.com/chat/completions',) + options = post.call_args.kwargs + assert options['headers']['Authorization'] == 'Bearer ' + ('deepseek-server-key' if use_server_key else 'deepseek-browser-key') + assert options['allow_redirects'] is False + assert options['timeout'] == (10, 45) + assert options['json']['model'] == model + assert options['json']['temperature'] == 0.4 + assert options['json']['response_format'] == {'type': 'json_object'} + assert options['json']['thinking'] == {'type': 'disabled'} + assert 'Population' in options['json']['messages'][1]['content'] + assert 'ukraine' in options['json']['messages'][1]['content'] + response.close.assert_called_once() + functions.genai.Client.assert_not_called() + + +def test_deepseek_never_falls_back_to_gemini_credentials(monkeypatch): + post = Mock() + monkeypatch.setattr(requests, 'post', post) + monkeypatch.setenv('GEMINI_API_KEY', 'gemini-only') + with pytest.raises(functions.AIServiceError, match='DEEPSEEK_API_KEY'): + functions.ai_request('Population', 'default', 'deepseek-flash') + post.assert_not_called() + + +@pytest.mark.parametrize('status,expected,phrase', [ + (401, 400, 'API key'), (403, 400, 'API key'), (402, 402, 'balance'), + (429, 429, 'limit'), (503, 503, 'overloaded'), (400, 400, 'model'), (302, 400, 'model'), +]) +def test_deepseek_http_errors_are_actionable_without_leaking_keys(monkeypatch, status, expected, phrase): + response = reply({'error': {'message': 'secret-key-in-upstream-error'}}, status) + monkeypatch.setattr(requests, 'post', Mock(return_value=response)) + with pytest.raises(functions.AIServiceError, match=phrase) as failure: + functions.ai_request('Population', 'world', 'deepseek-flash', 'secret-key') + assert failure.value.status_code == expected + assert 'secret-key' not in str(failure.value) + response.close.assert_called_once() + + +@pytest.mark.parametrize('failure,status', [(requests.Timeout('secret'), 504), (requests.ConnectionError('secret'), 503)]) +def test_deepseek_network_errors(monkeypatch, failure, status): + monkeypatch.setattr(requests, 'post', Mock(side_effect=failure)) + with pytest.raises(functions.AIServiceError) as caught: + functions.ai_request('Population', 'world', 'deepseek-flash', 'key') + assert caught.value.status_code == status + assert 'secret' not in str(caught.value) + + +@pytest.mark.parametrize('body', [ + [], {}, {'error': {'message': 'failure after HTTP200'}}, {'choices': []}, + {'choices': [{'message': {'content': ' '}}]}, + {'choices': [{'message': {'content': '{}'}, 'finish_reason': 'length'}]}, + {'choices': [{'message': {'content': None}}]}, +]) +def test_deepseek_invalid_or_incomplete_responses(monkeypatch, body): + monkeypatch.setattr(requests, 'post', Mock(return_value=reply(body))) + with pytest.raises(functions.AIServiceError) as caught: + functions.ai_request('Population', 'default', 'deepseek-flash', 'key') + assert caught.value.status_code == 502 + + +def test_deepseek_invalid_json_response(monkeypatch): + response = reply({}) + response.json.side_effect = ValueError('bad json') + monkeypatch.setattr(requests, 'post', Mock(return_value=response)) + with pytest.raises(functions.AIServiceError, match='invalid response'): + functions.ai_request('Population', 'default', 'deepseek-flash', 'key') + response.close.assert_called_once() + + +def test_deepseek_model_discovery(monkeypatch): + get = Mock(return_value=reply({'data': [{'id': 'deepseek-flash'}, {'id': 'deepseek-v4-pro'}, + {'id': 'other-provider'}, {}, None]})) + monkeypatch.setattr(requests, 'get', get) + assert functions.list_deepseek_models('key') == ['deepseek-flash', 'deepseek-v4-pro'] + assert get.call_args.args == ('https://api.deepseek.com/models',) + assert get.call_args.kwargs['timeout'] == (10, 15) + + +@pytest.mark.parametrize('source', ['browser', 'environment', 'file']) +def test_web_deepseek_key_sources_are_independent(web, monkeypatch, tmp_path, source): + monkeypatch.setattr(web, 'auto_map', Mock(return_value=io.BytesIO(b'png'))) + body = dict(topic='Population', mode='number', map_type='default', model='deepseek-flash', provider='deepseek') + if source == 'browser': + body['api_key'] = 'deepseek-only-key' + elif source == 'environment': + monkeypatch.setenv('DEEPSEEK_API_KEY', 'deepseek-only-key') + else: + env = tmp_path / '.env' + env.write_text('DEEPSEEK_API_KEY=deepseek-only-key\nGEMINI_API_KEY=your_gemini_key\n') + monkeypatch.setattr(web, 'env', env) + monkeypatch.setattr(web, 'dotenv_values', dotenv_values) + client = web.app.test_client() + assert client.get('/api/status?provider=deepseek').json == {'needs_api_key': source == 'browser'} + assert client.get('/api/status').json == {'needs_api_key': True} + response = client.post('/api/generate', json=body) + assert response.status_code == 200 + assert web.auto_map.call_args.kwargs['api_key'] == 'deepseek-only-key' + assert 'deepseek-only-key' not in response.json['command'] + + +def test_web_provider_mismatch_cannot_send_a_key_to_another_provider(web, monkeypatch): + render = Mock() + monkeypatch.setattr(web, 'auto_map', render) + result = web.app.test_client().post('/api/generate', json={ + 'topic': 'Population', 'model': 'deepseek-flash', 'provider': 'gemini', 'api_key': 'gemini-key', + }) + assert result.status_code == 400 + assert 'different AI provider' in result.json['error'] + render.assert_not_called() + + +def test_web_deepseek_discovery_uses_its_key(web, monkeypatch): + monkeypatch.setenv('DEEPSEEK_API_KEY', 'deepseek-server') + listing = Mock(return_value=['deepseek-flash']) + monkeypatch.setattr(web, 'list_deepseek_models', listing) + result = web.app.test_client().post('/api/models', json={'provider': 'deepseek'}) + assert result.json == {'status': 'success', 'models': ['deepseek-flash']} + listing.assert_called_once_with('deepseek-server') + + +def test_deepseek_batch_does_not_forward_browser_key_to_gemini(web, monkeypatch): + monkeypatch.setattr(web, 'auto_map', Mock(return_value=io.BytesIO(b'png'))) + result = web.app.test_client().post('/api/generate_batch', json={ + 'provider': 'deepseek', 'model': 'deepseek-flash', 'api_key': 'deepseek-browser', + 'filename': 'ideas.csv', 'ideas_text': 'prompt,model\nOne,deepseek-flash\nTwo,gemini-3.8-flash\n', + }) + assert result.status_code == 200 + assert result.json['succeeded'] == 1 + assert result.json['results'][0]['provider'] == 'deepseek' + assert result.json['results'][1]['status'] == 'error' + web.auto_map.assert_called_once() + assert web.auto_map.call_args.kwargs['api_key'] == 'deepseek-browser' From d499773156cadab08131c89707abc8b1b19a0233 Mon Sep 17 00:00:00 2001 From: deltaaa00 Date: Sun, 15 Nov 2026 10:19:38 +0200 Subject: [PATCH 02/23] Transfer map topics to video titles and add render progress and stickers --- Dockerfile.ytserver | 2 +- main.py | 17 ++++ pyproject.toml | 1 + readme.md | 11 +++ render_progress.py | 89 +++++++++++++++++ requirements.txt | 1 + static/studio-storage.js | 4 +- sticker_themes.py | 7 ++ templates/index.html | 12 +-- templates/video.html | 150 +++++++++++++++++++++++++---- tests/browser/test_video_studio.py | 79 ++++++++++++++- tests/test_render_progress.py | 109 +++++++++++++++++++++ tests/test_video_render.py | 20 ++++ yt-dlp_serv.py | 125 +++++++++++++++++++++++- 14 files changed, 596 insertions(+), 31 deletions(-) create mode 100644 render_progress.py create mode 100644 tests/test_render_progress.py diff --git a/Dockerfile.ytserver b/Dockerfile.ytserver index 22a03a4..7155f0a 100644 --- a/Dockerfile.ytserver +++ b/Dockerfile.ytserver @@ -14,7 +14,7 @@ COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt -COPY yt-dlp_serv.py sticker_themes.py server_runtime.py ./ +COPY yt-dlp_serv.py sticker_themes.py server_runtime.py render_progress.py ./ COPY resources/fonts/ resources/fonts/ EXPOSE 8080 diff --git a/main.py b/main.py index f6c0ba0..fae14d2 100644 --- a/main.py +++ b/main.py @@ -1076,6 +1076,7 @@ def make_video() -> tuple[object, int]: "subscribe_text": data.get('subscribe_text', 'Subscribe!'), "subscribe_delay": data.get('subscribe_delay', 2.5), "intro_fade": data.get('intro_fade', False), + **({'render_id': data['render_id']} if data.get('render_id') is not None else {}), }, # Video rendering (moviepy/ffmpeg) can take a while - allow more time. timeout=300, @@ -1095,6 +1096,22 @@ def make_video() -> tuple[object, int]: return jsonify(payload), response.status_code +@app.get('/api/render_progress/') +def video_render_progress(render_id): + try: + response = requests.get(f'{WORKER_URL}/api/render_progress/{render_id}', timeout=5) + except requests.RequestException: + return jsonify(error='Render progress is temporarily unavailable.'), 502 + try: + result = jsonify(response.json()) + result.headers['Cache-Control'] = 'no-store' + return result, response.status_code + except ValueError: + return jsonify(error='Render service returned invalid progress.'), 502 + finally: + response.close() + + @app.route('/api/download_video', methods=['GET']) @app.route('/api/download_video/', methods=['GET']) def download_video(): diff --git a/pyproject.toml b/pyproject.toml index 76e61e9..b519aeb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,6 +17,7 @@ dependencies = [ "requests>=2.31.0", "pydub>=0.25.1", "moviepy>=2.0.0", + "proglog>=0.1.10,<1.0.0", ] [project.scripts] diff --git a/readme.md b/readme.md index bdf5791..e859208 100644 --- a/readme.md +++ b/readme.md @@ -138,6 +138,11 @@ Choose a model, enter a topic, and select text or numeric mode and a map type. A Open **Theme & Style** from the left panel to choose a palette or edit category colors, numeric ranges, and label appearance. **Apply & Re-Paint Current Map** reuses the generated values, so changing colors does not require another AI call. Selecting a batch result restores its generation parameters before repainting it. +The generated map's topic travels with its image into Video Studio and fills the +video headline automatically. Batch selection and restored history use the topic +of the selected map. Headlines you edit manually are kept when returning to the +studio or selecting another generated map. + The temperature slider accepts `0`–`2` and defaults to `0.75` in the web UI. It controls sampling where the selected model supports it; it does not ensure factual accuracy, and some models manage sampling themselves. The browser remembers API keys, the last topic, and palette preferences. Video Studio also stores selected materials and settings locally. Storage limits or disabled storage can prevent persistence; download important outputs rather than treating browser storage as an archive. @@ -154,6 +159,12 @@ The worker renders a **1080 × 1920** portrait frame. Video duration is limited The CPU encoder is `libx264`. NVIDIA `h264_nvenc` and macOS `h264_videotoolbox` are selectable only when detected by the worker. Installing an encoder does not guarantee compatible hardware or drivers. +During rendering, a progress bar shows the worker's audio/video encoding progress +and current phase. It reaches 100% only after the finished MP4 is ready. The +sticker library also includes mountains, volcanoes, islands, weather, robots, +satellites, laptops, graduation caps, medals, cats, dogs, fish, burgers, and trains; +these have offline artwork as well as the existing sticker styles. + Maps, background footage, audio, and custom overlay materials have a **10 MiB per-material limit**. URL downloads are also rejected if the resulting MP3 exceeds that limit. A long track may therefore need to be replaced with a shorter source. Each audio download has a unique identifier, independent of its display title. Worker files older than approximately **30 minutes** are cleaned up when relevant operations run. Download completed videos promptly. Browser-restored inline audio can keep a selected track usable after worker files have expired, subject to browser storage availability. diff --git a/render_progress.py b/render_progress.py new file mode 100644 index 0000000..61897f1 --- /dev/null +++ b/render_progress.py @@ -0,0 +1,89 @@ +"""Thread-safe per-render progress for the single-process media worker.""" +from functools import wraps +from threading import Lock +from time import monotonic +from uuid import UUID + +from flask import current_app, g, jsonify, request +from proglog import ProgressBarLogger + + +class RenderProgress: + def __init__(self): + self.records = {} + self.lock = Lock() + + def update(self, render_id, *, progress=None, phase=None, status=None, error=None): + with self.lock: + record = self.records.get(render_id) + if record is None: + return + if progress is not None: + record['progress'] = max(record['progress'], min(100, progress)) + for key, value in (('phase', phase), ('status', status), ('error', error)): + if value is not None: + record[key] = value + record['updated_at'] = monotonic() + + def snapshot(self, render_id): + with self.lock: + record = self.records.get(render_id) + return {key: value for key, value in record.items() if key != 'updated_at'} if record else None + + def track(self, function): + @wraps(function) + def tracked(*args, **kwargs): + data = request.get_json(silent=True) or {} + raw_id = data.get('render_id') + if raw_id is None: + return function(*args, **kwargs) + try: + render_id = str(UUID(str(raw_id))) + except ValueError: + return jsonify(status='error', error='Invalid render ID.'), 400 + with self.lock: + now = monotonic() + self.records = {key: value for key, value in self.records.items() + if now - value['updated_at'] < 3600} + if render_id in self.records: + return jsonify(status='error', error='This render ID has already been used.'), 409 + if len(self.records) >= 256: + return jsonify(status='error', error='Render service is busy. Try again later.'), 503 + self.records[render_id] = dict(status='rendering', progress=0, + phase='Preparing materials', updated_at=now) + g.render_progress_id = render_id + try: + response = current_app.make_response(function(*args, **kwargs)) + payload = response.get_json(silent=True) or {} + if response.status_code < 400 and payload.get('status') == 'success': + self.update(render_id, status='success', progress=100, phase='Complete') + else: + self.update(render_id, status='error', phase='Render failed', + error=payload.get('error') or 'Video render failed.') + return response + except Exception: + self.update(render_id, status='error', phase='Render failed', error='Video render failed.') + raise + return tracked + + def logger(self): + render_id = getattr(g, 'render_progress_id', None) + if render_id is None: + return None + tracker = self + + class Logger(ProgressBarLogger): + def bars_callback(self, bar, attr, value, old_value=None): + if attr != 'index': + return + total = self.bars[bar].get('total') or 0 + if total <= 0: + return + fraction = max(0, min(1, value / total)) + if bar == 'chunk': + tracker.update(render_id, progress=round(5 + 10 * fraction), phase='Encoding audio') + elif bar == 'frame_index': + tracker.update(render_id, progress=round(15 + 80 * fraction), + phase='Finalizing video' if fraction == 1 else 'Encoding video') + + return Logger() diff --git a/requirements.txt b/requirements.txt index 4ae01cb..e81ca4b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,4 @@ rich>=13.0.0 yt-dlp>=2024.12.0 pydub>=0.25.1 moviepy>=2.0.0 +proglog>=0.1.10,<1.0.0 diff --git a/static/studio-storage.js b/static/studio-storage.js index df635d4..393dd8d 100644 --- a/static/studio-storage.js +++ b/static/studio-storage.js @@ -26,6 +26,7 @@ window.StudioStorage = (() => { const record = request.result; resolve(record ? { updatedAt: record.updatedAt, + topic: typeof record.topic === 'string' ? record.topic : '', file: record.blob ? new File([record.blob], record.name, { type: record.type, lastModified: record.lastModified, }) : null, @@ -35,11 +36,12 @@ window.StudioStorage = (() => { }); } - async function setAsset(key, file) { + async function setAsset(key, file, metadata = {}) { const db = await open(); const record = { blob: file, name: file?.name, type: file?.type, lastModified: file?.lastModified, updatedAt: Date.now(), + topic: typeof metadata.topic === 'string' ? metadata.topic.trim().slice(0, 1000) : '', }; return new Promise((resolve, reject) => { const transaction = db.transaction('assets', 'readwrite'); diff --git a/sticker_themes.py b/sticker_themes.py index a0528eb..b5460fe 100644 --- a/sticker_themes.py +++ b/sticker_themes.py @@ -9,6 +9,9 @@ 'bear', 'paw', 'tree', 'wolf', 'beer', 'coffee', 'wine', 'pizza', 'soccer', 'car', 'brain', 'book', 'sun', + 'mountain', 'volcano', 'island', 'rainbow', 'moon', 'rain', + 'robot', 'satellite', 'laptop', 'graduation', 'medal', + 'cat', 'dog', 'fish', 'burger', 'train', ) TWEMOJI_CODEPOINTS: dict[str, str] = { @@ -46,6 +49,10 @@ 'brain': '1f9e0', 'book': '1f4da', 'sun': '2600', + 'mountain': '26f0', 'volcano': '1f30b', 'island': '1f3dd', 'rainbow': '1f308', + 'moon': '1f319', 'rain': '1f327', 'robot': '1f916', 'satellite': '1f6f0', + 'laptop': '1f4bb', 'graduation': '1f393', 'medal': '1f3c5', 'cat': '1f431', + 'dog': '1f436', 'fish': '1f41f', 'burger': '1f354', 'train': '1f686', } AUTO_STICKER_THEMES: tuple[tuple[tuple[str, ...], tuple[str, str]], ...] = ( diff --git a/templates/index.html b/templates/index.html index b293b31..b72c400 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1507,11 +1507,11 @@

} } - async function syncLatestMapToStudioStorage(imageUrl) { + async function syncLatestMapToStudioStorage(imageUrl, topic = '') { if (!imageUrl || typeof StudioStorage === 'undefined' || !StudioStorage.setAsset) return; try { const blob = await (await fetch(imageUrl)).blob(); - await StudioStorage.setAsset('latest-map', new File([blob], 'generated-map.png', { type: 'image/png' })); + await StudioStorage.setAsset('latest-map', new File([blob], 'generated-map.png', { type: 'image/png' }), {topic}); } catch (_) { // Ignore storage quota or fetch errors during preview sync } @@ -1600,7 +1600,7 @@

if (item.analysis) { applyAnalysisToFillingStudio(item.analysis, item.map_values || null, false); } - syncLatestMapToStudioStorage(item.image_url); + syncLatestMapToStudioStorage(item.image_url, item.prompt); showToast(`Viewing #${item.index}: ${item.prompt}`, 'info'); } @@ -2018,7 +2018,7 @@

}); try { const blob = await (await fetch(result.image_url)).blob(); - await StudioStorage.setAsset('latest-map', new File([blob], 'generated-map.png', {type: 'image/png'})); + await StudioStorage.setAsset('latest-map', new File([blob], 'generated-map.png', {type: 'image/png'}), {topic: state.topic}); showToast("Map generated & auto-styled! Ready in Video Studio.", "success"); } catch (error) { showToast("Map generated, but automatic transfer could not be saved. Download it to use in Video Studio.", "warn"); @@ -2401,7 +2401,7 @@

logToConsole('[Auto-Style] Re-painted active map with updated Color Filling Studio rules & typography style.', 'success'); try { const blob = await (await fetch(data.image_url)).blob(); - await StudioStorage.setAsset('latest-map', new File([blob], 'generated-map.png', {type: 'image/png'})); + await StudioStorage.setAsset('latest-map', new File([blob], 'generated-map.png', {type: 'image/png'}), {topic: state.topic}); } catch { logToConsole('[Storage] Map was re-painted, but could not be saved for Video Studio.', 'warn'); } @@ -2703,7 +2703,7 @@

if (item.analysis) { applyAnalysisToFillingStudio(item.analysis, item.map_values || null, false); } - syncLatestMapToStudioStorage(item.image_url); + syncLatestMapToStudioStorage(item.image_url, s.topic); } updateCommandPreview(); diff --git a/templates/video.html b/templates/video.html index e4881e2..05efa4d 100644 --- a/templates/video.html +++ b/templates/video.html @@ -646,9 +646,10 @@

Frame Com

- -
-
+
+
Preparing materials0%
+ +

Progress will update when the worker starts encoding.

@@ -904,6 +905,24 @@

Frame Com + + + + + + + + + + + + + + + + + + @@ -977,6 +996,24 @@

Frame Com + + + + + + + + + + + + + + + + + + @@ -1252,6 +1289,7 @@

'subscribeTextInput', 'subscribeDelayInput', 'watermarkTextInput', 'watermarkFontSelect', 'watermarkColor', 'watermarkStarsToggle', 'codecSelect']; let videoSettingsReady = false; + let manualVideoTitle = false; const materialVersions = {mapFileInput: 0, bgFileInput: 0}; let pendingAssetSaves = Promise.resolve(); @@ -1261,6 +1299,7 @@

const input = document.getElementById(id); return [id, input.type === 'checkbox' ? input.checked : input.value]; })); + values.manualVideoTitle = manualVideoTitle; try { localStorage.setItem(VIDEO_SETTINGS_KEY, JSON.stringify(values)); } catch (error) { @@ -1268,9 +1307,9 @@

} } - function rememberVideoAsset(key, file) { + function rememberVideoAsset(key, file, metadata = {}) { // Serialize writes so a removal cannot be overtaken by an earlier upload. - pendingAssetSaves = pendingAssetSaves.then(() => StudioStorage.setAsset(key, file)).catch(() => { + pendingAssetSaves = pendingAssetSaves.then(() => StudioStorage.setAsset(key, file, metadata)).catch(() => { document.getElementById('studioSaveStatus').textContent = 'Some files could not be saved. You can still render this video.'; }); return pendingAssetSaves; @@ -1280,6 +1319,9 @@

try { const saved = JSON.parse(localStorage.getItem(VIDEO_SETTINGS_KEY) || '{}'); if (saved && typeof saved === 'object') { + // Older saved titles were entered manually before topic transfer existed. + manualVideoTitle = typeof saved.manualVideoTitle === 'boolean' + ? saved.manualVideoTitle : !!saved.videoTitleInput; VIDEO_SETTING_IDS.forEach(id => { const input = document.getElementById(id); const value = saved[id]; @@ -1327,7 +1369,13 @@

if (map?.file && materialVersions.mapFileInput === 0) { const file = map === latest ? await prepareGeneratedMap(map.file) : map.file; if (materialVersions.mapFileInput === 0) { - acceptFile('mapFileInput', file, map === latest); + acceptFile('mapFileInput', file, map === latest, map.topic); + if (renderFiles.mapFileInput === file && !manualVideoTitle && typeof map.topic === 'string' && map.topic.trim()) { + const title = document.getElementById('videoTitleInput'); + title.value = map.topic.trim().slice(0, title.maxLength); + updateStageTitlePreview(title.value); + saveVideoSettings(); + } } } if (background?.file && materialVersions.bgFileInput === 0) { @@ -2094,38 +2142,55 @@

brain: '🧠 Brain', book: '📚 Book', sun: '☀️ Sun', + mountain: '🏔️ Mountain', + volcano: '🌋 Volcano', + island: '🏝️ Island', + rainbow: '🌈 Rainbow', + moon: '🌙 Moon', + rain: '🌧️ Rain', + robot: '🤖 Robot', + satellite: '🛰️ Satellite', + laptop: '💻 Laptop', + graduation: '🎓 Graduation', + medal: '🏅 Medal', + cat: '🐱 Cat', + dog: '🐶 Dog', + fish: '🐟 Fish', + burger: '🍔 Burger', + train: '🚆 Train', + }; const AUTO_STICKER_THEME_VARIANTS = [ { name: 'Wildlife & Nature', keywords: ['bear', 'bears', 'grizzly', 'panda', 'wolf', 'wolves', 'fox', 'deer', 'moose', 'boar', 'lion', 'tiger', 'dog', 'dogs', 'cat', 'cats', 'pet', 'pets', 'animal', 'animals', 'wildlife', 'wild', 'forest', 'tree', 'trees', 'woods', 'nature', 'park', 'hunt', 'bird', 'fish', 'shark', 'snake', 'horse', 'cow'], - left: ['auto', 'bear', 'paw', 'tree', 'wolf'], - right: ['auto', 'tree', 'paw', 'bear', 'wolf'], + left: ['auto', 'bear', 'paw', 'tree', 'wolf', 'cat', 'dog', 'fish'], + right: ['auto', 'tree', 'paw', 'bear', 'wolf', 'cat', 'dog', 'fish'], }, { name: 'Food, Drinks & Lifestyle', keywords: ['beer', 'beers', 'wine', 'alcohol', 'vodka', 'whiskey', 'drink', 'drinks', 'pub', 'bar', 'coffee', 'tea', 'cafe', 'pizza', 'burger', 'food', 'cuisine', 'cheese', 'meat', 'bread', 'milk', 'water'], - left: ['auto', 'beer', 'coffee', 'wine', 'pizza'], - right: ['auto', 'coffee', 'beer', 'pizza', 'wine'], + left: ['auto', 'beer', 'coffee', 'wine', 'pizza', 'burger'], + right: ['auto', 'coffee', 'beer', 'pizza', 'wine', 'burger'], }, { name: 'People, IQ & Education', keywords: ['iq', 'smart', 'intelligent', 'brain', 'genius', 'education', 'school', 'university', 'student', 'literacy', 'book', 'books', 'read', 'language', 'languages', 'speak', 'word', 'words', 'alphabet', 'name', 'names', 'population', 'people', 'human'], - left: ['auto', 'brain', 'book', 'crown', 'star'], - right: ['auto', 'book', 'brain', 'trophy', 'globe'], + left: ['auto', 'brain', 'book', 'crown', 'star', 'graduation', 'laptop'], + right: ['auto', 'book', 'brain', 'trophy', 'globe', 'graduation', 'laptop'], }, { name: 'Cars, Transport & Energy', keywords: ['car', 'cars', 'auto', 'vehicle', 'drive', 'driving', 'highway', 'road', 'roads', 'train', 'rail', 'railway', 'metro', 'bus', 'traffic', 'fuel', 'gas', 'petrol', 'oil', 'tesla', 'bmw'], - left: ['auto', 'car', 'bolt', 'compass', 'fire'], - right: ['auto', 'bolt', 'car', 'pin', 'globe'], + left: ['auto', 'car', 'bolt', 'compass', 'fire', 'train'], + right: ['auto', 'bolt', 'car', 'pin', 'globe', 'train'], }, { name: 'Sports, Football & Games', keywords: ['sport', 'sports', 'football', 'soccer', 'fifa', 'uefa', 'world cup', 'basketball', 'hockey', 'tennis', 'olympic', 'olympics', 'medal', 'athlete', 'gym', 'game', 'games', 'chess'], - left: ['auto', 'soccer', 'trophy', 'fire', 'crown'], - right: ['auto', 'trophy', 'soccer', 'star', 'fire'], + left: ['auto', 'soccer', 'trophy', 'fire', 'crown', 'medal'], + right: ['auto', 'trophy', 'soccer', 'star', 'fire', 'medal'], }, { name: 'Cold, Ice & Winter Weather', @@ -2365,6 +2430,7 @@

const titleEl = document.getElementById('videoTitleInput'); if (!titleEl.value.trim()) { titleEl.value = 'When is Saint Nicholas\nDay in your country?'; + manualVideoTitle = true; } const wmEl = document.getElementById('watermarkTextInput'); if (!wmEl.value.trim()) { @@ -3109,7 +3175,7 @@

} } - function acceptFile(inputId, file, persist = true) { + function acceptFile(inputId, file, persist = true, topic = '') { materialVersions[inputId]++; // Size guard: reject material files larger than 10 MB. if (file.size > MAX_MATERIAL_BYTES) { @@ -3138,7 +3204,7 @@

// Update live Cinema Stage preview updateCinemaStagePreview(inputId, file); - if (persist) rememberVideoAsset(inputId === 'mapFileInput' ? 'video-map' : 'video-background', file); + if (persist) rememberVideoAsset(inputId === 'mapFileInput' ? 'video-map' : 'video-background', file, {topic}); logToConsole(`[File] Selected ${file.name} (${humanSize(file.size)})`, 'info'); } @@ -3224,6 +3290,42 @@

}); } + function setRenderProgress(progress, phase) { + const value = Math.max(0, Math.min(100, Number(progress) || 0)); + document.getElementById('renderProgressBar').value = value; + document.getElementById('renderProgressPercent').textContent = `${Math.round(value)}%`; + document.getElementById('renderProgressPhase').textContent = phase; + } + + function startRenderProgress(renderId) { + let stopped = false; + let timer; + let controller; + const poll = async () => { + controller = new AbortController(); + const deadline = setTimeout(() => controller.abort(), 6000); + try { + const response = await fetch(`/api/render_progress/${renderId}`, {signal: controller.signal, cache: 'no-store'}); + if (stopped) return; + if (response.ok) { + const status = await response.json(); + if (stopped) return; + setRenderProgress(status.progress, status.phase || 'Rendering'); + document.getElementById('renderProgressHint').textContent = status.error || 'Progress is reported by the render worker.'; + } else if (response.status !== 404) { + document.getElementById('renderProgressHint').textContent = 'Progress connection interrupted. Rendering may still be running.'; + } + } catch { + if (!stopped) document.getElementById('renderProgressHint').textContent = 'Reconnecting to render progress…'; + } finally { + clearTimeout(deadline); + if (!stopped) timer = setTimeout(poll, 750); + } + }; + timer = setTimeout(poll, 250); + return () => { stopped = true; clearTimeout(timer); controller?.abort(); }; + } + async function handleRenderVideo(e) { e.preventDefault(); const btn = document.getElementById('renderBtn'); @@ -3269,6 +3371,12 @@

btnIcon.className = "fa-solid fa-circle-notch animate-spin"; overlay.classList.remove('hidden'); metaBadge.textContent = "Rendering..."; + setRenderProgress(0, 'Preparing materials'); + document.getElementById('renderProgressHint').textContent = 'Preparing and uploading selected materials…'; + const renderId = typeof crypto.randomUUID === 'function' ? crypto.randomUUID() + : Array.from(crypto.getRandomValues(new Uint8Array(16)), byte => byte.toString(16).padStart(2, '0')).join('') + .replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, '$1-$2-$3-$4-$5'); + let stopProgress = () => {}; logToConsole(`[Render] POST /api/render_video title="${title}" codec=${codec} duration=${durationSecs}s map=${mapFile.name} bg=${bgFile.name}`, 'cmd'); @@ -3313,6 +3421,8 @@

top_right_sticker_image: rightStickerImageB64, }; + payload.render_id = renderId; + stopProgress = startRenderProgress(renderId); const response = await fetch('/api/render_video', { method: 'POST', headers: { 'Content-Type': 'application/json' }, @@ -3331,6 +3441,8 @@

throw new Error(result.error || 'Video render failed'); } + stopProgress(); + setRenderProgress(100, 'Complete'); renderResultData = result; const filePath = result.file_path || result.path || result.file || '(unknown path)'; @@ -3362,6 +3474,7 @@

showToast(error.message || "Video render failed. Check console logs.", 'error'); metaBadge.textContent = "Error"; } finally { + stopProgress(); overlay.classList.add('hidden'); btn.disabled = false; btn.classList.remove('opacity-70'); @@ -3398,6 +3511,7 @@

window.videoStudioReady = restoreVideoStudio(); loadCodecs(); ['input', 'change'].forEach(name => document.addEventListener(name, event => { + if (event.target.id === 'videoTitleInput') manualVideoTitle = true; if (VIDEO_SETTING_IDS.includes(event.target.id)) saveVideoSettings(); })); const stage = document.querySelector('.video-preview-stage'); diff --git a/tests/browser/test_video_studio.py b/tests/browser/test_video_studio.py index 0d788e9..4cbdfba 100644 --- a/tests/browser/test_video_studio.py +++ b/tests/browser/test_video_studio.py @@ -378,7 +378,7 @@ def test_generated_map_transfers_and_respects_manual_selection(studio, tmp_path) import json page, _ = studio - def generate_map(size): + def generate_map(size, topic="Test map"): output = io.BytesIO() Image.new('RGBA', size, (20, 160, 90, 255)).save(output, format='PNG') data_url = 'data:image/png;base64,' + base64.b64encode(output.getvalue()).decode() @@ -387,13 +387,14 @@ def generate_map(size): page.route('**/api/generate', lambda route: route.fulfill( content_type='application/json', body=json.dumps({'status': 'success', 'image_url': data_url}))) page.goto('http://automap.test/') - page.locator('#topicInput').fill('Test map') + page.locator('#topicInput').fill(topic) page.evaluate('apiKeyRequired = false; handleGenerate({preventDefault(){}})') generate_map((450, 300)) reopen_studio(page) page.wait_for_function("document.querySelector('#stageMapPreview').naturalWidth === 450") assert page.locator('#mapDropzone .dropzone-name').inner_text().startswith('generated-map.png') assert page.evaluate('renderFiles.mapFileInput.type') == 'image/png' + assert page.locator('#videoTitleInput').input_value() == 'Test map' custom = tmp_path / 'custom.png' Image.new('RGBA', (320, 200), 'blue').save(custom) page.locator('#mapFileInput').set_input_files(str(custom)) @@ -405,9 +406,15 @@ def generate_map(size): page.evaluate('async () => { await pendingAssetSaves; }') reopen_studio(page) assert page.locator('#stageMapPreview').is_hidden() - generate_map((500, 300)) + generate_map((500, 300), 'Нова тема карти') reopen_studio(page) page.wait_for_function("document.querySelector('#stageMapPreview').naturalWidth === 500") + assert page.locator('#videoTitleInput').input_value() == 'Нова тема карти' + page.locator('#videoTitleInput').fill('My edited headline') + generate_map((550, 300), 'Another generated topic') + reopen_studio(page) + page.wait_for_function("document.querySelector('#stageMapPreview').naturalWidth === 550") + assert page.locator('#videoTitleInput').input_value() == 'My edited headline' def test_video_settings_and_uploaded_materials_survive_navigation(studio, tmp_path): @@ -575,3 +582,69 @@ def capture(route): page.evaluate('handleRenderVideo({preventDefault(){}})') assert requests[1]['include_audio'] is True assert requests[1]['audio_title'] == 'Selected_song' + + +def test_render_progress_bar_follows_worker_and_stops_after_failure(studio): + page, _ = studio + page.evaluate('async () => { await window.videoStudioReady; }') + page.locator('#videoTitleInput').fill('Progress test') + page.evaluate('''() => { + renderFiles.mapFileInput = new File(['map'], 'map.png', {type:'image/png'}); + renderFiles.bgFileInput = new File(['video'], 'background.mp4', {type:'video/mp4'}); + }''') + pending = [] + page.route('**/api/render_video', lambda route: pending.append(route)) + progress_requests = [] + def progress(route): + progress_requests.append(route.request.url) + route.fulfill(json={'status': 'rendering', 'progress': 64, 'phase': 'Encoding video'}) + page.route('**/api/render_progress/*', progress) + page.evaluate('() => { void handleRenderVideo({preventDefault(){}}); }') + page.wait_for_function("document.getElementById('renderProgressBar').value === 64") + assert page.locator('#renderProgressPercent').inner_text() == '64%' + assert page.locator('#renderProgressPhase').inner_text() == 'Encoding video' + assert page.locator('#renderBtn').is_disabled() + assert len(pending) == 1 + render_id = pending[0].request.post_data_json['render_id'] + assert progress_requests[0].endswith(render_id) + pending[0].fulfill(status=500, json={'status': 'error', 'error': 'Encoder failed'}) + page.wait_for_function("!document.getElementById('renderBtn').disabled") + assert page.locator('#renderProgressBar').get_attribute('value') != '100' + assert 'Encoder failed' in page.locator('#toastMsg').inner_text() + assert page.locator('#renderOverlay').is_hidden() + assert page.locator('#renderProgressBar').evaluate('el => el.value') == 64 + + +def test_slow_map_restore_does_not_replace_a_title_edited_in_the_meantime(studio): + page, _ = studio + page.evaluate('async () => { await window.videoStudioReady; }') + page.evaluate('''() => { + const original = StudioStorage.getAsset; + let release; + const pending = new Promise(resolve => release = resolve); + StudioStorage.getAsset = key => key === 'latest-map' ? pending : original(key); + window.restoringTitle = restoreVideoStudio(); + window.releaseLatestMap = release; + }''') + page.locator('#videoTitleInput').fill('Typed while loading') + page.evaluate('''async () => { + const canvas = document.createElement('canvas'); canvas.width=80; canvas.height=40; + const blob = await new Promise(resolve => canvas.toBlob(resolve)); + releaseLatestMap({file: new File([blob], 'generated.png', {type:'image/png'}), + topic:'Old generated topic', updatedAt:Date.now()}); + await restoringTitle; + }''') + assert page.locator('#videoTitleInput').input_value() == 'Typed while loading' + + +def test_new_stickers_exist_on_both_sides_and_preview_offline(studio): + page, previews = studio + for side in ('Left', 'Right'): + for kind in ('mountain', 'volcano', 'island', 'rainbow', 'moon', 'rain', 'robot', 'satellite', + 'laptop', 'graduation', 'medal', 'cat', 'dog', 'fish', 'burger', 'train'): + assert page.locator(f'#top{side}StickerSelect option[value="{kind}"]').count() == 1 + page.locator('#topLeftStickerSelect').select_option('robot') + page.locator('#topRightStickerSelect').select_option('mountain') + page.wait_for_function("document.querySelector('#stageTopLeftSticker img')?.naturalWidth > 0") + page.wait_for_function("document.querySelector('#stageTopRightSticker img')?.naturalWidth > 0") + assert page.locator('#stageCompositeBox').evaluate("el => el.classList.contains('has-top-stickers')") diff --git a/tests/test_render_progress.py b/tests/test_render_progress.py new file mode 100644 index 0000000..91fe0a8 --- /dev/null +++ b/tests/test_render_progress.py @@ -0,0 +1,109 @@ +"""Progress is isolated per request and follows actual MoviePy callbacks.""" +from unittest.mock import Mock +from uuid import uuid4 + +import pytest +from flask import Flask, jsonify +import requests + +from render_progress import RenderProgress + + +def test_progress_tracks_encoding_and_completes_only_after_response(): + app = Flask(__name__) + tracker = RenderProgress() + render_id = str(uuid4()) + seen = [] + + @app.post('/render') + @tracker.track + def render(): + logger = tracker.logger() + for _ in logger.iter_bar(chunk=range(4)): + pass + seen.append(tracker.snapshot(render_id)) + for _ in logger.iter_bar(frame_index=range(10)): + pass + seen.append(tracker.snapshot(render_id)) + return jsonify(status='success') + + assert app.test_client().post('/render', json={'render_id': render_id}).status_code == 200 + assert seen[0]['phase'] == 'Encoding audio' + assert 0 < seen[0]['progress'] < seen[1]['progress'] < 100 + assert seen[1]['status'] == 'rendering' + assert tracker.snapshot(render_id) == {'status': 'success', 'progress': 100, 'phase': 'Complete'} + assert app.test_client().post('/render', json={'render_id': render_id}).status_code == 409 + + +def test_worker_records_validation_failures_and_rejects_bad_ids(worker): + client = worker.app.test_client() + render_id = str(uuid4()) + assert client.get(f'/api/render_progress/{render_id}').status_code == 404 + assert client.post('/api/render_video', json={'render_id': render_id}).status_code == 400 + result = client.get(f'/api/render_progress/{render_id}') + assert result.json['status'] == 'error' + assert result.json['progress'] == 0 + assert 'map_file' in result.json['error'] + assert client.post('/api/render_video', json={'render_id': 'invalid'}).status_code == 400 + + +def test_failed_render_does_not_claim_completion(): + app = Flask(__name__) + tracker = RenderProgress() + render_id = str(uuid4()) + + @app.post('/render') + @tracker.track + def render(): + tracker.update(render_id, progress=50, phase='Encoding video') + return jsonify(error='Encoder failed'), 500 + + result = app.test_client().post('/render', json={'render_id': render_id}) + assert result.status_code == 500 + assert tracker.snapshot(render_id)['status'] == 'error' + assert tracker.snapshot(render_id)['progress'] == 50 + assert tracker.snapshot(render_id)['error'] == 'Encoder failed' + + +def test_render_ids_are_isolated_and_percentages_monotonic(): + tracker = RenderProgress() + tracker.records = {'first': {'progress': 0, 'status': 'rendering'}, 'second': {'progress': 0, 'status': 'rendering'}} + tracker.update('first', progress=70) + tracker.update('first', progress=30) + tracker.update('unknown', progress=100) + assert tracker.snapshot('first')['progress'] == 70 + assert tracker.snapshot('second')['progress'] == 0 + assert tracker.snapshot('unknown') is None + + +@pytest.mark.parametrize('status', [200, 404]) +def test_web_progress_proxy(web, monkeypatch, status): + render_id = str(uuid4()) + upstream = Mock(status_code=status) + upstream.json.return_value = {'status': 'rendering', 'progress': 63, 'phase': 'Encoding video'} + get = Mock(return_value=upstream) + monkeypatch.setattr(requests, 'get', get) + result = web.app.test_client().get(f'/api/render_progress/{render_id}') + assert result.status_code == status + assert result.json == upstream.json.return_value + get.assert_called_once_with(f'{web.WORKER_URL}/api/render_progress/{render_id}', timeout=5) + upstream.close.assert_called_once() + + +@pytest.mark.parametrize('failure', ['connection', 'json']) +def test_web_progress_errors_are_recoverable(web, monkeypatch, failure): + upstream = Mock() + upstream.json.side_effect = ValueError('bad json') + get = Mock(side_effect=requests.Timeout()) if failure == 'connection' else Mock(return_value=upstream) + monkeypatch.setattr(requests, 'get', get) + result = web.app.test_client().get(f'/api/render_progress/{uuid4()}') + assert result.status_code == 502 + + +def test_render_progress_id_is_forwarded_to_worker(web, monkeypatch): + render_id = str(uuid4()) + post = Mock(return_value=Mock(status_code=200, json=lambda: {'status': 'success'})) + monkeypatch.setattr(requests, 'post', post) + result = web.app.test_client().post('/api/render_video', json={'video_title': 'Test', 'render_id': render_id}) + assert result.status_code == 200 + assert post.call_args.kwargs['json']['render_id'] == render_id diff --git a/tests/test_video_render.py b/tests/test_video_render.py index 40bd889..5088d27 100644 --- a/tests/test_video_render.py +++ b/tests/test_video_render.py @@ -1,6 +1,7 @@ import base64 import io from urllib.parse import parse_qs, urlsplit +from uuid import uuid4 import pytest @@ -76,7 +77,9 @@ def test_portrait_render_with_centered_title_and_right_aligned_map(worker, tmp_p clip.write_videofile(str(background), fps=30, codec='libx264', logger=None) map_file = tmp_path / 'map.png' Image.new('RGB', map_size, (0, 255, 0)).save(map_file) + render_id = str(uuid4()) response = worker.app.test_client().post('/api/render_video', json={ + 'render_id': render_id, 'map_file': base64.b64encode(map_file.read_bytes()).decode(), 'background_video': base64.b64encode(background.read_bytes()).decode(), 'video_title': 'Найкрасивіші країни Європи', @@ -85,6 +88,8 @@ def test_portrait_render_with_centered_title_and_right_aligned_map(worker, tmp_p 'duration': 1, 'include_audio': False, }) assert response.status_code == 200, response.json + progress = worker.app.test_client().get(f'/api/render_progress/{render_id}').json + assert progress == {'status': 'success', 'progress': 100, 'phase': 'Complete'} assert response.json['download_url'].isascii() assert parse_qs(urlsplit(response.json['download_url']).query)['path'] == [response.json['file_path']] with VideoFileClip(response.json['file_path']) as result: @@ -393,3 +398,18 @@ def test_custom_subscribe_stars_and_top_sticker_images_render(worker, tmp_path, else: assert np.count_nonzero(yellow_footer) == 0 + + +def test_new_stickers_have_distinct_offline_artwork_and_generated_variants(worker): + kinds = ['mountain', 'volcano', 'island', 'rainbow', 'moon', 'rain', 'robot', 'satellite', + 'laptop', 'graduation', 'medal', 'cat', 'dog', 'fish', 'burger', 'train'] + images = [] + for kind in kinds: + image = worker._sticker_image(kind, size=100, effects=False, allow_remote=False) + assert image.shape == (100, 100, 4) + assert np.any(image[:, :, 3]) + images.append(image.tobytes()) + generated = worker._sticker_image('gen_diecut:' + kind, size=100, effects=False, allow_remote=False) + assert generated.shape == image.shape + assert np.any(generated[:, :, 3]) + assert len(set(images)) == len(kinds) diff --git a/yt-dlp_serv.py b/yt-dlp_serv.py index 49f78e6..64cfbde 100644 --- a/yt-dlp_serv.py +++ b/yt-dlp_serv.py @@ -11,8 +11,18 @@ from urllib.parse import urlencode, urlsplit from flask import Flask, request, jsonify, send_file +from render_progress import RenderProgress app = Flask(__name__) +render_progress = RenderProgress() + + +@app.get('/api/render_progress/') +def get_render_progress(render_id): + result = render_progress.snapshot(str(render_id)) + response = jsonify(result if result else {'error': 'Render not found.'}) + response.headers['Cache-Control'] = 'no-store' + return response, 200 if result else 404 STORAGE_DIR = "/tmp/automap_storage" DOWNLOADS = os.path.join(STORAGE_DIR, "downloads") @@ -815,6 +825,114 @@ def _style_generated_sticker(icon_canvas, gen_style: str, size: int): return stamp.resize((size, size), Image.Resampling.LANCZOS) +def _draw_extra_sticker(kind, draw, s, outline, ow): + """Offline vector artwork for the extended sticker library.""" + extras = {'mountain', 'volcano', 'island', 'rainbow', 'moon', 'rain', 'robot', + 'satellite', 'laptop', 'graduation', 'medal', 'cat', 'dog', 'fish', 'burger', 'train'} + if kind not in extras: + return False + + def coords(values): + return tuple(value * s for value in values) + + def ellipse(box, color): + draw.ellipse(coords(box), fill=color, outline=outline, width=ow) + + def rect(box, color): + draw.rounded_rectangle(coords(box), radius=ow * 2, fill=color, outline=outline, width=ow) + + def poly(points, color): + draw.polygon([coords(point) for point in points], fill=color, outline=outline, width=ow) + + def line(points, color, width=None): + draw.line([coords(point) for point in points], fill=color, width=width or ow) + + blue, gold, white = '#38bdf8', '#fbbf24', '#f8fafc' + if kind in ('mountain', 'volcano'): + if kind == 'volcano': + ellipse((.35, .06, .61, .3), '#94a3b8') + poly([(.08, .9), (.48, .2), (.9, .9)], '#78716c' if kind == 'volcano' else '#64748b') + poly([(.32, .49), (.48, .2), (.66, .49), (.53, .42), (.43, .51)], '#fb923c' if kind == 'volcano' else white) + if kind == 'volcano': + line([(.49, .42), (.6, .65), (.55, .86)], '#ef4444', ow * 2) + elif kind == 'island': + ellipse((.07, .65, .94, .94), blue) + ellipse((.19, .66, .84, .87), gold) + line([(.48, .74), (.6, .3)], '#92400e', ow * 2) + for tip in [(.23, .3), (.38, .1), (.82, .16), (.91, .39), (.7, .5)]: + poly([(.58, .31), tip, (.55, .4)], '#22c55e') + elif kind == 'rainbow': + for index, color in enumerate(['#ef4444', '#fb923c', gold, '#22c55e', '#38bdf8', '#a78bfa']): + inset = index * .055 + draw.arc(coords((.08 + inset, .13 + inset, .92 - inset, .97 - inset)), 180, 360, fill=color, width=ow * 2) + elif kind == 'moon': + ellipse((.15, .08, .85, .89), gold) + draw.ellipse(coords((.37, .01, .99, .7)), fill=(0, 0, 0, 0)) + elif kind == 'rain': + for box in [(.12, .25, .57, .61), (.31, .12, .75, .61), (.56, .29, .9, .63)]: + ellipse(box, '#cbd5e1') + for x in (.27, .5, .73): + line([(x, .7), (x - .06, .89)], blue, ow * 2) + elif kind == 'robot': + line([(.5, .1), (.5, .25)], '#94a3b8') + ellipse((.44, .05, .56, .18), '#ef4444') + rect((.14, .25, .86, .86), '#94a3b8') + for x in (.27, .6): + ellipse((x, .39, x + .13, .55), blue) + rect((.29, .65, .71, .77), white) + line([(.43, .66), (.43, .76)], outline) + line([(.57, .66), (.57, .76)], outline) + elif kind == 'satellite': + rect((.05, .3, .31, .73), '#2563eb') + rect((.69, .3, .95, .73), '#2563eb') + line([(.29, .52), (.72, .52)], gold, ow * 2) + rect((.35, .25, .65, .78), '#cbd5e1') + poly([(.41, .24), (.64, .07), (.76, .28)], white) + elif kind == 'laptop': + rect((.18, .16, .82, .69), '#475569') + rect((.25, .24, .75, .6), blue) + poly([(.18, .69), (.82, .69), (.96, .86), (.04, .86)], '#cbd5e1') + elif kind == 'graduation': + rect((.25, .4, .74, .75), '#1e293b') + poly([(.07, .34), (.5, .13), (.94, .34), (.5, .55)], '#334155') + line([(.51, .34), (.83, .42), (.83, .78)], gold, ow * 2) + elif kind == 'medal': + poly([(.25, .09), (.47, .09), (.61, .51), (.36, .55)], '#ef4444') + poly([(.54, .09), (.76, .09), (.64, .55), (.4, .51)], '#2563eb') + ellipse((.2, .4, .81, .96), gold) + draw.polygon(_star_polygon(s * .5, s * .68, s * .18, s * .08), fill=white) + elif kind in ('cat', 'dog'): + color = '#fb923c' if kind == 'cat' else '#b45309' + if kind == 'cat': + poly([(.18, .43), (.18, .12), (.47, .35)], color) + poly([(.53, .35), (.83, .12), (.83, .43)], color) + else: + ellipse((.04, .19, .34, .72), color) + ellipse((.67, .19, .96, .72), color) + ellipse((.18, .27, .83, .91), color) + for x in (.33, .62): + ellipse((x, .47, x + .06, .55), outline) + ellipse((.36, .61, .66, .81), '#ffedd5') + poly([(.44, .64), (.59, .64), (.51, .73)], outline) + elif kind == 'fish': + poly([(.59, .51), (.92, .27), (.92, .79)], '#fb923c') + ellipse((.08, .26, .74, .8), blue) + ellipse((.22, .41, .29, .48), outline) + line([(.47, .39), (.41, .54), (.49, .65)], '#2563eb') + elif kind == 'burger': + ellipse((.13, .13, .88, .62), gold) + for y, color in [(.44, '#22c55e'), (.56, '#ef4444'), (.68, '#78350f'), (.78, gold)]: + rect((.12, y, .89, y + .12), color) + elif kind == 'train': + rect((.21, .09, .8, .8), '#ef4444') + rect((.28, .22, .72, .48), blue) + for x in (.3, .61): + ellipse((x, .59, x + .1, .69), gold) + line([(.27, .83), (.14, .95)], outline, ow * 2) + line([(.74, .83), (.87, .95)], outline, ow * 2) + return True + + def _sticker_image(kind: str, size: int = 140, effects: bool = True, allow_remote: bool = False): """Render a decorative top-corner sticker (library Twemoji or procedural generated style).""" import numpy as np @@ -861,7 +979,9 @@ def _sticker_image(kind: str, size: int = 140, effects: bool = True, allow_remot outline = (18, 18, 24, 255) ow = 4 * scale - if kind == 'santa': + if _draw_extra_sticker(kind, draw, s, outline, ow): + pass + elif kind == 'santa': # Fluffy white beard base draw.ellipse((s * 0.16, s * 0.44, s * 0.84, s * 0.94), fill=(250, 252, 255, 255), outline=outline, width=ow) draw.ellipse((s * 0.12, s * 0.52, s * 0.46, s * 0.84), fill=(250, 252, 255, 255), outline=outline, width=ow) @@ -1235,6 +1355,7 @@ def video_title_preview(): @app.route('/api/render_video', methods=['POST']) @app.route('/api/render_video/', methods=['POST']) +@render_progress.track def make_video() -> tuple[object, int]: cleanup_old_files() data: dict[str, object] = request.get_json(silent=True) or {} @@ -1562,7 +1683,7 @@ def _veil_mask(t: float) -> np.ndarray: codec=codec, audio_codec="aac", fps=30, - logger=None, + logger=render_progress.logger(), ) return jsonify({ From 8b1d57e09a9e5878a50eba566158ee1b8dd6314d Mon Sep 17 00:00:00 2001 From: deltaaa00 Date: Sun, 15 Nov 2026 10:27:14 +0200 Subject: [PATCH 03/23] Add global horizontal label shift and move repaint controls to main panel --- map/auto_map.py | 8 ++++++ readme.md | 6 ++++ templates/index.html | 44 ++++++++++++++++++++++++------ tests/browser/test_map_palettes.py | 34 +++++++++++++++++++++++ tests/test_auto_map.py | 18 ++++++++++++ 5 files changed, 101 insertions(+), 9 deletions(-) diff --git a/map/auto_map.py b/map/auto_map.py index 5f5134b..c40c1ad 100644 --- a/map/auto_map.py +++ b/map/auto_map.py @@ -419,6 +419,12 @@ def get_font(size): except (ValueError, TypeError, OverflowError): label_stroke_width = 15 label_auto_fit = bool(ls.get("auto_fit", False)) if "auto_fit" in ls else False + try: + raw_offset = ls.get('offset_x', 0) + offset = float(raw_offset) if not isinstance(raw_offset, bool) else 0 + label_offset_x = max(-500, min(500, round(offset))) if math.isfinite(offset) else 0 + except (TypeError, ValueError, OverflowError): + label_offset_x = 0 # Most least and short form conflict solution # @TODO Make most/least and short form compatible @@ -630,6 +636,7 @@ def get_font(size): # The first fill seed also anchors the region's single label. x, y = points[0] + x += label_offset_x info = str(info) @@ -703,6 +710,7 @@ def get_font(size): "stroke_color": f"#{label_stroke_fill[0]:02x}{label_stroke_fill[1]:02x}{label_stroke_fill[2]:02x}", "stroke_width": label_stroke_width, "auto_fit": label_auto_fit, + "offset_x": label_offset_x, "recommended_font": font_name or ("Bips.ttf" if mode == "number" else "DIN Condensed.ttf"), "recommended_gsm": size_mod, }, diff --git a/readme.md b/readme.md index e859208..8a270bc 100644 --- a/readme.md +++ b/readme.md @@ -138,6 +138,12 @@ Choose a model, enter a topic, and select text or numeric mode and a map type. A Open **Theme & Style** from the left panel to choose a palette or edit category colors, numeric ranges, and label appearance. **Apply & Re-Paint Current Map** reuses the generated values, so changing colors does not require another AI call. Selecting a batch result restores its generation parameters before repainting it. +The **Global label position** slider in the main left panel moves all country or +continent labels horizontally, from −500 to +500 pixels. Negative values move +left; positive values move right. The value is remembered in this browser. Use +**Apply & Re-Paint Current Map**, next to the generation controls, to apply the +shift without another AI request. The slider's **Reset** restores zero offset. + The generated map's topic travels with its image into Video Studio and fills the video headline automatically. Batch selection and restored history use the topic of the selected map. Headlines you edit manually are kept when returning to the diff --git a/templates/index.html b/templates/index.html index b72c400..2e73707 100644 --- a/templates/index.html +++ b/templates/index.html @@ -649,11 +649,29 @@

+
+ + 0 px +
+ +
← LeftRight →
+

Moves every country label horizontally. Click Re-Paint to apply without another AI request.

+ + + + @@ -1105,19 +1123,11 @@

-
- @@ -1189,10 +1199,12 @@

const providerKeys = {gemini: '', deepseek: ''}; const MAP_TOPIC_STORAGE_KEY = 'automap-map-topic'; const DEFAULT_AI_TEMPERATURE = 0.75; + const LABEL_OFFSET_STORAGE_KEY = 'automap-label-offset-x'; // --- Initialize on Load --- window.addEventListener('DOMContentLoaded', () => { restoreSavedMapInputs(); + try { updateLabelOffset(localStorage.getItem(LABEL_OFFSET_STORAGE_KEY) || 0, false); } catch {} updateAiTemperature(DEFAULT_AI_TEMPERATURE, false); try { const saved = localStorage.getItem(AI_TEMPERATURE_STORAGE_KEY); @@ -1760,7 +1772,7 @@

palette_theme, // Automatic previews are not manual overrides for the next map. ...(!auto_style ? { filling_txt, filling_num } : {}), - label_style: { ...labelStyleConfig } + label_style: { ...labelStyleConfig, offset_x: Number(document.getElementById('labelOffsetX').value) } }; } @@ -2166,6 +2178,15 @@

document.getElementById('autoStyleFromMapToggle').checked = false; } + function updateLabelOffset(value, persist = true) { + const parsed = Number(value); + const offset = Number.isFinite(parsed) ? Math.max(-500, Math.min(500, Math.round(parsed))) : 0; + document.getElementById('labelOffsetX').value = offset; + document.getElementById('labelOffsetXValue').textContent = `${offset > 0 ? '+' : ''}${offset} px`; + labelStyleConfig.offset_x = offset; + if (persist) saveBrowserValue(LABEL_OFFSET_STORAGE_KEY, String(offset)); + } + function updateLabelStyleField(field, val) { if (field === 'stroke_width') { labelStyleConfig.stroke_width = Math.max(2, Math.min(25, parseInt(val, 10) || 15)); @@ -2216,6 +2237,7 @@

...labelStyleConfig, ...analysis.label_style }; + if (analysis.label_style.offset_x !== undefined) updateLabelOffset(analysis.label_style.offset_x); } const statsBar = document.getElementById('detectedMapStatsBar'); @@ -2355,6 +2377,7 @@

btn.disabled = busy || paletteAnalysisPending || !paletteAnalysisReady; btn.setAttribute('aria-busy', String(busy)); document.getElementById('fillingPaletteTheme').disabled = busy; + document.getElementById('labelOffsetX').disabled = busy; document.getElementById('openFillingStudioBtn').disabled = busy; document.getElementById('mapRepaintButtonLabel').textContent = busy ? 'Re-painting…' : 'Apply & Re-Paint Current Map'; @@ -2487,6 +2510,7 @@

stroke_color: labelStyleConfig.stroke_color, stroke_width: labelStyleConfig.stroke_width, auto_fit: labelStyleConfig.auto_fit, + offset_x: Number(document.getElementById('labelOffsetX').value), recommended_font: labelStyleConfig.recommended_font } }; @@ -2677,6 +2701,7 @@

if (!item) return; const s = item.state; + updateLabelOffset(s.label_style?.offset_x ?? 0); changeAiProvider(s.provider || modelProvider(s.model), s.model); const modeRadio = document.querySelector(`input[name="mode"][value="${s.mode}"]`); if (modeRadio) modeRadio.checked = true; @@ -2737,6 +2762,7 @@

// --- Reset Form --- function resetForm() { + updateLabelOffset(0); updateAiTemperature(DEFAULT_AI_TEMPERATURE); document.querySelector('input[name="mode"][value="text"]').checked = true; document.querySelector('input[name="map_type"][value="default"]').checked = true; diff --git a/tests/browser/test_map_palettes.py b/tests/browser/test_map_palettes.py index 4a2fc03..7de4081 100644 --- a/tests/browser/test_map_palettes.py +++ b/tests/browser/test_map_palettes.py @@ -78,6 +78,7 @@ def repaint(route): route.fulfill(status=500, json={'status': 'error', 'error': 'Stop after inspecting payload'}) page.route('**/api/restyle_map', repaint) + page.evaluate('closeFillingStudio()') page.click('#restyleCurrentMapBtn') page.wait_for_function("!document.getElementById('restyleCurrentMapBtn').disabled") assert len(payloads) == 1 @@ -157,6 +158,7 @@ def test_repaint_shows_busy_state_until_image_or_error_is_ready(map_page, status page = map_page pending = [] page.route('**/api/restyle_map', lambda route: pending.append(route)) + page.evaluate('closeFillingStudio()') page.click('#restyleCurrentMapBtn') page.wait_for_function('mapRepaintPending') assert page.locator('#mapRepaintStatus').is_visible() @@ -515,3 +517,35 @@ def test_history_uses_model_provider_and_its_saved_key(map_page): assert page.evaluate('getFormState().provider') == 'deepseek' assert page.locator('#apiKeyInput').input_value() == 'deepseek-history-key' assert page.locator('#modelSelect').input_value() == 'deepseek-flash' + + +def test_label_shift_and_repaint_are_accessible_next_to_generate_without_ai(map_page): + page = map_page + page.evaluate('closeFillingStudio()') + assert page.locator('#cliForm #restyleCurrentMapBtn').count() == 1 + assert page.locator('#fillingStudioModal #restyleCurrentMapBtn').count() == 0 + assert page.locator('#restyleCurrentMapBtn').is_visible() + payloads = [] + ai_calls = [] + page.on('request', lambda request: ai_calls.append(request.url) if '/api/generate' in request.url else None) + png = io.BytesIO() + Image.new('RGB', (2, 2), 'green').save(png, format='PNG') + image_url = 'data:image/png;base64,' + base64.b64encode(png.getvalue()).decode('ascii') + def repaint(route): + payloads.append(route.request.post_data_json) + route.fulfill(json={'status': 'success', 'image_url': image_url}) + page.route('**/api/restyle_map', repaint) + for key, offset in [('End', 500), ('Home', -500)]: + page.locator('#labelOffsetX').press(key) + assert page.evaluate('getFormState().label_style.offset_x') == offset + assert page.locator('#labelOffsetXValue').inner_text() == f'{offset:+d} px' + page.click('#restyleCurrentMapBtn') + page.wait_for_function('!mapRepaintPending') + assert payloads[-1]['label_style']['offset_x'] == offset + assert payloads[-1]['map_values'] == VALUES + assert not ai_calls + page.reload() + assert page.locator('#labelOffsetX').input_value() == '-500' + page.add_style_tag(content='.hidden {display:none!important}') + page.locator('button[onclick="updateLabelOffset(0)"]').click() + assert page.evaluate('getFormState().label_style.offset_x') == 0 diff --git a/tests/test_auto_map.py b/tests/test_auto_map.py index 5049d4f..a598f25 100644 --- a/tests/test_auto_map.py +++ b/tests/test_auto_map.py @@ -382,3 +382,21 @@ def test_renderer_forwards_temperature_to_ai_but_repaint_does_not_call_ai(scene) + + +@pytest.mark.parametrize('map_type', ['default', 'flag', 'world']) +@pytest.mark.parametrize('offset', [-45, 45]) +def test_global_label_shift_moves_all_labels_but_not_country_fill_seeds(scene, monkeypatch, map_type, offset): + draw = Mock() + fill = Mock() + monkeypatch.setattr(ImageDraw.ImageDraw, 'text', draw) + monkeypatch.setattr(ImageDraw, 'floodfill', fill) + render(map_type=map_type, label_style={'offset_x': 0}) + baseline = [call.args[0] for call in draw.call_args_list] + seeds = [call.kwargs['xy'] for call in fill.call_args_list] + draw.reset_mock() + fill.reset_mock() + output = render(map_type=map_type, label_style={'offset_x': offset}) + assert [call.args[0] for call in draw.call_args_list] == [(x + offset, y) for x, y in baseline] + assert [call.kwargs['xy'] for call in fill.call_args_list] == seeds + assert output.map_meta['label_style']['offset_x'] == offset From 1254049f73a33fc3afd6db568ee7d913d940e56c Mon Sep 17 00:00:00 2001 From: deltaaa00 Date: Sun, 15 Nov 2026 10:31:35 +0200 Subject: [PATCH 04/23] Show repaint status overlay on map preview --- templates/index.html | 9 +++++++++ tests/browser/test_map_palettes.py | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/templates/index.html b/templates/index.html index 2e73707..3039409 100644 --- a/templates/index.html +++ b/templates/index.html @@ -760,6 +760,13 @@

Cartograp
+
@@ -2384,6 +2391,8 @@

document.getElementById('mapRepaintIcon').className = busy ? 'fa-solid fa-circle-notch animate-spin' : 'fa-solid fa-paintbrush'; document.getElementById('mapRepaintStatus').classList.toggle('hidden', !busy); + document.getElementById('mapRepaintOverlay').classList.toggle('hidden', !busy); + document.getElementById('mapViewport').setAttribute('aria-busy', String(busy)); } async function restyleCurrentMapFromStudio() { diff --git a/tests/browser/test_map_palettes.py b/tests/browser/test_map_palettes.py index 7de4081..66363b0 100644 --- a/tests/browser/test_map_palettes.py +++ b/tests/browser/test_map_palettes.py @@ -162,6 +162,9 @@ def test_repaint_shows_busy_state_until_image_or_error_is_ready(map_page, status page.click('#restyleCurrentMapBtn') page.wait_for_function('mapRepaintPending') assert page.locator('#mapRepaintStatus').is_visible() + assert page.locator('#mapViewport #mapRepaintOverlay').is_visible() + assert 'Re-painting map' in page.locator('#mapRepaintOverlay').inner_text() + assert page.locator('#mapViewport').get_attribute('aria-busy') == 'true' assert page.locator('#mapRepaintButtonLabel').inner_text() == 'Re-painting…' assert page.locator('#restyleCurrentMapBtn').get_attribute('aria-busy') == 'true' assert page.locator('#restyleCurrentMapBtn').is_disabled() @@ -179,6 +182,8 @@ def test_repaint_shows_busy_state_until_image_or_error_is_ready(map_page, status pending[0].fulfill(status=status, json=payload) page.wait_for_function('!mapRepaintPending') assert page.locator('#mapRepaintStatus').is_hidden() + assert page.locator('#mapRepaintOverlay').is_hidden() + assert page.locator('#mapViewport').get_attribute('aria-busy') == 'false' assert page.locator('#restyleCurrentMapBtn').get_attribute('aria-busy') == 'false' assert page.locator('#fillingPaletteTheme').is_enabled() assert page.locator('#mapRepaintButtonLabel').inner_text() == 'Apply & Re-Paint Current Map' From d54bf6885b7c79904d4962880b604dff0b5517a9 Mon Sep 17 00:00:00 2001 From: deltaaa00 Date: Sun, 15 Nov 2026 10:41:22 +0200 Subject: [PATCH 05/23] Persist run history in browser and expire runs after 24 hours --- readme.md | 5 ++ static/studio-storage.js | 56 +++++++++++++++++++-- templates/index.html | 78 +++++++++++++++++++++++++--- tests/browser/test_map_palettes.py | 81 ++++++++++++++++++++++++++++++ 4 files changed, 209 insertions(+), 11 deletions(-) diff --git a/readme.md b/readme.md index 8a270bc..3e710df 100644 --- a/readme.md +++ b/readme.md @@ -153,6 +153,11 @@ The temperature slider accepts `0`–`2` and defaults to `0.75` in the web UI. I The browser remembers API keys, the last topic, and palette preferences. Video Studio also stores selected materials and settings locally. Storage limits or disabled storage can prevent persistence; download important outputs rather than treating browser storage as an archive. +**Run History** is saved in this browser's IndexedDB with map images, values, and +generation settings, but without API keys. It survives reloads and reopening the +site. Runs older than 24 hours are removed when the page opens and checked every +minute while it is open. **Clear History** removes the saved history as well. + ### Create a portrait video 1. Generate a map or select a map image in Video Studio. diff --git a/static/studio-storage.js b/static/studio-storage.js index 393dd8d..537033a 100644 --- a/static/studio-storage.js +++ b/static/studio-storage.js @@ -4,8 +4,14 @@ window.StudioStorage = (() => { function open() { if (!connection) { connection = new Promise((resolve, reject) => { - const request = indexedDB.open('automap-studio', 1); - request.onupgradeneeded = () => request.result.createObjectStore('assets'); + const request = indexedDB.open('automap-studio', 2); + request.onupgradeneeded = () => { + const db = request.result; + if (!db.objectStoreNames.contains('assets')) db.createObjectStore('assets'); + const history = db.objectStoreNames.contains('history') + ? request.transaction.objectStore('history') : db.createObjectStore('history', {keyPath: 'id'}); + if (!history.indexNames.contains('createdAt')) history.createIndex('createdAt', 'createdAt'); + }; request.onsuccess = () => { const db = request.result; db.onversionchange = () => { db.close(); connection = null; }; @@ -52,5 +58,49 @@ window.StudioStorage = (() => { }); } - return {getAsset, setAsset}; + const HISTORY_RETENTION_MS = 24 * 60 * 60 * 1000; + + async function historyTransaction(action) { + const db = await open(); + return new Promise((resolve, reject) => { + const transaction = db.transaction('history', 'readwrite'); + const store = transaction.objectStore('history'); + let entries = []; + if (action?.clear) { + store.clear(); + } else { + const cutoff = Date.now() - HISTORY_RETENTION_MS; + const cursorRequest = store.index('createdAt').openCursor(IDBKeyRange.upperBound(cutoff, true)); + cursorRequest.onsuccess = () => { + const cursor = cursorRequest.result; + if (cursor) { + cursor.delete(); + cursor.continue(); + } else if (action?.entry) { + store.put(action.entry); + } else if (!action?.prune) { + const all = store.getAll(); + all.onsuccess = () => { + entries = all.result.filter(entry => { + const valid = Number.isFinite(entry.createdAt) && entry.createdAt >= cutoff && + entry.state && typeof entry.state === 'object' && !Array.isArray(entry.state); + if (!valid) store.delete(entry.id); + return valid; + }); + }; + } + }; + } + transaction.oncomplete = () => resolve(entries.sort((a, b) => b.createdAt - a.createdAt)); + transaction.onabort = () => reject(transaction.error || new Error('History could not be saved.')); + transaction.onerror = () => reject(transaction.error); + }); + } + + return {getAsset, setAsset, HISTORY_RETENTION_MS, + getHistory: () => historyTransaction(), + pruneHistory: () => historyTransaction({prune: true}), + addHistory: entry => historyTransaction({entry}), + clearHistory: () => historyTransaction({clear: true}), + }; })(); diff --git a/templates/index.html b/templates/index.html index 3039409..1a0ea1e 100644 --- a/templates/index.html +++ b/templates/index.html @@ -894,8 +894,9 @@

Ready to Synthesize
[Info] Select your desired flags -m, -mp, -gsm, -eop, --model, --font and click "Run CLI & Generate Map".

- +