Sampling and annotation¶
CC4Q Workbench samples a small, reproducible spot-check from a labeled corpus, then collects human labels in a schema-driven Streamlit UI. The same session can be exported directly as a training file.
Sampling¶
Three modes are available over any list of records or JSONL file:
random —
sample(data, n=200)orfrac=0.1;stratified —
sample(data, by=["SDK"], n=200)allocates the sample proportionally across strata;quota —
sample(data, by=["SDK"], quotas={"qiskit": 100})uses explicit per-stratum sizes.
- cc4q.sample.sample(data, by=None, n=None, frac=None, quotas=None, seed=42)[source]¶
Sample records from a corpus.
Three modes are supported:
random:
sample(data, n=200)orsample(data, frac=0.1).stratified:
sample(data, by=["SDK"], n=200)allocatesn(orfrac) proportionally across strata.quota:
sample(data, by=["SDK"], quotas={"qiskit": 100, ...})draws explicit per-stratum sizes.
- Parameters:
data – JSONL path or iterable of record dicts.
by – One or more columns used to form strata.
n – Total sample size (proportional across strata when
byis set). Mutually exclusive withfrac.frac – Fraction to sample (proportional across strata when
byis set). Mutually exclusive withn.quotas – Mapping from stratum key to sample size. Requires
by.seed – Random seed; sampling is deterministic for a fixed seed.
- Returns:
A new list of sampled records. Input order is not preserved – strata are processed in sorted key order and records are drawn without replacement.
- Raises:
ValueError – If both
nandfracare given, if no sampling mode is specified, or ifquotasis used withoutby.
Annotation¶
The standalone UI is launched by cc4q.annotate.annotate() (or
cc4q annotate). The UI is schema-driven: any text column, id column,
and label vocabulary can be used. Progress is written to
<output_dir>/annotations.jsonl and resumes after a restart.
- cc4q.annotate.prepare(data, schema=None, mode='blind', model_predictions=None, output_dir='annotations')[source]¶
Materialize an annotation session on disk.
Normalizes the records/schema, writes the session items (and model predictions in adjudicate mode), and returns the path to the session config JSON. The Streamlit app reads that config from the
CC4Q_ANNOTATE_CONFIGenvironment variable, which makes the setup testable withstreamlit.testingand reusable by the CLI--dry-runpath.- Parameters:
data – JSONL/JSON/CSV path or an iterable of record dicts.
schema – Schema dict, or path to a YAML/JSON schema file. Must define
labelsandtext_column;id_columnmay be inferred fromid/no/uuidfields.mode –
"blind"(no model input) or"adjudicate"(show model suggestions for confirmation/override).model_predictions – Optional predictions file (JSONL/JSON/CSV) or iterable joined to items by the id column.
output_dir – Directory for the session config and private item files.
- Returns:
Path to the generated
.cc4q_session.jsonconfig.- Raises:
ValueError – If there are no records, the schema is incomplete, or a required input is missing.
- cc4q.annotate.annotate(data, schema=None, mode='blind', model_predictions=None, output_dir='annotations', streamlit_args=None)[source]¶
Launch the annotation UI in Streamlit (blocking until closed).
This calls
prepare()and then startsstreamlit run cc4q/annotate/app.pyas a subprocess with the session config in the environment. Progress is persisted underoutput_dirso a closed session resumes at the first unlabeled item.- Parameters:
data – JSONL/JSON/CSV path or an iterable of record dicts.
schema – Schema dict or path to a YAML/JSON schema file.
mode –
"blind"or"adjudicate".model_predictions – Optional model predictions for adjudicate mode.
output_dir – Directory for session state and exports.
streamlit_args – Extra arguments appended to the
streamlit runcommand (for example["--server.port", "8503"]).
- Returns:
The subprocess exit code (
0on a normal close).- Raises:
ValueError – If the session inputs are invalid (see
prepare()).
Schema¶
text_column: comment_text
id_column: id
labels: [quantum, non-quantum]
title: Quantum comment spot-check # optional
Modes¶
blindNo model input; pure human labeling.
adjudicateA predictions file joined by the id column shows the model suggestion for every item. The human still confirms or overrides every item.
Embedding the UI¶
- cc4q.annotate.ui.render_annotation(config, session_prefix='ann', embedded=False)[source]¶
Render the click-through annotation UI.
configkeys: schema (text_column/id_column/labels/title?), mode (blind|adjudicate), items or data, predictions_map or predictions, output_dir (optional persistence). Returns the list of annotated rows (records +labelcolumn) for host pages to consume.
The Workbench embeds render_annotation() in its
Annotate page; the standalone app in cc4q/annotate/app.py is a thin
launcher around the same renderer. cc4q.annotate.prepare is also useful
for validating a session in CI without starting Streamlit.
Example¶
from cc4q.sample import sample
from cc4q.annotate import annotate
spot = sample("out/labeled.jsonl", by=["SDK"], n=200, seed=42)
annotate(
spot,
schema={"text_column": "comment_text", "id_column": "id",
"labels": ["quantum", "non-quantum"]},
mode="adjudicate",
model_predictions="out/labeled.jsonl",
output_dir="out/ann",
)
Note
cc4q.annotate.app is a Streamlit entrypoint, not an importable API.
Use cc4q.annotate.annotate() or the cc4q annotate command.