Rust SDK
High-performance Veriafy SDK for Rust applications
Installation
Add to your Cargo.toml:
[dependencies]
veriafy = "1.0"
# With GPU support
veriafy = { version = "1.0", features = ["gpu"] }Quick Start
use veriafy_ai::{Veriafy, ClassifyOptions};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize client
let client = Veriafy::new()?;
// Classify a file
let result = client.classify(ClassifyOptions {
file: "document.pdf".into(),
model: "veriafy/fraud-detection".into(),
..Default::default()
}).await?;
println!("Vector ID: {}", result.vector_id);
println!("Action: {}", result.action);
println!("Confidence: {:.2}", result.confidence);
Ok(())
}API Reference
Types
pub struct Veriafy {
config: Config,
}
pub struct Config {
pub api_key: Option<String>,
pub local: bool,
pub gpu: bool,
}
pub struct ClassifyOptions {
pub file: PathBuf,
pub model: String,
pub threshold: f64,
}
pub struct ClassificationResult {
pub vector_id: String,
pub file_type: String,
pub categories: HashMap<String, f64>,
pub confidence: f64,
pub action: Action,
pub processing_time_ms: f64,
}
pub enum Action {
Allow,
Flag,
Block,
}Batch Processing with Streams
use futures::StreamExt;
use veriafy_ai::{Veriafy, BatchOptions};
async fn process_directory(path: &str) -> Result<(), Box<dyn std::error::Error>> {
let client = Veriafy::new()?;
let files: Vec<_> = std::fs::read_dir(path)?
.filter_map(|e| e.ok())
.map(|e| e.path())
.filter(|p| p.extension().map_or(false, |e| e == "pdf"))
.collect();
let mut stream = client.classify_batch_stream(BatchOptions {
files,
model: "veriafy/fraud-detection".into(),
batch_size: 32,
});
while let Some(result) = stream.next().await {
match result {
Ok(r) if r.action == Action::Block => {
println!("BLOCKED: {:?}", r.file);
}
Err(e) => eprintln!("Error: {}", e),
_ => {}
}
}
Ok(())
}Axum Web Server Example
use axum::{
extract::Multipart,
routing::post,
Json, Router,
};
use veriafy_ai::{Veriafy, ClassifyOptions};
use std::sync::Arc;
struct AppState {
veriafy: Veriafy,
}
async fn moderate(
state: axum::extract::State<Arc<AppState>>,
mut multipart: Multipart,
) -> Json<ClassificationResult> {
while let Some(field) = multipart.next_field().await.unwrap() {
if field.name() == Some("file") {
let data = field.bytes().await.unwrap();
let result = state.veriafy
.classify_bytes(&data, ClassifyOptions {
model: "veriafy/nsfw-classifier".into(),
..Default::default()
})
.await
.unwrap();
return Json(result);
}
}
panic!("No file provided")
}
#[tokio::main]
async fn main() {
let state = Arc::new(AppState {
veriafy: Veriafy::new().unwrap(),
});
let app = Router::new()
.route("/api/moderate", post(moderate))
.with_state(state);
axum::Server::bind(&"0.0.0.0:8080".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}