Documentation
¶
Overview ¶
Package extract is the parse stage: one extractor per media type, each turning an item's bytes into units, such as a thread's post and comments or a book's pages. A registry keyed by media type picks the extractor, so an item parses the same whatever source sent it.
An extractor registers by joining builtin. One written in Go sets Extract. One whose library is not Go is a Program from the worker image, run through isolatedprocess. One whose item holds no bytes, such as a video, reads the item at its source, and the source registers it through Register. Each output is keyed by item, extractor and version, so a changed extractor takes a new version and its outputs write beside the old. A media type holds one primary extractor, which landing queues, and may hold secondary ones, whose parse another stage's write queues and writes beside the primary's.
Index ¶
- Constants
- Variables
- func AfterParse(written, deferred []server.AfterStage) server.AfterStage
- func Capability(e Extractor) string
- func QueueDeferredParse(ctx context.Context, tx *store.Tx, task store.Task) error
- func Register(extractors ...Extractor)
- func SourceHandler(e Extractor, api string, client *http.Client, log *slog.Logger) (worker.Handler, bool)
- type Extractor
- type Item
- type Read
- type Reader
- type Registry
- type RetryError
- type Segment
- type Transcript
- type TranscriptInfo
- type Unit
Constants ¶
const DoclingPins = "e919138066e6"
DoclingPins is the first 12 hex digits of the SHA-256 of the pin of each library in doclingLibraries' closure in release/python-requirements.txt, in the lock's order, then every model pin in release/download-docling-models, one a line. The closure is each of those libraries and every library that the lock's via lines say one in the closure needs. Each shapes Docling's markdown, and a test fails until this equals them.
const DoclingTimeout = 3 * time.Hour
DoclingTimeout bounds one run of pdf-docling. A 1,245-page PDF, the longest that sections selected when this was measured, took 34 minutes under `/usr/bin/time -v pdf-docling < <the PDF>` on a busy desk of 16 CPUs, 1.6 s a page. A node with half those CPUs takes about twice that, and the bound leaves more than twice that again.
const DoclingVersion = "2.153.0"
DoclingVersion is the Docling that release/python-requirements.txt pins, and that the worker image installs for pdf-docling.
const FasterWhisperVersion = "1.2.1"
FasterWhisperVersion is the faster-whisper that release/python-requirements.txt pins, and that the worker image installs for audio-whisper.
const MediaTypePDF = "application/pdf"
MediaTypePDF is a PDF's media type, as a folder connector lands one.
const ProgramDir = "/usr/local/libexec/ingestion/extractors"
ProgramDir is where the worker image installs each extractor program.
const ProgramTimeout = 30 * time.Minute
ProgramTimeout is a program extractor's Timeout unless its registration sets another.
const PyMuPDFVersion = "1.28.2"
PyMuPDFVersion is the PyMuPDF that release/python-requirements.txt pins, and that the worker image installs for the PDF programs.
const ReadAttempts = 4
ReadAttempts is how many claims a parse makes of a read that fails boundedly before it fails. A claim that ended waiting is not among them.
const ReadWaitCeiling = 48 * time.Hour
ReadWaitCeiling is how long a parse waits on its source in all before it fails, counted from the source's first answer to wait. It outlasts the longest pause that ypl declares, a day, so only a source that never frees fails a parse this way.
const SlotGroupHeavy = "heavy"
SlotGroupHeavy is the slot group of each parse whose run holds gigabytes, such as Docling's and Whisper's.
const WhisperModel = "large-v3-turbo"
WhisperModel is the model that audio-whisper runs, whose files release/download-whisper-model pins.
const WhisperPins = "a764b748ef26"
WhisperPins is the first 12 hex digits of the SHA-256 of the pin of each library in whisperLibraries' closure in release/python-requirements.txt, as DoclingPins reads its own, then every model pin in release/download-whisper-model, then ffmpeg's version and SHA-256 in release/install-tools, one a line. Each shapes a transcript, and a test fails until this equals them.
const WhisperProgram = ProgramDir + "/audio-whisper"
WhisperProgram is where the worker image installs audio-whisper, which transcribes the audio it reads on stdin with faster-whisper.
const WhisperVersion = "1+faster-whisper-" + FasterWhisperVersion + "-" + WhisperModel + "-" + WhisperPins
WhisperVersion names the transcription that audio-whisper makes, so a transcript by other libraries, weights or decoding writes beside the old. A change to the program's own settings raises the leading number.
Variables ¶
var PDFDocling = func() Extractor { e := Program("docling", "1+docling-"+DoclingVersion+"-"+DoclingPins, MediaTypePDF, ProgramDir+"/pdf-docling") e.Positional, e.Secondary, e.Timeout, e.SlotGroup = true, true, DoclingTimeout, SlotGroupHeavy return e }()
PDFDocling reads a PDF with Docling's standard pipeline, one unit per PDF page, so unit n is PDF page n, a blank page included. A unit's text is the page's markdown, with each code block fenced and each table's rows kept, and its label is the page's printed label, as PDFText reads it. Docling drops every formula, so it reads only the books whose profile sections.FontShares.CodeHeavyMathLight picks.
It is secondary, so landing never queues it. The write that ends such a book's structure task queues it, and its parse writes beside PDFText's. It runs in SlotGroupHeavy, whose limit bounds its parses and Whisper's together. Its version names Docling's and DoclingPins, so a parse by other libraries or weights writes beside the old one. A change to the program's own reading raises the leading number.
var PDFText = func() Extractor { e := Program("pdf-text", "1+pymupdf-"+PyMuPDFVersion, MediaTypePDF, ProgramDir+"/pdf-text") e.Positional = true return e }()
PDFText reads a PDF's text with PyMuPDF, one unit per PDF page, so unit n is PDF page n, a blank page included. A unit's label is the page's printed label where the PDF declares page labels, and null where it declares none. Its version names PyMuPDF's, so a library upgrade writes its pages beside the old ones.
var SlotGroups = map[string]int{SlotGroupHeavy: 1}
SlotGroups holds each slot group's default limit, which a [worker.slot_limits] key naming the group overrides. heavy runs one parse at a time, because a worker's memory holds one.
var ThreadText = Extractor{Name: "thread-text", Version: "1", MediaType: redditexport.MediaTypeSave, Extract: threadText}
ThreadText parses a Reddit save: its post, then each comment of its thread depth first, in the order Reddit sorted them. A comment that its author deleted or a moderator removed is left out, and its replies are kept. Each unit's label is its author.
Functions ¶
func AfterParse ¶ added in v0.29.0
func AfterParse(written, deferred []server.AfterStage) server.AfterStage
AfterParse runs in the write that ends a parse task done, and routes on the output that the task's extractor wrote: written runs where the parse holds text, and deferred where it handed the item to another extractor. A task whose extractor this build does not name runs written.
func Capability ¶
Capability is the capability of e's parse tasks, so a worker claims only the tasks of extractors it runs.
func QueueDeferredParse ¶ added in v0.29.0
QueueDeferredParse runs where AfterParse finds a parse deferred. It queues a parse by the extractor that the task's extractor's DefersTo names, unless one is queued or leased, or a done one at its version is held. So a parse replay over a deferred item queues the other extractor's parse again only where it has not read the item at its version, and an item whose other parse failed is read again.
func Register ¶ added in v0.29.0
func Register(extractors ...Extractor)
Register adds extractors to the builtin registry, as a source registers the ones that read its items, from its own init.
func SourceHandler ¶ added in v0.29.0
func SourceHandler(e Extractor, api string, client *http.Client, log *slog.Logger) (worker.Handler, bool)
SourceHandler is the worker handler of e, an extractor that ReadsURL, whose reader reaches its source at api through client. It is false where e's program is not installed here, so the worker claims none of its tasks.
Types ¶
type Extractor ¶
type Extractor struct {
// Name names the extractor in each output it writes, such as thread-text.
Name string
// Version is written beside each output, so a changed extractor takes a
// new one.
Version string
MediaType string
// Program is the absolute path of the extractor's program, and empty for
// one written in Go. A worker runs it only where the program is installed.
Program string
// Extract reads body and answers its units in order.
Extract func(ctx context.Context, body io.Reader) ([]Unit, error)
// Timeout bounds one run of Extract, and none where it is zero. A run
// past it fails its task, so a program that hangs on one item's bytes
// frees its worker slot and the item waits in review.
Timeout time.Duration
// Positional keeps a unit with no text in its place, so unit n is the
// artifact's own part n, such as a book's PDF page n. Otherwise such a
// unit is dropped and the rest are numbered again.
Positional bool
// Secondary marks an extractor that landing never queues and a parse
// replay never picks, because another stage's write queues its parse, as
// the structure's queues PDFDocling's.
Secondary bool
// SlotLimit caps the extractor's parses that one worker runs at once,
// where [worker.slot_limits] names no limit for it. Zero leaves them
// uncapped. An extractor in a slot group sets none.
SlotLimit int
// SlotGroup names the slot group whose limit the extractor's parses share
// with every other extractor in it, such as SlotGroupHeavy. SlotGroups
// holds the group's limit.
SlotGroup string
// ReadsURL marks an extractor whose item holds no bytes, so it reads the
// item at its source through the reader that Open opens on that source's
// client. A source registers it, and a worker runs its parses only where
// it opened that source. It sets no Extract.
ReadsURL bool
// Open opens the reader of an extractor that ReadsURL, which reaches its
// source at api through client, carrying the source client's token.
Open func(api string, client *http.Client) Reader
// DefersTo names the secondary extractor whose parse the write that ends a
// deferred parse of this one queues, such as Whisper for a video whose
// captions hold no track.
DefersTo string
}
Extractor turns the bytes of one media type into a parse's units.
func Named ¶ added in v0.29.0
Named is the builtin or registered extractor called name, and false where none is.
func Program ¶
Program is an extractor that runs the program at path with args, such as one whose library is Python. The program reads the item's bytes on stdin, and writes one JSON line per unit on stdout, in order:
{"label": "Chapter 1", "text": "It was a bright cold day…"}
label may be null or left out, and any other field is refused. A run that exits non-zero fails its task, with the end of its stderr kept, and so does a run past ProgramTimeout. The program runs through isolatedprocess, so it holds none of the worker's credentials.
type Item ¶ added in v0.29.0
Item is what an extractor that ReadsURL reads: the item's key, its URL and its media type.
type Read ¶ added in v0.29.0
type Read struct {
Units []Unit
// Deferred says the source holds no text of its own for the item, so its
// parse hands the item to the extractor that DefersTo names, such as a
// video with no caption track.
Deferred bool
// Answer is kept as the output's answer, such as the caption track that
// a read used.
Answer json.RawMessage
}
Read is what a Reader found.
type Reader ¶ added in v0.29.0
Reader reads one item at its source, as an extractor that ReadsURL does.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry picks the primary extractor for an item by its media type, and holds every extractor that a worker runs.
func Builtin ¶
func Builtin() *Registry
Builtin is the registry of every extractor that ingestion holds, and every one that a source registered.
func NewRegistry ¶
NewRegistry registers extractors. It refuses one with no name, version, media type or way to run, two of one name, two primary ones of one media type, and one that defers to an extractor it does not hold as secondary. It refuses a slot group that SlotGroups does not hold, and a SlotLimit beside a slot group, whose limit is the group's.
func (*Registry) For ¶
For is the primary extractor that reads mediaType, and false where none does.
func (*Registry) Handlers ¶
Handlers is a worker handler for each extractor of r that reads an item's bytes and runs here: each written in Go, and each whose program is installed. An extractor that ReadsURL runs through its source's handlers, as SourceHandler opens it. A worker claims only these extractors' tasks.
func (*Registry) QueueParse ¶
QueueParse queues a parse task for item where an extractor reads its media type. Landing calls it in the write that adds the item, so every item lands with its parse queued.
func (*Registry) Replayable ¶
func (r *Registry) Replayable() server.Replayable
Replayable is the parse stage as a replay runs it again: a task for each item whose media type an extractor reads.
type RetryError ¶ added in v0.29.0
RetryError is a read that may succeed if made again, and its parse queues again after After. A bounded one fails once its task's claims reach ReadAttempts, such as when the source gave no answer. Where the source asked the read to wait, as while it is busy, the parse waits: it spends no claim, and fails once it has waited ReadWaitCeiling.
func (*RetryError) Error ¶ added in v0.29.0
func (e *RetryError) Error() string
func (*RetryError) Unwrap ¶ added in v0.29.0
func (e *RetryError) Unwrap() error
type Segment ¶ added in v0.29.0
type Segment struct {
StartMs int64 `json:"start_ms"`
DurationMs int64 `json:"duration_ms"`
Text string `json:"text"`
}
Segment is one stretch of timed text, such as a caption or a run of speech: when it starts and how long it runs, in milliseconds, and its text on one line.
type Transcript ¶ added in v0.29.0
type Transcript struct {
Info TranscriptInfo
Segments []Segment
}
Transcript is what audio-whisper heard.
func Transcribe ¶ added in v0.29.0
Transcribe runs the program at program, as audio-whisper, over audio. The program reads the audio on stdin in any container that ffmpeg reads. It writes its TranscriptInfo as its first line on stdout, then one Segment a line in order. It runs through isolatedprocess, so it holds none of the worker's credentials. A run that exits non-zero answers an error with the end of its stderr, and one that exits zero with no info line answers an error of its own.
type TranscriptInfo ¶ added in v0.29.0
type TranscriptInfo struct {
Language string `json:"language"`
LanguageProbability float64 `json:"language_probability"`
DurationMs int64 `json:"duration_ms"`
VoicedMs int64 `json:"voiced_ms"`
}
TranscriptInfo is what audio-whisper reports of the audio before its segments: the language it heard and how sure it is, how long the audio runs, and how much of it the voice-activity filter kept.