-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathncf_demo.rs
More file actions
80 lines (65 loc) · 2.25 KB
/
Copy pathncf_demo.rs
File metadata and controls
80 lines (65 loc) · 2.25 KB
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
use std::sync::Arc;
use anyhow::Result;
use arrow::array::{Float32Array, Int64Array};
use datafusion::prelude::SessionContext;
use model::OnnxModelRegistry;
#[tokio::main]
async fn main() -> Result<()> {
// 1. Create a DataFusion context
let mut ctx = SessionContext::new();
// 2. Register the movie_embeddings Lance dataset as a table
sources::providers::lance::register_lance_table(
&mut ctx,
"movie_embeddings",
"data/movie_embeddings.lance",
None,
)
.await?;
println!("Registered movie_embeddings.lance table");
// 3. Register the onnx_predict UDF (no model pre-registration needed)
let registry = Arc::new(OnnxModelRegistry::new());
registry.register_onnx_predict_udf(&mut ctx);
println!("Registered onnx_predict UDF");
// 4. Query using onnx_predict — model path inline, loaded lazily on first call
let sql = r#"
SELECT
movie_id,
onnx_predict('models/ncf.onnx', CAST(1 AS BIGINT), movie_id) AS prediction_score
FROM movie_embeddings
WHERE movie_id < 3700
ORDER BY prediction_score DESC
LIMIT 10
"#;
println!("\nExecuting query:");
println!("{}", sql);
// 5. Execute the query
let df = ctx.sql(sql).await?;
let batches = df.collect().await?;
// 6. Extract and print the results
println!("\nMovie Prediction Results:");
println!("{:-<60}", "");
println!("{:<15} | {:<20}", "Movie ID", "Prediction Score");
println!("{:-<60}", "");
for batch in &batches {
let movie_ids = batch
.column(batch.schema().index_of("movie_id")?)
.as_any()
.downcast_ref::<Int64Array>()
.expect("movie_id should be Int64Array");
let predictions = batch
.column(batch.schema().index_of("prediction_score")?)
.as_any()
.downcast_ref::<Float32Array>()
.expect("prediction_score should be Float32Array");
for i in 0..batch.num_rows() {
println!(
"{:<15} | {:<20.6}",
movie_ids.value(i),
predictions.value(i)
);
}
}
println!("{:-<60}", "");
println!("\nNCF demo completed successfully!");
Ok(())
}