super-voice-daemon/src/tts/download_progress.rs
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-3.0-only |
2 | | //! What a model load reports as it provisions the model's files: |
3 | | //! `super_engine_daemon::download_progress`. Text to speech has one load at a |
4 | | //! time, so its slot has nothing to tell loads apart by; it only says whose |
5 | | //! load it is. |
6 | | |
7 | | use super_engine_daemon::download_progress as engine; |
8 | | use super_voice_shared::tts::models::protocol::DownloadProgress; |
9 | | |
10 | | pub use super_engine_daemon::download_progress::{Progress, status}; |
11 | | |
12 | | /// The synthesis stage's load. Serializes as `{"feature": "tts", "stage": 1}`: |
13 | | /// speech to text numbers its stages from 1 too, and both report on the one |
14 | | /// `download_progress` topic. |
15 | | #[derive(Clone, Copy, Debug, Default)] |
16 | | pub struct SynthesisSlot; |
17 | | |
18 | | impl serde::Serialize for SynthesisSlot { |
19 | 1 | fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { |
20 | | use serde::ser::SerializeStruct; |
21 | 1 | let mut slot = serializer.serialize_struct("SynthesisSlot", 2)?0 ; |
22 | 1 | slot.serialize_field("feature", "tts")?0 ; |
23 | 1 | slot.serialize_field("stage", &1)?0 ; |
24 | 1 | slot.end() |
25 | 1 | } |
26 | | } |
27 | | |
28 | | impl engine::Slot for SynthesisSlot { |
29 | | type Key = (); |
30 | | |
31 | 1 | fn key(&self) {} |
32 | | |
33 | 0 | fn describe((): ()) -> Option<String> { |
34 | 0 | None |
35 | 0 | } |
36 | | } |
37 | | |
38 | | /// Progress of one model load. See |
39 | | /// `super_engine_daemon::download_progress::DownloadProgressTracker`. |
40 | | pub type DownloadProgressTracker = engine::DownloadProgressTracker<SynthesisSlot>; |
41 | | |
42 | | /// The one load in flight. See |
43 | | /// `super_engine_daemon::download_progress::DownloadStateManager`. |
44 | | pub type DownloadStateManager = engine::DownloadStateManager<SynthesisSlot>; |
45 | | |
46 | | /// A load's progress as the wire carries it. |
47 | | #[must_use] |
48 | 2 | pub fn report(progress: Progress<SynthesisSlot>) -> DownloadProgress { |
49 | 2 | DownloadProgress { |
50 | 2 | model_name: progress.model_name, |
51 | 2 | current_file: progress.current_file, |
52 | 2 | file_index: progress.file_index, |
53 | 2 | total_files: progress.total_files, |
54 | 2 | bytes_downloaded: progress.bytes_downloaded, |
55 | 2 | total_bytes: progress.total_bytes, |
56 | 2 | percentage: progress.percentage, |
57 | 2 | status: progress.status, |
58 | 2 | started_at: progress.started_at, |
59 | 2 | eta_seconds: progress.eta_seconds, |
60 | 2 | error: progress.error, |
61 | 2 | load: progress.load, |
62 | 2 | } |
63 | 2 | } |
64 | | |
65 | | #[cfg(test)] |
66 | | mod tests { |
67 | | use std::sync::Arc; |
68 | | use std::sync::atomic::AtomicBool; |
69 | | |
70 | | use super::*; |
71 | | |
72 | | /// The event the engine publishes reads back as the wire type a client |
73 | | /// parses, and carries what the typed report does. |
74 | | #[test] |
75 | 1 | fn the_published_event_is_the_wire_type() { |
76 | 1 | let tracker = DownloadProgressTracker::new( |
77 | 1 | "kokoro-82m".to_string(), |
78 | 1 | crate::tts::download_progress::SynthesisSlot, |
79 | 1 | 2, |
80 | 1 | Arc::new(AtomicBool::new(false)), |
81 | 1 | ); |
82 | 1 | tracker.mark_error("disk full"); |
83 | | |
84 | 1 | let wire = report(tracker.get_progress()); |
85 | 1 | assert_eq!(wire.model_name, "kokoro-82m"); |
86 | 1 | assert_eq!(wire.total_files, 2); |
87 | 1 | assert_eq!(wire.error.as_deref(), Some("disk full")); |
88 | | |
89 | 1 | let event = serde_json::to_value(tracker.get_progress()).unwrap(); |
90 | | let parsed: DownloadProgress = |
91 | 1 | serde_json::from_value(event).expect("the published event is the wire type"); |
92 | 1 | assert_eq!(parsed.model_name, wire.model_name); |
93 | 1 | assert_eq!(parsed.status, "error"); |
94 | 1 | assert_eq!(parsed.error, wire.error); |
95 | 1 | } |
96 | | } |