1
- use anyhow:: { Context , Error , Result } ;
2
1
use candle_core:: Tensor ;
3
2
use hf_hub:: api:: sync:: { Api , ApiRepo } ;
4
3
use hf_hub:: { Repo , RepoType } ;
@@ -10,7 +9,7 @@ use crate::model::embedder::{
10
9
encode_batch, encode_batch_with_usage, load_pretrained_model, EmbedderModel ,
11
10
} ;
12
11
use crate :: model:: utils;
13
- use crate :: Usage ;
12
+ use crate :: { Error , Result , Usage } ;
14
13
15
14
#[ cfg( test) ]
16
15
use crate :: model:: embedder:: { load_zeros_model, parse_config} ;
@@ -57,8 +56,9 @@ impl SentenceTransformer {
57
56
///
58
57
/// ```rust
59
58
/// # use glowrs::SentenceTransformer;
59
+ /// # use std::error::Error;
60
60
///
61
- /// # fn main() -> anyhow:: Result<()> {
61
+ /// # fn main() -> Result<(), Box<dyn Error> > {
62
62
/// let encoder = SentenceTransformer::from_repo_string("sentence-transformers/all-MiniLM-L6-v2")?;
63
63
///
64
64
/// # Ok(())
@@ -84,26 +84,19 @@ impl SentenceTransformer {
84
84
}
85
85
86
86
pub fn from_api ( api : ApiRepo ) -> Result < Self > {
87
- let model_path = api
88
- . get ( "model.safetensors" )
89
- . context ( "Model repository is not available or doesn't contain `model.safetensors`." ) ?;
87
+ let model_path = api. get ( "model.safetensors" ) ?;
90
88
91
- let config_path = api
92
- . get ( "config.json" )
93
- . context ( "Model repository doesn't contain `config.json`." ) ?;
89
+ let config_path = api. get ( "config.json" ) ?;
94
90
95
- let tokenizer_path = api
96
- . get ( "tokenizer.json" )
97
- . context ( "Model repository doesn't contain `tokenizer.json`." ) ?;
91
+ let tokenizer_path = api. get ( "tokenizer.json" ) ?;
98
92
99
93
Self :: from_path ( & model_path, & config_path, & tokenizer_path)
100
94
}
101
95
102
96
pub fn from_path ( model_path : & Path , config_path : & Path , tokenizer_path : & Path ) -> Result < Self > {
103
- let tokenizer = Tokenizer :: from_file ( tokenizer_path) . map_err ( Error :: msg ) ?;
97
+ let tokenizer = Tokenizer :: from_file ( tokenizer_path) ?;
104
98
105
- let model = load_pretrained_model ( model_path, config_path)
106
- . context ( "Something went wrong while loading the model." ) ?;
99
+ let model = load_pretrained_model ( model_path, config_path) ?;
107
100
108
101
Ok ( Self :: new ( model, tokenizer) )
109
102
}
@@ -119,7 +112,9 @@ impl SentenceTransformer {
119
112
/// use glowrs::SentenceTransformer;
120
113
/// use std::path::Path;
121
114
///
122
- /// # fn main() -> anyhow::Result<()> {
115
+ /// # type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
116
+ ///
117
+ /// # fn main() -> Result<()> {
123
118
/// let path = Path::new("path/to/folder");
124
119
///
125
120
/// let encoder = SentenceTransformer::from_folder(path)?;
@@ -133,7 +128,9 @@ impl SentenceTransformer {
133
128
let tokenizer_path = folder_path. join ( "tokenizer.json" ) ;
134
129
135
130
if !model_path. exists ( ) || !config_path. exists ( ) || !tokenizer_path. exists ( ) {
136
- Err ( anyhow:: anyhow!( "model.safetensors, config.json, or tokenizer.json does not exist in the given directory" ) )
131
+ Err ( Error :: ModelLoad (
132
+ "model.safetensors, config.json, or tokenizer.json does not exist in the given directory"
133
+ ) )
137
134
} else {
138
135
Self :: from_path ( & model_path, & config_path, & tokenizer_path)
139
136
}
@@ -147,7 +144,9 @@ impl SentenceTransformer {
147
144
/// # use glowrs::SentenceTransformer;
148
145
/// # use glowrs::PoolingStrategy;
149
146
///
150
- /// # fn main() -> anyhow::Result<()> {
147
+ /// # type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
148
+ ///
149
+ /// # fn main() -> Result<()> {
151
150
/// let encoder = SentenceTransformer::from_repo_string("sentence-transformers/all-MiniLM-L6-v2")?
152
151
/// .with_pooling_strategy(PoolingStrategy::Sum);
153
152
///
@@ -161,7 +160,7 @@ impl SentenceTransformer {
161
160
162
161
#[ cfg( test) ]
163
162
pub ( crate ) fn test_from_config_json ( config_path : & Path , tokenizer_path : & Path ) -> Result < Self > {
164
- let tokenizer = Tokenizer :: from_file ( tokenizer_path) . map_err ( Error :: msg ) ?;
163
+ let tokenizer = Tokenizer :: from_file ( tokenizer_path) ?;
165
164
166
165
let config_str = std:: fs:: read_to_string ( config_path) ?;
167
166
0 commit comments