Spaces:
Sleeping
Sleeping
File size: 9,453 Bytes
5caedb4 |
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
import os
from abc import abstractmethod
from typing import Any, Callable, List, Optional, Sequence, Set, Tuple
from pydantic.dataclasses import dataclass
def _scan_dirs(dirname: str) -> List[str]:
"""
Recursively scans a directory for subfolders.
Args:
dirname (str): The directory to scan.
Returns:
List[str]: A list of subfolder paths, with '/' appended to each path.
"""
subfolders = [f.path for f in os.scandir(dirname) if f.is_dir()]
for dirname in list(subfolders):
subfolders.extend(_scan_dirs(dirname))
subfolders = [x + "/" if x[-1] != "/" else x for x in subfolders]
return subfolders
def _scan_files(
dirname: str, extensions: Tuple[str, ...] = (".csv", ".pq", ".parquet", ".json")
) -> List[str]:
"""
Scans a directory for files with given extension
Excludes files starting with "__meta_info__".
Args:
dirname (str): The directory to scan.
extensions (Tuple[str, ...]): File extensions to consider.
Returns:
List[str]: A sorted list of file paths matching the given extensions.
"""
path_list = [
os.path.join(dirpath, filename)
for dirpath, _, filenames in os.walk(dirname)
for filename in filenames
if any(map(filename.__contains__, extensions))
and not filename.startswith("__meta_info__")
]
return sorted(path_list)
def strip_common_prefix(
paths: Sequence[str], ignore_set: Set[str] = set()
) -> Tuple[str, ...]:
"""
Strips the common prefix from all given paths.
Args:
paths (Sequence[str]): The paths to strip.
ignore_set (Set[str]): Set of path names to ignore when computing the prefix.
Returns:
Tuple[str, ...]: A tuple of paths with common prefixes removed.
"""
paths_to_check = [
os.path.split(os.path.normpath(path))[0]
for path in paths
if path not in ignore_set
]
if len(paths_to_check) == 0:
return tuple(paths)
prefix = os.path.commonpath(paths_to_check)
stripped = tuple(
[
path if path in ignore_set else os.path.relpath(path, prefix)
for path in paths
]
)
return stripped
class Value:
"""Base class for value types."""
pass
@dataclass
class Number:
"""
Represents a numeric range for a setting with optional constraints.
Attributes:
min (float | int): Minimum allowed value. Must be less than or equal to `max`.
step (float | int]): Step size for value increments
max (float | None): Maximum allowed value. Optional.
If provided, the UI component will be rendered as a slider. Otherwise as \
a spinbox.
"""
min: float | int
step: float | int
max: Optional[float | int] = None
def __post_init__(self):
if self.max is not None and self.min > self.max:
raise ValueError(
f"Expected `min <= max`, got min={self.min} > max={self.max}"
)
@dataclass
class String:
"""
Represents possible string values for a setting with optional constraints.
Attributes:
values (Tuple[str, ...] | Tuple[Tuple[str, str], ...]):
Possible values for the string.
- a tuple of tuples (value, name)
- a tuple of strings. In that case the value will be used for name and value
allow_custom (bool): Whether custom values are allowed. This will render a \
combobox. If False (default), a dropdown will be rendered.
placeholder (Optional[str]): Placeholder text for input fields.
"""
values: Tuple[str, ...] | Tuple[Tuple[str, str], ...]
allow_custom: bool = False
placeholder: Optional[str] = None
class DatasetValue:
"""Base class for dataset-related values."""
@abstractmethod
def get_value(
self, dataset: Any, value: Any, type_annotation: type
) -> Tuple[String, Any]:
"""
Abstract method to get the value for a dataset.
Args:
dataset (Any): The dataset object.
value (Any): The current value.
type_annotation (type): The expected type of the value.
Returns:
Tuple[String, Any]: A tuple containing the String object and the value.
"""
raise NotImplementedError
@staticmethod
def _compute_current_values(
current_values: List[str],
possible_values: List[str],
prefer_with: Optional[Callable[[str], bool]] = None,
) -> List[str]:
"""
Compute current values based on possible values and preferences.
This method does not handle duplicate values and raises an error if either \
`current_values` or `possible_values` contain duplicates.
Args:
current_values (List[str]): The preliminary current values.
possible_values (List[str]): All possible values.
prefer_with (Optional[Callable[[str], bool]]): Function determining which \
values to prefer as default.
Returns:
List[str]: A list of computed current values.
Raises:
ValueError: If either `current_values` or `possible_values` contain \
duplicate
"""
if len(set(current_values)) != len(current_values):
raise ValueError("Duplicate values in `current_values`")
if len(set(possible_values)) != len(possible_values):
raise ValueError("Duplicate values in `possible_values`")
if len(possible_values) == 0:
return [""]
# allow only values which are in the possible values
current_values = list(
filter(lambda value: value in possible_values, current_values)
)
if len(current_values) == 0:
# if the values are empty, take all the values where `prefer_with` is true
for c in possible_values:
if prefer_with is not None and prefer_with(c):
current_values.append(c)
# if they are still empty, just take the first possible value
if len(current_values) == 0:
current_values = [possible_values[0]]
return current_values
@dataclass
class Files(DatasetValue):
"""
Represents a selection of files from a dataset.
Used to select a file from a dataset for e.g. `train_dataframe`.
Attributes:
add_none (bool): Whether to add a "None" option.
prefer_with (Optional[Callable[[str], bool]]): Function to determine preferred \
values.
prefer_none (bool): Whether to prefer "None" as the default option.
"""
add_none: bool = False
prefer_with: Optional[Callable[[str], bool]] = None
# For the case where no match found, whether to prioritize
# selecting any file or selecting no file
prefer_none: bool = True
def get_value(
self, dataset: Any, value: Any, type_annotation: type
) -> Tuple[String, Any]:
"""
Get the value for file selection.
Args:
dataset (Any): The dataset object.
value (Any): The current value.
type_annotation (type): The expected type of the value.
Returns:
Tuple[String, Any]: Tuple containing the String object and the current \
value.
"""
if dataset is None:
return String(tuple()), value
available_files = _scan_files(dataset["path"])
if self.add_none is True:
if self.prefer_none:
available_files.insert(0, "None")
else:
available_files.insert(len(available_files), "None")
if isinstance(value, str):
value = [value]
value = DatasetValue._compute_current_values(
value, available_files, self.prefer_with
)
return (
String(
tuple(
zip(
available_files,
strip_common_prefix(available_files, ignore_set={"None"}),
)
)
),
value if type_annotation == Tuple[str, ...] else value[0],
)
@dataclass
class Columns(DatasetValue):
"""
Represents a selection of columns from a dataset.
Used to select a column from a dataset for e.g. `prompt_column`.
Attributes:
add_none (bool): Whether to add a "None" option.
prefer_with (Optional[Callable[[str], bool]]): Function to determine preferred \
values.
"""
add_none: bool = False
prefer_with: Optional[Callable[[str], bool]] = None
def get_value(
self, dataset: Any, value: Any, type_annotation: type
) -> Tuple[String, Any]:
if dataset is None:
return String(tuple()), value
try:
columns = list(dataset["dataframe"].columns)
except KeyError:
columns = []
if self.add_none is True:
columns.insert(0, "None")
if isinstance(value, str):
value = [value]
if value is None:
value = [columns[0]]
value = DatasetValue._compute_current_values(value, columns, self.prefer_with)
return (
String(tuple(columns)),
value if type_annotation == Tuple[str, ...] else value[0],
)
|