Skip to content

About

Pipeline for building Kurmanji Kurdish instruction-tuning data from encyclopedia-style texts in FineWeb2 and Kurdish theses. Gemini writes one question per text chunk; the answer is copied word for word from the source. Pairs are kept only if every answer span matches exactly. ~31k cleaned Q&A pairs on Hugging Face.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

kurdish-ins

Instruction-tuning data for Kurmanji Kurdish, built from the Kurmanji (kmr_Latn) subsets of FineWeb2, kurdish-theses-yok, and kurdish-ins.

The data this pipeline produces is published at muzaffercky/kurdish-ins.

The pipeline is built one stage at a time. So far it has two stages, classify and instruction-gen.

Installation

pipenv install --dev

instruction-gen calls Gemini, so it needs GEMINI_API_KEY in .env. classify does not.

Usage

Run every command from the repository root.

python pipeline.py                     # every stage in config.yml, in order
python pipeline.py --only classify     # one stage
python pipeline.py --start-from STAGE  # a stage and all the stages after it
python pipeline.py --config other.yml  # a different config file

A stage can also be run on its own, as a module:

python -m kurdish_ins.classify

Its settings still come from config.yml, so running a stage on its own and running it through pipeline.py behave the same.

Running on Modal

modal_app/app.py runs a stage on a Modal GPU, with the same stage code pipeline.py runs locally. pipenv install installs Modal; log in once:

modal setup

Then, from the repository root:

modal run --detach -m modal_app.app::classify
modal volume get kurdish-ins /classify/<run> data/modal/   # <run> is full, or e.g. s5

It reads classify.input_file like a local run. Commented out or null, it classifies every source into /classify/full; a file such as data/s5.jsonl is uploaded to the volume for you and classified into /classify/s5. The other settings come from config.yml as usual. The GPU, the timeout, and the GPU's batch size and dtype come from its modal block; Modal runs the model in bfloat16. The volume kurdish-ins holds data/ and the Hugging Face cache, so the model and the datasets are downloaded only on the first run. Running on Modal bills your Modal account.

Pipeline stages

Each stage writes data/<stage>/<run>/manifest.jsonl. The next stage reads the same run's file. data/ is gitignored.

The run is full when classify.input_file is commented out or null, and otherwise the input file's name without its extension (data/sample.jsonl → sample). A sample run and a full run therefore never overwrite each other:

data/classify/full/manifest.jsonl
data/classify/sample/manifest.jsonl
Stage Reads Writes
classify sources, or input_file data/classify/<run>/manifest.jsonl
instruction-gen data/classify/<run>/manifest.jsonl data/instruction-gen/<run>/manifest.jsonl

classify

Labels each entry of every source with its web register using TurkuNLP/web-register-classification-multilingual (for example IN/en for an encyclopedia article, NA/ne for news). It drops nothing. Choosing which registers become instruction pairs is left to a later stage, so changing that rule never means classifying again.

