Skip to content

Commit bf86561

Browse files
authored
feat: update auto-pull logic to run in parallel with pip install (#744)
This also fixes a bug in the model puller to avoid pulling the same model more than once. Signed-off-by: Nick Mitchell <[email protected]>
1 parent e50fff9 commit bf86561

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

pdl-live-react/src-tauri/Cargo.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdl-live-react/src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ file_diff = "1.0.0"
3131
duct = "0.13.7"
3232
rayon = "1.10.0"
3333
yaml-rust2 = "0.10.0"
34+
futures = "0.3.31"
3435

3536
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
3637
tauri-plugin-cli = "2"

pdl-live-react/src-tauri/src/cli/run.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ use ::file_diff::diff;
22
use ::std::fs::{copy, create_dir_all};
33
use ::std::path::{Path, PathBuf};
44
use duct::cmd;
5+
use futures::executor::block_on;
6+
use yaml_rust2::yaml::LoadError;
57

68
use tauri::path::BaseDirectory;
79
use tauri::Manager;
810

911
use crate::interpreter::load;
1012

1113
#[cfg(desktop)]
12-
fn pip_install_if_needed(app_handle: tauri::AppHandle) -> Result<PathBuf, tauri::Error> {
14+
async fn pip_install_if_needed(app_handle: tauri::AppHandle) -> Result<PathBuf, tauri::Error> {
1315
let cache_path = app_handle.path().cache_dir()?.join("pdl");
1416

1517
create_dir_all(&cache_path)?;
@@ -82,9 +84,9 @@ pub fn run_pdl_program(
8284
Path::new(&source_file_path).file_name().unwrap()
8385
);
8486

85-
let _ = load::pull_if_needed(&source_file_path);
87+
let pull_future = load::pull_if_needed(&source_file_path);
88+
let bin_path_future = pip_install_if_needed(app_handle);
8689

87-
let bin_path = pip_install_if_needed(app_handle)?;
8890
let trace_arg = if let Some(arg) = trace_file {
8991
if let serde_json::Value::String(f) = &arg.value {
9092
"--trace=".to_owned() + f
@@ -115,6 +117,15 @@ pub fn run_pdl_program(
115117
"".to_owned()
116118
};
117119

120+
// wait for any model pulls to finish
121+
block_on(pull_future).map_err(|e| match e {
122+
LoadError::IO(ee) => tauri::Error::Io(ee),
123+
LoadError::Scan(ee) => tauri::Error::Anyhow(ee.into()),
124+
_ => tauri::Error::FailedToReceiveMessage,
125+
})?;
126+
127+
let bin_path = block_on(bin_path_future)?;
128+
118129
let mut args = vec![
119130
source_file_path.as_str(),
120131
trace_arg.as_str(),

pdl-live-react/src-tauri/src/interpreter/load.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,18 @@ fn extract_models(program: Yaml) -> Vec<String> {
4747
}
4848

4949
/// Pull models (in parallel) from the PDL program in the given filepath.
50-
pub fn pull_if_needed(path: &String) -> Result<(), LoadError> {
51-
from_path(path)
50+
pub async fn pull_if_needed(path: &String) -> Result<(), LoadError> {
51+
let mut models = from_path(path)
5252
.unwrap()
5353
.into_iter()
5454
.flat_map(extract_models)
55-
.collect::<Vec<String>>()
55+
.collect::<Vec<String>>();
56+
57+
// A single program may specify the same model more than once. Dedup!
58+
models.sort();
59+
models.dedup();
60+
61+
models
5662
.into_par_iter()
5763
.try_for_each(|model| match model {
5864
m if model.starts_with("ollama/") => ollama_pull(&m[7..]),

0 commit comments

Comments
 (0)