anno/backends/
deberta_v3.rs1use crate::{Entity, EntityType, Model, Result};
23
24#[cfg(feature = "onnx")]
25use crate::backends::onnx::BertNEROnnx;
26
27pub struct DeBERTaV3NER {
32 #[cfg(feature = "onnx")]
33 inner: BertNEROnnx,
34 #[allow(dead_code)] model_name: String,
37}
38
39impl DeBERTaV3NER {
40 pub fn new(model_name: &str) -> Result<Self> {
45 #[cfg(feature = "onnx")]
46 {
47 let inner = BertNEROnnx::new(model_name)?;
49 Ok(Self {
50 inner,
51 model_name: model_name.to_string(),
52 })
53 }
54 #[cfg(not(feature = "onnx"))]
55 {
56 Err(crate::Error::FeatureNotAvailable(
57 "DeBERTa-v3 NER requires 'onnx' feature".to_string(),
58 ))
59 }
60 }
61}
62
63impl Model for DeBERTaV3NER {
64 fn extract_entities(&self, text: &str, language: Option<&str>) -> Result<Vec<Entity>> {
65 #[cfg(feature = "onnx")]
66 {
67 self.inner.extract_entities(text, language)
68 }
69 #[cfg(not(feature = "onnx"))]
70 {
71 Err(crate::Error::FeatureNotAvailable(
72 "DeBERTa-v3 NER requires 'onnx' feature".to_string(),
73 ))
74 }
75 }
76
77 fn supported_types(&self) -> Vec<EntityType> {
78 vec![
79 EntityType::Person,
80 EntityType::Organization,
81 EntityType::Location,
82 ]
83 }
84
85 fn is_available(&self) -> bool {
86 #[cfg(feature = "onnx")]
87 {
88 self.inner.is_available()
89 }
90 #[cfg(not(feature = "onnx"))]
91 {
92 false
93 }
94 }
95
96 fn name(&self) -> &'static str {
97 "deberta_v3"
98 }
99
100 fn description(&self) -> &'static str {
101 "DeBERTa-v3 NER with disentangled attention (wraps BERT ONNX backend)"
102 }
103}
104
105impl crate::BatchCapable for DeBERTaV3NER {
106 fn extract_entities_batch(
107 &self,
108 texts: &[&str],
109 language: Option<&str>,
110 ) -> Result<Vec<Vec<Entity>>> {
111 #[cfg(feature = "onnx")]
112 {
113 self.inner.extract_entities_batch(texts, language)
114 }
115 #[cfg(not(feature = "onnx"))]
116 {
117 Err(crate::Error::FeatureNotAvailable(
118 "DeBERTa-v3 NER requires 'onnx' feature".to_string(),
119 ))
120 }
121 }
122}
123
124impl crate::StreamingCapable for DeBERTaV3NER {
125 fn extract_entities_streaming(&self, chunk: &str, offset: usize) -> Result<Vec<Entity>> {
126 #[cfg(feature = "onnx")]
127 {
128 self.inner.extract_entities_streaming(chunk, offset)
129 }
130 #[cfg(not(feature = "onnx"))]
131 {
132 Err(crate::Error::FeatureNotAvailable(
133 "DeBERTa-v3 NER requires 'onnx' feature".to_string(),
134 ))
135 }
136 }
137}
138
139#[cfg(test)]
140mod tests {
141 use super::*;
142
143 #[test]
144 fn test_deberta_v3_name() {
145 if let Ok(model) = DeBERTaV3NER::new("microsoft/deberta-v3-base") {
146 assert_eq!(model.name(), "deberta_v3");
147 }
148 }
150}