| 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 |
use quasi_declare::declare; |
| 128 |
use quasi_router::layout::Tone; |
| 129 |
use quasi_router::{ |
| 130 |
Accepted, Action, Choice, Locating, Request, Response, RouteError, Router, Sought, Tag, |
| 131 |
}; |
| 132 |
|
| 133 |
use super::{Panels, Part, Shareable}; |
| 134 |
|
| 135 |
|
| 136 |
const BODY: &str = "classifier-body"; |
| 137 |
|
| 138 |
|
| 139 |
const SHARED: &str = ".afcl"; |
| 140 |
|
| 141 |
|
| 142 |
const FILE: &str = "file"; |
| 143 |
|
| 144 |
|
| 145 |
const DROPS_LAYER: &str = |
| 146 |
"Permanently delete this layer and its imported rules? This cannot be undone."; |
| 147 |
|
| 148 |
|
| 149 |
pub fn routes(router: Router<Panels<'_>>) -> Router<Panels<'_>> { |
| 150 |
let router = router |
| 151 |
.get("/classifier", index) |
| 152 |
.post("/classifier/rules/new", new_rule) |
| 153 |
.post("/classifier/rules/apply", apply_rules) |
| 154 |
.post("/classifier/rules/starter", seed_rules) |
| 155 |
.post("/classifier/rules/{id}/enabled", enable_rule) |
| 156 |
.post("/classifier/rules/{id}/edit", edit_rule) |
| 157 |
.post("/classifier/rules/{id}/delete", delete_rule) |
| 158 |
.post("/classifier/rules/{id}/up", move_up) |
| 159 |
.post("/classifier/rules/{id}/down", move_down); |
| 160 |
|
| 161 |
let router = router |
| 162 |
.post("/classifier/draft/name", name_draft) |
| 163 |
.post("/classifier/draft/enabled", enable_draft) |
| 164 |
.post("/classifier/draft/match", match_mode) |
| 165 |
.post("/classifier/draft/conditions/add", add_condition) |
| 166 |
.post("/classifier/draft/conditions/{at}/remove", drop_condition) |
| 167 |
.post( |
| 168 |
"/classifier/draft/conditions/{at}/set/{part}", |
| 169 |
set_condition, |
| 170 |
) |
| 171 |
.post("/classifier/draft/actions/add", add_action) |
| 172 |
.post("/classifier/draft/actions/{at}/remove", drop_action) |
| 173 |
.post("/classifier/draft/actions/{at}/set/{part}", set_action) |
| 174 |
.post("/classifier/draft/test", test_draft) |
| 175 |
.post("/classifier/draft/save", save_draft) |
| 176 |
.post("/classifier/draft/cancel", cancel_draft); |
| 177 |
|
| 178 |
let router = router |
| 179 |
.post("/classifier/autotag/suggest", suggest) |
| 180 |
.post("/classifier/autotag/review", review) |
| 181 |
.post("/classifier/autotag/reopen", reopen) |
| 182 |
.post("/classifier/head/train", train) |
| 183 |
.post("/classifier/head/clear", clear_head) |
| 184 |
.post("/classifier/policies/typing", typing_policy) |
| 185 |
.post("/classifier/policies/add", add_policy) |
| 186 |
.post("/classifier/policies/{tag}/thresholds", set_policy); |
| 187 |
|
| 188 |
let router = router |
| 189 |
.post("/classifier/clusters/k", cluster_k) |
| 190 |
.post("/classifier/clusters/find", find_clusters) |
| 191 |
.post("/classifier/clusters/{at}/play", play_cluster) |
| 192 |
.post("/classifier/clusters/{at}/name", name_cluster) |
| 193 |
.post("/classifier/clusters/{at}/tag", tag_cluster) |
| 194 |
.post("/classifier/folders/scan", scan_folders) |
| 195 |
.post("/classifier/folders/{at}/tag", name_folder) |
| 196 |
.post("/classifier/folders/{at}/apply", apply_folder) |
| 197 |
.post("/classifier/undo/{source}", undo_source); |
| 198 |
|
| 199 |
router |
| 200 |
.post("/classifier/sharing/name", name_export) |
| 201 |
.post("/classifier/sharing/include/{part}", include) |
| 202 |
.post("/classifier/sharing/export", export) |
| 203 |
.post("/classifier/sharing/exported", exported) |
| 204 |
.post("/classifier/sharing/import", import) |
| 205 |
.post("/classifier/sharing/imported", imported) |
| 206 |
.post("/classifier/layers/{id}/enabled", enable_layer) |
| 207 |
.post("/classifier/layers/{id}/weight", weigh_layer) |
| 208 |
.post("/classifier/layers/{id}/remove", remove_layer) |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
fn index(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 215 |
Ok(showing(state)) |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
fn new_rule(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 220 |
state.classifier.new_rule(); |
| 221 |
settled(state) |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
fn apply_rules(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 226 |
state.classifier.apply_rules(); |
| 227 |
settled(state) |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
fn seed_rules(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 232 |
state.classifier.seed_rules(); |
| 233 |
settled(state) |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
fn enable_rule(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 238 |
let id = named(&request, "id")?; |
| 239 |
state.classifier.enable_rule(id, ticked(&request, "on")); |
| 240 |
settled(state) |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
fn edit_rule(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 245 |
state.classifier.edit_rule(named(&request, "id")?); |
| 246 |
settled(state) |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
fn delete_rule(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 251 |
state.classifier.delete_rule(named(&request, "id")?); |
| 252 |
settled(state) |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
fn move_up(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 257 |
state.classifier.move_rule(named(&request, "id")?, true); |
| 258 |
settled(state) |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
fn move_down(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 263 |
state.classifier.move_rule(named(&request, "id")?, false); |
| 264 |
settled(state) |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
fn name_draft(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 271 |
state |
| 272 |
.classifier |
| 273 |
.name_draft(request.payload.get("name").unwrap_or_default()); |
| 274 |
settled(state) |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
fn enable_draft(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 279 |
state.classifier.enable_draft(ticked(&request, "enabled")); |
| 280 |
settled(state) |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
fn match_mode(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 285 |
let answered = request.payload.get("match").unwrap_or_default(); |
| 286 |
state.classifier.match_all(answered != "any"); |
| 287 |
settled(state) |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
fn add_condition(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 292 |
state.classifier.add_condition(); |
| 293 |
settled(state) |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
fn drop_condition(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 298 |
state.classifier.drop_condition(slot(&request)?); |
| 299 |
settled(state) |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
fn set_condition(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 304 |
let at = slot(&request)?; |
| 305 |
let part = condition_part(&request)?; |
| 306 |
let name = request.captures.require("part")?; |
| 307 |
state |
| 308 |
.classifier |
| 309 |
.set_condition(at, part, request.payload.get(name).unwrap_or_default()); |
| 310 |
settled(state) |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
fn add_action(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 315 |
state.classifier.add_action(); |
| 316 |
settled(state) |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
fn drop_action(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 321 |
state.classifier.drop_action(slot(&request)?); |
| 322 |
settled(state) |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
fn set_action(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 327 |
let at = slot(&request)?; |
| 328 |
let name = request.captures.require("part")?; |
| 329 |
let part = match name { |
| 330 |
"kind" => Part::Kind, |
| 331 |
"tag" => Part::Value, |
| 332 |
_ => return Err(RouteError::not_found("no such part of an action")), |
| 333 |
}; |
| 334 |
state |
| 335 |
.classifier |
| 336 |
.set_action(at, part, request.payload.get(name).unwrap_or_default()); |
| 337 |
settled(state) |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
fn test_draft(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 342 |
state.classifier.test_draft(); |
| 343 |
settled(state) |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
fn save_draft(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 348 |
state.classifier.save_draft(); |
| 349 |
settled(state) |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
fn cancel_draft(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 354 |
state.classifier.cancel_draft(); |
| 355 |
settled(state) |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
fn suggest(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 362 |
state.classifier.suggest(); |
| 363 |
settled(state) |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
fn undo_source(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 372 |
let source = request.captures.require("source")?; |
| 373 |
if !matches!(source, "ml" | "cluster" | "harvest") { |
| 374 |
return Err(RouteError::not_found("nothing applied tags that way")); |
| 375 |
} |
| 376 |
state.classifier.undo_source(source); |
| 377 |
settled(state) |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
fn review(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 382 |
state.classifier.review(); |
| 383 |
settled(state) |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
fn reopen(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 388 |
if state.classifier.waiting() == 0 { |
| 389 |
return Err(RouteError::not_found("nothing is waiting")); |
| 390 |
} |
| 391 |
state.classifier.reopen_review(); |
| 392 |
settled(state) |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
fn train(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 397 |
state.classifier.train(); |
| 398 |
settled(state) |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
fn clear_head(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 403 |
state.classifier.clear_head(); |
| 404 |
settled(state) |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
fn set_policy(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 413 |
let tag = request.captures.require("tag")?; |
| 414 |
let current = state |
| 415 |
.classifier |
| 416 |
.policies() |
| 417 |
.into_iter() |
| 418 |
.find(|policy| policy.tag == tag) |
| 419 |
.ok_or_else(|| RouteError::not_found("no threshold for that tag"))?; |
| 420 |
let review = number(&request, "review").unwrap_or(current.review); |
| 421 |
let auto = number(&request, "auto").unwrap_or(current.auto); |
| 422 |
state.classifier.set_policy(tag, review, auto); |
| 423 |
settled(state) |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
fn typing_policy(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 428 |
state |
| 429 |
.classifier |
| 430 |
.typing_policy(request.payload.get("tag").unwrap_or_default()); |
| 431 |
settled(state) |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
fn add_policy(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 436 |
state.classifier.add_policy(); |
| 437 |
settled(state) |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
fn cluster_k(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 444 |
let asked = request |
| 445 |
.payload |
| 446 |
.get("cluster_k") |
| 447 |
.and_then(|value| value.parse().ok()) |
| 448 |
.ok_or_else(|| RouteError::not_found("that is not a number of groups"))?; |
| 449 |
state.classifier.set_cluster_k(asked); |
| 450 |
settled(state) |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
fn find_clusters(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 455 |
state.classifier.find_clusters(); |
| 456 |
settled(state) |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
fn play_cluster(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 461 |
state.classifier.play_cluster(slot(&request)?); |
| 462 |
settled(state) |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
fn name_cluster(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 467 |
let at = slot(&request)?; |
| 468 |
state |
| 469 |
.classifier |
| 470 |
.name_cluster(at, request.payload.get("name").unwrap_or_default()); |
| 471 |
settled(state) |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
fn tag_cluster(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 476 |
state.classifier.tag_cluster(slot(&request)?); |
| 477 |
settled(state) |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
fn scan_folders(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 482 |
state.classifier.scan_folders(); |
| 483 |
settled(state) |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
fn name_folder(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 488 |
let at = slot(&request)?; |
| 489 |
state |
| 490 |
.classifier |
| 491 |
.name_folder(at, request.payload.get("tag").unwrap_or_default()); |
| 492 |
settled(state) |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
fn apply_folder(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 497 |
state.classifier.apply_folder(slot(&request)?); |
| 498 |
settled(state) |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
fn name_export(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 505 |
state |
| 506 |
.classifier |
| 507 |
.name_export(request.payload.get("export_name").unwrap_or_default()); |
| 508 |
settled(state) |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
fn include(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 513 |
let name = request.captures.require("part")?; |
| 514 |
let part = match name { |
| 515 |
"exemplars" => Shareable::Exemplars, |
| 516 |
"rules" => Shareable::Rules, |
| 517 |
"thresholds" => Shareable::Thresholds, |
| 518 |
_ => return Err(RouteError::not_found("nothing of that name is shared")), |
| 519 |
}; |
| 520 |
state.classifier.include(part, ticked(&request, name)); |
| 521 |
settled(state) |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
fn export(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 534 |
let sharing = state.classifier.sharing(); |
| 535 |
if !sharing.worth_writing() { |
| 536 |
return Err(RouteError::not_found("there is nothing to export")); |
| 537 |
} |
| 538 |
Ok(Response::locate(Locating::new( |
| 539 |
Sought::Save { |
| 540 |
name: format!("{}{SHARED}", export_filename(&sharing.name)), |
| 541 |
accept: vec![Accepted::suffix(SHARED)], |
| 542 |
}, |
| 543 |
"Export classifier", |
| 544 |
Action::post("/classifier/sharing/exported"), |
| 545 |
FILE, |
| 546 |
))) |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
fn exported(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 555 |
let file = request.payload.get(FILE).unwrap_or_default(); |
| 556 |
if !file.is_empty() { |
| 557 |
state.classifier.export_to(file); |
| 558 |
} |
| 559 |
settled(state) |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
fn export_filename(name: &str) -> String { |
| 570 |
let cleaned: String = name |
| 571 |
.trim() |
| 572 |
.chars() |
| 573 |
.map(|c| { |
| 574 |
if c.is_alphanumeric() || c == '-' || c == '_' { |
| 575 |
c |
| 576 |
} else { |
| 577 |
'_' |
| 578 |
} |
| 579 |
}) |
| 580 |
.collect(); |
| 581 |
if cleaned.is_empty() { |
| 582 |
"classifier".to_owned() |
| 583 |
} else { |
| 584 |
cleaned |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
fn import(_state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 593 |
Ok(Response::locate(Locating::new( |
| 594 |
Sought::File { |
| 595 |
accept: vec![Accepted::suffix(SHARED)], |
| 596 |
}, |
| 597 |
"Import classifier", |
| 598 |
Action::post("/classifier/sharing/imported"), |
| 599 |
FILE, |
| 600 |
))) |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
fn imported(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 609 |
let file = request.payload.get(FILE).unwrap_or_default(); |
| 610 |
if !file.is_empty() { |
| 611 |
state.classifier.import(file); |
| 612 |
} |
| 613 |
settled(state) |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
fn enable_layer(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 618 |
let id = named(&request, "id")?; |
| 619 |
state.classifier.enable_layer(id, ticked(&request, "on")); |
| 620 |
settled(state) |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
fn weigh_layer(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 625 |
let id = named(&request, "id")?; |
| 626 |
let weight = |
| 627 |
number(&request, "weight").ok_or_else(|| RouteError::not_found("that is not a weight"))?; |
| 628 |
state.classifier.weigh_layer(id, weight); |
| 629 |
settled(state) |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
fn remove_layer(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 634 |
state.classifier.remove_layer(named(&request, "id")?); |
| 635 |
settled(state) |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
fn settled(state: &Panels<'_>) -> Result<Response, RouteError> { |
| 642 |
Ok(showing(state)) |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
fn named<'a>(request: &'a Request, capture: &str) -> Result<&'a str, RouteError> { |
| 647 |
request.captures.require(capture) |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
fn slot(request: &Request) -> Result<usize, RouteError> { |
| 652 |
request |
| 653 |
.captures |
| 654 |
.require("at")? |
| 655 |
.parse() |
| 656 |
.map_err(|_| RouteError::not_found("that is not a slot")) |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
fn condition_part(request: &Request) -> Result<Part, RouteError> { |
| 661 |
match request.captures.require("part")? { |
| 662 |
"field" => Ok(Part::Field), |
| 663 |
"op" => Ok(Part::Op), |
| 664 |
"value" => Ok(Part::Value), |
| 665 |
_ => Err(RouteError::not_found("no such part of a condition")), |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
fn ticked(request: &Request, name: &str) -> bool { |
| 675 |
!request.payload.get(name).unwrap_or_default().is_empty() |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
fn number(request: &Request, name: &str) -> Option<f64> { |
| 680 |
request.payload.get(name)?.parse().ok() |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
fn showing(state: &Panels<'_>) -> Response { |
| 688 |
Response::from(screen(&read(state))) |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
struct Classifying { |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
doing: Option<String>, |
| 699 |
|
| 700 |
failed: Option<String>, |
| 701 |
|
| 702 |
busy: bool, |
| 703 |
rules: Rules, |
| 704 |
autotag: Autotag, |
| 705 |
clusters: Clusters, |
| 706 |
folders: Folders, |
| 707 |
sharing: Shared, |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
struct Rules { |
| 712 |
|
| 713 |
listing: Option<RuleList>, |
| 714 |
|
| 715 |
editor: Option<Editor>, |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
struct RuleList { |
| 720 |
|
| 721 |
last: Option<String>, |
| 722 |
|
| 723 |
bare: bool, |
| 724 |
each: Vec<Listed>, |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
struct Listed { |
| 729 |
id: String, |
| 730 |
|
| 731 |
name: String, |
| 732 |
|
| 733 |
meta: String, |
| 734 |
enabled: bool, |
| 735 |
|
| 736 |
first: bool, |
| 737 |
last: bool, |
| 738 |
|
| 739 |
confirm: String, |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
struct Editor { |
| 744 |
name: String, |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
problem: Option<&'static str>, |
| 750 |
enabled: &'static str, |
| 751 |
|
| 752 |
matching: &'static str, |
| 753 |
conditions: Vec<Condition>, |
| 754 |
actions: Vec<Doing>, |
| 755 |
|
| 756 |
matched: Option<String>, |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
struct Condition { |
| 761 |
|
| 762 |
at: usize, |
| 763 |
|
| 764 |
fields: Vec<Choice>, |
| 765 |
field: String, |
| 766 |
|
| 767 |
|
| 768 |
ops: Vec<Choice>, |
| 769 |
op: String, |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
value: Option<String>, |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
struct Doing { |
| 779 |
|
| 780 |
at: usize, |
| 781 |
kind: String, |
| 782 |
|
| 783 |
tag: Option<String>, |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
struct Autotag { |
| 788 |
|
| 789 |
last: Option<String>, |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
reopen: Option<String>, |
| 795 |
|
| 796 |
reviewed: Option<String>, |
| 797 |
head: Head, |
| 798 |
thresholds: Thresholds, |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
struct Head { |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
said: Option<String>, |
| 808 |
|
| 809 |
|
| 810 |
train: &'static str, |
| 811 |
|
| 812 |
trained: bool, |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
struct Thresholds { |
| 817 |
each: Vec<Threshold>, |
| 818 |
|
| 819 |
typing: String, |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
struct Threshold { |
| 824 |
tag: String, |
| 825 |
review: String, |
| 826 |
auto: String, |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
struct Clusters { |
| 831 |
|
| 832 |
k: String, |
| 833 |
piles: Vec<Pile>, |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
struct Pile { |
| 838 |
|
| 839 |
at: usize, |
| 840 |
|
| 841 |
said: String, |
| 842 |
name: String, |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
playable: bool, |
| 848 |
|
| 849 |
named: bool, |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
struct Folders { |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
bare: bool, |
| 860 |
each: Vec<Harvested>, |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
struct Harvested { |
| 865 |
|
| 866 |
at: usize, |
| 867 |
|
| 868 |
said: String, |
| 869 |
tag: String, |
| 870 |
|
| 871 |
named: bool, |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
struct Shared { |
| 876 |
|
| 877 |
name: String, |
| 878 |
|
| 879 |
parts: Vec<Included>, |
| 880 |
|
| 881 |
worth: bool, |
| 882 |
|
| 883 |
exported: Option<String>, |
| 884 |
|
| 885 |
imported: Option<String>, |
| 886 |
layers: Vec<Layer>, |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
struct Included { |
| 891 |
name: &'static str, |
| 892 |
label: &'static str, |
| 893 |
|
| 894 |
value: &'static str, |
| 895 |
|
| 896 |
|
| 897 |
hint: Option<&'static str>, |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
struct Layer { |
| 902 |
id: String, |
| 903 |
name: String, |
| 904 |
|
| 905 |
said: String, |
| 906 |
enabled: &'static str, |
| 907 |
|
| 908 |
weight: String, |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
fn read(state: &Panels<'_>) -> Classifying { |
| 913 |
let doing = state.classifier.busy(); |
| 914 |
let busy = doing.is_some(); |
| 915 |
Classifying { |
| 916 |
|
| 917 |
|
| 918 |
failed: (!busy).then(|| state.classifier.failed()).flatten(), |
| 919 |
doing, |
| 920 |
busy, |
| 921 |
rules: rules_read(state), |
| 922 |
autotag: autotag_read(state), |
| 923 |
clusters: clusters_read(state), |
| 924 |
folders: folders_read(state), |
| 925 |
sharing: sharing_read(state), |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
|
| 930 |
fn rules_read(state: &Panels<'_>) -> Rules { |
| 931 |
let authoring = state.classifier.authoring(); |
| 932 |
Rules { |
| 933 |
listing: authoring.is_none().then(|| rule_list_read(state)), |
| 934 |
editor: authoring.map(|draft| editor_read(state, &draft)), |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
fn rule_list_read(state: &Panels<'_>) -> RuleList { |
| 940 |
let all = state.classifier.rules(); |
| 941 |
RuleList { |
| 942 |
last: state |
| 943 |
.classifier |
| 944 |
.last_apply() |
| 945 |
.map(|changed| format!("Last apply: {changed} sample{} updated", plural(changed))), |
| 946 |
bare: all.is_empty(), |
| 947 |
each: all |
| 948 |
.iter() |
| 949 |
.map(|rule| { |
| 950 |
let named = if rule.name.trim().is_empty() { |
| 951 |
"(unnamed)" |
| 952 |
} else { |
| 953 |
rule.name.trim() |
| 954 |
}; |
| 955 |
Listed { |
| 956 |
id: rule.id.clone(), |
| 957 |
name: named.to_owned(), |
| 958 |
meta: format!("{} cond \u{2192} {} act", rule.conditions, rule.actions), |
| 959 |
enabled: rule.enabled, |
| 960 |
first: rule.first, |
| 961 |
last: rule.last, |
| 962 |
confirm: format!( |
| 963 |
"Delete the rule \"{named}\"? Tags it already applied stay where they are." |
| 964 |
), |
| 965 |
} |
| 966 |
}) |
| 967 |
.collect(), |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
fn editor_read(state: &Panels<'_>, draft: &super::Authoring) -> Editor { |
| 973 |
let testable = state.classifier.testable(); |
| 974 |
Editor { |
| 975 |
name: draft.name.clone(), |
| 976 |
problem: (draft.refused && draft.name.trim().is_empty()) |
| 977 |
.then_some("Name the rule before saving."), |
| 978 |
enabled: switched(draft.enabled), |
| 979 |
matching: if draft.all { "all" } else { "any" }, |
| 980 |
conditions: draft |
| 981 |
.conditions |
| 982 |
.iter() |
| 983 |
.enumerate() |
| 984 |
.map(|(at, condition)| condition_read(at, condition, &testable)) |
| 985 |
.collect(), |
| 986 |
actions: draft |
| 987 |
.actions |
| 988 |
.iter() |
| 989 |
.enumerate() |
| 990 |
.map(|(at, action)| Doing { |
| 991 |
at, |
| 992 |
kind: action.kind.clone(), |
| 993 |
tag: (action.kind != "stop").then(|| action.tag.clone()), |
| 994 |
}) |
| 995 |
.collect(), |
| 996 |
matched: draft |
| 997 |
.matched |
| 998 |
.map(|matched| format!("Matches {matched} sample{}", plural(matched))), |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
|
| 1003 |
fn condition_read( |
| 1004 |
at: usize, |
| 1005 |
condition: &super::Testing, |
| 1006 |
testable: &[super::Testable], |
| 1007 |
) -> Condition { |
| 1008 |
let chosen = testable.iter().find(|field| field.value == condition.field); |
| 1009 |
let ops = chosen.map(|field| field.ops.as_slice()).unwrap_or_default(); |
| 1010 |
Condition { |
| 1011 |
at, |
| 1012 |
fields: testable |
| 1013 |
.iter() |
| 1014 |
.map(|field| Choice::new(field.value.clone(), field.label.clone())) |
| 1015 |
.collect(), |
| 1016 |
field: condition.field.clone(), |
| 1017 |
ops: ops |
| 1018 |
.iter() |
| 1019 |
.map(|op| Choice::new(op.value.clone(), op.label.clone())) |
| 1020 |
.collect(), |
| 1021 |
op: condition.op.clone(), |
| 1022 |
value: ops |
| 1023 |
.iter() |
| 1024 |
.find(|allowed| allowed.value == condition.op) |
| 1025 |
.is_some_and(|allowed| allowed.takes_value) |
| 1026 |
.then(|| condition.value.clone()), |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
fn autotag_read(state: &Panels<'_>) -> Autotag { |
| 1032 |
let waiting = state.classifier.waiting(); |
| 1033 |
Autotag { |
| 1034 |
last: state |
| 1035 |
.classifier |
| 1036 |
.last_suggest() |
| 1037 |
.map(|applied| format!("Last run: {applied} tag{} applied", plural(applied))), |
| 1038 |
reopen: (waiting > 0).then(|| format!("Reopen ({waiting})")), |
| 1039 |
reviewed: state.classifier.last_review(), |
| 1040 |
head: head_read(state), |
| 1041 |
thresholds: Thresholds { |
| 1042 |
each: state |
| 1043 |
.classifier |
| 1044 |
.policies() |
| 1045 |
.iter() |
| 1046 |
.map(|policy| Threshold { |
| 1047 |
tag: policy.tag.clone(), |
| 1048 |
review: format!("{:.2}", policy.review), |
| 1049 |
auto: format!("{:.2}", policy.auto), |
| 1050 |
}) |
| 1051 |
.collect(), |
| 1052 |
typing: state.classifier.policy_typing(), |
| 1053 |
}, |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
|
| 1058 |
fn head_read(state: &Panels<'_>) -> Head { |
| 1059 |
let head = state.classifier.head(); |
| 1060 |
Head { |
| 1061 |
said: match &head { |
| 1062 |
Some(head) => Some(format!( |
| 1063 |
"Active: {} tag{} from {} sample{}.", |
| 1064 |
head.classes, |
| 1065 |
plural(head.classes), |
| 1066 |
head.exemplars, |
| 1067 |
plural(head.exemplars), |
| 1068 |
)), |
| 1069 |
None => state.classifier.readiness().map(|ready| { |
| 1070 |
if ready.worthwhile { |
| 1071 |
format!( |
| 1072 |
"No model yet. With {} tagged samples, training is recommended.", |
| 1073 |
ready.tagged |
| 1074 |
) |
| 1075 |
} else { |
| 1076 |
format!( |
| 1077 |
"No model: nearest-neighbor matching is fast enough at {} tagged sample{}.", |
| 1078 |
ready.tagged, |
| 1079 |
plural(ready.tagged), |
| 1080 |
) |
| 1081 |
} |
| 1082 |
}), |
| 1083 |
}, |
| 1084 |
train: if head.is_some() { |
| 1085 |
"Retrain model" |
| 1086 |
} else { |
| 1087 |
"Train model" |
| 1088 |
}, |
| 1089 |
trained: head.is_some(), |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
|
| 1094 |
fn clusters_read(state: &Panels<'_>) -> Clusters { |
| 1095 |
Clusters { |
| 1096 |
k: state.classifier.cluster_k().to_string(), |
| 1097 |
piles: state |
| 1098 |
.classifier |
| 1099 |
.clusters() |
| 1100 |
.iter() |
| 1101 |
.enumerate() |
| 1102 |
.map(|(at, pile)| Pile { |
| 1103 |
at, |
| 1104 |
said: format!("{} sample{}", pile.members, plural(pile.members)), |
| 1105 |
name: pile.name.clone(), |
| 1106 |
playable: pile.playable, |
| 1107 |
named: !pile.name.trim().is_empty(), |
| 1108 |
}) |
| 1109 |
.collect(), |
| 1110 |
} |
| 1111 |
} |
| 1112 |
|
| 1113 |
|
| 1114 |
fn folders_read(state: &Panels<'_>) -> Folders { |
| 1115 |
let found = state.classifier.folders(); |
| 1116 |
Folders { |
| 1117 |
bare: found.as_ref().is_some_and(Vec::is_empty), |
| 1118 |
each: found |
| 1119 |
.unwrap_or_default() |
| 1120 |
.iter() |
| 1121 |
.enumerate() |
| 1122 |
.map(|(at, folder)| Harvested { |
| 1123 |
at, |
| 1124 |
said: format!( |
| 1125 |
"{} ({} sample{})", |
| 1126 |
folder.folder, |
| 1127 |
folder.samples, |
| 1128 |
plural(folder.samples) |
| 1129 |
), |
| 1130 |
tag: folder.tag.clone(), |
| 1131 |
named: !folder.tag.trim().is_empty(), |
| 1132 |
}) |
| 1133 |
.collect(), |
| 1134 |
} |
| 1135 |
} |
| 1136 |
|
| 1137 |
|
| 1138 |
fn sharing_read(state: &Panels<'_>) -> Shared { |
| 1139 |
let share = state.classifier.sharing(); |
| 1140 |
Shared { |
| 1141 |
name: share.name.clone(), |
| 1142 |
parts: [ |
| 1143 |
("exemplars", "Exemplars", share.exemplars), |
| 1144 |
("rules", "Rules", share.rules), |
| 1145 |
("thresholds", "Thresholds", share.thresholds), |
| 1146 |
] |
| 1147 |
.into_iter() |
| 1148 |
.map(|(name, label, part)| Included { |
| 1149 |
name, |
| 1150 |
label, |
| 1151 |
value: switched(part.wanted), |
| 1152 |
hint: (!part.available).then_some("Nothing of this kind yet."), |
| 1153 |
}) |
| 1154 |
.collect(), |
| 1155 |
worth: share.worth_writing(), |
| 1156 |
exported: share.exported.clone(), |
| 1157 |
imported: share.imported.clone(), |
| 1158 |
layers: state |
| 1159 |
.classifier |
| 1160 |
.layers() |
| 1161 |
.iter() |
| 1162 |
.map(|layer| Layer { |
| 1163 |
id: layer.id.clone(), |
| 1164 |
name: layer.name.clone(), |
| 1165 |
said: format!( |
| 1166 |
"{} ex \u{b7} {} rule{}", |
| 1167 |
layer.exemplars, |
| 1168 |
layer.rules, |
| 1169 |
plural(layer.rules) |
| 1170 |
), |
| 1171 |
enabled: switched(layer.enabled), |
| 1172 |
weight: format!("{:.2}", layer.weight), |
| 1173 |
}) |
| 1174 |
.collect(), |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
declare! { |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
shape screen(classify: &Classifying) -> Screen; |
| 1184 |
|
| 1185 |
screen sidebar_content "Tag classifier" { |
| 1186 |
region BODY as Pane { |
| 1187 |
page "Tag classifier"; |
| 1188 |
for doing in classify.doing.iter() { |
| 1189 |
underway doing; |
| 1190 |
} |
| 1191 |
for failed in classify.failed.iter() { |
| 1192 |
banner Tone::Warning failed; |
| 1193 |
} |
| 1194 |
extend rules(&classify.rules, classify.busy); |
| 1195 |
extend autotag(&classify.autotag, classify.busy); |
| 1196 |
extend clusters(&classify.clusters, classify.busy); |
| 1197 |
extend folders(&classify.folders); |
| 1198 |
extend sharing(&classify.sharing, classify.busy); |
| 1199 |
} |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
declare! { |
| 1204 |
|
| 1205 |
shape rules(rules: &Rules, busy: bool) -> Vec<Node>; |
| 1206 |
|
| 1207 |
section "Tag Rules"; |
| 1208 |
text "Deterministic rules that auto-apply tags by sample metadata and audio \ |
| 1209 |
features. Rules never remove tags you added by hand."; |
| 1210 |
for listing in rules.listing.iter() { |
| 1211 |
extend rule_list(listing, busy); |
| 1212 |
} |
| 1213 |
for editor in rules.editor.iter() { |
| 1214 |
extend rule_editor(editor); |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
declare! { |
| 1219 |
|
| 1220 |
shape rule_list(listing: &RuleList, busy: bool) -> Vec<Node>; |
| 1221 |
|
| 1222 |
act "New rule" to post "/classifier/rules/new" { |
| 1223 |
disabled when busy; |
| 1224 |
} |
| 1225 |
text "Re-evaluate every rule across the whole library."; |
| 1226 |
act "Apply rules now" to post "/classifier/rules/apply" { |
| 1227 |
disabled when busy; |
| 1228 |
} |
| 1229 |
text "Starter rules cover the common instrument and format words (Kick.wav \ |
| 1230 |
becomes instrument.drum.kick). They arrive disabled: review them, \ |
| 1231 |
then enable the ones you want."; |
| 1232 |
act "Add starter rules" to post "/classifier/rules/starter" { |
| 1233 |
disabled when busy; |
| 1234 |
} |
| 1235 |
for last in listing.last.iter() { |
| 1236 |
text last; |
| 1237 |
} |
| 1238 |
|
| 1239 |
empty "No rules yet. New rules start empty; you decide what gets tagged." |
| 1240 |
when listing.bare; |
| 1241 |
list { |
| 1242 |
for rule in listing.each.iter() { |
| 1243 |
row &rule.name { |
| 1244 |
meta &rule.meta; |
| 1245 |
toggling rule.enabled Action::post("/classifier/rules/{rule.id}/enabled"); |
| 1246 |
token Tag::badge("off") unless rule.enabled; |
| 1247 |
act "Edit" to post "/classifier/rules/{rule.id}/edit"; |
| 1248 |
act "Earlier" to post "/classifier/rules/{rule.id}/up" { |
| 1249 |
disabled when rule.first; |
| 1250 |
} |
| 1251 |
act "Later" to post "/classifier/rules/{rule.id}/down" { |
| 1252 |
disabled when rule.last; |
| 1253 |
} |
| 1254 |
act "Delete" to post "/classifier/rules/{rule.id}/delete" { |
| 1255 |
tone Danger; |
| 1256 |
confirm &rule.confirm; |
| 1257 |
} |
| 1258 |
} |
| 1259 |
} |
| 1260 |
} unless listing.bare; |
| 1261 |
} |
| 1262 |
|
| 1263 |
declare! { |
| 1264 |
|
| 1265 |
shape rule_editor(editor: &Editor) -> Vec<Node>; |
| 1266 |
|
| 1267 |
field Text "name" "Name" { |
| 1268 |
required; |
| 1269 |
placeholder "Kick drums"; |
| 1270 |
value &editor.name; |
| 1271 |
writes Action::post("/classifier/draft/name"); |
| 1272 |
for &problem in editor.problem.iter() { |
| 1273 |
error problem; |
| 1274 |
} |
| 1275 |
} |
| 1276 |
field Checkbox "enabled" "Enabled" { |
| 1277 |
value editor.enabled; |
| 1278 |
writes Action::post("/classifier/draft/enabled"); |
| 1279 |
} |
| 1280 |
field Radio "match" "Match" { |
| 1281 |
option Choice::new("all", "All of these conditions"); |
| 1282 |
option Choice::new("any", "Any of these conditions"); |
| 1283 |
value editor.matching; |
| 1284 |
writes Action::post("/classifier/draft/match"); |
| 1285 |
} |
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
section "When"; |
| 1296 |
region "conditions" as Group { |
| 1297 |
repeats "Condition" adds "Add condition" |
| 1298 |
to post "/classifier/draft/conditions/add" { |
| 1299 |
least 1; |
| 1300 |
} |
| 1301 |
for held in editor.conditions.iter() { |
| 1302 |
include condition(held); |
| 1303 |
} |
| 1304 |
} |
| 1305 |
|
| 1306 |
section "Then"; |
| 1307 |
region "actions" as Group { |
| 1308 |
repeats "Action" adds "Add action" to post "/classifier/draft/actions/add"; |
| 1309 |
for held in editor.actions.iter() { |
| 1310 |
include doing(held); |
| 1311 |
} |
| 1312 |
} |
| 1313 |
|
| 1314 |
text "Count how many samples match, without writing."; |
| 1315 |
act "Test" to post "/classifier/draft/test"; |
| 1316 |
for matched in editor.matched.iter() { |
| 1317 |
text matched; |
| 1318 |
} |
| 1319 |
act "Save" to post "/classifier/draft/save"; |
| 1320 |
act "Cancel" to post "/classifier/draft/cancel" { |
| 1321 |
key "esc"; |
| 1322 |
} |
| 1323 |
} |
| 1324 |
|
| 1325 |
declare! { |
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
shape condition(condition: &Condition) -> Node; |
| 1333 |
|
| 1334 |
region "condition-{condition.at}" as Group { |
| 1335 |
field Select "field" "Test" { |
| 1336 |
options condition.fields.clone(); |
| 1337 |
value &condition.field; |
| 1338 |
writes Action::post("/classifier/draft/conditions/{condition.at}/set/field"); |
| 1339 |
} |
| 1340 |
field Select "op" "Comparison" { |
| 1341 |
options condition.ops.clone(); |
| 1342 |
value &condition.op; |
| 1343 |
writes Action::post("/classifier/draft/conditions/{condition.at}/set/op"); |
| 1344 |
} |
| 1345 |
for operand in condition.value.iter() { |
| 1346 |
field Text "value" "Value" { |
| 1347 |
value operand; |
| 1348 |
writes Action::post("/classifier/draft/conditions/{condition.at}/set/value"); |
| 1349 |
} |
| 1350 |
} |
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
removes "Remove condition" |
| 1355 |
to post "/classifier/draft/conditions/{condition.at}/remove"; |
| 1356 |
} |
| 1357 |
} |
| 1358 |
|
| 1359 |
declare! { |
| 1360 |
|
| 1361 |
shape doing(does: &Doing) -> Node; |
| 1362 |
|
| 1363 |
region "action-{does.at}" as Group { |
| 1364 |
field Select "kind" "Do" { |
| 1365 |
option Choice::new("add", "Add tag"); |
| 1366 |
option Choice::new("remove", "Remove tag"); |
| 1367 |
option Choice::new("stop", "Stop"); |
| 1368 |
value &does.kind; |
| 1369 |
writes Action::post("/classifier/draft/actions/{does.at}/set/kind"); |
| 1370 |
} |
| 1371 |
for tag in does.tag.iter() { |
| 1372 |
field Text "tag" "Tag" { |
| 1373 |
placeholder "instrument.drum.kick"; |
| 1374 |
value tag; |
| 1375 |
writes Action::post("/classifier/draft/actions/{does.at}/set/tag"); |
| 1376 |
} |
| 1377 |
} |
| 1378 |
removes "Remove action" to post "/classifier/draft/actions/{does.at}/remove"; |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
declare! { |
| 1383 |
|
| 1384 |
shape autotag(auto: &Autotag, busy: bool) -> Vec<Node>; |
| 1385 |
|
| 1386 |
section "Auto-Tagging"; |
| 1387 |
text "Suggests tags from samples that look like ones you've already tagged. \ |
| 1388 |
Tags above a per-tag threshold are applied automatically."; |
| 1389 |
act "Suggest tags across library" to post "/classifier/autotag/suggest" { |
| 1390 |
disabled when busy; |
| 1391 |
} |
| 1392 |
act "Undo auto-tagging" to post "/classifier/undo/ml" { |
| 1393 |
confirm "Remove every tag auto-tagging applied?"; |
| 1394 |
disabled when busy; |
| 1395 |
} |
| 1396 |
for last in auto.last.iter() { |
| 1397 |
text last; |
| 1398 |
} |
| 1399 |
|
| 1400 |
section "Review suggestions"; |
| 1401 |
text "Collects what auto-tagging would apply, without applying any of it, \ |
| 1402 |
then opens a screen to accept it a tag at a time."; |
| 1403 |
act "Review suggestions" to post "/classifier/autotag/review" { |
| 1404 |
disabled when busy; |
| 1405 |
} |
| 1406 |
for reopen in auto.reopen.iter() { |
| 1407 |
act reopen to post "/classifier/autotag/reopen" { |
| 1408 |
disabled when busy; |
| 1409 |
} |
| 1410 |
} |
| 1411 |
for reviewed in auto.reviewed.iter() { |
| 1412 |
text reviewed; |
| 1413 |
} |
| 1414 |
|
| 1415 |
extend trained(&auto.head, busy); |
| 1416 |
extend thresholds(&auto.thresholds); |
| 1417 |
} |
| 1418 |
|
| 1419 |
declare! { |
| 1420 |
|
| 1421 |
shape trained(head: &Head, busy: bool) -> Vec<Node>; |
| 1422 |
|
| 1423 |
section "Trained model"; |
| 1424 |
text "For large libraries, distil your tagged samples into a compact model \ |
| 1425 |
so auto-tagging runs much faster. Optional: auto-tagging works \ |
| 1426 |
without it."; |
| 1427 |
for said in head.said.iter() { |
| 1428 |
text said; |
| 1429 |
} |
| 1430 |
act head.train to post "/classifier/head/train" { |
| 1431 |
disabled when busy; |
| 1432 |
} |
| 1433 |
act "Clear model" to post "/classifier/head/clear" when head.trained { |
| 1434 |
confirm "Remove the model? Auto-tagging reverts to nearest-neighbor \ |
| 1435 |
matching."; |
| 1436 |
disabled when busy; |
| 1437 |
} |
| 1438 |
} |
| 1439 |
|
| 1440 |
declare! { |
| 1441 |
|
| 1442 |
shape thresholds(thresholds: &Thresholds) -> Vec<Node>; |
| 1443 |
|
| 1444 |
section "Per-tag thresholds"; |
| 1445 |
text "review = surface for review \u{b7} auto = apply automatically"; |
| 1446 |
for held in thresholds.each.iter() { |
| 1447 |
include threshold(held); |
| 1448 |
} |
| 1449 |
field Text "tag" "Tag to configure" { |
| 1450 |
value &thresholds.typing; |
| 1451 |
writes Action::post("/classifier/policies/typing"); |
| 1452 |
} |
| 1453 |
act "Add threshold" to post "/classifier/policies/add"; |
| 1454 |
} |
| 1455 |
|
| 1456 |
declare! { |
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
shape threshold(policy: &Threshold) -> Node; |
| 1463 |
|
| 1464 |
region "policy-{policy.tag}" as Group { |
| 1465 |
text &policy.tag; |
| 1466 |
field Range "review" "review" { |
| 1467 |
within "0" "1"; |
| 1468 |
step "0.01"; |
| 1469 |
value &policy.review; |
| 1470 |
writes Action::post("/classifier/policies/{policy.tag}/thresholds") |
| 1471 |
.with("review", &policy.review); |
| 1472 |
} |
| 1473 |
field Range "auto" "auto" { |
| 1474 |
within "0" "1"; |
| 1475 |
step "0.01"; |
| 1476 |
value &policy.auto; |
| 1477 |
writes Action::post("/classifier/policies/{policy.tag}/thresholds") |
| 1478 |
.with("auto", &policy.auto); |
| 1479 |
} |
| 1480 |
} |
| 1481 |
} |
| 1482 |
|
| 1483 |
declare! { |
| 1484 |
|
| 1485 |
shape clusters(clusters: &Clusters, busy: bool) -> Vec<Node>; |
| 1486 |
|
| 1487 |
section "Clustering"; |
| 1488 |
text "Groups similar samples so you can name them and seed your first tags. \ |
| 1489 |
Useful when nothing is tagged yet."; |
| 1490 |
|
| 1491 |
|
| 1492 |
field Range "cluster_k" "Groups" { |
| 1493 |
within "2" "24"; |
| 1494 |
step "1"; |
| 1495 |
value &clusters.k; |
| 1496 |
writes Action::post("/classifier/clusters/k"); |
| 1497 |
} |
| 1498 |
act "Find clusters" to post "/classifier/clusters/find" { |
| 1499 |
disabled when busy; |
| 1500 |
} |
| 1501 |
for held in clusters.piles.iter() { |
| 1502 |
include pile(held); |
| 1503 |
} |
| 1504 |
act "Remove all cluster tags" to post "/classifier/undo/cluster" unless clusters.piles.is_empty() { |
| 1505 |
confirm "Remove every tag applied from clustering?"; |
| 1506 |
} |
| 1507 |
} |
| 1508 |
|
| 1509 |
declare! { |
| 1510 |
|
| 1511 |
shape pile(pile: &Pile) -> Node; |
| 1512 |
|
| 1513 |
region "cluster-{pile.at}" as Group { |
| 1514 |
text &pile.said; |
| 1515 |
field Text "name" "Name this group" { |
| 1516 |
value &pile.name; |
| 1517 |
writes Action::post("/classifier/clusters/{pile.at}/name"); |
| 1518 |
} |
| 1519 |
act "Play" to post "/classifier/clusters/{pile.at}/play" { |
| 1520 |
disabled unless pile.playable; |
| 1521 |
} |
| 1522 |
act "Tag" to post "/classifier/clusters/{pile.at}/tag" { |
| 1523 |
disabled unless pile.named; |
| 1524 |
} |
| 1525 |
} |
| 1526 |
} |
| 1527 |
|
| 1528 |
declare! { |
| 1529 |
|
| 1530 |
shape folders(folders: &Folders) -> Vec<Node>; |
| 1531 |
|
| 1532 |
section "Folder Tags"; |
| 1533 |
text "Turns folders that directly contain samples into tags. Useful when \ |
| 1534 |
your library is already organized into folders."; |
| 1535 |
act "Scan folders" to post "/classifier/folders/scan"; |
| 1536 |
act "Undo folder tags" to post "/classifier/undo/harvest" { |
| 1537 |
confirm "Remove every tag applied from folders?"; |
| 1538 |
} |
| 1539 |
empty "No folders with samples found." when folders.bare; |
| 1540 |
for held in folders.each.iter() { |
| 1541 |
include harvested(held); |
| 1542 |
} |
| 1543 |
} |
| 1544 |
|
| 1545 |
declare! { |
| 1546 |
|
| 1547 |
shape harvested(folder: &Harvested) -> Node; |
| 1548 |
|
| 1549 |
region "folder-{folder.at}" as Group { |
| 1550 |
text &folder.said; |
| 1551 |
field Text "tag" "Tag" { |
| 1552 |
value &folder.tag; |
| 1553 |
writes Action::post("/classifier/folders/{folder.at}/tag"); |
| 1554 |
} |
| 1555 |
act "Apply" to post "/classifier/folders/{folder.at}/apply" { |
| 1556 |
disabled unless folder.named; |
| 1557 |
} |
| 1558 |
} |
| 1559 |
} |
| 1560 |
|
| 1561 |
declare! { |
| 1562 |
|
| 1563 |
shape sharing(shared: &Shared, busy: bool) -> Vec<Node>; |
| 1564 |
|
| 1565 |
section "Shared Classifiers (.afcl)"; |
| 1566 |
text "Share your tagging as a portable .afcl file. It carries your feature \ |
| 1567 |
vectors, tags, rules, and thresholds, but never any audio. Imported \ |
| 1568 |
files become removable layers that sit below your own tagging."; |
| 1569 |
field Text "export_name" "Name" { |
| 1570 |
placeholder "My classifier"; |
| 1571 |
value &shared.name; |
| 1572 |
writes Action::post("/classifier/sharing/name"); |
| 1573 |
} |
| 1574 |
for part in shared.parts.iter() { |
| 1575 |
field Checkbox part.name part.label { |
| 1576 |
value part.value; |
| 1577 |
writes Action::post("/classifier/sharing/include/{part.name}"); |
| 1578 |
for &said in part.hint.iter() { |
| 1579 |
hint said; |
| 1580 |
} |
| 1581 |
} |
| 1582 |
} |
| 1583 |
|
| 1584 |
act "Export to file..." to post "/classifier/sharing/export" { |
| 1585 |
disabled when busy or not shared.worth; |
| 1586 |
} |
| 1587 |
text "Nothing to export yet. Tag samples or add rules first." unless shared.worth; |
| 1588 |
for exported in shared.exported.iter() { |
| 1589 |
text exported; |
| 1590 |
} |
| 1591 |
act "Import .afcl file..." to post "/classifier/sharing/import" { |
| 1592 |
disabled when busy; |
| 1593 |
} |
| 1594 |
for imported in shared.imported.iter() { |
| 1595 |
text imported; |
| 1596 |
} |
| 1597 |
|
| 1598 |
section "Imported layers" unless shared.layers.is_empty(); |
| 1599 |
text "Imported rules arrive disabled. Review them in Tag Rules above before \ |
| 1600 |
enabling. Weight sets how much a layer counts next to your own \ |
| 1601 |
tagging." unless shared.layers.is_empty(); |
| 1602 |
for held in shared.layers.iter() { |
| 1603 |
include layer(held); |
| 1604 |
} |
| 1605 |
} |
| 1606 |
|
| 1607 |
declare! { |
| 1608 |
|
| 1609 |
shape layer(layer: &Layer) -> Node; |
| 1610 |
|
| 1611 |
region "layer-{layer.id}" as Group { |
| 1612 |
text &layer.name; |
| 1613 |
text &layer.said; |
| 1614 |
field Checkbox "on" "Use when auto-tagging" { |
| 1615 |
value layer.enabled; |
| 1616 |
writes Action::post("/classifier/layers/{layer.id}/enabled"); |
| 1617 |
} |
| 1618 |
field Range "weight" "Weight" { |
| 1619 |
within "0" "1"; |
| 1620 |
step "0.01"; |
| 1621 |
hint "Your own tags always count 1.0; lower means weaker."; |
| 1622 |
value &layer.weight; |
| 1623 |
writes Action::post("/classifier/layers/{layer.id}/weight"); |
| 1624 |
} |
| 1625 |
act "Remove layer" to post "/classifier/layers/{layer.id}/remove" { |
| 1626 |
tone Danger; |
| 1627 |
confirm DROPS_LAYER; |
| 1628 |
} |
| 1629 |
} |
| 1630 |
} |
| 1631 |
|
| 1632 |
|
| 1633 |
const fn switched(on: bool) -> &'static str { |
| 1634 |
if on { "on" } else { "" } |
| 1635 |
} |
| 1636 |
|
| 1637 |
|
| 1638 |
const fn plural(count: usize) -> &'static str { |
| 1639 |
if count == 1 { "" } else { "s" } |
| 1640 |
} |
| 1641 |
|