utils¶
Utility functions for Direktor.
direktor.core.utils
¶
Utility functions for Direktor.
This module contains helper functions for file operations, API calls, text processing, and subprocess execution.
create_temp_dir(input_file, base_dir=None)
¶
Create a temporary directory based on the input file's hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_file
|
str | PathLike[str]
|
Path to the input file. |
required |
base_dir
|
str | None
|
Base directory for temporary files. Defaults to the
|
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created temporary directory. |
Source code in direktor/core/utils.py
download_file(url, local_filename)
¶
Download a file from a URL with progress tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL to download from. |
required |
local_filename
|
str | PathLike[str]
|
Local path to save the file. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to the downloaded file. |
Raises:
| Type | Description |
|---|---|
HTTPError
|
If the download fails. |
Source code in direktor/core/utils.py
group_sentences(sentences, max_chars=100)
¶
Group sentences into chunks with a maximum character count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sentences
|
Sequence[str]
|
List of sentences to group. |
required |
max_chars
|
int
|
Maximum characters per chunk. |
100
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of grouped text chunks. |
Source code in direktor/core/utils.py
run_replicate_model(model, input_data, *, show_spinner=True)
¶
Run a Replicate model with the given input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
The Replicate model identifier. |
required |
input_data
|
dict[str, Any]
|
Dictionary of input parameters. |
required |
show_spinner
|
bool
|
Whether to show a Halo spinner in the terminal. |
True
|
Returns:
| Type | Description |
|---|---|
Any
|
The model output. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the prediction fails or is canceled. |
Source code in direktor/core/utils.py
run_subprocess(command, cwd=None, timeout=300, **kwargs)
¶
Run a subprocess command safely with captured output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
Sequence[str]
|
Command and arguments to execute. |
required |
cwd
|
str | PathLike[str] | None
|
Working directory for the subprocess. |
None
|
timeout
|
float
|
Maximum allowed duration in seconds. |
300
|
**kwargs
|
Any
|
Extra arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
CompletedProcess[str]
|
The completed process result. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the command fails or times out. |
Source code in direktor/core/utils.py
split_into_sentences(text)
¶
Split text into sentences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to split. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of sentences. |
split_text(text, max_tokens)
¶
Split text into chunks based on token count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to split. |
required |
max_tokens
|
int
|
Maximum tokens per chunk. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of text chunks. |
Source code in direktor/core/utils.py
upload_to_r2(file_path, object_name)
¶
Upload a file to S3-compatible storage and return a presigned URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | PathLike[str]
|
Path to the local file. |
required |
object_name
|
str
|
Object name in the bucket. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Presigned URL for the uploaded file. |
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If AWS credentials are incomplete. |