Utils
Dict
Module evalsense.utils.dict
.
Functions:
Name | Description |
---|---|
deep_update |
Recursively updates a dictionary with data from another dictionary. |
deep_update
Recursively updates a dictionary with data from another dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
old_dict
|
dict
|
The dictionary to update. |
required |
new_dict
|
dict
|
The new dictionary data. |
required |
Source code in evalsense/utils/dict.py
Files
Module evalsense.utils.files
.
Functions:
Name | Description |
---|---|
download_file |
Downloads a file from a URL. |
get_remote_file_headers |
Gets the HTTP headers of a remote file. |
to_safe_filename |
Converts a string to a safe filename. |
verify_file |
Verifies the integrity of a file against the provided metadata. |
download_file
download_file(
url: str,
target_path: str | Path,
resume_download: bool = True,
force_download: bool = False,
show_progress: bool = True,
max_attempts: int = 2,
expected_hash: str | None = None,
hash_type: str = DEFAULT_HASH_TYPE,
chunk_size: int = 10 * 1024**2,
) -> None
Downloads a file from a URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
The URL of the file to download. |
required |
target_path
|
str | Path
|
The path to save the downloaded file. |
required |
resume_download
|
bool
|
Whether to resume a partially downloaded file. |
True
|
force_download
|
bool
|
Whether to force the download even if the file already exists. |
False
|
show_progress
|
bool
|
Whether to show download progress. |
True
|
max_attempts
|
int
|
The maximum number of download attempts. Defaults to 2. |
2
|
expected_hash
|
str
|
The expected hash of the downloaded file. |
None
|
hash_type
|
str
|
The hash algorithm to use. Defaults to "sha256". |
DEFAULT_HASH_TYPE
|
chunk_size
|
int
|
The size of each download chunk in bytes. Defaults to 10MB. |
10 * 1024 ** 2
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If the download fails after the maximum number of attempts. |
ValueError
|
If max_attempts is invalid. |
Source code in evalsense/utils/files.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
|
get_remote_file_headers
Gets the HTTP headers of a remote file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
The URL of the file. |
required |
max_attempts
|
int
|
The maximum number of attempts to get the headers. Defaults to 2. |
2
|
Returns:
Type | Description |
---|---|
CaseInsensitiveDict[str]
|
The headers of the file. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the headers cannot be determined in the maximum number of attempts. |
Source code in evalsense/utils/files.py
to_safe_filename
Converts a string to a safe filename.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The string to convert. |
required |
Returns:
Type | Description |
---|---|
str
|
The safe filename. |
Source code in evalsense/utils/files.py
verify_file
verify_file(
file_path: str | Path,
expected_size: int | None = None,
expected_hash: str | None = None,
hash_type: str = DEFAULT_HASH_TYPE,
show_progress: bool = True,
chunk_size: int = 10 * 1024**2,
) -> bool
Verifies the integrity of a file against the provided metadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str | Path
|
The path to the file to verify. |
required |
expected_size
|
int
|
The expected size of the file in bytes (skips checking size if None). |
None
|
expected_hash
|
str
|
The expected hash of the file (skips checking hash if None). |
None
|
hash_type
|
str
|
The hash algorithm to use. Defaults to "sha256". |
DEFAULT_HASH_TYPE
|
show_progress
|
bool
|
Whether to show verification progress. |
True
|
chunk_size
|
int
|
The size of each verification chunk in bytes. Defaults to 10MB. |
10 * 1024 ** 2
|
Returns:
Type | Description |
---|---|
bool
|
True if the file matches the expected metadata, False otherwise. |
Raises:
Type | Description |
---|---|
ValueError
|
If the hash type is unsupported. |
Source code in evalsense/utils/files.py
Huggingface
Module evalsense.utils.huggingface
.
Functions:
Name | Description |
---|---|
disable_dataset_progress_bars |
Context manager to disable progress bars for Hugging Face datasets. |
disable_dataset_progress_bars
Context manager to disable progress bars for Hugging Face datasets.
Source code in evalsense/utils/huggingface.py
Text
Functions:
Name | Description |
---|---|
extract_lines |
Extract lines from the text based on a filter function. |
extract_score |
Extract the first numerical score from text that falls between min_score and max_score. |
extract_ternary_answer |
Extract a ternary answer (True/False/Unknown) from the text. |
extract_weighted_binary_answer |
Extract a weighted binary answer from the model output. |
extract_weighted_score |
Extract a weighted evaluation score from the model output. |
format_template |
Format a template string with the provided keyword arguments. |
extract_lines
extract_lines(
text: str,
include_filter_fun: Callable[
[str], bool
] = lambda _: True,
trim_lines: bool = True,
) -> list[str]
Extract lines from the text based on a filter function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to extract lines from. |
required |
include_filter_fun
|
Callable[[str], bool]
|
A function that takes a line and returns True if it should be included, False otherwise. Defaults to a function that includes all lines. |
lambda _: True
|
trim_lines
|
bool
|
Whether to trim bullet points, list numbers and whitespace from the beginning/end of each line. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: A list of extracted lines. |
Source code in evalsense/utils/text.py
extract_score
Extract the first numerical score from text that falls between min_score and max_score.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to extract the score from. |
required |
min_score
|
int
|
The minimum valid score. Defaults to 1. |
1
|
max_score
|
int
|
The maximum valid score. Defaults to 10. |
10
|
Returns:
Type | Description |
---|---|
int
|
float | None: The extracted score if found and valid, otherwise None. |
Source code in evalsense/utils/text.py
extract_ternary_answer
extract_ternary_answer(
text: str,
binary_only: bool,
unknown_on_mismatch: bool = True,
) -> bool | None
Extract a ternary answer (True/False/Unknown) from the text.
Valid answers are 'yes', 'no', 'true', 'false', 'unknown', and 'I don't know'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to extract the answer from. |
required |
binary_only
|
bool
|
If True, only 'yes' or 'no' are valid answers. |
required |
unknown_on_mismatch
|
bool
|
If True, return None for answers not matching any of the valid answers. If False, raise an error. Only relevant if binary_only is False. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
bool | None
|
bool | None: The extracted answer - bool for True/False, None for Unknown. |
Source code in evalsense/utils/text.py
extract_weighted_binary_answer
Extract a weighted binary answer from the model output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output
|
ModelOutput
|
The model output containing logprobs. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The model probability of the answer being True. |
Source code in evalsense/utils/text.py
extract_weighted_score
Extract a weighted evaluation score from the model output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output
|
ModelOutput
|
The model output containing logprobs. |
required |
min_score
|
int
|
The minimum valid score. Defaults to 1. |
1
|
max_score
|
int
|
The maximum valid score. Defaults to 10. |
10
|
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The weighted score. |
Source code in evalsense/utils/text.py
format_template
Format a template string with the provided keyword arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template
|
str
|
The template string to format. |
required |
**kwargs
|
dict[str, any]
|
Keyword arguments to replace placeholders in the template. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The formatted string. |