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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//! Model information and management operations for `HuggingFace` API.
mod private
{
use crate::
{
client::Client,
components::
{
models::ModelInfo,
// common::TaskType,
},
error::Result,
validation::validate_model_identifier,
};
#[ cfg( feature = "env-config" ) ]
use crate::environment::{ HuggingFaceEnvironment, EnvironmentInterface };
use serde::{ Deserialize, Serialize };
use core::time::Duration;
/// `HuggingFace` Hub API base URL — separate from the inference API base URL
const HF_HUB_API_BASE : &str = "https://huggingface.co/api";
/// Configuration for model waiting behavior
#[ derive( Debug, Clone ) ]
pub struct ModelWaitConfig
{
/// Polling interval between status checks
pub poll_interval : Duration,
}
impl ModelWaitConfig
{
/// Create explicit model wait configuration
#[ inline ]
#[ must_use ]
pub fn with_explicit_config( poll_interval : Duration ) -> Self
{
Self { poll_interval }
}
/// Create model wait configuration with HuggingFace-recommended values
///
/// # Governing Principle Compliance
///
/// This provides HuggingFace-recommended polling configuration without making it implicit.
/// Developers must explicitly choose to use these recommended values.
#[ inline ]
#[ must_use ]
pub fn recommended() -> Self
{
Self
{
poll_interval : Duration::from_secs( 5 ), // Balanced polling for model loading
}
}
/// Create conservative configuration for production environments
#[ inline ]
#[ must_use ]
pub fn conservative() -> Self
{
Self
{
poll_interval : Duration::from_secs( 10 ), // Longer intervals to reduce API load
}
}
/// Create aggressive configuration for development/testing
#[ inline ]
#[ must_use ]
pub fn aggressive() -> Self
{
Self
{
poll_interval : Duration::from_secs( 2 ), // Faster polling for development
}
}
}
/// API group for `HuggingFace` model operations
#[ derive( Debug ) ]
pub struct Models< E >
where
E : Clone,
{
client : Client< E >,
}
#[ cfg( feature = "env-config" ) ]
impl< E > Models< E >
where
E : HuggingFaceEnvironment + EnvironmentInterface + Send + Sync + 'static + Clone,
{
/// Create a new Models API group
#[ inline ]
#[ must_use ]
pub fn new( client : &Client< E > ) -> Self
{
Self
{
client : client.clone(),
}
}
/// Get information about a specific model
///
/// # Arguments
/// - `model_id`: Model identifier (e.g., "gpt2", "meta-llama/Llama-2-7b-hf")
///
/// # Errors
/// Returns error if the model is not found or request fails
#[ inline ]
pub async fn get( &self, model_id : impl AsRef< str > ) -> Result< ModelInfo >
{
let model_ref = model_id.as_ref();
// Validate model identifier
validate_model_identifier( model_ref )?;
let url = format!( "{HF_HUB_API_BASE}/models/{model_ref}" );
self.client.get( &url ).await
}
/// Check if a model is available for inference
///
/// # Arguments
/// - `model_id`: Model identifier to check
///
/// # Errors
/// Returns error if the availability check fails
#[ inline ]
pub async fn is_available( &self, model_id : impl AsRef< str > ) -> Result< bool >
{
let model_ref = model_id.as_ref();
// Validate model identifier
validate_model_identifier( model_ref )?;
// Use Hub API to check existence — inference endpoints are provider-specific
let url = format!( "{HF_HUB_API_BASE}/models/{model_ref}" );
match self.client.get::< serde_json::Value >( &url ).await
{
Ok( _ ) => Ok( true ),
Err( _ ) => Ok( false ),
}
}
/// Get model status information
///
/// # Arguments
/// - `model_id`: Model identifier
///
/// # Errors
/// Returns error if the status check fails
#[ inline ]
pub async fn status( &self, model_id : impl AsRef< str > ) -> Result< ModelStatus >
{
let model_ref = model_id.as_ref();
// Validate model identifier
validate_model_identifier( model_ref )?;
// Use Hub API to determine model status — inference endpoints are provider-specific
let url = format!( "{HF_HUB_API_BASE}/models/{model_ref}" );
match self.client.get::< serde_json::Value >( &url ).await
{
Ok( _ ) => Ok( ModelStatus::Available ),
Err( e ) =>
{
let error_msg = e.to_string().to_lowercase();
if error_msg.contains( "not found" ) || error_msg.contains( "does not exist" )
{
Ok( ModelStatus::NotFound )
}
else
{
Ok( ModelStatus::Error( e.to_string() ) )
}
}
}
}
/// Wait for a model to become available with explicit configuration
///
/// # Governing Principle Compliance
///
/// This requires explicit configuration for polling behavior, providing full transparency
/// and control over model waiting strategy.
///
/// # Arguments
/// - `model_id`: Model identifier to wait for
/// - `timeout_secs`: Maximum time to wait in seconds
/// - `wait_config`: Explicit configuration for polling behavior
///
/// # Errors
/// Returns error if the model doesn't become available within timeout
#[ inline ]
pub async fn wait_for_model_with_config(
&self,
model_id : impl AsRef< str >,
timeout_secs : u64,
wait_config : ModelWaitConfig,
) -> Result< () >
{
use tokio::time::sleep;
let model_ref = model_id.as_ref();
// Validate model identifier
validate_model_identifier( model_ref )?;
let mut elapsed = 0;
let poll_interval_secs = wait_config.poll_interval.as_secs();
while elapsed < timeout_secs
{
match self.status( model_ref ).await?
{
ModelStatus::Available => return Ok( () ),
ModelStatus::Loading =>
{
sleep( wait_config.poll_interval ).await;
elapsed += poll_interval_secs;
},
ModelStatus::NotFound =>
{
return Err( crate::error::HuggingFaceError::ModelUnavailable(
format!( "Model '{model_ref}' not found" )
) );
},
ModelStatus::Error( msg ) =>
{
return Err( crate::error::HuggingFaceError::ModelUnavailable(
format!( "Model '{model_ref}' error : {msg}" )
) );
}
}
}
Err( crate::error::HuggingFaceError::ModelUnavailable(
format!( "Model '{model_ref}' did not become available within {timeout_secs} seconds" )
) )
}
/// Wait for a model to become available with recommended configuration
///
/// # Governing Principle Compliance
///
/// This provides HuggingFace-recommended waiting configuration without making it implicit.
/// Developers must explicitly choose to use this recommended approach.
///
/// # Arguments
/// - `model_id`: Model identifier to wait for
/// - `timeout_secs`: Maximum time to wait in seconds
///
/// # Errors
/// Returns error if the model doesn't become available within timeout
#[ inline ]
pub async fn wait_for_model(
&self,
model_id : impl AsRef< str >,
timeout_secs : u64
) -> Result< () >
{
self.wait_for_model_with_config(
model_id,
timeout_secs,
ModelWaitConfig::recommended()
).await
}
}
// Basic implementation for when env-config is not available
#[ cfg( not( feature = "env-config" ) ) ]
impl< E > Models< E >
where
E : Clone,
{
/// Create a new Models API group
#[ inline ]
#[ must_use ]
pub fn new( client : &Client< E > ) -> Self
{
Self
{
client : (*client).clone(),
}
}
}
/// Status of a `HuggingFace` model
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq ) ]
pub enum ModelStatus
{
/// Model is available and ready for inference
Available,
/// Model is currently loading
Loading,
/// Model was not found
NotFound,
/// Model encountered an error
Error( String ),
}
} // end mod private
crate::mod_interface!
{
exposed use
{
private::Models,
private::ModelStatus,
};
}