use crate::model_resolver::ModelId;
use crate::RuntimeBackend;
use latexsnipper_foundation::Result;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ModelTask {
FormulaDetection,
FormulaRecognition,
TextDetection,
TextRecognition,
TableDetection,
TableStructure,
LayoutAnalysis,
HandwritingRecognition,
VisionLanguageRecognition,
DocumentUnderstanding,
FormulaCorrection,
TextCorrection,
TableSemanticParsing,
DiagramUnderstanding,
ChartUnderstanding,
ReadingOrderAnalysis,
StyleClassification,
}
#[derive(Debug, Clone)]
pub struct TensorSpec {
pub name: String,
pub shape: Vec<usize>,
pub dtype: TensorDtype,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TensorDtype {
Float32,
Int64,
Int32,
UInt8,
}
#[derive(Debug, Clone)]
pub struct ModelDescriptor {
pub id: ModelId,
pub task: ModelTask,
pub version: String,
pub input_spec: TensorSpec,
pub output_spec: Vec<TensorSpec>,
pub artifact_paths: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct ModelInput {
pub name: String,
pub data: Vec<u8>,
pub shape: Vec<usize>,
pub dtype: TensorDtype,
}
#[derive(Debug, Clone)]
pub enum ModelOutput {
Detections(Vec<DetectionResult>),
Text(Vec<TextResult>),
Formula(Vec<FormulaResult>),
Table(TableResult),
Layout(Vec<LayoutResult>),
Raw(Vec<Vec<f32>>),
}
#[derive(Debug, Clone, Copy)]
pub struct DetectionQuad {
pub x1: f32,
pub y1: f32,
pub x2: f32,
pub y2: f32,
pub x3: f32,
pub y3: f32,
pub x4: f32,
pub y4: f32,
}
#[derive(Debug, Clone)]
pub struct DetectionResult {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
pub quad: Option<DetectionQuad>,
pub confidence: f32,
pub class_id: usize,
pub class_name: String,
}
#[derive(Debug, Clone)]
pub struct TextResult {
pub text: String,
pub confidence: f32,
}
#[derive(Debug, Clone)]
pub struct FormulaResult {
pub latex: String,
pub confidence: f32,
}
#[derive(Debug, Clone)]
pub struct TableResult {
pub rows: Vec<TableRow>,
pub columns: Vec<TableColumn>,
pub cells: Vec<TableCell>,
}
#[derive(Debug, Clone)]
pub struct TableRow {
pub y_start: f32,
pub y_end: f32,
}
#[derive(Debug, Clone)]
pub struct TableColumn {
pub x_start: f32,
pub x_end: f32,
}
#[derive(Debug, Clone)]
pub struct TableCell {
pub row: usize,
pub col: usize,
pub rowspan: u32,
pub colspan: u32,
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
#[derive(Debug, Clone)]
pub struct LayoutResult {
pub region_type: String,
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
pub confidence: f32,
}
pub struct InferenceContext {
pub metadata: std::collections::HashMap<String, serde_json::Value>,
}
impl InferenceContext {
pub fn new() -> Self {
Self {
metadata: std::collections::HashMap::new(),
}
}
}
impl Default for InferenceContext {
fn default() -> Self {
Self::new()
}
}
pub trait ModelPackage: Send + Sync {
fn descriptor(&self) -> &ModelDescriptor;
fn create_executor(&self, runtime: Arc<dyn RuntimeBackend>) -> Result<Box<dyn ModelExecutor>>;
}
pub trait ModelExecutor: Send {
fn run(&mut self, input: ModelInput, ctx: &mut InferenceContext) -> Result<ModelOutput>;
fn descriptor(&self) -> &ModelDescriptor;
}