use super::task::{Task, TaskType};
use crate::core::OCRError;
use std::fmt::Debug;
#[derive(Debug, Clone)]
pub struct AdapterInfo {
pub model_name: String,
pub task_type: TaskType,
pub description: String,
}
impl AdapterInfo {
pub fn new(
model_name: impl Into<String>,
task_type: TaskType,
description: impl Into<String>,
) -> Self {
Self {
model_name: model_name.into(),
task_type,
description: description.into(),
}
}
}
pub trait ModelAdapter: Send + Sync + Debug {
type Task: Task;
fn info(&self) -> AdapterInfo;
fn execute(
&self,
input: <Self::Task as Task>::Input,
config: Option<&<Self::Task as Task>::Config>,
) -> Result<<Self::Task as Task>::Output, OCRError>;
fn supports_batching(&self) -> bool {
true }
fn recommended_batch_size(&self) -> usize {
6 }
}
pub trait AdapterBuilder: Sized {
type Config: Send + Sync + Debug + Clone;
type Adapter: ModelAdapter;
fn build(
self,
model_source: impl Into<crate::core::inference::ModelSource>,
) -> Result<Self::Adapter, OCRError>;
fn with_config(self, config: Self::Config) -> Self;
fn adapter_type(&self) -> &str;
}
pub trait OrtConfigurable: Sized {
fn with_ort_config(self, config: crate::core::config::OrtSessionConfig) -> Self;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_adapter_info_creation() {
let info = AdapterInfo::new(
"DB",
TaskType::TextDetection,
"Differentiable Binarization text detector",
);
assert_eq!(info.model_name, "DB");
assert_eq!(info.task_type, TaskType::TextDetection);
}
}