Metadata-Version: 2.4
Name: salmon-fish
Version: 0.1.0
Summary: Inspectable S2-Pro Dual-AR inference for teaching, using official PyTorch components
License-Expression: MIT AND LicenseRef-Fish-Audio-Community
Keywords: tts,speech-synthesis,s2-pro,fish-audio,autoregressive,education
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Education
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Multimedia :: Sound/Audio :: Speech
Requires-Python: <3.14,>=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: Notice
License-File: third_party/FISH_SPEECH_LICENSE
Requires-Dist: torch==2.11.0
Requires-Dist: torchaudio==2.11.0
Requires-Dist: transformers==4.57.1
Requires-Dist: huggingface-hub<1,>=0.34
Requires-Dist: safetensors>=0.6
Requires-Dist: numpy>=2.1
Requires-Dist: soundfile>=0.13
Requires-Dist: einops>=0.8
Requires-Dist: hydra-core==1.3.2
Requires-Dist: loguru>=0.7
Requires-Dist: click>=8.1
Requires-Dist: tqdm>=4.67
Requires-Dist: descript-audio-codec==1.0.0
Requires-Dist: protobuf<3.20,>=3.19.6
Requires-Dist: tensorboard==2.18.0
Requires-Dist: ipywidgets>=8.1.9
Requires-Dist: ipykernel>=7.4.0
Dynamic: license-file

# salmon-fish

Built with Fish Audio

以簡潔 Python API 使用 **Fish Audio S2-Pro 真實權重**，支援文字轉語音、參考語音 ICL、
可檢查的 prompt packing 與 WAV 輸出。復用官方模型層、KV cache、採樣器與 DAC codec，
不需要 SGLang 或常駐服務。

教學核心集中在 [`ar.py`](src/salmon_fish/ar.py)：公開的 `decode_one_token_ar()` 保持官方
單幀解碼行為，並可透過 `on_event` 觀察 fusion、slow logits／條件 hidden、semantic／RAS
採樣、fast prefill、各 codebook 的 logits／probs／token，以及完整 frame。
詳見 [Dual-AR 逐步教學與等價性驗證](docs/teaching.md)。

套件名稱：`salmon-fish`；Python import：`salmon_fish`。目前為發布前版本，尚未上傳 PyPI。

## 安裝

Python 3.11–3.13。從本地原始碼以標準 pip 安裝：

```bash
python -m pip install .
```

