unprofane 0.1.0__py3-none-any.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.
unprofane/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ """Unprofane: a profanity and toxicity API that is hard to fool."""
2
+
3
+ from .client import AsyncUnprofane, Unprofane, UnprofaneError, __version__
4
+
5
+ __all__ = ["AsyncUnprofane", "Unprofane", "UnprofaneError", "__version__"]
unprofane/client.py ADDED
@@ -0,0 +1,228 @@
1
+ """The sync and async clients: one method per endpoint, retries with jitter
2
+ on 429 and 5xx honouring Retry-After, a batch helper that chunks by 100.
3
+ No dependency beyond httpx. Responses are plain dicts in the API's own
4
+ field names (https://unprofane.com/docs/api/), so nothing here can drift
5
+ from the contract without the contract changing."""
6
+
7
+ from __future__ import annotations
8
+
9
+ import random
10
+ import time
11
+ from typing import Any, Dict, Iterable, List, Optional, Union
12
+
13
+ import httpx
14
+
15
+ __version__ = "0.1.0"
16
+ _DEFAULT_BASE = "https://api.unprofane.com"
17
+ _RETRYABLE = {429, 502, 503, 504}
18
+ _MAX_BATCH = 100
19
+
20
+ Policy = Union[str, Dict[str, str]]
21
+
22
+
23
+ class UnprofaneError(Exception):
24
+ """A failure the API answered, with its problem details."""
25
+
26
+ def __init__(self, message: str, status: int, problem: Optional[dict], request_id: Optional[str]):
27
+ super().__init__(message)
28
+ self.status = status
29
+ self.problem = problem
30
+ self.request_id = request_id
31
+
32
+
33
+ def backoff(attempt: int, retry_after: Optional[str]) -> float:
34
+ """Exponential with full jitter, capped at eight seconds; Retry-After wins."""
35
+ if retry_after is not None:
36
+ try:
37
+ value = float(retry_after)
38
+ if value >= 0:
39
+ return min(value, 30.0)
40
+ except ValueError:
41
+ pass
42
+ return random.random() * min(8.0, 0.25 * (2**attempt))
43
+
44
+
45
+ def _check_key(api_key: str) -> None:
46
+ if not api_key or not api_key.startswith(("up_live_", "up_test_", "up_pk_")):
47
+ raise TypeError("pass an API key (up_live_…, up_test_… or up_pk_…)")
48
+
49
+
50
+ def _problem(res: httpx.Response) -> Optional[dict]:
51
+ try:
52
+ body = res.json()
53
+ except ValueError:
54
+ return None
55
+ if isinstance(body, dict) and isinstance(body.get("title"), str):
56
+ body.setdefault("status", res.status_code)
57
+ return body
58
+ return None
59
+
60
+
61
+ def _error(res: httpx.Response, method: str, path: str) -> UnprofaneError:
62
+ problem = _problem(res)
63
+ message = (problem or {}).get("detail") or (problem or {}).get("title") or f"Unprofane {method} {path} failed ({res.status_code})"
64
+ return UnprofaneError(message, res.status_code, problem, res.headers.get("request-id"))
65
+
66
+
67
+ def _chunks(texts: List[str]) -> List[List[dict]]:
68
+ return [
69
+ [{"id": str(i + j), "text": text} for j, text in enumerate(texts[i : i + _MAX_BATCH])]
70
+ for i in range(0, len(texts), _MAX_BATCH)
71
+ ]
72
+
73
+
74
+ def _drop_none(body: Dict[str, Any]) -> Dict[str, Any]:
75
+ return {k: v for k, v in body.items() if v is not None}
76
+
77
+
78
+ class _Base:
79
+ def __init__(self, api_key: str, base_url: str = _DEFAULT_BASE, retries: int = 3, timeout: float = 10.0):
80
+ _check_key(api_key)
81
+ self._headers = {"authorization": f"Bearer {api_key}", "user-agent": f"unprofane-python/{__version__}"}
82
+ self._base = base_url.rstrip("/")
83
+ self._retries = retries
84
+ self._timeout = timeout
85
+
86
+
87
+ class Unprofane(_Base):
88
+ """The synchronous client. Use it as a context manager or call close()."""
89
+
90
+ def __init__(self, api_key: str, base_url: str = _DEFAULT_BASE, retries: int = 3, timeout: float = 10.0, transport: Optional[httpx.BaseTransport] = None):
91
+ super().__init__(api_key, base_url, retries, timeout)
92
+ self._http = httpx.Client(base_url=self._base, headers=self._headers, timeout=timeout, transport=transport)
93
+
94
+ def __enter__(self) -> "Unprofane":
95
+ return self
96
+
97
+ def __exit__(self, *_: object) -> None:
98
+ self.close()
99
+
100
+ def close(self) -> None:
101
+ self._http.close()
102
+
103
+ def _call(self, method: str, path: str, body: Optional[dict] = None) -> Any:
104
+ attempt = 0
105
+ while True:
106
+ try:
107
+ res = self._http.request(method, path, json=body)
108
+ except httpx.TransportError:
109
+ if attempt >= self._retries:
110
+ raise
111
+ attempt += 1
112
+ time.sleep(backoff(attempt, None))
113
+ continue
114
+ if res.is_success:
115
+ return None if res.status_code == 204 else res.json()
116
+ if res.status_code in _RETRYABLE and attempt < self._retries:
117
+ attempt += 1
118
+ time.sleep(backoff(attempt, res.headers.get("retry-after")))
119
+ continue
120
+ raise _error(res, method, path)
121
+
122
+ def check(self, text: str, *, context: Optional[str] = None, policy: Optional[Policy] = None, mode: Optional[str] = None, language: Optional[str] = None, mask: Optional[str] = None, engine: Optional[str] = None, lists: Optional[dict] = None) -> dict:
123
+ """One text: should it be allowed, with the evidence."""
124
+ return self._call("POST", "/v1/check", _drop_none({"text": text, "context": context, "policy": policy, "mode": mode, "language": language, "mask": mask, "engine": engine, "lists": lists}))
125
+
126
+ def check_batch(self, items: List[dict], *, context: Optional[str] = None, policy: Optional[Policy] = None, mode: Optional[str] = None, mask: Optional[str] = None, engine: Optional[str] = None, lists: Optional[dict] = None) -> dict:
127
+ """Up to 100 items of {id, text}, answered in order."""
128
+ if len(items) > _MAX_BATCH:
129
+ raise ValueError(f"check_batch takes at most {_MAX_BATCH} items; use check_many")
130
+ return self._call("POST", "/v1/check/batch", _drop_none({"items": items, "context": context, "policy": policy, "mode": mode, "mask": mask, "engine": engine, "lists": lists}))
131
+
132
+ def check_many(self, texts: Iterable[str], **options: Any) -> List[dict]:
133
+ """Any number of texts, chunked by 100, results in input order."""
134
+ out: List[dict] = []
135
+ for chunk in _chunks(list(texts)):
136
+ out.extend(self.check_batch(chunk, **options)["results"])
137
+ return out
138
+
139
+ def feedback(self, check_id: str, verdict: str, *, expected_action: Optional[str] = None, note: Optional[str] = None, text: Optional[str] = None) -> dict:
140
+ """Report a wrong result. `text` is stored only if you include it."""
141
+ return self._call("POST", "/v1/feedback", _drop_none({"check_id": check_id, "verdict": verdict, "expected_action": expected_action, "note": note, "text": text}))
142
+
143
+ def usage(self) -> dict:
144
+ return self._call("GET", "/v1/usage")
145
+
146
+ def get_list(self, project_id: str, kind: str) -> dict:
147
+ return self._call("GET", f"/v1/projects/{project_id}/lists/{kind}")
148
+
149
+ def put_list(self, project_id: str, kind: str, terms: List[str]) -> dict:
150
+ return self._call("PUT", f"/v1/projects/{project_id}/lists/{kind}", {"terms": terms})
151
+
152
+ def delete_list(self, project_id: str, kind: str) -> None:
153
+ self._call("DELETE", f"/v1/projects/{project_id}/lists/{kind}")
154
+
155
+
156
+ class AsyncUnprofane(_Base):
157
+ """The asynchronous client, for asyncio. Use it as an async context manager."""
158
+
159
+ def __init__(self, api_key: str, base_url: str = _DEFAULT_BASE, retries: int = 3, timeout: float = 10.0, transport: Optional[httpx.AsyncBaseTransport] = None):
160
+ super().__init__(api_key, base_url, retries, timeout)
161
+ self._http = httpx.AsyncClient(base_url=self._base, headers=self._headers, timeout=timeout, transport=transport)
162
+
163
+ async def __aenter__(self) -> "AsyncUnprofane":
164
+ return self
165
+
166
+ async def __aexit__(self, *_: object) -> None:
167
+ await self.aclose()
168
+
169
+ async def aclose(self) -> None:
170
+ await self._http.aclose()
171
+
172
+ async def _call(self, method: str, path: str, body: Optional[dict] = None) -> Any:
173
+ import asyncio
174
+
175
+ attempt = 0
176
+ while True:
177
+ try:
178
+ res = await self._http.request(method, path, json=body)
179
+ except httpx.TransportError:
180
+ if attempt >= self._retries:
181
+ raise
182
+ attempt += 1
183
+ await asyncio.sleep(backoff(attempt, None))
184
+ continue
185
+ if res.is_success:
186
+ return None if res.status_code == 204 else res.json()
187
+ if res.status_code in _RETRYABLE and attempt < self._retries:
188
+ attempt += 1
189
+ await asyncio.sleep(backoff(attempt, res.headers.get("retry-after")))
190
+ continue
191
+ raise _error(res, method, path)
192
+
193
+ async def check(self, text: str, *, context: Optional[str] = None, policy: Optional[Policy] = None, mode: Optional[str] = None, language: Optional[str] = None, mask: Optional[str] = None, engine: Optional[str] = None, lists: Optional[dict] = None) -> dict:
194
+ return await self._call("POST", "/v1/check", _drop_none({"text": text, "context": context, "policy": policy, "mode": mode, "language": language, "mask": mask, "engine": engine, "lists": lists}))
195
+
196
+ async def check_batch(self, items: List[dict], *, context: Optional[str] = None, policy: Optional[Policy] = None, mode: Optional[str] = None, mask: Optional[str] = None, engine: Optional[str] = None, lists: Optional[dict] = None) -> dict:
197
+ if len(items) > _MAX_BATCH:
198
+ raise ValueError(f"check_batch takes at most {_MAX_BATCH} items; use check_many")
199
+ return await self._call("POST", "/v1/check/batch", _drop_none({"items": items, "context": context, "policy": policy, "mode": mode, "mask": mask, "engine": engine, "lists": lists}))
200
+
201
+ async def check_many(self, texts: Iterable[str], *, concurrency: int = 2, **options: Any) -> List[dict]:
202
+ import asyncio
203
+
204
+ chunks = _chunks(list(texts))
205
+ out: List[Optional[dict]] = [None] * len(chunks)
206
+ sem = asyncio.Semaphore(max(1, concurrency))
207
+
208
+ async def one(i: int) -> None:
209
+ async with sem:
210
+ out[i] = await self.check_batch(chunks[i], **options)
211
+
212
+ await asyncio.gather(*(one(i) for i in range(len(chunks))))
213
+ return [r for batch in out for r in (batch or {}).get("results", [])]
214
+
215
+ async def feedback(self, check_id: str, verdict: str, *, expected_action: Optional[str] = None, note: Optional[str] = None, text: Optional[str] = None) -> dict:
216
+ return await self._call("POST", "/v1/feedback", _drop_none({"check_id": check_id, "verdict": verdict, "expected_action": expected_action, "note": note, "text": text}))
217
+
218
+ async def usage(self) -> dict:
219
+ return await self._call("GET", "/v1/usage")
220
+
221
+ async def get_list(self, project_id: str, kind: str) -> dict:
222
+ return await self._call("GET", f"/v1/projects/{project_id}/lists/{kind}")
223
+
224
+ async def put_list(self, project_id: str, kind: str, terms: List[str]) -> dict:
225
+ return await self._call("PUT", f"/v1/projects/{project_id}/lists/{kind}", {"terms": terms})
226
+
227
+ async def delete_list(self, project_id: str, kind: str) -> None:
228
+ await self._call("DELETE", f"/v1/projects/{project_id}/lists/{kind}")
@@ -0,0 +1,49 @@
1
+ Metadata-Version: 2.5
2
+ Name: unprofane
3
+ Version: 0.1.0
4
+ Summary: Unprofane: a profanity and toxicity API that is hard to fool. Sync and async client with retries and a batch helper.
5
+ Project-URL: Homepage, https://unprofane.com
6
+ Project-URL: Documentation, https://unprofane.com/docs/
7
+ Project-URL: Source, https://github.com/unprofane/unprofane-python
8
+ Project-URL: Issues, https://github.com/unprofane/unprofane-python/issues
9
+ Author-email: Unprofane <hello@unprofane.com>
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: filter,moderation,perspective-api,profanity,toxicity
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Topic :: Software Development :: Libraries
16
+ Requires-Python: >=3.9
17
+ Requires-Dist: httpx>=0.25
18
+ Provides-Extra: test
19
+ Requires-Dist: pytest-asyncio>=0.23; extra == 'test'
20
+ Requires-Dist: pytest>=8; extra == 'test'
21
+ Description-Content-Type: text/markdown
22
+
23
+ # unprofane
24
+
25
+ The Python client for [Unprofane](https://unprofane.com), a profanity and toxicity API that is hard to fool.
26
+
27
+ ```bash
28
+ pip install unprofane
29
+ ```
30
+
31
+ ```python
32
+ from unprofane import Unprofane
33
+
34
+ client = Unprofane("up_live_YOUR_KEY")
35
+ r = client.check("xXsh1tXx", context="username", policy="teen")
36
+ if not r["allowed"]:
37
+ print(r["action"], r["evasion"], r["censored"])
38
+ ```
39
+
40
+ Async:
41
+
42
+ ```python
43
+ from unprofane import AsyncUnprofane
44
+
45
+ async with AsyncUnprofane("up_live_YOUR_KEY") as client:
46
+ r = await client.check("hello", context="comment")
47
+ ```
48
+
49
+ `check`, `check_batch` (up to 100), `check_many` (any number, chunked), `feedback`, `usage`, `get_list`, `put_list`, `delete_list`. Retries 429 and 5xx with jitter and honours `Retry-After`; other failures raise `UnprofaneError` with the problem details and the request id. Docs: https://unprofane.com/docs/.
@@ -0,0 +1,6 @@
1
+ unprofane/__init__.py,sha256=4Af08OxHabhYkNWtUzBzPafK5VOaKLGw_WMJxzEZFjI,220
2
+ unprofane/client.py,sha256=CHHO2-MuRRD9UnOewUOimBa1dr2XKcxLqkqxKZQbLcE,10706
3
+ unprofane-0.1.0.dist-info/METADATA,sha256=rz4Fqa4-1iAcKPWoZ3DP-XtsV7UJJKskwjF8IQH8ito,1801
4
+ unprofane-0.1.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
5
+ unprofane-0.1.0.dist-info/licenses/LICENSE,sha256=uvNz6sTaM3hkOUXzGLUrAlOImQO6ErxH_bCPvrhjmSY,1089
6
+ unprofane-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.32.4
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Tomer N. Marketing and Sales Ltd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.