Predictingthepast
Google DeepMindOfficialPublished by Google DeepMind
- Skill
- Tool integration
- Science and research
- Apache-2.0
- Low risk
- Has scripts
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
Predicting The Past provides an interface for the Aeneas and Ithaca neural network models to analyze ancient epigraphic documents. It supports Latin and Ancient Greek texts, enabling users to restore damaged inscriptions, determine their geographical and chronological origins, retrieve parallel texts, and generate embedding vectors. The skill includes preprocessing utilities for text cleaning and visualization tools to generate interactive HTML dashboards for research results.
What it does
- Restores missing or damaged characters in ancient inscriptions
- Determines the geographical and chronological origin of texts
- Retrieves parallel inscriptions for contextual analysis
- Generates text embedding vectors for ancient language data
- Cleans and normalizes Latin and Ancient Greek input text
- Visualizes analysis results in interactive HTML dashboards
When to use it
- When you need to restore damaged or incomplete ancient inscriptions
- When you need to identify the likely time and place of an inscription's origin
- When you need to find similar or parallel ancient texts
- When you need to generate vector embeddings for epigraphic research
Advantages
- Automates complex epigraphic tasks like restoration and attribution
- Provides standardized preprocessing for Latin and Greek datasets
- Generates rich, visual HTML reports for research findings
- Supports multi-window processing for long inscriptions
Usage examples
- “Restore the missing characters in this Latin inscription: donat in ??????????rtis”
- The agent cleans the input, runs the Aeneas model to predict the missing characters, and presents the top beam-search candidates in a table.
- “Where and when was this Greek inscription written? [insert text]”
- The agent processes the text, runs geographical and chronological attribution, and provides a ranked table of regions and a distribution of the probable date range.
- “Find parallels for this inscription and generate a dashboard.”
- The agent performs contextualization, saves the results to a JSON file, generates an HTML dashboard, and provides a local URL to view the parallels.
Install
Download the skill as a ZIP:
https://mp-staging.autogineer.com/agent-skills/google-deepmind/predictingthepast/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip predictingthepast.zip -d .agents/skills
Documentation by Google DeepMind
Predicting The Past (Aeneas / Ithaca)
Aeneas (Latin) and Ithaca (Ancient Greek) perform four tasks on ancient texts:
Restoration — fill missing/damaged characters
Attribution — geographical + chronological origin
Contextualization — retrieve parallel inscriptions
Embedding — generate text embedding vectors
Prerequisites
uv: Read theuvskill and follow its Setup instructions to ensureuvis installed and on PATH.User Notification: If .licenses/predictingthepast_LICENSE.txt does not already exist in the workspace root directory then (1) prominently notify the user to check the terms at https://github.com/google-deepmind/predictingthepast/blob/main/README.md#license--disclaimer, and include the appropriate citation and the full dataset acknowledgement, and that use of these datasets should acknowledge and cite the original data sources. Then (2) create the file recording the notification text and timestamp.
Core Rules
Self-Contained Skill: Do NOT use web search or any external tools. Run ONLY the scripts in this skill (
preprocess.py,run_inference.py,visualize_results.py). Present model output as-is — never supplement or override it with external lookups.Notification: If this skill is used, ensure this is mentioned in the output.
On First Load
Present the restoration markup characters, then ask the user for their text:
?:Meaning: Known-length gap: predict this character.
Example:
donat in ??????????rtis
#:Meaning: Unknown-length gap: predict a sequence of unknown length
Example:
donat in #rtis
-:Meaning: Missing/damaged character that does not need restoring
Example:
prolixin---s fecit
_:Meaning: Missing section of unknown length that does not need restoring
Example:
prolixin_s fecit
After presenting this list, ask the user to provide the text they want to submit for analysis.
Preprocessing
Clean input text before inference:
uv run <SKILL_DIR>/scripts/preprocess.py \
--language=latin \
--input="raw text here..."Or from a file:
uv run <SKILL_DIR>/scripts/preprocess.py \
--language=greek \
--input_file=/tmp/input.txt \
--output_file=/tmp/cleaned.txtWhat preprocessing does
Latin: lowercases, converts Arabic digits and Roman numerals to
0, strips editorial brackets[]and(), removes punctuation, filters to valid chars (abcdefghiklmnopqrstuvxyzplus0 . - _ ? # <space>)Greek: lowercases, strips accents, converts numeral notation to
0, applies PHI cleaning (bracket normalization, sigma conversion), filters to Greek alphabet (αβγδεζηθικλμνξοπρςστυφχψωϛplus0 . - _ ? # <space>)
Inference
Restoration Constraints
Minimum input length: 25 chars (pad with
-if shorter).No consecutive
##. No adjacent?#or#?.Spaces inside
?sequences count toward total.If the user's text contains
#, ask how many characters to restore and set--restore_max_lenaccordingly.If the user tries to restore multiple parts of the text at once, suggest to restore texts section by section. Suggest to focus on one damaged region per query — this is faster, produces higher-quality predictions.
Pre-Flight Checks
Confirm with the user before proceeding if either applies:
Restoration complexity — if input contains more than 10
?characters, or uses#with--restore_max_len > 10, warn: "This restoration involves N characters which will take approximately M minutes (restoration time scales roughly linearly ~10 s per additional?on a high-end CPU machine: 5 → ~1 min, 10 → ~2.5 min, 20 → ~5 min, 30 → ~8 min). Do you want to proceed, or simplify the query first (e.g. fewer?marks, shorter--restore_max_len, or restoring section by section)?"Multi-window splitting — if the input text exceeds 750 characters and will be split into multiple windows, warn: "This text is N characters long and will be split into W overlapping windows, each run independently. This will be significantly slower. Do you want to proceed, or shorten the input?"
These factors compound: a complex restoration across multiple windows will be substantially slower than either factor alone.
Task Selection
Each task is controlled by its own flag. At least one must be provided:
--attribute— geographical + chronological attribution--restore— text restoration (requires?or#in input)--contextualize— parallel inscription retrieval
Any combination is valid. All three can be used together.
When --embedding is provided, a text embedding vector is also generated
alongside the other tasks.
Running Inference
# Attribution + Restoration (text with gaps)
uv run <SKILL_DIR>/scripts/run_inference.py \
--language=latin \
--input="cleaned text with ???" \
--attribute --restore \
--output_json=/tmp/results.json
# Attribution + Contextualization (no gaps)
uv run <SKILL_DIR>/scripts/run_inference.py \
--language=latin \
--input="cleaned text" \
--attribute --contextualize \
--output_json=/tmp/results.json
# All tasks
uv run <SKILL_DIR>/scripts/run_inference.py \
--language=latin \
--input="cleaned text with ???" \
--attribute --restore --contextualize \
--output_json=/tmp/results.json
# Generate dashboard
uv run \
<SKILL_DIR>/scripts/visualize_results.py \
--input=/tmp/results.json \
--output=/tmp/dashboard.htmlOutputs are always returned to the console (JSON/Summary) and must always be written to an HTML dashboard for rich visualization.
Serving HTML artifacts: After generating any HTML file (the dashboard or
any other HTML artifact you create), start a background HTTP server so the user
can view it in their browser. Run the server in the foreground of the background
task process (without trailing &) to prevent the process from being
terminated. Use a random high port to avoid conflicts.
In the example below, replace <HTML_FILE> with the absolute path of the HTML
file you produced (e.g. /tmp/dashboard.html or C:\tmp\dashboard.html).
For Linux/macOS:
# Pick an unused port and start the server as a persistent process
HTML_FILE=/tmp/dashboard.html # ← set to the actual HTML file path
PORT=$(export PATH="$HOME/.local/bin:$PATH" && uv run python -c "import socket; s=socket.socket(); s.bind(('',0)); print(s.getsockname()[1]); s.close()")
FILENAME=$(basename "$HTML_FILE")
echo "View URL: http://localhost:${PORT}/${FILENAME}"
export PATH="$HOME/.local/bin:$PATH" && uv run python -m http.server "$PORT" --bind 0.0.0.0 --directory "$(dirname "$HTML_FILE")"For Windows (PowerShell):
# Pick an unused port and start the server as a persistent process
$HTML_FILE = "C:\tmp\dashboard.html" # ← set to the actual HTML file path
$PORT = & "$HOME\.local\bin\uv" run python -c "import socket; s=socket.socket(); s.bind(('',0)); print(s.getsockname()[1]); s.close()"
$FILENAME = Split-Path $HTML_FILE -Leaf
$DIRNAME = Split-Path $HTML_FILE -Parent
Write-Host "View URL: http://localhost:${PORT}/${FILENAME}"
& "$HOME\.local\bin\uv" run python -m http.server $PORT --bind 0.0.0.0 --directory $DIRNAMEThen provide http://localhost:<port>/<filename> (or
http://127.0.0.1:<port>/<filename>) as the primary clickable link in your
response. Additionally, provide http://<hostname>:<port>/<filename> as a
fallback link for remote setups. Do not give the user a raw file:// path.
Flags
--input: Text string (mutually exclusive with--input_file)--input_file: Path to UTF-8 text file--language:latinorgreek--models_dir: Model files directory (default:$HOME/.predictingthepast/models)--attribute: Run geographical and chronological attribution--restore: Run text restoration (requires?or#in input)--contextualize: Run parallel inscription retrieval--contextualize_top_k: Number of top parallels to return (default: 10)--contextualize_exclude_test_valid: Exclude test/validation texts from contextualization results. Filters on the internal numericid(whereid % 10is 3 or 4).--embedding: Generate a text embedding vector (default: off). When set, the output includes anembeddingfield with the full-precision float vector.--output_json: Save combined JSON to file (forvisualize_results.py)--restore_beam_width: Number of candidate restorations to explore in beam search. Higher = more thorough but slower (default: 100)--restore_max_len: Maximum total characters to restore across all?and#gaps (default: 15)--restore_temperature: Sampling temperature. Lower = conservative, higher = creative (default: 1.0)--window_overlap: Overlap fraction for long-text windowing (default: 0.33)
Long Texts (>750 characters)
Each window must be between 25 and 750 characters. If the input text exceeds 750 characters, it is automatically split into overlapping windows (default 33% overlap). Each window is run through the model independently, and the geographical and chronological attribution results are averaged across all windows. Restoration and contextualization are run per-window and concatenated.
Output Format
Use --output_json to save the combined JSON to a file. For the full
--output_json format including all fields for attribution, restoration, and
contextualization, see output_format.md.
Presenting Results
Present results for every task requested by the user. Always generate the
HTML dashboard, serve it via the background HTTP server described above, and
provide the http:// URL as a clickable link. Format years as BCE/CE.
Restoration
Bold the restored characters in the top prediction. Show top-10 beam-search candidates in a numbered table (rank, restored text, score). Summarise the most salient words (not characters) — do not dump the raw saliency array.
Geographical Attribution
Show the top 10 regions in a ranked table (rank, region, score). Highlight the top-1 prediction in the narrative. Summarise the most salient words (not characters) — do not dump the raw saliency array.
Chronological Attribution
Report top year and weighted average year. Describe the distribution shape (peak decade, probable range) — do not list all 160 bins. Summarise the most salient words (not characters) — do not dump the raw saliency array.
Contextualization (Parallels)
Use jp or write your own code to extract the contextualization data from
--output_json. Show the top parallels in a table (rank, ID, Trismegistos ID,
region, date range, score, partner link). Quote the full text of the top
parallels from the JSON.
If the user requests a specific number of parallels, use --contextualize_top_k
to control how many are returned.
If the user asks to exclude test/validation texts, add
--contextualize_exclude_test_valid. This filters on the internal numeric
id (where id % 10 is 3 for test or 4 for validation).
Embedding (when --embedding is set)
State the vector dimensionality (384 floats).
References
For more information, see: predictingthepast.com
IMPORTANT: The first time you return inference results to the user, remind them to cite the appropriate paper below (Ithaca for Greek, Aeneas for Latin), and the dataset acknowledgements below.
Ithaca (Ancient Greek): > Assael, Y., Sommerschield, T., Shillingford, B. et al. Restoring and attributing ancient texts using deep neural networks. Nature 603, 280–283 (2022).
Aeneas (Latin): > Assael, Y., Sommerschield, T., Cooley, A. et al. Contextualizing ancient texts with generative neural networks. Nature 645, 141–147 (2025).
Dataset Acknowledgements
IMPORTANT: The first time you present inference results to the user, include the appropriate citation and the full dataset acknowledgement for the language used (see below).
The Ithaca and Aeneas projects rely on the availability of high-quality datasets of ancient Greek and Latin inscriptions, respectively. These datasets were built through centuries of scholarly collection and decades of digital editorial work.
Ithaca (Ancient Greek): In particular, Ithaca draws on a 2019 version of the Searchable Greek Inscriptions database made available under "Fair Use" license by the Packard Humanities Institute, generously supported by David Packard: inscriptions.packhum.org.
Aeneas (Latin): Aeneas was trained on data from:
Epigraphic Database Roma (EDR): Made available pursuant to a Creative Commons Attribution 4.0 International License (CC-BY) on Zenodo. EDR is also available at edr-edr.it.
Epigraphic Database Heidelberg (EDH): Made available pursuant to a Creative Commons Attribution-ShareAlike 4.0 International License (CC-BY-SA) on Zenodo. EDH is also available at edh.ub.uni-heidelberg.de.
ETL repository for Epigraphic Database Clauss Slaby (EDCS_ETL): Made available pursuant to a Creative Commons Attribution 4.0 International License (CC-BY) on Zenodo. EDCS_ETL is also available at manfredclauss.de and github.com/sdam-au/EDCS_ETL.
These resources bring together a large proportion of published inscriptions in a searchable digital format.
Files
- ATTRIBUTION.mdAdded by AutoGineer916 B
- LICENSELicence file added by AutoGineer11.1 KiB
- SKILL.md14.3 KiB
- references/iphi-region-sub-loc.json6.2 KiB
- references/iphi-region-sub.txt1.1 KiB
- references/led-proper-names.txt330.0 KiB
- references/led-region-sub-loc.json4.5 KiB
- references/led-region-sub.txt735 B
- references/output_format.md5.6 KiB
- scripts/preprocess.pyScript8.0 KiB
- scripts/run_inference.pyScript26.9 KiB
- scripts/visualize_results.pyScript23.3 KiB
scripts/run_inference.py
# Copyright 2026 Google LLC
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
"""Run Predicting The Past inference on preprocessed ancient text.
Supports two models: Aeneas (Latin) and Ithaca (Ancient Greek).
Performs up to three tasks per input: geographical and chronological
attribution, text restoration (if gaps marked with ? or # are present),
and contextualization via parallel inscription retrieval. Optionally
generates a text embedding vector.
For texts exceeding 750 characters, the input is split into overlapping
windows and attribution scores are averaged across all windows.
"""
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "jax",
# "predictingthepast",
# ]
# ///
import argparse
import functools
import json
import os
import pickle
import ssl
import sys
import tempfile
from typing import Any
import urllib.error
import urllib.request
import jax
from predictingthepast.eval import inference
from predictingthepast.models import model as model_lib
from predictingthepast.util import alphabet as util_alphabet
# Per-language model checkpoint, dataset, and retrieval-embedding filenames.
_LATIN_FILES: dict[str, str] = {
'checkpoint': 'aeneas_117149994_2.pkl',
'dataset': 'led.json',
'retrieval': 'led_emb_xid117149994.pkl',
}
_GREEK_FILES: dict[str, str] = {
'checkpoint': 'ithaca_153143996_2.pkl',
'dataset': 'iphi.json',
'retrieval': 'iphi_emb_xid153143996.pkl',
}
_VALID_MODEL_FILENAMES: frozenset[str] = frozenset(
_LATIN_FILES.values()
) | frozenset(_GREEK_FILES.values())
# Download configuration:
# Chunks are kept well below 32 MiB proxy limits (e.g. Harpoon/Trawler).
_DOWNLOAD_CHUNK_SIZE: int = 16 * 1024 * 1024 # 16 MiB
_DOWNLOAD_TIMEOUT_SECONDS: int = 120 # ~136 kB/s floor before timeout
_DOWNLOAD_PROBE_TIMEOUT_SECONDS: int = 60
# Region-name lookup files mapping numeric location IDs to human-readable names.
# These files are bundled in the skill's references/ directory.
_LATIN_REGION_FILE: str = 'led-region-sub.txt'
_GREEK_REGION_FILE: str = 'iphi-region-sub.txt'
# Path to the skill's references/ directory, resolved relative to this script.
_REFERENCES_DIR: str = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'references'
)
# Date-bin boundaries: years are discretised into 10-year bins from 800 BCE
# to 800 CE, giving 160 bins total.
_DATE_MIN: int = -800
_DATE_MAX: int = 800
_DATE_INTERVAL: int = 10
_MODELS_DIR: str = os.path.join(
os.path.expanduser('~'), '.predictingthepast', 'models'
)
GCS_BASE: str = 'https://storage.googleapis.com/ithaca-resources/models'
# Windowing constraints: texts longer than _MAX_LEN characters are split into
# overlapping windows. The final window is discarded if shorter than _MIN_LEN.
_MAX_LEN: int = 750
_MIN_LEN: int = 25
@functools.cache
def _load_region_names(language: str) -> list[str]:
"""Loads region names from a line-delimited text file.
Args:
language: 'latin' or 'greek', selects the correct region file.
Returns:
Ordered list of region name strings, indexed by location_id.
"""
filename = _LATIN_REGION_FILE if language == 'latin' else _GREEK_REGION_FILE
path = os.path.join(_REFERENCES_DIR, filename)
if not os.path.isfile(path):
print(f'WARNING: Region file not found: {path}', file=sys.stderr)
return []
with open(path, 'r', encoding='utf-8') as f:
return [line.strip() for line in f if line.strip()]
def _year_bin_to_year(bin_idx: int) -> int:
"""Converts a year-bin index to the midpoint calendar year of that bin."""
return _DATE_MIN + bin_idx * _DATE_INTERVAL + _DATE_INTERVAL // 2
def _load_checkpoint(
path: str, language: str
) -> tuple[dict[str, Any], Any, Any, Any, Any]:
"""Loads a model checkpoint from a pickle file.
Args:
path: Absolute path to the .pkl checkpoint.
language: 'latin' or 'greek', determines which alphabet to instantiate.
Returns:
A tuple of (model_config, region_map, alphabet, params, forward_fn).
"""
with open(path, 'rb') as f:
checkpoint = pickle.load(f)
params = jax.device_put(checkpoint['params'])
model = model_lib.Model(**checkpoint['model_config'])
forward = model.apply
region_map = checkpoint['region_map']
if language == 'latin':
alphabet = util_alphabet.LatinAlphabet()
elif language == 'greek':
alphabet = util_alphabet.GreekAlphabet()
else:
raise ValueError(f'Unknown language: {language}')
return checkpoint['model_config'], region_map, alphabet, params, forward
def _enrich_attribution(
attr: Any | None,
region_names: list[str],
) -> dict[str, Any] | None:
"""Enriches attribution result with years and region names.
Calculates the continuous calendar year from date bins, maps region IDs to
human-readable strings, and adds helper fields for summaries.
Args:
attr: The raw attribution result.
region_names: List of region names indexed by location_id.
Returns:
Enriched attribution dictionary, or None if input is None.
"""
if not attr:
return None
if hasattr(attr, 'dict'):
attr_dict = attr.dict()
elif hasattr(attr, 'json'):
attr_dict = json.loads(attr.json())
else:
attr_dict = attr
if 'year_scores' in attr_dict:
scores = attr_dict['year_scores']
years = [_year_bin_to_year(i) for i in range(len(scores))]
attr_dict['year_distribution'] = {str(y): s for y, s in zip(years, scores)}
if scores:
top_idx = max(range(len(scores)), key=lambda i: scores[i])
attr_dict['top_year'] = years[top_idx]
total = sum(scores)
if total > 0:
attr_dict['weighted_average_year'] = round(
sum(y * s for y, s in zip(years, scores)) / total
)
if region_names and 'locations' in attr_dict:
for loc in attr_dict['locations']:
loc_id = loc.get('location_id', -1)
if 0 <= loc_id < len(region_names):
loc['location_name'] = region_names[loc_id]
return attr_dict
def _enrich_contextualization(
ctx: Any | None,
region_names: list[str],
) -> dict[str, Any] | None:
"""Enriches contextualization result with region names.
Maps parallel arrays of location IDs to human-readable region names.
Args:
ctx: The raw contextualization result.
region_names: List of region names indexed by location_id.
Returns:
Enriched contextualization dictionary, or None if input is None.
"""
if not ctx:
return None
if hasattr(ctx, 'dict'):
ctx_dict = ctx.dict()
elif hasattr(ctx, 'json'):
ctx_dict = json.loads(ctx.json())
else:
ctx_dict = ctx
if region_names and 'location_ids' in ctx_dict:
ctx_dict['location_names'] = [
region_names[lid]
if lid is not None and 0 <= lid < len(region_names)
else None
for lid in ctx_dict['location_ids']
]
return ctx_dict
def split_into_windows(
text: str,
max_len: int = _MAX_LEN,
overlap: float = 0.33,
) -> list[str]:
"""Splits text into overlapping windows.
If text exceeds max_len, it is divided into chunks. An overlap ensures
context is preserved across boundaries. if the final window is too small,
it expands backwards to maintain length.
Args:
text: Input text string.
max_len: Maximum length of each window.
overlap: Overlap fraction between windows.
Returns:
List of text windows.
"""
if len(text) <= max_len:
return [text]
step = int(max_len * (1 - overlap))
windows = []
for start in range(0, len(text), step):
window = text[start : start + max_len]
if len(window) < _MIN_LEN:
if windows:
new_start = max(0, len(text) - max_len)
windows[-1] = text[new_start:]
break
windows.append(window)
if start + max_len >= len(text):
break
return windows
def average_attributions(
attr_list: list[dict[str, Any] | None],
) -> dict[str, Any] | None:
"""Averages attribution scores across windows.
Takes a list of attribution dictionaries (one per window) and returns a
single dictionary with numerical scores (like date_scores and region_scores)
averaged together.
Args:
attr_list: List of attribution dictionaries to average.
Returns:
A dictionary with averaged scores, or None if input list is empty.
"""
if not attr_list or not any(attr_list):
return None
valid = [a for a in attr_list if a is not None]
if len(valid) == 1:
return valid[0]
averaged = json.loads(json.dumps(valid[0]))
if 'date_scores' in averaged:
n = len(valid)
scores = averaged['date_scores']
for attr in valid[1:]:
if 'date_scores' in attr:
for i, score in enumerate(attr['date_scores']):
if i < len(scores):
scores[i] = scores[i] + score
averaged['date_scores'] = [s / n for s in scores]
if 'region_scores' in averaged:
n = len(valid)
scores = averaged['region_scores']
for attr in valid[1:]:
if 'region_scores' in attr:
for i, score in enumerate(attr['region_scores']):
if i < len(scores):
scores[i] = scores[i] + score
averaged['region_scores'] = [s / n for s in scores]
return averaged
def _download_model_file(url: str, dest_path: str):
"""Downloads a model file from a URL to a local path using 16 MiB Range chunks."""
print(f'[*] Downloading {url} to {dest_path}...', file=sys.stderr)
cafile = os.environ.get('SSL_CERT_FILE') or os.environ.get(
'REQUESTS_CA_BUNDLE'
)
ssl_ctx = ssl.create_default_context(cafile=cafile) if cafile else None
dest_dir = os.path.dirname(dest_path)
# Make sure the folder exists before trying to save the file
os.makedirs(dest_dir, exist_ok=True)
tmp_path = None
try:
# Open the connection to the URL to probe total file size
probe_req = urllib.request.Request(url, headers={'Range': 'bytes=0-0'})
with urllib.request.urlopen(
probe_req, timeout=_DOWNLOAD_PROBE_TIMEOUT_SECONDS, context=ssl_ctx
) as resp:
content_range = resp.headers.get('Content-Range', '')
if '/' in content_range:
total_size = int(content_range.split('/')[-1])
else:
total_size = int(resp.headers.get('Content-Length', 0))
# Use a named temporary file in the destination directory to ensure
# atomic replacement on the same filesystem.
with tempfile.NamedTemporaryFile(
dir=dest_dir,
prefix=f'{os.path.basename(dest_path)}.',
suffix='.tmp',
delete=False,
) as tmp_file:
tmp_path = tmp_file.name
# Open the local file and save the downloaded data in chunks
start = 0
if total_size > 0:
with open(tmp_path, 'wb') as f:
while start < total_size:
end = min(start + _DOWNLOAD_CHUNK_SIZE - 1, total_size - 1)
req = urllib.request.Request(
url, headers={'Range': f'bytes={start}-{end}'}
)
with urllib.request.urlopen(
req, timeout=_DOWNLOAD_TIMEOUT_SECONDS, context=ssl_ctx
) as resp:
data = resp.read()
if not data: # Stop when there is no more data
break
f.write(data)
start += len(data)
else:
with urllib.request.urlopen(
url, timeout=_DOWNLOAD_TIMEOUT_SECONDS, context=ssl_ctx
) as resp:
with open(tmp_path, 'wb') as f:
while True:
chunk = resp.read(8192)
if not chunk: # Stop when there is no more data
break
f.write(chunk)
start += len(chunk)
if total_size and start != total_size:
raise OSError(
f'Incomplete download for {url}: received {start} bytes, expected'
f' {total_size} bytes'
)
os.replace(tmp_path, dest_path)
tmp_path = None
print(
f'[*] Successfully downloaded to {dest_path} ({start} bytes)',
file=sys.stderr,
)
except Exception as e:
# Clean up the temporary file on error.
if tmp_path and os.path.exists(tmp_path):
os.remove(tmp_path)
raise RuntimeError(
f'Failed to download model file from {url} to {dest_path}: {e}'
) from e
def _load_or_redownload(path: str, url: str, loader_fn: Any) -> Any:
"""Loads a file with loader_fn, re-downloading if truncated or corrupted."""
try:
return loader_fn(path)
except (pickle.UnpicklingError, EOFError, json.JSONDecodeError) as e:
filename = os.path.basename(path)
if filename not in _VALID_MODEL_FILENAMES:
raise RuntimeError(
f'Refusing to delete unrecognized file {path}: not in'
f' _VALID_MODEL_FILENAMES ({_VALID_MODEL_FILENAMES})'
) from e
print(
f'WARNING: Corrupted or truncated file at {path} ({e});'
' re-downloading...',
file=sys.stderr,
)
if os.path.exists(path):
os.remove(path)
_download_model_file(url, path)
return loader_fn(path)
def _load_resources(
args: argparse.Namespace,
) -> tuple[Any, Any, Any, Any, Any, Any, Any]:
"""Loads the model checkpoint, dataset, and retrieval embeddings.
Validates that all required files exist under args.models_dir before loading.
Args:
args: Parsed command-line arguments.
Returns:
A tuple of (model_config, region_map, alphabet, params, forward_fn,
dataset, retrieval).
"""
files = _LATIN_FILES if args.language == 'latin' else _GREEK_FILES
for _, filename in files.items():
path = os.path.join(args.models_dir, filename)
if not os.path.isfile(path):
url = f'{GCS_BASE}/{filename}'
_download_model_file(url, path)
checkpoint_path = os.path.join(args.models_dir, files['checkpoint'])
print(f'Loading checkpoint from {checkpoint_path}...', file=sys.stderr)
model_config, region_map, alphabet, params, forward = _load_or_redownload(
checkpoint_path,
f"{GCS_BASE}/{files['checkpoint']}",
lambda p: _load_checkpoint(p, args.language),
)
dataset_path = os.path.join(args.models_dir, files['dataset'])
retrieval_path = os.path.join(args.models_dir, files['retrieval'])
print(f'Loading dataset from {dataset_path}...', file=sys.stderr)
dataset = _load_or_redownload(
dataset_path,
f"{GCS_BASE}/{files['dataset']}",
inference.load_dataset,
)
print(f'Loading retrieval from {retrieval_path}...', file=sys.stderr)
retrieval = _load_or_redownload(
retrieval_path,
f"{GCS_BASE}/{files['retrieval']}",
inference.load_retrieval,
)
return model_config, region_map, alphabet, params, forward, dataset, retrieval
def _process_windows(
*,
windows: list[str],
args: argparse.Namespace,
forward: Any,
params: Any,
alphabet: Any,
vocab_char_size: int,
dataset: Any,
retrieval: Any,
region_map: Any,
) -> tuple[
list[dict[str, Any] | None],
list[dict[str, Any] | None],
list[dict[str, Any] | None],
]:
"""Runs attribution, restoration, and contextualization on each window.
Each window is processed independently. Only the tasks selected via
--attribute, --restore, and --contextualize flags are executed.
Args:
windows: List of text windows produced by split_into_windows().
args: Parsed command-line arguments.
forward: JAX model forward function.
params: JAX model parameters.
alphabet: Language-specific alphabet encoder.
vocab_char_size: Character vocabulary size from model_config.
dataset: Loaded dataset for contextualization.
retrieval: Loaded retrieval embeddings for contextualization.
region_map: Region ID mapping from the checkpoint.
Returns:
Three parallel lists of per-window results for attribution, restoration,
and contextualization. Entries are None on failure or when skipped.
"""
all_attr = []
all_restore = []
all_ctx = []
include_test = not args.contextualize_exclude_test_valid
for i, window in enumerate(windows):
print(f'Processing window {i+1}/{len(windows)}...', file=sys.stderr)
# 1. Attribution
if args.attribute:
try:
attr = inference.attribute(
window,
forward=forward,
params=params,
alphabet=alphabet,
vocab_char_size=vocab_char_size,
)
attr_dict = _enrich_attribution(attr, [])
if attr_dict:
attr_dict['input_text'] = window
all_attr.append(attr_dict)
except Exception as e:
print(f'Attribution failed for window {i+1}: {e}', file=sys.stderr)
all_attr.append(None)
else: # Attribution not requested.
all_attr.append(None)
# 2. Restoration
if args.restore and ('?' in window or '#' in window):
try:
restore = inference.restore(
window,
forward=forward,
params=params,
alphabet=alphabet,
vocab_char_size=vocab_char_size,
beam_width=args.restore_beam_width,
temperature=args.restore_temperature,
unk_restoration_max_len=args.restore_max_len,
)
if hasattr(restore, 'dict'):
restore_dict = restore.dict()
elif hasattr(restore, 'json'):
restore_dict = json.loads(restore.json())
else:
restore_dict = restore
if restore_dict:
restore_dict['input_text'] = window
all_restore.append(restore_dict)
except Exception as e:
print(f'Restoration failed for window {i+1}: {e}', file=sys.stderr)
all_restore.append(None)
else:
all_restore.append(None)
# 3. Contextualization
if args.contextualize:
try:
ctx = inference.contextualize(
text=window,
dataset=dataset,
retrieval=retrieval,
forward=forward,
params=params,
alphabet=alphabet,
region_map=region_map,
include_test=include_test,
top_k=args.contextualize_top_k,
)
if hasattr(ctx, 'dict'):
ctx_dict = ctx.dict()
elif hasattr(ctx, 'json'):
ctx_dict = json.loads(ctx.json())
else:
ctx_dict = ctx
all_ctx.append(ctx_dict)
except Exception as e:
print(
f'Contextualization failed for window {i+1}: {e}', file=sys.stderr
)
all_ctx.append(None)
else: # Contextualization not requested.
all_ctx.append(None)
return all_attr, all_restore, all_ctx
def _handle_output(
*,
output: dict[str, Any],
args: argparse.Namespace,
final_attr: dict[str, Any] | None,
final_restore: list[dict[str, Any] | None] | None,
final_ctx: list[dict[str, Any] | None],
embedding: list[float] | None = None,
) -> None:
"""Prints a human-readable inference summary and writes the output JSON.
Attribution and restoration summaries are printed to stderr. Contextualization
results (including full texts) are only written to the output JSON file —
use jq or a script to extract them.
Args:
output: Combined output dictionary to serialise as JSON.
args: Parsed command-line arguments (used for --output_json path).
final_attr: Enriched attribution dictionary, or None.
final_restore: List of per-window restoration dictionaries, or None.
final_ctx: List of per-window contextualization dictionaries.
embedding: List of embedding floats, or None if not requested.
"""
print('\n=== Inference Summary ===', file=sys.stderr)
if final_attr:
locations = final_attr.get('locations', [])[:10]
if locations:
print('\nTop Locations:', file=sys.stderr)
for i, loc in enumerate(locations, 1):
name = loc.get('location_name', 'Unknown')
score = loc.get('score', 0)
print(f' {i:2d}. {name} ({score:.4f})', file=sys.stderr)
top_year = final_attr.get('top_year')
avg_year = final_attr.get('weighted_average_year')
if top_year is not None:
print(f'\nMost Probable Year: {top_year} CE', file=sys.stderr)
if avg_year is not None:
print(f'Weighted Average Year: {avg_year} CE', file=sys.stderr)
elif args.attribute:
print('\nAttribution: failed or empty', file=sys.stderr)
if final_restore:
print('\nTop Restorations:', file=sys.stderr)
count = 0
for window_restore in final_restore:
if not window_restore:
continue
for pred in window_restore.get('predictions', []):
if count >= 10:
break
restored_indices = pred.get('restored', [])
text = pred.get('text', '')
restored_text = ''.join(
text[i] for i in restored_indices if i < len(text)
)
score = pred.get('score', 0)
count += 1
print(f' {count:2d}. {restored_text} ({score:.6f})', file=sys.stderr)
if count >= 10:
break
elif args.restore:
print('\nRestoration: no ? or # markers found', file=sys.stderr)
if any(c for c in final_ctx if c is not None):
ctx_count = sum(len(c.get('ids', [])) for c in final_ctx if c is not None)
print(
f'\nContextualization: {ctx_count} parallels written to output_json',
file=sys.stderr,
)
elif args.contextualize:
print('\nContextualization: failed or empty', file=sys.stderr)
if embedding is not None:
print(f'\nEmbedding ({len(embedding)} dimensions):', file=sys.stderr)
print(repr(embedding), file=sys.stderr)
print('\n=========================\n', file=sys.stderr)
if args.output_json:
with open(args.output_json, 'w') as f:
json.dump(output, f, indent=2)
else:
print(json.dumps(output, indent=2))
def generate_embedding(
*,
text: str,
forward: Any,
params: Any,
alphabet: Any,
) -> list[float] | None:
"""Generates a text embedding vector.
Args:
text: Input text string.
forward: JAX model forward function.
params: JAX model parameters.
alphabet: Language-specific alphabet encoder.
Returns:
List of embedding floats, or None on failure.
"""
try:
emb = inference._generate_text_emb(
params, forward, alphabet, text, emb_mode='avg'
)
return emb.tolist()
except Exception as e:
print(f'Embedding generation failed: {e}', file=sys.stderr)
return None
def main() -> None:
"""Entry point: parses args, loads models, runs inference, and outputs."""
args = _parse_args()
if not (args.attribute or args.contextualize or args.restore):
print(
'ERROR: At least one of --attribute, --contextualize, or --restore '
'must be provided.',
file=sys.stderr,
)
sys.exit(1)
if args.input:
text = args.input
else:
with open(args.input_file, 'r', encoding='utf-8') as f:
text = f.read()
if args.restore and ('?' not in text and '#' not in text):
print(
'WARNING: --restore was requested but no ? or # found in input. '
'Restoration will be skipped.',
file=sys.stderr,
)
windows = split_into_windows(
text, max_len=_MAX_LEN, overlap=args.window_overlap
)
print(
f'Text length: {len(text)} chars, split into {len(windows)} window(s)',
file=sys.stderr,
)
model_config, region_map, alphabet, params, forward, dataset, retrieval = (
_load_resources(args)
)
vocab_char_size = model_config['vocab_char_size']
all_attr, all_restore, all_ctx = _process_windows(
windows=windows,
args=args,
forward=forward,
params=params,
alphabet=alphabet,
vocab_char_size=vocab_char_size,
dataset=dataset,
retrieval=retrieval,
region_map=region_map,
)
if args.attribute:
if len(windows) > 1:
final_attr = average_attributions(all_attr)
else:
final_attr = all_attr[0] if all_attr else None
region_names_list = _load_region_names(args.language)
if final_attr:
final_attr = _enrich_attribution(final_attr, region_names_list)
else:
final_attr = None
region_names_list = []
final_restore = all_restore if any(all_restore) else None
if args.contextualize:
if not region_names_list:
region_names_list = _load_region_names(args.language)
final_ctx = [
_enrich_contextualization(c, region_names_list) for c in all_ctx
]
else:
final_ctx = [None] * len(all_ctx)
embedding = None
if args.embedding:
embedding = generate_embedding(
text=text, forward=forward, params=params, alphabet=alphabet
)
output = {
'attribution': final_attr,
'restoration': final_restore,
'contextualize': final_ctx if args.contextualize else None,
'embedding': embedding,
'num_windows': len(windows),
}
_handle_output(
output=output,
args=args,
final_attr=final_attr,
final_restore=final_restore,
final_ctx=final_ctx,
embedding=embedding,
)
def _parse_args() -> argparse.Namespace:
"""Parses and returns command-line arguments for the inference script."""
parser = argparse.ArgumentParser(
description='Run Predicting The Past inference.'
)
parser.add_argument(
'--language',
required=True,
choices=['latin', 'greek'],
help='Language of the input text.',
)
input_group = parser.add_mutually_exclusive_group(required=True)
input_group.add_argument('--input', help='Input text string.')
input_group.add_argument('--input_file', help='Path to input text file.')
parser.add_argument(
'--models_dir',
default=_MODELS_DIR,
help='Directory containing model files.',
)
parser.add_argument(
'--output_json', default='', help='Save combined JSON to file.'
)
parser.add_argument('--restore_beam_width', type=int, default=100)
parser.add_argument('--restore_max_len', type=int, default=15)
parser.add_argument('--restore_temperature', type=float, default=1.0)
parser.add_argument(
'--window_overlap',
type=float,
default=0.33,
help='Overlap fraction for long-text windowing.',
)
parser.add_argument(
'--attribute',
action='store_true',
default=False,
help='Run geographical and chronological attribution.',
)
parser.add_argument(
'--contextualize',
action='store_true',
default=False,
help='Run contextualization (parallel inscription retrieval).',
)
parser.add_argument(
'--restore',
action='store_true',
default=False,
help='Run text restoration (requires ? or # in input).',
)
parser.add_argument(
'--contextualize_top_k',
type=int,
default=10,
help='Number of top parallel inscriptions to return (default: 10).',
)
parser.add_argument(
'--contextualize_exclude_test_valid',
action='store_true',
default=False,
help=(
'Exclude test/validation texts from contextualization '
'(IDs ending in 3 or 4).'
),
)
parser.add_argument(
'--embedding',
action='store_true',
default=False,
help='Generate a text embedding vector.',
)
return parser.parse_args()
if __name__ == '__main__':
main()
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Needs Python
Security
What our scanner noticed· Low risk
- Scripts that your agent may run
This skill includes scripts that your agent may run. Read them before you use it.
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Published by
- Google DeepMind
- Publisher
- Google DeepMind (Official)
- Source
- google-deepmind/science-skills · skills/predictingthepast · commit 4de6786
- Licence
- Apache License 2.0 (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by Google DeepMind.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download predictingthepast.zip