glitchlings 1.0.0__cp313-cp313-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- glitchlings/__init__.py +101 -0
- glitchlings/__main__.py +8 -0
- glitchlings/_corruption_engine/__init__.py +12 -0
- glitchlings/_corruption_engine.cp313-win_amd64.pyd +0 -0
- glitchlings/assets/__init__.py +180 -0
- glitchlings/assets/apostrofae_pairs.json +32 -0
- glitchlings/assets/ekkokin_homophones.json +2014 -0
- glitchlings/assets/hokey_assets.json +193 -0
- glitchlings/assets/lexemes/academic.json +1049 -0
- glitchlings/assets/lexemes/colors.json +1333 -0
- glitchlings/assets/lexemes/corporate.json +716 -0
- glitchlings/assets/lexemes/cyberpunk.json +22 -0
- glitchlings/assets/lexemes/lovecraftian.json +23 -0
- glitchlings/assets/lexemes/synonyms.json +3354 -0
- glitchlings/assets/mim1c_homoglyphs.json.gz.b64 +1064 -0
- glitchlings/assets/ocr_confusions.tsv +30 -0
- glitchlings/assets/pipeline_assets.json +29 -0
- glitchlings/attack/__init__.py +184 -0
- glitchlings/attack/analysis.py +1321 -0
- glitchlings/attack/core.py +819 -0
- glitchlings/attack/core_execution.py +378 -0
- glitchlings/attack/core_planning.py +612 -0
- glitchlings/attack/encode.py +114 -0
- glitchlings/attack/metrics.py +211 -0
- glitchlings/attack/metrics_dispatch.py +70 -0
- glitchlings/attack/tokenization.py +338 -0
- glitchlings/attack/tokenizer_metrics.py +373 -0
- glitchlings/auggie.py +285 -0
- glitchlings/compat/__init__.py +9 -0
- glitchlings/compat/loaders.py +355 -0
- glitchlings/compat/types.py +41 -0
- glitchlings/conf/__init__.py +39 -0
- glitchlings/conf/loaders.py +331 -0
- glitchlings/conf/schema.py +156 -0
- glitchlings/conf/types.py +72 -0
- glitchlings/config.toml +2 -0
- glitchlings/constants.py +139 -0
- glitchlings/dev/__init__.py +3 -0
- glitchlings/dev/docs.py +45 -0
- glitchlings/dlc/__init__.py +21 -0
- glitchlings/dlc/_shared.py +300 -0
- glitchlings/dlc/gutenberg.py +400 -0
- glitchlings/dlc/huggingface.py +68 -0
- glitchlings/dlc/langchain.py +147 -0
- glitchlings/dlc/nemo.py +283 -0
- glitchlings/dlc/prime.py +215 -0
- glitchlings/dlc/pytorch.py +98 -0
- glitchlings/dlc/pytorch_lightning.py +173 -0
- glitchlings/internal/__init__.py +16 -0
- glitchlings/internal/rust.py +159 -0
- glitchlings/internal/rust_ffi.py +599 -0
- glitchlings/main.py +426 -0
- glitchlings/protocols.py +91 -0
- glitchlings/runtime_config.py +24 -0
- glitchlings/util/__init__.py +41 -0
- glitchlings/util/adapters.py +65 -0
- glitchlings/util/keyboards.py +508 -0
- glitchlings/util/transcripts.py +108 -0
- glitchlings/zoo/__init__.py +161 -0
- glitchlings/zoo/assets/__init__.py +29 -0
- glitchlings/zoo/core.py +852 -0
- glitchlings/zoo/core_execution.py +154 -0
- glitchlings/zoo/core_planning.py +451 -0
- glitchlings/zoo/corrupt_dispatch.py +291 -0
- glitchlings/zoo/hokey.py +139 -0
- glitchlings/zoo/jargoyle.py +301 -0
- glitchlings/zoo/mim1c.py +269 -0
- glitchlings/zoo/pedant/__init__.py +109 -0
- glitchlings/zoo/pedant/core.py +99 -0
- glitchlings/zoo/pedant/forms.py +50 -0
- glitchlings/zoo/pedant/stones.py +83 -0
- glitchlings/zoo/redactyl.py +94 -0
- glitchlings/zoo/rng.py +280 -0
- glitchlings/zoo/rushmore.py +416 -0
- glitchlings/zoo/scannequin.py +370 -0
- glitchlings/zoo/transforms.py +331 -0
- glitchlings/zoo/typogre.py +194 -0
- glitchlings/zoo/validation.py +643 -0
- glitchlings/zoo/wherewolf.py +120 -0
- glitchlings/zoo/zeedub.py +165 -0
- glitchlings-1.0.0.dist-info/METADATA +404 -0
- glitchlings-1.0.0.dist-info/RECORD +86 -0
- glitchlings-1.0.0.dist-info/WHEEL +5 -0
- glitchlings-1.0.0.dist-info/entry_points.txt +3 -0
- glitchlings-1.0.0.dist-info/licenses/LICENSE +201 -0
- glitchlings-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
"""Integration helpers for the py-gutenberg library.
|
|
2
|
+
|
|
3
|
+
This module provides a wrapper around the GutenbergAPI that applies
|
|
4
|
+
glitchlings to book text as it's fetched.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from collections.abc import Iterable
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
from functools import cached_property
|
|
12
|
+
from typing import Any, Protocol, TypeAlias, cast
|
|
13
|
+
|
|
14
|
+
from ..util.adapters import coerce_gaggle
|
|
15
|
+
from ..zoo import Gaggle, Glitchling
|
|
16
|
+
from ._shared import corrupt_text_value
|
|
17
|
+
|
|
18
|
+
#: Default Gutendex API instance URL (public instance hosted at gutendex.com).
|
|
19
|
+
DEFAULT_GUTENDEX_URL = "https://gutendex.com"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class PersonProtocol(Protocol):
|
|
23
|
+
"""Minimal interface for py-gutenberg Person objects."""
|
|
24
|
+
|
|
25
|
+
name: str
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class BookProtocol(Protocol):
|
|
29
|
+
"""Minimal interface for py-gutenberg Book objects."""
|
|
30
|
+
|
|
31
|
+
id: int
|
|
32
|
+
title: str
|
|
33
|
+
authors: list[PersonProtocol]
|
|
34
|
+
translators: list[PersonProtocol]
|
|
35
|
+
subjects: list[str]
|
|
36
|
+
bookshelves: list[str]
|
|
37
|
+
languages: list[str]
|
|
38
|
+
copyright: bool
|
|
39
|
+
media_type: str
|
|
40
|
+
formats: dict[str, str]
|
|
41
|
+
download_count: int
|
|
42
|
+
|
|
43
|
+
def get_text(self) -> str: ...
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class GutenbergAPIProtocol(Protocol):
|
|
47
|
+
"""Subset of the py-gutenberg API we rely on."""
|
|
48
|
+
|
|
49
|
+
instance_url: str
|
|
50
|
+
|
|
51
|
+
def get_all_books(self) -> Iterable[BookProtocol]: ...
|
|
52
|
+
|
|
53
|
+
def get_public_domain_books(self) -> Iterable[BookProtocol]: ...
|
|
54
|
+
|
|
55
|
+
def get_copyrighted_books(self) -> Iterable[BookProtocol]: ...
|
|
56
|
+
|
|
57
|
+
def get_books_by_author(self, author: str) -> Iterable[BookProtocol]: ...
|
|
58
|
+
|
|
59
|
+
def get_books_by_ids(self, ids: list[int]) -> Iterable[BookProtocol]: ...
|
|
60
|
+
|
|
61
|
+
def get_books_by_language(self, languages: list[str]) -> Iterable[BookProtocol]: ...
|
|
62
|
+
|
|
63
|
+
def get_books_by_search(self, query: str) -> Iterable[BookProtocol]: ...
|
|
64
|
+
|
|
65
|
+
def get_books_by_mime_type(self, mime_type: str) -> Iterable[BookProtocol]: ...
|
|
66
|
+
|
|
67
|
+
def get_books_ascending(self) -> Iterable[BookProtocol]: ...
|
|
68
|
+
|
|
69
|
+
def get_oldest(self) -> Iterable[BookProtocol]: ...
|
|
70
|
+
|
|
71
|
+
def get_latest(self, topic: str = "recent") -> Iterable[BookProtocol]: ...
|
|
72
|
+
|
|
73
|
+
def get_book(self, book_id: int) -> BookProtocol: ...
|
|
74
|
+
|
|
75
|
+
def get_book_metadata(self, book_id: int) -> BookProtocol: ...
|
|
76
|
+
|
|
77
|
+
def get_book_text(self, book_id: int) -> BookProtocol: ...
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
Person: TypeAlias = PersonProtocol
|
|
81
|
+
Book: TypeAlias = BookProtocol
|
|
82
|
+
GutenbergAPI: TypeAlias = GutenbergAPIProtocol
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@dataclass
|
|
86
|
+
class GlitchedBook:
|
|
87
|
+
"""A Book wrapper that corrupts text content via glitchlings.
|
|
88
|
+
|
|
89
|
+
This class wraps a py-gutenberg Book object but provides corrupted text
|
|
90
|
+
when accessed. The original Book attributes are preserved.
|
|
91
|
+
|
|
92
|
+
Attributes:
|
|
93
|
+
id: The Gutenberg book ID.
|
|
94
|
+
title: The corrupted book title.
|
|
95
|
+
original_title: The original (uncorrupted) book title.
|
|
96
|
+
authors: List of book authors.
|
|
97
|
+
translators: List of book translators.
|
|
98
|
+
subjects: List of subject categories.
|
|
99
|
+
bookshelves: List of bookshelf categories.
|
|
100
|
+
languages: List of language codes.
|
|
101
|
+
copyright: Whether the book is under copyright.
|
|
102
|
+
media_type: The media type of the book.
|
|
103
|
+
formats: Dictionary mapping MIME types to download URLs.
|
|
104
|
+
download_count: Number of times the book has been downloaded.
|
|
105
|
+
"""
|
|
106
|
+
|
|
107
|
+
id: int
|
|
108
|
+
title: str
|
|
109
|
+
original_title: str
|
|
110
|
+
authors: list[Person]
|
|
111
|
+
translators: list[Person]
|
|
112
|
+
subjects: list[str]
|
|
113
|
+
bookshelves: list[str]
|
|
114
|
+
languages: list[str]
|
|
115
|
+
copyright: bool
|
|
116
|
+
media_type: str
|
|
117
|
+
formats: dict[str, str]
|
|
118
|
+
download_count: int
|
|
119
|
+
_original_book: Book = field(repr=False)
|
|
120
|
+
_gaggle: Gaggle = field(repr=False)
|
|
121
|
+
|
|
122
|
+
@classmethod
|
|
123
|
+
def from_book(cls, book: Book, gaggle: Gaggle) -> GlitchedBook:
|
|
124
|
+
"""Create a GlitchedBook from a py-gutenberg Book.
|
|
125
|
+
|
|
126
|
+
Args:
|
|
127
|
+
book: The original Book object from py-gutenberg.
|
|
128
|
+
gaggle: The gaggle of glitchlings to apply to text.
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
A GlitchedBook that corrupts text with the provided gaggle.
|
|
132
|
+
"""
|
|
133
|
+
# Use shared utility for consistent corruption; cast tells mypy this is str
|
|
134
|
+
corrupted_title = cast(str, corrupt_text_value(book.title, gaggle))
|
|
135
|
+
return cls(
|
|
136
|
+
id=book.id,
|
|
137
|
+
title=corrupted_title,
|
|
138
|
+
original_title=book.title,
|
|
139
|
+
authors=book.authors,
|
|
140
|
+
translators=book.translators,
|
|
141
|
+
subjects=book.subjects,
|
|
142
|
+
bookshelves=book.bookshelves,
|
|
143
|
+
languages=book.languages,
|
|
144
|
+
copyright=book.copyright,
|
|
145
|
+
media_type=book.media_type,
|
|
146
|
+
formats=book.formats,
|
|
147
|
+
download_count=book.download_count,
|
|
148
|
+
_original_book=book,
|
|
149
|
+
_gaggle=gaggle,
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
@cached_property
|
|
153
|
+
def _text_content(self) -> str:
|
|
154
|
+
"""Lazily fetch and corrupt the full text content of the book."""
|
|
155
|
+
original_text: str = self._original_book.get_text()
|
|
156
|
+
return cast(str, corrupt_text_value(original_text, self._gaggle))
|
|
157
|
+
|
|
158
|
+
def get_text(self) -> str:
|
|
159
|
+
"""Fetch and corrupt the full text content of the book.
|
|
160
|
+
|
|
161
|
+
This method fetches the book's text from Project Gutenberg and applies
|
|
162
|
+
glitchlings corruption to it. The text is fetched fresh on the first call
|
|
163
|
+
and cached for subsequent calls.
|
|
164
|
+
|
|
165
|
+
Returns:
|
|
166
|
+
The corrupted full text of the book.
|
|
167
|
+
|
|
168
|
+
Raises:
|
|
169
|
+
AttributeError: If the underlying Book doesn't support get_text().
|
|
170
|
+
"""
|
|
171
|
+
return self._text_content
|
|
172
|
+
|
|
173
|
+
def __repr__(self) -> str:
|
|
174
|
+
"""Return a concise representation of the GlitchedBook."""
|
|
175
|
+
return (
|
|
176
|
+
f"GlitchedBook(id={self.id}, title={self.title!r}, "
|
|
177
|
+
f"authors={[a.name for a in self.authors]!r})"
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
def __getattr__(self, name: str) -> Any:
|
|
181
|
+
"""Delegate attribute access to the original book."""
|
|
182
|
+
return getattr(self._original_book, name)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
class GlitchenbergAPI:
|
|
186
|
+
"""A wrapper around GutenbergAPI that corrupts book text with glitchlings.
|
|
187
|
+
|
|
188
|
+
This class provides the same interface as GutenbergAPI but applies
|
|
189
|
+
glitchlings to corrupt book text as it's fetched.
|
|
190
|
+
|
|
191
|
+
Example:
|
|
192
|
+
>>> from glitchlings.dlc.gutenberg import GlitchenbergAPI
|
|
193
|
+
>>> from glitchlings import Typogre
|
|
194
|
+
>>> api = GlitchenbergAPI(Typogre(rate=0.05), seed=42)
|
|
195
|
+
>>> book = api.get_book(1342) # Pride and Prejudice
|
|
196
|
+
>>> print(book.title) # Title will have typos applied
|
|
197
|
+
"""
|
|
198
|
+
|
|
199
|
+
def __init__(
|
|
200
|
+
self,
|
|
201
|
+
glitchlings: Glitchling | Gaggle | str | Iterable[str | Glitchling],
|
|
202
|
+
*,
|
|
203
|
+
seed: int = 151,
|
|
204
|
+
instance_url: str = DEFAULT_GUTENDEX_URL,
|
|
205
|
+
) -> None:
|
|
206
|
+
"""Initialize the GlitchenbergAPI.
|
|
207
|
+
|
|
208
|
+
Args:
|
|
209
|
+
glitchlings: A glitchling, gaggle, or specification of glitchlings to apply.
|
|
210
|
+
seed: RNG seed for deterministic corruption (default: 151).
|
|
211
|
+
instance_url: The Gutendex instance URL to use for API requests.
|
|
212
|
+
Defaults to the public instance at gutendex.com. For production use,
|
|
213
|
+
consider self-hosting Gutendex.
|
|
214
|
+
"""
|
|
215
|
+
self._gaggle = coerce_gaggle(glitchlings, seed=seed)
|
|
216
|
+
self._api = _get_gutenberg_api(instance_url)
|
|
217
|
+
|
|
218
|
+
@property
|
|
219
|
+
def instance_url(self) -> str:
|
|
220
|
+
"""Return the Gutendex instance URL."""
|
|
221
|
+
return str(self._api.instance_url)
|
|
222
|
+
|
|
223
|
+
@property
|
|
224
|
+
def gaggle(self) -> Gaggle:
|
|
225
|
+
"""Return the gaggle used for corruption."""
|
|
226
|
+
return self._gaggle
|
|
227
|
+
|
|
228
|
+
def _corrupt_book(self, book: Book) -> GlitchedBook:
|
|
229
|
+
"""Apply glitchlings to a Book object."""
|
|
230
|
+
return GlitchedBook.from_book(book, self._gaggle)
|
|
231
|
+
|
|
232
|
+
def _corrupt_books(self, books: Iterable[Book]) -> list[GlitchedBook]:
|
|
233
|
+
"""Apply glitchlings to a list of Book objects."""
|
|
234
|
+
return [self._corrupt_book(book) for book in books]
|
|
235
|
+
|
|
236
|
+
def corrupt_books(self, books: list[Book]) -> list[GlitchedBook]:
|
|
237
|
+
"""Apply glitchlings to a list of Book objects.
|
|
238
|
+
|
|
239
|
+
This method allows batch corruption of books fetched from other sources
|
|
240
|
+
or the underlying API.
|
|
241
|
+
|
|
242
|
+
Args:
|
|
243
|
+
books: List of py-gutenberg Book objects to corrupt.
|
|
244
|
+
|
|
245
|
+
Returns:
|
|
246
|
+
List of GlitchedBook objects with corrupted text.
|
|
247
|
+
|
|
248
|
+
Example:
|
|
249
|
+
>>> # Fetch from underlying API and corrupt separately
|
|
250
|
+
>>> raw_books = api._api.get_books_by_author("Austen")
|
|
251
|
+
>>> glitched = api.corrupt_books(raw_books)
|
|
252
|
+
"""
|
|
253
|
+
return self._corrupt_books(books)
|
|
254
|
+
|
|
255
|
+
# Methods that return lists of books
|
|
256
|
+
def get_all_books(self) -> list[GlitchedBook]:
|
|
257
|
+
"""Get all books with glitchling corruption applied."""
|
|
258
|
+
return self._corrupt_books(self._api.get_all_books())
|
|
259
|
+
|
|
260
|
+
def get_public_domain_books(self) -> list[GlitchedBook]:
|
|
261
|
+
"""Get public domain books with glitchling corruption applied."""
|
|
262
|
+
return self._corrupt_books(self._api.get_public_domain_books())
|
|
263
|
+
|
|
264
|
+
def get_copyrighted_books(self) -> list[GlitchedBook]:
|
|
265
|
+
"""Get copyrighted books with glitchling corruption applied."""
|
|
266
|
+
return self._corrupt_books(self._api.get_copyrighted_books())
|
|
267
|
+
|
|
268
|
+
def get_books_by_author(self, author: str) -> list[GlitchedBook]:
|
|
269
|
+
"""Get books by author with glitchling corruption applied.
|
|
270
|
+
|
|
271
|
+
Args:
|
|
272
|
+
author: Author name to search for.
|
|
273
|
+
|
|
274
|
+
Returns:
|
|
275
|
+
List of GlitchedBook objects with corrupted text.
|
|
276
|
+
"""
|
|
277
|
+
return self._corrupt_books(self._api.get_books_by_author(author))
|
|
278
|
+
|
|
279
|
+
def get_books_by_ids(self, ids: list[int]) -> list[GlitchedBook]:
|
|
280
|
+
"""Get books by IDs with glitchling corruption applied.
|
|
281
|
+
|
|
282
|
+
Args:
|
|
283
|
+
ids: List of Gutenberg book IDs to retrieve.
|
|
284
|
+
|
|
285
|
+
Returns:
|
|
286
|
+
List of GlitchedBook objects with corrupted text.
|
|
287
|
+
"""
|
|
288
|
+
return self._corrupt_books(self._api.get_books_by_ids(ids))
|
|
289
|
+
|
|
290
|
+
def get_books_by_language(self, languages: list[str]) -> list[GlitchedBook]:
|
|
291
|
+
"""Get books by language with glitchling corruption applied.
|
|
292
|
+
|
|
293
|
+
Args:
|
|
294
|
+
languages: List of language codes (e.g., ["en", "fr"]).
|
|
295
|
+
|
|
296
|
+
Returns:
|
|
297
|
+
List of GlitchedBook objects with corrupted text.
|
|
298
|
+
"""
|
|
299
|
+
return self._corrupt_books(self._api.get_books_by_language(languages))
|
|
300
|
+
|
|
301
|
+
def get_books_by_search(self, query: str) -> list[GlitchedBook]:
|
|
302
|
+
"""Search for books with glitchling corruption applied.
|
|
303
|
+
|
|
304
|
+
Args:
|
|
305
|
+
query: Search query string.
|
|
306
|
+
|
|
307
|
+
Returns:
|
|
308
|
+
List of GlitchedBook objects with corrupted text.
|
|
309
|
+
"""
|
|
310
|
+
return self._corrupt_books(self._api.get_books_by_search(query))
|
|
311
|
+
|
|
312
|
+
def get_books_by_mime_type(self, mime_type: str) -> list[GlitchedBook]:
|
|
313
|
+
"""Get books by MIME type with glitchling corruption applied.
|
|
314
|
+
|
|
315
|
+
Args:
|
|
316
|
+
mime_type: MIME type filter (e.g., "text/plain").
|
|
317
|
+
|
|
318
|
+
Returns:
|
|
319
|
+
List of GlitchedBook objects with corrupted text.
|
|
320
|
+
"""
|
|
321
|
+
return self._corrupt_books(self._api.get_books_by_mime_type(mime_type))
|
|
322
|
+
|
|
323
|
+
def get_books_ascending(self) -> list[GlitchedBook]:
|
|
324
|
+
"""Get books sorted ascending with glitchling corruption applied."""
|
|
325
|
+
return self._corrupt_books(self._api.get_books_ascending())
|
|
326
|
+
|
|
327
|
+
def get_oldest(self) -> list[GlitchedBook]:
|
|
328
|
+
"""Get oldest books with glitchling corruption applied."""
|
|
329
|
+
return self._corrupt_books(self._api.get_oldest())
|
|
330
|
+
|
|
331
|
+
def get_latest(self, topic: str = "recent") -> list[GlitchedBook]:
|
|
332
|
+
"""Get latest books by topic with glitchling corruption applied.
|
|
333
|
+
|
|
334
|
+
Args:
|
|
335
|
+
topic: Topic string to filter books by (e.g., "fiction", "science").
|
|
336
|
+
Defaults to "recent".
|
|
337
|
+
|
|
338
|
+
Returns:
|
|
339
|
+
List of GlitchedBook objects with corrupted text.
|
|
340
|
+
"""
|
|
341
|
+
return self._corrupt_books(self._api.get_latest(topic))
|
|
342
|
+
|
|
343
|
+
# Methods that return single books
|
|
344
|
+
def get_book(self, book_id: int) -> GlitchedBook:
|
|
345
|
+
"""Get a book by ID with glitchling corruption applied.
|
|
346
|
+
|
|
347
|
+
Args:
|
|
348
|
+
book_id: Gutenberg book ID.
|
|
349
|
+
|
|
350
|
+
Returns:
|
|
351
|
+
GlitchedBook with corrupted text.
|
|
352
|
+
"""
|
|
353
|
+
return self._corrupt_book(self._api.get_book(book_id))
|
|
354
|
+
|
|
355
|
+
def get_book_metadata(self, book_id: int) -> GlitchedBook:
|
|
356
|
+
"""Get book metadata by ID with glitchling corruption applied.
|
|
357
|
+
|
|
358
|
+
Args:
|
|
359
|
+
book_id: Gutenberg book ID.
|
|
360
|
+
|
|
361
|
+
Returns:
|
|
362
|
+
GlitchedBook with corrupted metadata.
|
|
363
|
+
"""
|
|
364
|
+
return self._corrupt_book(self._api.get_book_metadata(book_id))
|
|
365
|
+
|
|
366
|
+
def get_book_text(self, book_id: int) -> GlitchedBook:
|
|
367
|
+
"""Get book text by ID with glitchling corruption applied.
|
|
368
|
+
|
|
369
|
+
Args:
|
|
370
|
+
book_id: Gutenberg book ID.
|
|
371
|
+
|
|
372
|
+
Returns:
|
|
373
|
+
GlitchedBook with corrupted text.
|
|
374
|
+
"""
|
|
375
|
+
return self._corrupt_book(self._api.get_book_text(book_id))
|
|
376
|
+
|
|
377
|
+
def __getattr__(self, name: str) -> Any:
|
|
378
|
+
"""Delegate attribute access to the underlying API."""
|
|
379
|
+
return getattr(self._api, name)
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
def _get_gutenberg_api(instance_url: str) -> GutenbergAPI:
|
|
383
|
+
"""Import and return a GutenbergAPI instance.
|
|
384
|
+
|
|
385
|
+
Raises:
|
|
386
|
+
ImportError: If py-gutenberg is not installed.
|
|
387
|
+
"""
|
|
388
|
+
try:
|
|
389
|
+
from gutenberg import GutenbergAPI
|
|
390
|
+
except ImportError as exc:
|
|
391
|
+
raise ImportError(
|
|
392
|
+
"py-gutenberg is required for the GlitchenbergAPI integration. "
|
|
393
|
+
"Install it with: pip install py-gutenberg"
|
|
394
|
+
) from exc
|
|
395
|
+
|
|
396
|
+
api = GutenbergAPI(instance_url=instance_url)
|
|
397
|
+
return cast(GutenbergAPIProtocol, api)
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
__all__ = ["DEFAULT_GUTENDEX_URL", "GlitchenbergAPI", "GlitchedBook"]
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""Integration helpers for the Hugging Face datasets library."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Iterable, Sequence
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from ..util.adapters import coerce_gaggle
|
|
9
|
+
from ..zoo import Gaggle, Glitchling
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _normalize_columns(column: str | Sequence[str]) -> list[str]:
|
|
13
|
+
"""Normalize a column specification to a list."""
|
|
14
|
+
if isinstance(column, str):
|
|
15
|
+
return [column]
|
|
16
|
+
|
|
17
|
+
normalized = list(column)
|
|
18
|
+
if not normalized:
|
|
19
|
+
raise ValueError("At least one column must be specified")
|
|
20
|
+
return normalized
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _glitch_dataset(
|
|
24
|
+
dataset: Any,
|
|
25
|
+
glitchlings: Glitchling | Gaggle | str | Iterable[str | Glitchling],
|
|
26
|
+
column: str | Sequence[str],
|
|
27
|
+
*,
|
|
28
|
+
seed: int = 151,
|
|
29
|
+
) -> Any:
|
|
30
|
+
"""Apply glitchlings to the provided dataset columns."""
|
|
31
|
+
columns = _normalize_columns(column)
|
|
32
|
+
gaggle = coerce_gaggle(glitchlings, seed=seed)
|
|
33
|
+
return gaggle.corrupt_dataset(dataset, columns)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def GlitchedDataset(
|
|
37
|
+
dataset: Any,
|
|
38
|
+
glitchlings: Glitchling | Gaggle | str | Iterable[str | Glitchling],
|
|
39
|
+
*,
|
|
40
|
+
column: str | Sequence[str],
|
|
41
|
+
seed: int = 151,
|
|
42
|
+
) -> Any:
|
|
43
|
+
"""Return a lazily corrupted copy of a Hugging Face dataset.
|
|
44
|
+
|
|
45
|
+
This function applies glitchlings to the specified columns of a dataset,
|
|
46
|
+
returning a new dataset that lazily corrupts data as it's accessed.
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
dataset: The Hugging Face Dataset to corrupt.
|
|
50
|
+
glitchlings: A glitchling, gaggle, or specification of glitchlings to apply.
|
|
51
|
+
column: The column name (string) or names (sequence of strings) to corrupt.
|
|
52
|
+
seed: RNG seed for deterministic corruption (default: 151).
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
A new dataset with the specified columns corrupted by the glitchlings.
|
|
56
|
+
|
|
57
|
+
Example:
|
|
58
|
+
>>> from datasets import Dataset
|
|
59
|
+
>>> from glitchlings.dlc.huggingface import GlitchedDataset
|
|
60
|
+
>>> dataset = Dataset.from_dict({"text": ["hello", "world"]})
|
|
61
|
+
>>> corrupted = GlitchedDataset(dataset, "typogre", column="text")
|
|
62
|
+
>>> list(corrupted)
|
|
63
|
+
[{'text': 'helo'}, {'text': 'wrold'}]
|
|
64
|
+
"""
|
|
65
|
+
return _glitch_dataset(dataset, glitchlings, column, seed=seed)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
__all__ = ["GlitchedDataset"]
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"""LangChain integration helpers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import AsyncIterator, Iterable, Sequence
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from ..util.adapters import coerce_gaggle
|
|
9
|
+
from ..zoo import Gaggle, Glitchling
|
|
10
|
+
from ._shared import (
|
|
11
|
+
corrupt_batch,
|
|
12
|
+
infer_batch_targets,
|
|
13
|
+
normalize_column_spec,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _resolve_columns(
|
|
18
|
+
sample: Any,
|
|
19
|
+
explicit: list[str | int] | None,
|
|
20
|
+
cached: list[str | int] | None,
|
|
21
|
+
) -> list[str | int] | None:
|
|
22
|
+
if explicit is not None:
|
|
23
|
+
return explicit
|
|
24
|
+
|
|
25
|
+
if cached is not None:
|
|
26
|
+
return cached
|
|
27
|
+
|
|
28
|
+
inferred = infer_batch_targets(sample)
|
|
29
|
+
return inferred
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class GlitchedRunnable:
|
|
33
|
+
"""Wrap a LangChain runnable to glitch inputs (and optionally outputs)."""
|
|
34
|
+
|
|
35
|
+
def __init__(
|
|
36
|
+
self,
|
|
37
|
+
runnable: Any,
|
|
38
|
+
glitchlings: Glitchling | Gaggle | str | Iterable[str | Glitchling],
|
|
39
|
+
*,
|
|
40
|
+
input_columns: str | int | Sequence[str | int] | None = None,
|
|
41
|
+
output_columns: str | int | Sequence[str | int] | None = None,
|
|
42
|
+
glitch_output: bool = False,
|
|
43
|
+
seed: int = 151,
|
|
44
|
+
) -> None:
|
|
45
|
+
self._runnable = runnable
|
|
46
|
+
self._gaggle = coerce_gaggle(glitchlings, seed=seed)
|
|
47
|
+
self._input_columns = normalize_column_spec(input_columns)
|
|
48
|
+
self._output_columns = normalize_column_spec(output_columns)
|
|
49
|
+
self._glitch_output = glitch_output
|
|
50
|
+
self._inferred_input_columns: list[str | int] | None = None
|
|
51
|
+
self._inferred_output_columns: list[str | int] | None = None
|
|
52
|
+
|
|
53
|
+
def _glitch_single(self, payload: Any) -> Any:
|
|
54
|
+
columns = _resolve_columns(payload, self._input_columns, self._inferred_input_columns)
|
|
55
|
+
if self._inferred_input_columns is None:
|
|
56
|
+
self._inferred_input_columns = columns
|
|
57
|
+
return corrupt_batch(payload, columns, self._gaggle)
|
|
58
|
+
|
|
59
|
+
def _glitch_many(self, values: Sequence[Any]) -> Sequence[Any]:
|
|
60
|
+
if not values:
|
|
61
|
+
return values
|
|
62
|
+
|
|
63
|
+
columns = _resolve_columns(values[0], self._input_columns, self._inferred_input_columns)
|
|
64
|
+
if self._inferred_input_columns is None:
|
|
65
|
+
self._inferred_input_columns = columns
|
|
66
|
+
|
|
67
|
+
glitched = [corrupt_batch(value, columns, self._gaggle) for value in values]
|
|
68
|
+
if isinstance(values, tuple):
|
|
69
|
+
return tuple(glitched)
|
|
70
|
+
return glitched
|
|
71
|
+
|
|
72
|
+
def _glitch_result(self, result: Any) -> Any:
|
|
73
|
+
if not self._glitch_output:
|
|
74
|
+
return result
|
|
75
|
+
|
|
76
|
+
columns = _resolve_columns(result, self._output_columns, self._inferred_output_columns)
|
|
77
|
+
if self._inferred_output_columns is None:
|
|
78
|
+
self._inferred_output_columns = columns
|
|
79
|
+
return corrupt_batch(result, columns, self._gaggle)
|
|
80
|
+
|
|
81
|
+
def invoke(self, input: Any, config: Any | None = None, **kwargs: Any) -> Any: # noqa: A003
|
|
82
|
+
glitched_input = self._glitch_single(input)
|
|
83
|
+
result = self._runnable.invoke(glitched_input, config=config, **kwargs)
|
|
84
|
+
return self._glitch_result(result)
|
|
85
|
+
|
|
86
|
+
def batch(self, inputs: Sequence[Any], config: Any | None = None, **kwargs: Any) -> Any:
|
|
87
|
+
glitched_inputs = self._glitch_many(inputs)
|
|
88
|
+
result = self._runnable.batch(glitched_inputs, config=config, **kwargs)
|
|
89
|
+
|
|
90
|
+
if not self._glitch_output:
|
|
91
|
+
return result
|
|
92
|
+
|
|
93
|
+
if isinstance(result, Sequence) and result:
|
|
94
|
+
columns = _resolve_columns(
|
|
95
|
+
result[0], self._output_columns, self._inferred_output_columns
|
|
96
|
+
)
|
|
97
|
+
if self._inferred_output_columns is None:
|
|
98
|
+
self._inferred_output_columns = columns
|
|
99
|
+
glitched = [corrupt_batch(value, columns, self._gaggle) for value in result]
|
|
100
|
+
if isinstance(result, tuple):
|
|
101
|
+
return tuple(glitched)
|
|
102
|
+
return glitched
|
|
103
|
+
|
|
104
|
+
return self._glitch_result(result)
|
|
105
|
+
|
|
106
|
+
async def ainvoke(self, input: Any, config: Any | None = None, **kwargs: Any) -> Any:
|
|
107
|
+
glitched_input = self._glitch_single(input)
|
|
108
|
+
result = await self._runnable.ainvoke(glitched_input, config=config, **kwargs)
|
|
109
|
+
return self._glitch_result(result)
|
|
110
|
+
|
|
111
|
+
async def abatch(self, inputs: Sequence[Any], config: Any | None = None, **kwargs: Any) -> Any:
|
|
112
|
+
glitched_inputs = self._glitch_many(inputs)
|
|
113
|
+
result = await self._runnable.abatch(glitched_inputs, config=config, **kwargs)
|
|
114
|
+
|
|
115
|
+
if not self._glitch_output:
|
|
116
|
+
return result
|
|
117
|
+
|
|
118
|
+
if isinstance(result, Sequence) and result:
|
|
119
|
+
columns = _resolve_columns(
|
|
120
|
+
result[0], self._output_columns, self._inferred_output_columns
|
|
121
|
+
)
|
|
122
|
+
if self._inferred_output_columns is None:
|
|
123
|
+
self._inferred_output_columns = columns
|
|
124
|
+
glitched = [corrupt_batch(value, columns, self._gaggle) for value in result]
|
|
125
|
+
if isinstance(result, tuple):
|
|
126
|
+
return tuple(glitched)
|
|
127
|
+
return glitched
|
|
128
|
+
|
|
129
|
+
return self._glitch_result(result)
|
|
130
|
+
|
|
131
|
+
def stream(self, input: Any, config: Any | None = None, **kwargs: Any) -> Any:
|
|
132
|
+
glitched_input = self._glitch_single(input)
|
|
133
|
+
for chunk in self._runnable.stream(glitched_input, config=config, **kwargs):
|
|
134
|
+
yield self._glitch_result(chunk)
|
|
135
|
+
|
|
136
|
+
async def astream(
|
|
137
|
+
self, input: Any, config: Any | None = None, **kwargs: Any
|
|
138
|
+
) -> AsyncIterator[Any]:
|
|
139
|
+
glitched_input = self._glitch_single(input)
|
|
140
|
+
async for chunk in self._runnable.astream(glitched_input, config=config, **kwargs):
|
|
141
|
+
yield self._glitch_result(chunk)
|
|
142
|
+
|
|
143
|
+
def __getattr__(self, name: str) -> Any:
|
|
144
|
+
return getattr(self._runnable, name)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
__all__ = ["GlitchedRunnable"]
|