Metadata-Version: 2.4
Name: torrenkt-tslm
Version: 0.2.0
Summary: Torrent Span Labeling Model python library.
Author-email: Haven Madray <sgpublic2002@gmail.com>
License-Expression: Apache-2.0
Requires-Python: <3.13,>=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy<3,>=2
Requires-Dist: dataclasses-json<0.7,>=0.6.7
Requires-Dist: huggingface-hub<2,>=0.16.4
Provides-Extra: cpu
Requires-Dist: onnxruntime<1.27,>=1.26.0; extra == "cpu"
Provides-Extra: cuda
Requires-Dist: onnxruntime-gpu<1.27,>=1.26.0; extra == "cuda"
Dynamic: license-file

# TSLM

### anitopy 兼容

此仓库提供 anitopy 兼容的接口，只需要将 `import anitopy` 替换为 `import tslm.anitopy` 即可。

## 使用方法

TSLM 会从 Hugging Face 下载推荐模型文件。首次使用时会自动下载，后续会复用本地缓存。

### 主动下载模型

如果希望在程序启动或部署阶段提前下载模型，可以主动调用下载方法。

GPU 推荐模型：

```python
from tslm import Tslm

model_path = Tslm.download_from_huggingface(use_cuda=True)
print(model_path)
```

CPU 推荐模型：

```python
from tslm import Tslm

model_path = Tslm.download_from_huggingface(use_cuda=False)
print(model_path)
```

`use_cuda=True` 会下载 `tslm-fp16.onnx`，适合 NVIDIA GPU 推理。

`use_cuda=False` 会下载 `tslm-int8.onnx`，适合 CPU 推理。

### CPU 推荐用法

没有 GPU 时推荐使用 CPU 配置。模型文件为 `tslm-int8.onnx`，推荐每次处理 1 条。

```python
from tslm import Tslm

tslm = Tslm(use_cuda=False, threads=4)

result = tslm("示例标题")
```

`threads` 表示 ONNX Runtime 使用的 CPU 线程数，可以按机器实际核心数和系统负载设置。例如低配 NAS 可使用 `2` 或 `4`，空闲机器可使用更高线程数。

也可以先主动下载模型，再显式传入模型路径：

```python
from tslm import Tslm

model_path = Tslm.download_from_huggingface(use_cuda=False)
tslm = Tslm(use_cuda=False, threads=4, model_path=model_path)

result = tslm("示例标题")
```

### GPU 推荐用法

有 NVIDIA GPU 时推荐使用 GPU 配置。模型文件为 `tslm-fp16.onnx`，推荐每次处理 2 条，可兼顾显存占用和处理速度。

```python
from tslm import Tslm

tslm = Tslm(use_cuda=True)

result = tslm.batch([
    "示例标题 1",
    "示例标题 2",
])
```

也可以先主动下载模型，再显式传入模型路径：

```python
from tslm import Tslm

model_path = Tslm.download_from_huggingface(use_cuda=True)
tslm = Tslm(use_cuda=True, model_path=model_path)

result = tslm.batch([
    "示例标题 1",
    "示例标题 2",
])
```

### 单条解析

`parse` 默认使用 CPU 推荐模型。

```python
from tslm import parse

result = parse("示例标题")
```

如果希望使用 GPU：

```python
from tslm import parse

result = parse("示例标题", use_cuda=True)
```

### 返回结果

`TSLM.batch()` 返回结果是以原始标题为 key 的字典，value 为识别出的字段列表。
