Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions encoderfile-py/python/encoderfile/encoderfile.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import Optional

class EncoderfileBuilder:
@classmethod
def from_config(
cls,
config: str,
output_path: Optional[str] = None,
base_binary_path: Optional[str] = None,
platform: Optional[str] = None,
version: Optional[str] = None,
no_download: bool = False,
directory: Optional[str] = None,
) -> "EncoderfileBuilder": ...
def build(self, cache_dir: Optional[str] = None): ...
6 changes: 3 additions & 3 deletions encoderfile-py/python/tests/test_all.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import encoderfile
# import encoderfile as ef


def test_sum_as_string():
assert encoderfile.sum_as_string(1, 1) == "2"
def test_todo():
assert True
63 changes: 63 additions & 0 deletions encoderfile-py/src/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::path::PathBuf;

use encoderfile::build_cli::cli::{BuildArgs, GlobalArguments};
use pyo3::{
exceptions::{PyRuntimeError, PyValueError},
prelude::*,
types::PyType,
};

#[pyclass]
pub struct EncoderfileBuilder(BuildArgs);

#[pymethods]
impl EncoderfileBuilder {
#[allow(clippy::too_many_arguments)]
#[classmethod]
#[pyo3(signature = (
config,
output_path = None,
base_binary_path = None,
platform = None,
version = None,
no_download = false,
directory = None))
]
fn from_config(
_cls: &Bound<'_, PyType>,
config: PathBuf,
output_path: Option<PathBuf>,
base_binary_path: Option<PathBuf>,
platform: Option<String>,
version: Option<String>,
no_download: Option<bool>,
directory: Option<PathBuf>,
) -> PyResult<EncoderfileBuilder> {
let platform = platform
.as_ref()
.map(|i| std::str::FromStr::from_str(i.as_str()))
.transpose()
.map_err(|_| PyValueError::new_err(format!("Invalid platform: {:?}", &platform)))?;

Ok(EncoderfileBuilder(BuildArgs {
config,
output_path,
base_binary_path,
platform,
version,
no_download: no_download.unwrap_or(false),
working_dir: directory,
}))
}

#[pyo3(signature = (cache_dir = None))]
fn build(&self, cache_dir: Option<PathBuf>) -> PyResult<()> {
let global = GlobalArguments { cache_dir };

self.0
.run(&global)
.map_err(|e| PyRuntimeError::new_err(format!("Error building encoderfile: {:?}", e)))?;

Ok(())
}
}
11 changes: 4 additions & 7 deletions encoderfile-py/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use pyo3::prelude::*;

mod builder;

/// A Python module implemented in Rust.
#[pymodule]
mod encoderfile {
use pyo3::prelude::*;

/// Formats the sum of two numbers as string.
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}
#[pymodule_export]
use super::builder::EncoderfileBuilder;
}
16 changes: 8 additions & 8 deletions encoderfile/src/build_cli/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,44 @@ use clap_derive::Args;
#[derive(Debug, Args)]
pub struct BuildArgs {
#[arg(short = 'f', help = "Path to config file. Required.")]
config: PathBuf,
pub config: PathBuf,
#[arg(
short = 'o',
long = "output-path",
help = "Output path, e.g., `./my_model.encoderfile`. Optional"
)]
output_path: Option<PathBuf>,
pub output_path: Option<PathBuf>,
#[arg(
long = "base-binary-path",
help = "Path to base binary to use. Optional."
)]
base_binary_path: Option<PathBuf>,
pub base_binary_path: Option<PathBuf>,
#[arg(
long = "platform",
help = "Target platform to build. Follows standard rust target triple format."
)]
platform: Option<TargetSpec>,
pub platform: Option<TargetSpec>,
#[arg(
long,
help = "Encoderfile version override (defaults to current version)."
)]
version: Option<String>,
pub version: Option<String>,
#[arg(
long = "no-download",
help = "Disable downloading",
default_value = "false"
)]
no_download: bool,
pub no_download: bool,
#[arg(
long = "directory", // working-dir???
help = "Set the working directory for the build process. Optional.",
default_value = None
)]
working_dir: Option<PathBuf>,
pub working_dir: Option<PathBuf>,
}

impl BuildArgs {
pub fn run(self, global: &GlobalArguments) -> Result<()> {
pub fn run(&self, global: &GlobalArguments) -> Result<()> {
terminal::info("Loading config...");
let mut config = crate::build_cli::config::BuildConfig::load(&self.config)?;

Expand Down
3 changes: 2 additions & 1 deletion encoderfile/src/build_cli/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod runtime;
#[cfg(feature = "dev-utils")]
pub use build::{test_build_args, test_build_args_working_dir};

pub use build::BuildArgs;
pub use inspect::inspect_encoderfile;

#[derive(Debug, Parser)]
Expand All @@ -27,7 +28,7 @@ pub struct GlobalArguments {
long = "cache-dir",
help = "Cache directory. This is used for build artifacts. Optional."
)]
cache_dir: Option<PathBuf>,
pub cache_dir: Option<PathBuf>,
}

impl GlobalArguments {
Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[project]
name = "encoderfile"
name = "encoderfile-workspace"
version = "0.3.0"
description = "Add your description here"
readme = "README.md"
Expand All @@ -11,3 +11,8 @@ dev = [ "pre-commit>=4.3.0", "ruff>=0.14.2",]
docs = [ "mkdocs>=1.6.1", "mkdocs-include-markdown-plugin>=7.2.0", "mkdocs-material>=9.7.0",]
setup = [ "onnxruntime>=1.23.1", "optimum[onnxruntime]>=2.0.0", "transformers>=4.55.0",]
models = [ "torch>=2.9.0", "click", {include-group = "setup"} ]

[tool.uv.workspace]
members = [
"encoderfile-py"
]
2 changes: 1 addition & 1 deletion test_config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
encoderfile:
name: my-model-2
path: models/token_classification
path: models/dummy_electra_token_classifier
model_type: token_classification
output_path: ./my-model-2.encoderfile
transform: |
Expand Down
Loading