正式發布後的安裝名稱為 `pip install salmon-fish`。本專案固定配對的 Torch／torchaudio
2.11.0；已有相同版本（包含相符的 CUDA build）可直接重用。需要選擇 CUDA runtime 時，
先依 [PyTorch 官方方式](https://pytorch.org/get-started/locally/) 安裝對應的兩個套件，再安裝本套件。
本地開發的 `uv sync --locked` 預設沿用 CUDA 13.0 wheel 索引；pip 安裝不依賴 uv 設定。

## 最小 Python API

```python
from salmon_fish import S2Pro

tts = S2Pro.from_pretrained()  # 自動下載 fishaudio/s2-pro，之後重用快取
result = tts.infer("你好，歡迎使用語音合成。", "output.wav")
print(result.sample_rate, result.codes.shape, result.finish_reason)
```

觀察中間狀態只需加入回呼；一般推論與教學觀察使用同一份核心流程：

```python
def inspect(event):
    if event.frame_index == 0:
        print(event.stage, event.codebook_index,
              {name: tuple(t.shape) for name, t in event.tensors.items()})

tts.infer("你好。", "teaching.wav", on_event=inspect)
```

回呼收到獨立 CPU tensor 快照；預設不收集。也可從 `salmon_fish` 直接匯入
`decode_one_token_ar` 與 `DecodeEvent`，逐幀操作：

```bash
uv run python examples/inspect_ar.py --model ./checkpoints/s2-pro --frames 2
```

首次使用下載約 11 GB。預設使用固定 revision
`1de9996b6be38b745688de084d87a5633f714e4e`；權重、tokenizer、codec 與模型 license
由 Hugging Face 官方 API 取得，不包含在 Python 發布檔或 GitHub 原始碼中。
只有 `from_pretrained()`／`download_model()` 會觸發模型資產下載；一般 import 與 CLI help 不會。

```python
from salmon_fish import download_model, S2Pro

checkpoint = download_model(cache_dir="/path/to/model-cache")  # 可選：預先下載
# 也可直接傳 cache_dir 給 from_pretrained()，自動下載並載入。
tts = S2Pro.from_pretrained(cache_dir="/path/to/model-cache", local_files_only=True)
# 已有獨立 checkpoint 目錄：S2Pro.from_pretrained("./checkpoints/s2-pro")
```

不指定 `cache_dir` 時沿用 Hugging Face 的 `HF_HOME`／`HF_HUB_CACHE`；離線使用
`local_files_only=True`。需要驗證身分時使用標準 `HF_TOKEN`／`hf auth login`，程式不保存金鑰。
下載行為遵循 [Hugging Face snapshot cache API](https://huggingface.co/docs/huggingface_hub/package_reference/file_download)。

## ICL：參考語音 → 新語音

```python
from salmon_fish import S2Pro

tts = S2Pro.from_pretrained()
reference = tts.encode_reference(
    audio_path="reference.wav",
    transcript="這裡必須是參考語音實際說出的文字。",
)
new_text = "這是希望使用參考聲音生成的新句子。"
prompt = tts.pack_prompt(new_text, reference=reference)  # [11, prompt_length]
result = tts.infer(new_text, "new.wav", reference=reference)
```

`reference` 可重用多次。ICL 不更新模型權重，也不自動辨識逐字稿。
[`pack_prompt()`](src/salmon_fish/pipeline.py) 保留完整格式與矩陣布局的程式註解：

```text
system: 參考逐字稿 + VQPart(參考音訊 codes)
user:   要生成的新文字
assistant: <|voice|>，保持未結束狀態，從此生成新音訊
```

每個參考音訊 frame 佔一欄：第 0 列是 `semantic_begin_id + codes[0]`，第 1～10 列
是 10 個 codebook；純文字欄位的第 1～10 列為零。官方 token fusion 直接處理此矩陣。
輸出 WAV 只包含新生成的語音，不接上參考錄音。

## CLI

```bash
salmon-fish infer '你好，歡迎使用語音合成。' --output hello.wav
salmon-fish infer '新しい日本語の文章です。' --output new.wav \
  --reference-audio reference.wav --reference-text '参照音声の正確な文字起こしです。'

salmon-fish download                            # 下載至標準 HF cache
salmon-fish download --local-dir checkpoints/s2-pro
salmon-fish infer '離線測試。' --model ./checkpoints/s2-pro --offline
python -m salmon_fish --help
```

CLI 可指定 `--cache-dir`、`--revision`、`--device`、context 長度與 sampling 參數。
舊版 `from s2_pro import S2Pro` 仍提供相容匯入；新程式請使用 `salmon_fish`。
原始碼目錄中的 `main.py` 也保留為舊 CLI 啟動器。

## 結構與開發

```text
src/salmon_fish/
  __init__.py          公開 API，延遲匯入模型
  pipeline.py          S2Pro、ICL、prompt packing、雙層 AR、WAV
  ar.py                單幀雙 AR 教學核心與 DecodeEvent 觀察介面
  types.py             SpeechReference／SpeechOutput
  hub.py               自動下載、cache、本地與離線解析
  _loading.py          實際權重與 codec 載入、架構／參數驗證
  cli.py               infer／download
  _vendor/fish_speech/  固定上游 commit 的官方推論元件，隔離命名空間
examples/              基本、通用 ICL、日語語料範例
scripts/check_dist.py  發布檔內容驗證
tests/                 單元測試與真實權重整合驗證
.github/workflows/     Python 3.11–3.13 CI 與手動發布工作流程
```

```bash
uv sync --locked
uv run ruff check src tests examples scripts main.py
uv run python -m unittest discover -s tests -v
uv run python tests/smoke_checkpoint.py --model checkpoints/s2-pro  # 可選，需要 GPU／權重
uv run python examples/icl_japanese.py --model checkpoints/s2-pro # 可選，需要本地 data/
```

`data/`、`checkpoints/`、`outputs/` 與本地憑證已排除 Git 與發布檔；語料範例需要自行準備資料。
CI 只下載 tokenizer，使用 CPU 測試，不下載 11 GB 模型。

- [實際架構、完整 ICL prompt、元件對應與 GPU 驗證](docs/architecture.md)
- [GitHub／PyPI 發布步驟](docs/publishing.md)
- [上游版本與命名空間調整](third_party/README.md)

## 授權

自行撰寫的整合程式碼採 [MIT](LICENSE)。`ar.py` 的官方單步流程改編及內附官方程式碼保留
[Fish Audio Community License](third_party/FISH_SPEECH_LICENSE)；此部分僅調整 Python import
與 Hydra target 的命名空間，模型計算保持不變。整體發布檔的 license expression 為
`MIT AND LicenseRef-Fish-Audio-Community`。

S2-Pro 權重另依模型庫的
[Fish Audio Research License](https://huggingface.co/fishaudio/s2-pro/blob/main/LICENSE.md)
授權；自動下載不變更其授權。MIT 不涵蓋官方程式碼或模型權重。
