Skip to content
Merged

Dev #12

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ htmlcov/
test-results/
playwright-report/
instance/

# Rust build artifacts
target/
automap_rs/target/
20 changes: 15 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from map.auto_map import (
auto_map,
_auto_fill_from_ai,
_format_short_number,
_parse_short_number,
_parse_filling_overrides,
_resolve_theme_name,
Expand Down Expand Up @@ -279,26 +280,35 @@ def _auto_map_with_ai_cache(*, refresh: bool, provider: str, api_key, **kwargs):
identity = str(session.get('web_identity', '') or '')
except RuntimeError:
identity = ''
cache_key = ai_cache.make_key(
ai_cache.scope(identity, api_key),
short_form = kwargs.get('optional_feature') == 'short_form'
scope = ai_cache.scope(identity, api_key)
base_fields = dict(
prompt=str(kwargs.get('prompt') or ''),
map_type=str(kwargs.get('map_type') or ''),
model=str(kwargs.get('model') or ''),
provider=provider,
temperature=temperature,
short_form=kwargs.get('optional_feature') == 'short_form',
web_search=bool(kwargs.get('web_search', False)),
max_search_requests=kwargs.get('max_search_requests', 5),
)
cache_key = ai_cache.make_key(scope, short_form=short_form, **base_fields)
cached = None if refresh else ai_cache.get(cache_key)
if cached is None and not refresh and short_form:
raw_cached = ai_cache.get(ai_cache.make_key(scope, short_form=False, **base_fields))
if raw_cached and any(_parse_short_number(v) is not None for v in raw_cached.values()):
cached = {k: _format_short_number(v) for k, v in raw_cached.items()}
if cached is not None:
kwargs['ai_answer_override'] = cached
buffer = auto_map(api_key=api_key, **kwargs)
if cached is None and buffer is not None:
if buffer is not None:
meta = getattr(buffer, 'map_meta', None)
answer = meta.get('ai_answer') if isinstance(meta, dict) else None
if isinstance(answer, dict):
ai_cache.put(cache_key, answer)
if short_form:
answer = {k: _format_short_number(v) for k, v in answer.items()}
meta['ai_answer'] = answer
if not refresh:
ai_cache.put(cache_key, answer)
return buffer, cached is not None


Expand Down
89 changes: 77 additions & 12 deletions map/auto_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,16 @@ def add_range(low, high, rgba):
return txt_out, num_out


_SHORT_UNITS: tuple[tuple[float, str], ...] = (
(1_000_000_000_000.0, "T"),
(1_000_000_000.0, "B"),
(1_000_000.0, "M"),
(1_000.0, "k"),
)


def _parse_short_number(val: object) -> float | None:
"""Parse plain numbers or short-form numbers like '10k', '2.5M', '1B'."""
"""Parse plain numbers or English short-form numbers like '10k', '30K', '4M', '10B', '13T'."""
if isinstance(val, bool) or val is None:
return None
s = str(val).replace(",", "").replace(" ", "").strip()
Expand All @@ -532,13 +540,52 @@ def _parse_short_number(val: object) -> float | None:
mult, s = 1_000_000.0, s[:-1]
elif low.endswith("b"):
mult, s = 1_000_000_000.0, s[:-1]
elif low.endswith("t"):
mult, s = 1_000_000_000_000.0, s[:-1]
try:
num = float(s) * mult
except ValueError:
return None
return num if math.isfinite(num) else None


def _format_number_value(num: float) -> str:
"""Format a finite float (>= 1,000 in magnitude) using compact English k/M/B/T suffixes."""
abs_num = abs(num)
for idx, (threshold, suffix) in enumerate(_SHORT_UNITS):
if abs_num >= threshold:
scaled = round(num / threshold, 1)
if abs(scaled) >= 1_000.0 and idx > 0:
prev_threshold, prev_suffix = _SHORT_UNITS[idx - 1]
scaled = round(num / prev_threshold, 1)
suffix = prev_suffix
if float(scaled).is_integer():
return f"{int(scaled)}{suffix}"
return f"{scaled:.1f}".rstrip("0").rstrip(".") + suffix
return str(int(num)) if float(num).is_integer() else str(round(num, 2))


def _format_short_number(val: object) -> object:
"""Abbreviate large numeric values (>= 1,000) into compact English k/M/B/T strings."""
if val is None or isinstance(val, bool):
return val
if isinstance(val, str):
clean = val.strip()
if clean and clean[-1].lower() in ("k", "m", "b", "t"):
try:
coeff = float(clean[:-1].replace(",", "").replace(" ", ""))
if math.isfinite(coeff) and 0 < abs(coeff) < 1_000.0:
return clean
except ValueError:
pass
parsed = _parse_short_number(val)
if parsed is None:
return val.strip() if isinstance(val, str) else val
if abs(parsed) >= 1_000.0:
return _format_number_value(parsed)
return val.strip() if isinstance(val, str) else val


def _auto_fill_from_ai(
ai_answer: dict[str, object],
mode: str,
Expand Down Expand Up @@ -698,10 +745,6 @@ def get_font(size):
# Most least and short form conflict solution
# @TODO Make most/least and short form compatible

if optional_feature == "short_form" and mode == "number":
showwarning("Warning", "Short form feature is only available for text mode. It will be disabled.")
optional_feature = None

if optional_feature == "show_most_least" and mode == "text":
showwarning("Warning", "Most/least feature may be incompatible with text mode. Turning off...")
optional_feature = None
Expand All @@ -711,7 +754,7 @@ def get_font(size):
optional_feature = None

if optional_feature == "short_form" and ai_answer_override is None:
prompt += " Write in short form, for example 1000=1k, 1000000=1M etc."
prompt += " Write in English short form only (k, M, B, T), for example 1000=1k, 1000000=1M, 1000000000=1B, 1000000000000=1T etc."

random_colors = {}

Expand Down Expand Up @@ -765,6 +808,10 @@ def get_font(size):
showerror("Error", "No information or bad info received from AI")
return

raw_ai_answer = dict(ai_answer)
if optional_feature == "short_form":
ai_answer = {k: _format_short_number(v) for k, v in ai_answer.items()}

log.success(f"Got information from AI ({len(ai_answer)} entries)")

resolved_theme = _resolve_theme_name(palette_theme, prompt=prompt, mode=mode)
Expand All @@ -783,7 +830,8 @@ def get_font(size):
if req_txt and (not theme_requested or req_keys_low != _DEFAULT_UI_TXT_KEYS):
ci_to_orig = {k.strip().lower(): k for k in merged_txt}
for rk, rv in req_txt.items():
target_k = ci_to_orig.get(rk.strip().lower(), rk)
short_rk = str(_format_short_number(rk)).strip() if optional_feature == "short_form" else rk
target_k = ci_to_orig.get(short_rk.lower(), ci_to_orig.get(rk.strip().lower(), short_rk))
merged_txt[target_k] = rv
filling_txt = merged_txt
filling_txt_ci = {str(k).strip().lower(): v for k, v in filling_txt.items()}
Expand All @@ -802,6 +850,12 @@ def get_font(size):
if not filling_num and auto_num:
filling_num = auto_num

if optional_feature == "short_form":
for k, v in list(filling_txt.items()):
short_k = str(_format_short_number(k)).strip().lower()
if short_k:
filling_txt_ci.setdefault(short_k, v)

most_least = None
extrema = _numeric_extrema(ai_answer) if optional_feature == "show_most_least" else None
if optional_feature == "show_most_least" and extrema is None:
Expand Down Expand Up @@ -842,13 +896,20 @@ def get_font(size):

if map_type == "world_countries":
ai_answer_norm = _world_countries_answer_norm(ai_answer or {}, dict_.keys())
raw_ai_answer_norm = _world_countries_answer_norm(raw_ai_answer or {}, dict_.keys())
else:
ai_answer_norm = {
str(k).strip().lower().replace(" ", "_").replace("-", "_"): v
for k, v in (ai_answer or {}).items()
}
raw_ai_answer_norm = {
str(k).strip().lower().replace(" ", "_").replace("-", "_"): v
for k, v in (raw_ai_answer or {}).items()
}
if "oceania" in ai_answer_norm and "australia" not in ai_answer_norm:
ai_answer_norm["australia"] = ai_answer_norm["oceania"]
if "oceania" in raw_ai_answer_norm and "australia" not in raw_ai_answer_norm:
raw_ai_answer_norm["australia"] = raw_ai_answer_norm["oceania"]

region_colors: dict[int, tuple[int, int, int, int]] = {}

Expand All @@ -864,18 +925,22 @@ def get_font(size):
info = ai_answer_norm[name_key]
color = (random.randint(80, 255), random.randint(80, 255), random.randint(80, 255), 255)
num_value = _parse_short_number(info)
raw_num_value = _parse_short_number(raw_ai_answer_norm.get(name_key))
if num_value is None:
log.warning(f"Non-numeric value for {name}: {info!r}; using random color")

if num_value is not None:
# Pick the colour of the first matching range. Ranges are
# treated as (low < value <= high) per the docs.
matched_bounds = None
for (low_lim, high_lim), color_tup in filling_num.items():
if low_lim < num_value <= high_lim:
color = color_tup
matched_bounds = (low_lim, high_lim)
break
for candidate in (num_value, raw_num_value):
if candidate is None or matched_bounds is not None:
continue
for (low_lim, high_lim), color_tup in filling_num.items():
if low_lim < candidate <= high_lim:
color = color_tup
matched_bounds = (low_lim, high_lim)
break
if matched_bounds is None and auto_style is True and auto_num:
for (low_lim, high_lim), color_tup in auto_num.items():
if low_lim < num_value <= high_lim:
Expand Down
3 changes: 0 additions & 3 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2118,9 +2118,6 @@ <h4 class="text-xs font-semibold text-white flex items-center gap-2">
} else if (state.extra_option === 'show_most_least' && state.map_type === 'world') {
hint.textContent = 'Note: show_most_least overlay is supported on default, flag, and world_countries maps.';
hint.className = 'text-[11px] text-amber-400/90 mt-1.5 block';
} else if (state.extra_option === 'short_form' && state.mode === 'number') {
hint.textContent = 'Note: short_form (1k/1M) works best in text mode (-m text).';
hint.className = 'text-[11px] text-amber-400/90 mt-1.5 block';
} else {
hint.className = 'hidden';
}
Expand Down
34 changes: 31 additions & 3 deletions tests/test_auto_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,38 @@ def test_numeric_ranges_use_exclusive_low_inclusive_high(scene, value, expected)
assert real_open(render(mode='number')).getpixel((80, 600)) == expected


def test_short_form_changes_ai_prompt(scene):
@pytest.mark.parametrize('mode', ['text', 'number'])
def test_short_form_changes_ai_prompt_and_abbreviates_large_numbers(scene, monkeypatch, mode):
answer, _, _ = scene
render(optional_feature='short_form')
answer.return_value = {'one': 4_000_000, 'two': '30,000', 'three': '10B'}
draw_text = Mock()
monkeypatch.setattr(ImageDraw.ImageDraw, 'text', draw_text)
buf = render(mode=mode, optional_feature='short_form', auto_style=True)
assert '1000000=1M' in answer.call_args.args[0]
assert [call.args[1] for call in draw_text.call_args_list] == ['4M', '30k', '10B']
assert buf.map_meta['ai_answer'] == {'one': '4M', 'two': '30k', 'three': '10B'}


@pytest.mark.parametrize('raw,expected', [
(999, 999),
(1_000, '1k'),
(30_000, '30k'),
('30K', '30K'),
(999_950, '1M'),
(4_000_000, '4M'),
('38,450,000', '38.5M'),
('10B', '10B'),
('10b', '10b'),
('1,500,000,000', '1.5B'),
(13_000_000_000_000, '13T'),
('13T', '13T'),
('6 Dec', '6 Dec'),
('Yes', 'Yes'),
(None, None),
(True, True),
])
def test_format_short_number_handles_plain_and_short_values(raw, expected):
assert renderer._format_short_number(raw) == expected


def test_fractional_measurements_use_actual_range_at_integer_boundary(scene):
Expand Down Expand Up @@ -144,7 +172,7 @@ def test_fractional_measurements_get_distinct_automatic_colors(scene):


@pytest.mark.parametrize('mode,map_type,feature', [
('number', 'default', 'short_form'), ('text', 'default', 'show_most_least'),
('text', 'default', 'show_most_least'),
('number', 'world', 'show_most_least'),
])
def test_incompatible_options_are_disabled(scene, monkeypatch, mode, map_type, feature):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_web_ai_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ def test_search_settings_bypass_cache_but_visual_changes_reuse_it(monkeypatch):
assert render.call_args_list[3].kwargs['ai_answer_override'] == {'france': 1}


def test_toggling_short_form_abbreviates_cached_large_numbers_on_regenerate(monkeypatch):
calls = []

def render(**kwargs):
calls.append(kwargs)
ans = kwargs.get('ai_answer_override') or {'ukraine': 38_000_000, 'germany': 84_500_000}
return _buffer(ans)

monkeypatch.setattr(web, 'auto_map', render)
client = web.app.test_client()
first = client.post('/api/generate', json=_body(mode='number', extra_option='none')).get_json()
second = client.post('/api/generate', json=_body(mode='number', extra_option='short_form')).get_json()
assert first['map_values'] == {'ukraine': 38_000_000, 'germany': 84_500_000}
assert second['ai_cached'] is True
assert second['map_values'] == {'ukraine': '38M', 'germany': '84.5M'}
assert calls[1]['ai_answer_override'] == {'ukraine': '38M', 'germany': '84.5M'}


def test_render_video_accepts_binary_multipart(monkeypatch):
upstream = Mock(status_code=200)
upstream.json.return_value = {'status': 'success'}
Expand Down
10 changes: 6 additions & 4 deletions yt-dlp_serv.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,10 @@ def _decode_optional_image(field: str, label: str):
branded = bool(watermark_image is not None or watermark_text or has_stars)
map_bottom = 1450 if watermark_image is not None else (1664 if branded else 1760)
map_area_height = max(240, map_bottom - map_top)
# Fit inside the map area while ensuring the map always spans the full Shorts width.
scale = max(canvas_w / map_clip.w, min(1040 / map_clip.w, map_area_height / map_clip.h) * 1.14)
# Fit inside the map area while ensuring non-portrait maps span the full Shorts width.
scale = min(1040 / map_clip.w, map_area_height / map_clip.h) * 1.14
if map_clip.w >= map_clip.h:
scale = max(canvas_w / map_clip.w, scale)
map_clip = map_clip.resized(scale)
map_y = map_top + (map_area_height - map_clip.h) / 2
if effects:
Expand Down Expand Up @@ -808,7 +810,7 @@ def _veil_mask(t: float) -> np.ndarray:
# Prefer the native Rust compositor when available; fall back to MoviePy locally.
used_rust = False
# ======== RUST ENGINE INJECTION =============
try:
try: # pragma: no cover
import automap_rs

def _to_rgba_bytes(arr: np.ndarray) -> bytes:
Expand Down Expand Up @@ -863,7 +865,7 @@ def _to_rgba_bytes(arr: np.ndarray) -> bytes:
except ImportError:
pass

if not used_rust:
if not used_rust: # pragma: no branch
final_clip = CompositeVideoClip(clips, size=(canvas_w, canvas_h))
media_resources.append(final_clip)
if has_audio:
Expand Down
Loading