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
//!
//! Request response structures for Orkhon

use smallvec::SmallVec;
use std::sync::Arc;

use tract_core::prelude::*;

pub enum Types {
    PyModel,
    TFModel,
}

/// Orkhon request container
#[derive(Debug, PartialEq, PartialOrd, Clone)]
pub struct ORequest<T> {
    pub body: T,
}

impl<T> ORequest<T> {
    /// Orkhon request container that takes backend specific response
    pub fn with_body(body: T) -> Self {
        ORequest { body }
    }
}

/// Orkhon response container
#[derive(Debug, PartialEq, PartialOrd, Clone)]
pub struct OResponse<T> {
    pub body: T,
}

impl<T> OResponse<T> {
    /// Orkhon response container that takes backend specific response
    pub fn with_body(body: T) -> Self {
        OResponse { body }
    }
}

pub(crate) trait ORequestBase<T> {}
pub(crate) trait OResponseBase<T> {}

impl<T> ORequestBase<T> for T {}
impl<T> OResponseBase<T> for T {}

cfg_if::cfg_if! {
    if #[cfg(feature = "pymodel")] {
        use pyo3::ToPyObject;

        #[derive(Default, Debug, Clone)]
        pub struct PyModelRequest<K, V, T>
        where
            K: hash::Hash + cmp::Eq + Default + ToPyObject,
            V: Default + ToPyObject,
            T: Default + ToPyObject,
        {
            pub args: HashMap<K, V>,
            pub kwargs: HashMap<&'static str, T>,
        }

        impl<K, V, T> PyModelRequest<K, V, T>
        where
            K: hash::Hash + cmp::Eq + Default + ToPyObject,
            V: Default + ToPyObject,
            T: Default + ToPyObject,
        {
            pub fn new() -> Self {
                PyModelRequest {
                    ..Default::default()
                }
            }

            pub fn with_args(mut self, args: HashMap<K, V>) -> Self {
                self.args = args;
                self
            }

            pub fn with_kwargs(mut self, kwargs: HashMap<&'static str, T>) -> Self {
                self.kwargs = kwargs;
                self
            }
        }

        #[derive(Default, Debug, Clone)]
        pub struct PyModelResponse {
           response: PyDict,
        }

        impl PyModelResponse {
           pub fn new() -> Self { PyModelResponse { ..Default::default() } }
        }
    } else if #[cfg(feature = "onnxmodel")] {
        /// ONNX request
        #[derive(Default, Debug)]
        pub struct ONNXRequest {
            pub input: Tensor,
        }

        impl ONNXRequest {
            /// Creates a new ONNX inference request
            pub fn new() -> Self {
                ONNXRequest {
                    ..Default::default()
                }
            }

            /// Append body to the ONNX request
            pub fn body(mut self, request: Tensor) -> Self {
                self.input = request;
                self
            }
        }

        /// ONNX response
        #[derive(Default, Debug)]
        pub struct ONNXResponse {
            pub output: SmallVec<[Arc<Tensor>; 4]>,
        }

        impl ONNXResponse {
            /// Creates a new ONNX inference response
            pub fn new() -> Self {
                ONNXResponse {
                    ..Default::default()
                }
            }

            /// Give output coming out from ONNX
            pub fn with_output(mut self, output: SmallVec<[Arc<Tensor>; 4]>) -> Self {
                self.output = output;
                self
            }
        }
    }
}

/// Tensorflow request
#[derive(Default, Debug)]
pub struct TFRequest {
    pub input: Tensor,
}

impl TFRequest {
    /// Creates a new tensorflow inference request
    pub fn new() -> Self {
        TFRequest {
            ..Default::default()
        }
    }

    /// Append body to the tensorflow request
    pub fn body(mut self, request: Tensor) -> Self {
        self.input = request;
        self
    }
}

/// Tensorflow response
#[derive(Default, Debug)]
pub struct TFResponse {
    pub output: SmallVec<[Arc<Tensor>; 4]>,
}

impl TFResponse {
    /// Creates a new tensorflow inference response
    pub fn new() -> Self {
        TFResponse {
            ..Default::default()
        }
    }

    /// Give output coming out from tensorflow
    pub fn with_output(mut self, output: SmallVec<[Arc<Tensor>; 4]>) -> Self {
        self.output = output;
        self
    }
}