nativelang-classify 0.2.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.
- nativelang_classify/__init__.py +108 -0
- nativelang_classify/bootstrap.sh +74 -0
- nativelang_classify-0.2.0.dist-info/METADATA +8 -0
- nativelang_classify-0.2.0.dist-info/RECORD +7 -0
- nativelang_classify-0.2.0.dist-info/WHEEL +5 -0
- nativelang_classify-0.2.0.dist-info/licenses/LICENSE +21 -0
- nativelang_classify-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"""Classify text locally using one shared worker per Python process."""
|
|
2
|
+
|
|
3
|
+
import atexit
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import subprocess
|
|
7
|
+
import threading
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Optional
|
|
10
|
+
|
|
11
|
+
__all__ = ["classify", "close"]
|
|
12
|
+
|
|
13
|
+
_lock = threading.Lock()
|
|
14
|
+
_worker = None
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _after_fork():
|
|
18
|
+
# Drop inherited pipes without signaling the parent process’s worker.
|
|
19
|
+
global _worker
|
|
20
|
+
try:
|
|
21
|
+
if _worker is not None:
|
|
22
|
+
_worker.stdin.close()
|
|
23
|
+
_worker.stdout.close()
|
|
24
|
+
_worker.poll()
|
|
25
|
+
_worker = None
|
|
26
|
+
finally:
|
|
27
|
+
_lock.release()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
if hasattr(os, "register_at_fork"):
|
|
31
|
+
os.register_at_fork(
|
|
32
|
+
before=_lock.acquire, after_in_parent=_lock.release, after_in_child=_after_fork
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _start():
|
|
37
|
+
if os.name != "posix":
|
|
38
|
+
raise RuntimeError("Local Classify for Python requires macOS or Linux.")
|
|
39
|
+
return subprocess.Popen(
|
|
40
|
+
["/bin/sh", str(Path(__file__).with_name("bootstrap.sh"))],
|
|
41
|
+
stdin=subprocess.PIPE,
|
|
42
|
+
stdout=subprocess.PIPE,
|
|
43
|
+
encoding="utf-8",
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _stop():
|
|
48
|
+
global _worker
|
|
49
|
+
worker, _worker = _worker, None
|
|
50
|
+
if worker is None:
|
|
51
|
+
return
|
|
52
|
+
try:
|
|
53
|
+
worker.stdin.close()
|
|
54
|
+
except BrokenPipeError:
|
|
55
|
+
pass
|
|
56
|
+
worker.stdout.close()
|
|
57
|
+
try:
|
|
58
|
+
worker.wait(timeout=3)
|
|
59
|
+
except subprocess.TimeoutExpired:
|
|
60
|
+
worker.kill()
|
|
61
|
+
worker.wait()
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def close() -> None:
|
|
65
|
+
"""Release the local worker. A subsequent call to classify starts it again."""
|
|
66
|
+
with _lock:
|
|
67
|
+
_stop()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
atexit.register(close)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def classify(text: str, categories) -> Optional[str]:
|
|
74
|
+
"""Return the selected category ID, or None when no category fits."""
|
|
75
|
+
global _worker
|
|
76
|
+
request = json.dumps({"text": text, "categories": categories}, allow_nan=False)
|
|
77
|
+
with _lock:
|
|
78
|
+
if _worker is not None and _worker.poll() is not None:
|
|
79
|
+
_stop()
|
|
80
|
+
if _worker is None:
|
|
81
|
+
_worker = _start()
|
|
82
|
+
try:
|
|
83
|
+
_worker.stdin.write(request + "\n")
|
|
84
|
+
_worker.stdin.flush()
|
|
85
|
+
line = _worker.stdout.readline()
|
|
86
|
+
if not line:
|
|
87
|
+
raise RuntimeError("Classify stopped before returning a result.")
|
|
88
|
+
result = json.loads(line)
|
|
89
|
+
except (OSError, UnicodeError, json.JSONDecodeError) as exc:
|
|
90
|
+
_stop()
|
|
91
|
+
raise RuntimeError("Classify could not complete the request.") from exc
|
|
92
|
+
except BaseException:
|
|
93
|
+
# A partial exchange cannot safely serve the next request.
|
|
94
|
+
_stop()
|
|
95
|
+
raise
|
|
96
|
+
|
|
97
|
+
if isinstance(result, dict):
|
|
98
|
+
error = result.get("error")
|
|
99
|
+
if isinstance(error, dict):
|
|
100
|
+
message = error.get("message") or "Classify could not complete the request."
|
|
101
|
+
if error.get("code") == "invalid_argument":
|
|
102
|
+
raise ValueError(message)
|
|
103
|
+
raise RuntimeError(message)
|
|
104
|
+
if result.get("status") == "unknown" and result.get("category") is None:
|
|
105
|
+
return None
|
|
106
|
+
if result.get("status") == "matched" and isinstance(result.get("category"), str):
|
|
107
|
+
return result["category"]
|
|
108
|
+
raise RuntimeError("Classify returned an unexpected result.")
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# Start the published Compact r3 CLI. Requests and responses are JSON Lines.
|
|
3
|
+
set -eu
|
|
4
|
+
|
|
5
|
+
version=0.1.0
|
|
6
|
+
model=dm-classify-compact-r3
|
|
7
|
+
tag="v$version-$model"
|
|
8
|
+
case "$(uname -s)/$(uname -m)" in
|
|
9
|
+
Darwin/arm64)
|
|
10
|
+
target=aarch64-apple-darwin
|
|
11
|
+
sha=64d0972c97a87128d4698aa2940b724f321094e476b41b18531aa93426395dd3
|
|
12
|
+
;;
|
|
13
|
+
Linux/x86_64)
|
|
14
|
+
target=x86_64-unknown-linux-gnu
|
|
15
|
+
sha=211b8fdb1637709e8ecb1a3c5d2be9de9b6c624cca18c8a6df93029477dc6d28
|
|
16
|
+
;;
|
|
17
|
+
Linux/aarch64|Linux/arm64)
|
|
18
|
+
target=aarch64-unknown-linux-gnu
|
|
19
|
+
sha=fe432526cb458c4df37603917e8c4e321c340e30b631b3f2571332998c674877
|
|
20
|
+
;;
|
|
21
|
+
*)
|
|
22
|
+
echo 'The local Classify app supports Apple Silicon macOS and ARM64 or x64 Linux.' >&2
|
|
23
|
+
exit 1
|
|
24
|
+
;;
|
|
25
|
+
esac
|
|
26
|
+
|
|
27
|
+
root="${HOME:?Set HOME to your home directory}/.nativelang/classify/$tag"
|
|
28
|
+
install="$root/$target"
|
|
29
|
+
ready() {
|
|
30
|
+
[ -x "$install/classify" ] && [ -f "$install/model/profile.json" ] && [ -f "$install/model/model.gguf" ]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if ! ready; then (
|
|
34
|
+
umask 077
|
|
35
|
+
mkdir -p "$root"
|
|
36
|
+
# A kernel lock survives waiting installers and releases on process exit.
|
|
37
|
+
# Close it at the end of this subshell, before running inference.
|
|
38
|
+
exec 9>>"$root/.$target.lock"
|
|
39
|
+
case "$target" in
|
|
40
|
+
*-darwin) lockf 9 ;;
|
|
41
|
+
*) flock -x 9 ;;
|
|
42
|
+
esac
|
|
43
|
+
if ready; then exit 0; fi
|
|
44
|
+
if [ -e "$install" ] || [ -L "$install" ]; then
|
|
45
|
+
echo "Incomplete Classify installation at $install; move it aside and try again." >&2
|
|
46
|
+
exit 1
|
|
47
|
+
fi
|
|
48
|
+
|
|
49
|
+
stage="$root/.$target.partial"
|
|
50
|
+
rm -rf "$stage"
|
|
51
|
+
mkdir -p "$stage/unpacked"
|
|
52
|
+
trap 'rm -rf "$stage"' EXIT
|
|
53
|
+
archive="classify-server-$version-$model-$target.tar.gz"
|
|
54
|
+
url="https://github.com/nativelang/classify-artifacts/releases/download/$tag/$archive"
|
|
55
|
+
echo "Downloading $model for $target (about 1.6 GB)..." >&2
|
|
56
|
+
curl -q --fail --location --silent --show-error --proto '=https' --proto-redir '=https' \
|
|
57
|
+
--output "$stage/package.tar.gz" "$url"
|
|
58
|
+
echo 'Verifying Classify download...' >&2
|
|
59
|
+
(
|
|
60
|
+
cd "$stage"
|
|
61
|
+
case "$target" in
|
|
62
|
+
*-darwin) set -- shasum -a 256 ;;
|
|
63
|
+
*) set -- sha256sum ;;
|
|
64
|
+
esac
|
|
65
|
+
printf '%s package.tar.gz\n' "$sha" | "$@" -c - >&2
|
|
66
|
+
)
|
|
67
|
+
echo 'Unpacking Classify...' >&2
|
|
68
|
+
tar -xzf "$stage/package.tar.gz" -C "$stage/unpacked"
|
|
69
|
+
test -x "$stage/unpacked/classify"
|
|
70
|
+
test -f "$stage/unpacked/model/profile.json"
|
|
71
|
+
test -f "$stage/unpacked/model/model.gguf"
|
|
72
|
+
mv "$stage/unpacked" "$install"
|
|
73
|
+
); fi
|
|
74
|
+
exec "$install/classify" infer --profile "$install/model/profile.json"
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
nativelang_classify/__init__.py,sha256=7DPZSWeoeeVa4U_lOFKBVAVpaBEx-1XECfFZr2DJzx0,3179
|
|
2
|
+
nativelang_classify/bootstrap.sh,sha256=vUPeDFUdiQ9EArdNqc3I54l_4uneRItJ5oxonbR6XTw,2596
|
|
3
|
+
nativelang_classify-0.2.0.dist-info/licenses/LICENSE,sha256=lv_mNddn_-tFbk6RtLea4zen19nXJGLkG3knBdtNDaw,1080
|
|
4
|
+
nativelang_classify-0.2.0.dist-info/METADATA,sha256=deF3Edx3aAhxsc5phKEC7_gsy_CeKVZ3WtaCtHebYwo,214
|
|
5
|
+
nativelang_classify-0.2.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
6
|
+
nativelang_classify-0.2.0.dist-info/top_level.txt,sha256=TZhO2wUTn0abTkH-A3f5igmN809_v96wKfxVWTFN_cQ,20
|
|
7
|
+
nativelang_classify-0.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 NativeLang contributors
|
|
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.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
nativelang_classify
|