-
-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathmod.rs
More file actions
376 lines (344 loc) · 12 KB
/
Copy pathmod.rs
File metadata and controls
376 lines (344 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//! Pipeline: runs an ordered set of engines across one or more pages and
//! wraps each engine's output in one `Op::Batch` before applying via the
//! session's history.
//!
//! **Engines don't mutate the scene.** They return `Vec<Op>`; this driver
//! applies them transactionally (per-engine) against the active session.
pub mod artifacts;
pub mod engine;
mod engines;
pub use artifacts::Artifact;
pub use engine::{
BoxFuture, Engine, EngineCtx, EngineInfo, EngineLoadFn, PipelineRunOptions, Registry,
build_order,
};
pub use engines::support;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use anyhow::{Result, bail};
use koharu_core::{Op, PageId, PipelineStep};
use koharu_runtime::RuntimeManager;
use tracing::Instrument;
/// Observer for pipeline progress. `step_id` is the engine id of the step
/// about to run (or just finished); step_index / page_index are 0-based.
pub type ProgressSink = Arc<dyn Fn(ProgressTick) + Send + Sync>;
/// Observer for non-fatal step failures. Called once per failed step; the
/// pipeline skips the rest of that page's steps and moves on to the next
/// page.
pub type WarningSink = Arc<dyn Fn(WarningTick) + Send + Sync>;
#[derive(Debug, Clone)]
pub struct ProgressTick {
/// Coarse UI-facing step tag derived from the engine's primary
/// produced artifact. `None` for the final 100% tick where no engine
/// is running.
pub step: Option<PipelineStep>,
/// Engine id (e.g. `"paddle-ocr-vl-1.6"`) for diagnostics + logs.
pub step_id: String,
pub step_index: usize,
pub total_steps: usize,
pub page_index: usize,
pub total_pages: usize,
pub overall_percent: u8,
}
#[derive(Debug, Clone)]
pub struct WarningTick {
pub step_id: String,
pub page_index: usize,
pub total_pages: usize,
pub message: String,
}
/// Returned by [`run`]. `warning_count == 0` means the run finished cleanly.
#[derive(Debug, Clone, Default)]
pub struct RunOutcome {
pub warning_count: usize,
}
/// Map an engine's produced artifact to its UI step category. Stays
/// co-located with the engine metadata so adding a new engine can't
/// silently bypass the toolbar spinner — only the registered artifact
/// matters, not the engine's string id.
fn step_for(info: &EngineInfo) -> Option<PipelineStep> {
info.produces.iter().find_map(|a| match a {
Artifact::TextBoxes
| Artifact::SegmentMask
| Artifact::FontPredictions
| Artifact::BubbleMask => Some(PipelineStep::Detect),
Artifact::OcrText => Some(PipelineStep::Ocr),
Artifact::Translations => Some(PipelineStep::LlmGenerate),
Artifact::Inpainted => Some(PipelineStep::Inpaint),
Artifact::FinalRender => Some(PipelineStep::Render),
// Non-UI-facing artifacts (inputs, intermediate sprites) — no
// toolbar step tag.
_ => None,
})
}
use crate::llm;
use crate::renderer;
use crate::session::ProjectSession;
// ---------------------------------------------------------------------------
// Spec + scope
// ---------------------------------------------------------------------------
#[derive(Debug, Clone)]
pub struct PipelineSpec {
pub scope: Scope,
pub steps: Vec<String>,
pub options: PipelineRunOptions,
}
#[derive(Debug, Clone)]
pub enum Scope {
WholeProject,
Pages(Vec<PageId>),
}
// ---------------------------------------------------------------------------
// Driver
// ---------------------------------------------------------------------------
/// Execute `spec` against `session`. Each engine step becomes one `Op::Batch`
/// applied via the session's history (one undo step per step per page).
///
/// A failed step on a given page is non-fatal: the rest of that page's steps
/// are skipped (they typically depend on the failed step's output), one
/// [`WarningTick`] is emitted via `warnings`, and the driver moves on to the
/// next page. The function returns the total number of per-step warnings
/// that fired, letting callers flag the run as `CompletedWithErrors`.
#[allow(clippy::too_many_arguments)]
#[tracing::instrument(level = "info", skip_all)]
pub async fn run(
session: Arc<ProjectSession>,
registry: Arc<Registry>,
runtime: Arc<RuntimeManager>,
cpu: bool,
llm: Arc<llm::Model>,
renderer: Arc<renderer::Renderer>,
spec: PipelineSpec,
cancel: Arc<AtomicBool>,
progress: Option<ProgressSink>,
warnings: Option<WarningSink>,
) -> Result<RunOutcome> {
let infos: Vec<&EngineInfo> = spec
.steps
.iter()
.map(|id| Registry::find(id))
.collect::<Result<_>>()?;
let order = build_order(&infos)?;
let pages = match &spec.scope {
Scope::WholeProject => session
.scene
.read()
.pages
.keys()
.copied()
.collect::<Vec<_>>(),
Scope::Pages(ids) => ids.clone(),
};
let total_pages = pages.len().max(1);
let total_steps = order.len().max(1);
let total_units = (total_pages * total_steps) as u64;
let mut completed: u64 = 0;
let mut warning_count: usize = 0;
'pages: for (page_index, page_id) in pages.iter().enumerate() {
for (seq, &i) in order.iter().enumerate() {
if cancel.load(Ordering::Relaxed) {
bail!("cancelled");
}
let info = infos[i];
if let Some(sink) = progress.as_ref() {
let percent = ((completed * 100) / total_units).min(100) as u8;
sink(ProgressTick {
step: step_for(info),
step_id: info.id.to_string(),
step_index: seq,
total_steps,
page_index,
total_pages,
overall_percent: percent,
});
}
// The page must still exist (user may have deleted it mid-run).
if !session.scene.read().pages.contains_key(page_id) {
// Skip the remaining steps for a deleted page and credit all
// of them against total_units so progress still reaches 100%.
completed += (total_steps - seq) as u64;
continue 'pages;
}
let engine = match registry.get(info.id, &runtime, cpu).await {
Ok(e) => e,
Err(err) => {
// Engine *load* failure: same recovery as a run failure.
report_step_failure(
info.id,
page_id,
seq,
page_index,
total_pages,
total_steps,
&err,
&mut warning_count,
warnings.as_ref(),
);
completed += (total_steps - seq) as u64;
continue 'pages;
}
};
let scene_snap = session.scene_snapshot();
let ctx = EngineCtx {
scene: &scene_snap,
page: *page_id,
blobs: &session.blobs,
runtime: &runtime,
cancel: &cancel,
options: &spec.options,
llm: &llm,
renderer: &renderer,
};
let step_result = async { engine.run(ctx).await }
.instrument(tracing::info_span!("step", engine = info.id, page = %page_id))
.await;
let ops = match step_result {
Ok(ops) => ops,
Err(err) => {
report_step_failure(
info.id,
page_id,
seq,
page_index,
total_pages,
total_steps,
&err,
&mut warning_count,
warnings.as_ref(),
);
// Subsequent steps on this page almost always consume the
// failed step's artifact; skip the rest and move on.
completed += (total_steps - seq) as u64;
continue 'pages;
}
};
completed += 1;
if ops.is_empty() {
continue;
}
let batch = Op::Batch {
ops,
label: format!("{}: page {}", info.id, page_id),
};
let apply_res = if spec.options.merge_with_previous_op {
session.apply_merge_up(batch)
} else {
session.apply(batch)
};
if let Err(err) = apply_res {
report_step_failure(
info.id,
page_id,
seq,
page_index,
total_pages,
total_steps,
&err,
&mut warning_count,
warnings.as_ref(),
);
continue 'pages;
}
}
}
if let Some(sink) = progress.as_ref() {
sink(ProgressTick {
step: None,
step_id: String::new(),
step_index: total_steps.saturating_sub(1),
total_steps,
page_index: total_pages.saturating_sub(1),
total_pages,
overall_percent: 100,
});
}
Ok(RunOutcome { warning_count })
}
#[allow(clippy::too_many_arguments)]
fn report_step_failure(
engine_id: &str,
page_id: &PageId,
step_index: usize,
page_index: usize,
total_pages: usize,
total_steps: usize,
err: &anyhow::Error,
warning_count: &mut usize,
sink: Option<&WarningSink>,
) {
let _ = total_steps;
tracing::warn!(
engine = engine_id,
page = %page_id,
step_index,
"pipeline step failed: {err:#}"
);
*warning_count += 1;
if let Some(sink) = sink {
sink(WarningTick {
step_id: engine_id.to_string(),
page_index,
total_pages,
message: format!("{err:#}"),
});
}
}
// ---------------------------------------------------------------------------
// Engine catalog building (API surface)
// ---------------------------------------------------------------------------
use koharu_core::{EngineCatalog, EngineCatalogEntry};
/// Build the engine catalog DTO for the API.
pub fn catalog() -> EngineCatalog {
let entry = |info: &&EngineInfo| EngineCatalogEntry {
id: info.id.to_string(),
name: info.name.to_string(),
produces: info.produces.iter().map(|a| format!("{a:?}")).collect(),
};
EngineCatalog {
detectors: Registry::providers(Artifact::TextBoxes)
.iter()
.map(entry)
.collect(),
font_detectors: Registry::providers(Artifact::FontPredictions)
.iter()
.map(entry)
.collect(),
segmenters: Registry::providers(Artifact::SegmentMask)
.iter()
.map(entry)
.collect(),
bubble_segmenters: Registry::providers(Artifact::BubbleMask)
.iter()
.map(entry)
.collect(),
ocr: Registry::providers(Artifact::OcrText)
.iter()
.map(entry)
.collect(),
translators: Registry::providers(Artifact::Translations)
.iter()
.map(entry)
.collect(),
inpainters: Registry::providers(Artifact::Inpainted)
.iter()
.map(entry)
.collect(),
renderers: Registry::providers(Artifact::FinalRender)
.iter()
.map(entry)
.collect(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn catalog_includes_anime_text_detector() {
let catalog = catalog();
assert!(catalog.detectors.iter().any(|engine| {
engine.id == "anime-text"
&& engine.name == "Anime Text YOLO (N)"
&& engine.produces.iter().map(String::as_str).eq(["TextBoxes"])
}));
}
}