Each manifest row has source (the dataset it came from), id, url, domain, text, register_labels and register_scores (every label's score, so a later stage can use a different threshold without running the model again).

A label is written only when its own score is above threshold. A sub-register does not add its main register: en alone means IN scored below the threshold.

When the run ends, the stage prints its stats for each source: entries, words, entries with no label, how many entries got each main register and sub-register, and how many entries got each label set (all of an entry's labels together, e.g. IN en). It also writes them to stats.json, next to manifest.jsonl.

Settings in config.yml:

Setting Meaning
sources the datasets read: each has dataset (Hugging Face id), files (glob of its parquet files) and columns (the dataset's names for id, url and text)
input_file JSONL of entries to classify instead, e.g. data/sample.jsonl; commented out or null means every source
threshold a register is predicted when its score is above this
batch_size entries classified at a time, and windows per model call
max_length tokens per window; 512 is XLM-R's limit
stride tokens neighbouring windows share; 0 means no overlap
dtype precision the model runs in: float32, or bfloat16 (several times faster on a GPU; scores move in the 2nd–3rd decimal)

Kurmanji is not one of the classifier's training languages. On a small test set it still labelled Wikipedia articles IN/en and news sites NA/ne.

instruction-gen

Asks Gemini for one question per chosen entry, answered by one or more spans copied from the entry (the prompt is in kurdish_ins/prompts.py). It keeps a pair only when every span appears verbatim in the entry, in order and without overlapping, and joins the spans in that order into the answer: by a blank line where a line break was skipped, by a space where other text was. The answer is therefore always the source's own Kurmanji.

It uses an entry when any label of allow_any scores above its threshold and no label of forbid_any does, and every entry of keep_sources whatever its scores. Entries of mt_exempt_domains (Kurdish Wikipedia, Wiktionary and kurdipedia) skip the MT check only, since the classifier gives many Wikipedia articles a moderate MT score. The thresholds are per label and read the scores classify wrote, so changing one needs no new classify run. An entry longer than chunk_words is cut at line breaks, and each chunk gets its own question.

It reads input_manifest when that is set, e.g. data/modal/full/manifest.jsonl for a classify run downloaded from Modal, and otherwise data/classify/<run>/manifest.jsonl.

limit sends only that many chunks, picked at random with seed, so two sample runs (with different prompts or models, say) ask about the same chunks. A sample run writes its files under data/instruction-gen/limit-<limit>-<model>-<prompt>-thinking-<level>/, e.g. data/instruction-gen/limit-200-gemini-3.8-flash-multi-span-thinking-low/, in place of the run's folder, so it never mixes with the full run. A full run writes to data/instruction-gen/<run>-<model>-<prompt>-thinking-<level>/. The model is model in config.yml; the prompt and thinking level are the stage's PROMPT_NAME and THINKING_LEVEL. So runs with different ones can be compared.

Each manifest row has source, id, url, domain, chunk, context (the chunk), question, answer, answer_spans (each span's start and end in context), model, register_labels and register_scores. Every chunk's outcome (kept, no_question, span_not_found, too_short or bad_response), the model's reply and the tokens it used (tokens: input, output and thinking) go to responses.jsonl. Their counts, in total and per label set, go to stats.json, with the average word counts of the kept questions and answers (question_words_avg, answer_words_avg) and the tokens in total (tokens) and per chunk (tokens_avg). Gemini bills thinking tokens as output; they are counted apart to show what the thinking level costs. Rows saved before tokens were recorded have none and are left out of tokens_avg.

The chunks go to Gemini's Batch API at half the price of one call per chunk, in jobs of batch_chunks chunks each. Google caps the tokens waiting in batch jobs per model (3M for gemini-3.8-flash at Tier 1), so a job must fit in it on its own. The stage submits jobs until Google answers 429 (quota full), then one more each time a job ends. A job that fails or expires (Google expires a job after 48 hours and returns none of its results) costs only its own chunks. The stage uploads each job's chunks as batch_requests.jsonl, writes the jobs' names and their chunks' keys to batch_job.json and checks them until they end (Google's target is 24 hours): after 10 seconds, then twice as long after each check, up to every 10 minutes. It saves each job's results as soon as that job ends and takes it out of batch_job.json, which is deleted once every job is saved. If the stage is stopped while it waits, running it again waits for the same jobs rather than submitting (and paying for) them again, and sends only the chunks none of them holds. Chunks a job failed, and the chunks of a job that failed or expired, are sent by the next run.

The stage resumes: running it again skips the chunks in responses.jsonl. So before running it with a changed prompt, move the folder of the earlier run away.

Scripts

Standalone tools in scripts/, run as modules:

python -m scripts.sample_fineweb   # 200 entries over 300 words, at most 3 per domain → data/sample.jsonl

Options: --total, --per-domain, --min-words and --output.

python -m scripts.count_filter     # entries, words and chunks instruction-gen's filter keeps, without calling Gemini

--save before.json, then edit allow_any or forbid_any and run it with --compare before.json to see what the change does.

python -m scripts.push_hf data/instruction-gen/<folder>/manifest.jsonl --dataset user/name --dry-run

Asks GlotLID the language of every question and answer, prints how many are not kmr_Latn and what they are instead, and pushes every pair (instruction for the question, output for the answer, context, id, domain, source, url) as the kmr_latn subset of --dataset, created private. It drops the pairs whose answer or context has a phone number or an email address (82 on the first full run), since FineWeb hides only emails and the theses end with their authors' contact details. The token is HF_TOKEN in .env; one set in the shell is ignored. Nothing is dropped on GlotLID's say: on the first full run it called 764 of 33,073 questions kiu_Latn, and the ones read were plain Kurmanji. --dry-run prints the counts and pushes nothing.

python -m scripts.clean            # writes the cleaned pairs to data/clean/
python -m scripts.clean --push     # and pushes them as the kmr_Latn_clean subset

Its settings are in clean.yml. It cleans the pairs push_hf pushed (dataset, subset) and writes them as a subset of their own (output_subset), so the published pairs stay as they are. The thesis PDFs and some FineWeb2 pages have garbled letters, and a model trained on them learns to write them:

Step What
Replace Letters whose correct form is certain (replacements): þ Þ → ş Ş (Turkish text read in the wrong code page), ÅŸ → ş (UTF-8 decoded twice), ș Ș → ş Ş and [...]
Drop A pair whose question or answer matches one of drop_patterns: garbling with no single correct form, such as Ģ ġ Ġ from the thesis PDFs (ġ is sometimes a broken ş, sometimes a real transliteration letter), broken quotation marks, subscript footnote numbers, font symbols, Kurmanji typed with Turkish ı (ı, İ), and ö ü Ğ, which are also real letters in Czech and Icelandic names

The question, the answer and the context get the same replacements, so every answer is still a verbatim copy of its context. The context is not checked for drops: it is not the text a model learns to write, and checking it would drop many more pairs. Text is put in Unicode NFC first.

On the first full run it kept 30,927 of 32,991 pairs and replaced 25,606 letters. The pairs go to data/clean/<output_subset>/train-00000-of-00001.parquet, the layout of a subset on the Hub, and the counts per pattern and per letter to data/clean/stats.json. --push pushes them to dataset with the token in .env, leaving the other subsets as they are.

Tests

pipenv run pytest

Layout

pipeline.py            runs the stages listed in config.yml
config.yml             stage order and every stage setting
clean.yml              settings of scripts/clean.py
s5.jsonl               a 5-entry sample for classify.input_file
kurdish_ins/
  stage.py             Stage base class, stage_config, run_name, load_manifest
  classify.py          classify stage
  register_classifier.py  the register model and its labels
  instruction_gen.py   instruction-gen stage
  prompts.py           the prompts instruction-gen sends to Gemini
  hub.py               reads a dataset from local parquet files
  logs.py              logging setup
modal_app/app.py       runs stages on Modal
scripts/               standalone tools
 tests/

About

Pipeline for building Kurmanji Kurdish instruction-tuning data from encyclopedia-style texts in FineWeb2 and Kurdish theses. Gemini writes one question per text chunk; the answer is copied word for word from the source. Pairs are kept only if every answer span matches exactly. ~31k cleaned Q&A pairs on Hugging Face.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages