Automatic Speech Recognition
Safetensors
GGUF
English
asr_model
audio
speech-recognition
asr
speech-to-text
qwen
qwen3.5
granite-speech
tiny-audio
custom_code
conversational
Instructions to use aaron-hu26/my-tiny-audio with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- llama.cpp
How to use aaron-hu26/my-tiny-audio with llama.cpp:
Install (macOS, Linux)
curl -LsSf https://llama.app/install.sh | sh # Start a local OpenAI-compatible server with a web UI: llama serve -hf aaron-hu26/my-tiny-audio:Q8_0 # Run inference directly in the terminal: llama cli -hf aaron-hu26/my-tiny-audio:Q8_0
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama serve -hf aaron-hu26/my-tiny-audio:Q8_0 # Run inference directly in the terminal: llama cli -hf aaron-hu26/my-tiny-audio:Q8_0
Build from source code
git clone https://github.com/ggml-org/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-app # Start a local OpenAI-compatible server with a web UI: ./build/bin/llama serve -hf aaron-hu26/my-tiny-audio:Q8_0 # Run inference directly in the terminal: ./build/bin/llama cli -hf aaron-hu26/my-tiny-audio:Q8_0
Use Docker
docker model run hf.co/aaron-hu26/my-tiny-audio:Q8_0
- Llama new
- LM Studio
- Jan
- Ollama
How to use aaron-hu26/my-tiny-audio with Ollama:
ollama run hf.co/aaron-hu26/my-tiny-audio:Q8_0
- Unsloth Desktop
- Pi
How to use aaron-hu26/my-tiny-audio with Pi:
Start the llama.cpp server
# Install llama.cpp: brew install llama.cpp # Start a local OpenAI-compatible server: llama serve -hf aaron-hu26/my-tiny-audio:Q8_0
Configure the model in Pi
# Install Pi: npm install -g @earendil-works/pi-coding-agent # Add to ~/.pi/agent/models.json: { "providers": { "llama-cpp": { "baseUrl": "http://localhost:9931/v1", "api": "openai-completions", "apiKey": "none", "models": [ { "id": "aaron-hu26/my-tiny-audio:Q8_0" } ] } } }Run Pi
# Start Pi in your project directory: pi
- Docker Model Runner
How to use aaron-hu26/my-tiny-audio with Docker Model Runner:
docker model run hf.co/aaron-hu26/my-tiny-audio:Q8_0
- Lemonade
How to use aaron-hu26/my-tiny-audio with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull aaron-hu26/my-tiny-audio:Q8_0
Run and chat with the model
lemonade run user.my-tiny-audio-Q8_0
List all available models
lemonade list
- Hermes Agent
How to use aaron-hu26/my-tiny-audio with Hermes Agent:
Start the llama.cpp server
# Install llama.cpp: brew install llama.cpp # Start a local OpenAI-compatible server: llama serve -hf aaron-hu26/my-tiny-audio:Q8_0
Configure Hermes
# Install Hermes: curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash hermes setup # Point Hermes at the local server: hermes config set model.provider custom hermes config set model.base_url http://127.0.0.1:9931/v1 hermes config set model.default aaron-hu26/my-tiny-audio:Q8_0
Run Hermes
hermes
- Atomic Chat
- OpenClaw
How to use aaron-hu26/my-tiny-audio with OpenClaw:
Start the llama.cpp server
# Install llama.cpp: brew install llama.cpp # Start a local OpenAI-compatible server: llama serve -hf aaron-hu26/my-tiny-audio:Q8_0
Configure OpenClaw
# Install OpenClaw: npm install -g openclaw@latest # Register the local server and set it as the default model: openclaw onboard --non-interactive --mode local \ --auth-choice custom-api-key \ --custom-base-url http://127.0.0.1:9931/v1 \ --custom-model-id "aaron-hu26/my-tiny-audio:Q8_0" \ --custom-provider-id llama-cpp \ --custom-compatibility openai \ --custom-text-input \ --accept-risk \ --skip-health
Run OpenClaw
openclaw agent --local --agent main --message "Hello from Hugging Face"
Initial release: Qwen3.5-0.8B + Granite Speech 5.0 ASR and GGUF models
Browse files- .gitattributes +4 -33
- Modelfile +16 -0
- README.md +119 -0
- app.py +269 -0
- decoder/chat_template.jinja +154 -0
- decoder/config.json +75 -0
- decoder/generation_config.json +6 -0
- decoder/model.safetensors +3 -0
- decoder/tokenizer.json +3 -0
- decoder/tokenizer_config.json +0 -0
- preprocessor_config.json +14 -0
- projector.pt +3 -0
- qwen_decoder.gguf +3 -0
- qwen_decoder_q8_0.gguf +3 -0
- tiny_audio_metadata.json +31 -0
.gitattributes
CHANGED
|
@@ -1,35 +1,6 @@
|
|
| 1 |
-
*.
|
| 2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
-
*.
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
| 1 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
|
|
|
| 2 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.gguf filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
tokenizer_config.json -filter -diff -merge text
|
| 6 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Modelfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1. 指定本地 GGUF 权重路径
|
| 2 |
+
FROM ./qwen_decoder_q8_0.gguf
|
| 3 |
+
|
| 4 |
+
# 2. 设置生成参数
|
| 5 |
+
PARAMETER temperature 0.2
|
| 6 |
+
PARAMETER top_p 0.9
|
| 7 |
+
PARAMETER stop "<|im_start|>"
|
| 8 |
+
PARAMETER stop "<|im_end|>"
|
| 9 |
+
|
| 10 |
+
# 3. 对齐 Qwen3.5 专用的 Chat 模板
|
| 11 |
+
TEMPLATE """{{ if .System }}<|im_start|>system
|
| 12 |
+
{{ .System }}<|im_end|>
|
| 13 |
+
{{ end }}{{ if .Prompt }}<|im_start|>user
|
| 14 |
+
{{ .Prompt }}<|im_end|>
|
| 15 |
+
{{ end }}<|im_start|>assistant
|
| 16 |
+
{{ .Response }}<|im_end|>"""
|
README.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
license: mit
|
| 5 |
+
tags:
|
| 6 |
+
- audio
|
| 7 |
+
- speech-recognition
|
| 8 |
+
- asr
|
| 9 |
+
- speech-to-text
|
| 10 |
+
- qwen
|
| 11 |
+
- qwen3.5
|
| 12 |
+
- gguf
|
| 13 |
+
- granite-speech
|
| 14 |
+
- tiny-audio
|
| 15 |
+
pipeline_tag: automatic-speech-recognition
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
# My Tiny Audio (Qwen3.5-0.8B + Granite Speech 5.0)
|
| 19 |
+
|
| 20 |
+
<div align="center">
|
| 21 |
+
|
| 22 |
+
**A lightweight, hackable, end-to-end Speech-to-Text Large Language Model.**
|
| 23 |
+
|
| 24 |
+
</div>
|
| 25 |
+
|
| 26 |
+
---
|
| 27 |
+
|
| 28 |
+
## 📖 Model Overview
|
| 29 |
+
|
| 30 |
+
**my-tiny-audio** is a lightweight multimodal speech recognition model built with:
|
| 31 |
+
1. **Audio Encoder (Frozen)**: `ibm-granite/granite-speech-5.0-470m-turboctc-nc` (470M Conformer-based feature extractor).
|
| 32 |
+
2. **Audio Projector (Trained)**: 2-layer MLP projector (`1024` → `1024` GELU → `1024`) with frame pooling stride 1.
|
| 33 |
+
3. **Text Decoder (Fine-tuned & Merged)**: `Qwen/Qwen3.5-0.8B` with LoRA adapter ($r=16, \alpha=32$) mathematically fused into the base weights via `merge_and_unload()`.
|
| 34 |
+
4. **Quantized GGUF Exports**: Both full precision (BF16) and 8-bit quantized (`Q8_0`) GGUF files included for native `llama.cpp` and `Ollama` deployment.
|
| 35 |
+
|
| 36 |
+
---
|
| 37 |
+
|
| 38 |
+
## 📊 Training Specifications
|
| 39 |
+
|
| 40 |
+
- **Hardware**: Single consumer GPU (NVIDIA GeForce RTX 4060 8GB VRAM).
|
| 41 |
+
- **Dataset**: LibriSpeech ASR `clean/validation` (dev-clean, 5.4 hours audio).
|
| 42 |
+
- **Batching**: Per-device batch size 2, gradient accumulation steps 2 (effective batch size 4).
|
| 43 |
+
- **Optimization**: AdamW ($\text{LR}=2\times 10^{-4}$ with warmup and cosine decay).
|
| 44 |
+
- **Convergence**:
|
| 45 |
+
- Initial Loss: **4.238** (Step 1)
|
| 46 |
+
- Final Loss: **0.1069** (Step 800)
|
| 47 |
+
- Status: Successfully converged with stable gradients.
|
| 48 |
+
|
| 49 |
+
---
|
| 50 |
+
|
| 51 |
+
## 📦 Repository Structure
|
| 52 |
+
|
| 53 |
+
```text
|
| 54 |
+
aaron-hu26/my-tiny-audio/
|
| 55 |
+
├── decoder/ # Fully merged Qwen3.5-0.8B Hugging Face model
|
| 56 |
+
│ ├── model.safetensors # 1.43 GB (Base + LoRA weights folded)
|
| 57 |
+
│ ├── config.json # Decoder config with <audio> token
|
| 58 |
+
│ ├── generation_config.json
|
| 59 |
+
│ ├── tokenizer.json # Aligned 248k vocab tokenizer
|
| 60 |
+
│ ├── tokenizer_config.json
|
| 61 |
+
│ └── chat_template.jinja
|
| 62 |
+
├── projector.pt # 4.01 MB (Trained 2-layer MLP audio projector)
|
| 63 |
+
├── preprocessor_config.json # Granite Speech 5.0 feature extraction config
|
| 64 |
+
├── tiny_audio_metadata.json # Multimodal architectural metadata
|
| 65 |
+
├── qwen_decoder_q8_0.gguf # 775 MB (8-bit quantized GGUF for Ollama/llama.cpp)
|
| 66 |
+
├── qwen_decoder.gguf # 1.51 GB (BF16 lossless GGUF)
|
| 67 |
+
├── Modelfile # Ollama model definition file
|
| 68 |
+
└── app.py # Single-file Gradio web demo with microphone & metrics
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
---
|
| 72 |
+
|
| 73 |
+
## 🚀 Quickstart
|
| 74 |
+
|
| 75 |
+
### 1. Web UI (Microphone & Audio Upload)
|
| 76 |
+
|
| 77 |
+
You can launch the complete Web UI with a single command:
|
| 78 |
+
|
| 79 |
+
```bash
|
| 80 |
+
git clone https://huggingface.co/aaron-hu26/my-tiny-audio
|
| 81 |
+
cd my-tiny-audio
|
| 82 |
+
pip install torch transformers torchaudio soundfile gradio peft librosa
|
| 83 |
+
python app.py
|
| 84 |
+
```
|
| 85 |
+
Open `http://localhost:7860` to record from your microphone or upload any WAV/MP3/FLAC audio.
|
| 86 |
+
|
| 87 |
+
### 2. Python Inference
|
| 88 |
+
|
| 89 |
+
```python
|
| 90 |
+
import torch
|
| 91 |
+
import soundfile as sf
|
| 92 |
+
from transformers import AutoFeatureExtractor, AutoTokenizer, AutoModelForCausalLM
|
| 93 |
+
from app import StandaloneASRModel, transcribe_audio
|
| 94 |
+
|
| 95 |
+
# Transcribe any audio file
|
| 96 |
+
text, metrics = transcribe_audio("sample.wav", checkpoint_dir=".")
|
| 97 |
+
print("Transcript:", text)
|
| 98 |
+
print("Metrics:", metrics)
|
| 99 |
+
```
|
| 100 |
+
|
| 101 |
+
### 3. Run GGUF with Ollama
|
| 102 |
+
|
| 103 |
+
The repository bundles an 8-bit quantized GGUF model (`qwen_decoder_q8_0.gguf`) and a ready-to-use `Modelfile`:
|
| 104 |
+
|
| 105 |
+
```bash
|
| 106 |
+
# Register the model in Ollama
|
| 107 |
+
ollama create my-tiny-audio -f Modelfile
|
| 108 |
+
|
| 109 |
+
# Run interactive conversation
|
| 110 |
+
ollama run my-tiny-audio
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
---
|
| 114 |
+
|
| 115 |
+
## 📄 License & Attribution
|
| 116 |
+
|
| 117 |
+
- Built using the **tiny-audio** architecture.
|
| 118 |
+
- Base models: [Granite Speech 5.0](https://huggingface.co/ibm-granite/granite-speech-5.0-470m-turboctc-nc) by IBM and [Qwen3.5-0.8B](https://huggingface.co/Qwen/Qwen3.5-0.8B) by Alibaba Cloud Qwen Team.
|
| 119 |
+
- License: **MIT License**.
|
app.py
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
🎙️ Tiny-Audio 单文件绿色独立版 Web 界面 (Standalone Web UI)
|
| 4 |
+
特点:
|
| 5 |
+
- 零项目目录依赖: 模型架构代码已全部内置于本脚本中,不再依赖 tiny_audio 包。
|
| 6 |
+
- 跨平台兼容: 无论是在 Windows、Mac (Apple Silicon) 还是 Linux 上均可直接运行。
|
| 7 |
+
- 只需携带本文件和 outputs/ 权重目录即可在任意电脑上部署运行。
|
| 8 |
+
|
| 9 |
+
依赖安装:
|
| 10 |
+
pip install torch transformers peft gradio soundfile
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
import os
|
| 14 |
+
import sys
|
| 15 |
+
import time
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
|
| 18 |
+
import gradio as gr
|
| 19 |
+
import numpy as np
|
| 20 |
+
import soundfile as sf
|
| 21 |
+
import torch
|
| 22 |
+
from peft import PeftModel
|
| 23 |
+
from torch import nn
|
| 24 |
+
from transformers import (
|
| 25 |
+
AutoFeatureExtractor,
|
| 26 |
+
AutoModelForCausalLM,
|
| 27 |
+
AutoTokenizer,
|
| 28 |
+
GraniteSpeech5Encoder,
|
| 29 |
+
PreTrainedTokenizerBase,
|
| 30 |
+
)
|
| 31 |
+
from transformers.models.llama.modeling_llama import LlamaRMSNorm
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# ==============================================================================
|
| 35 |
+
# 1. 内置投影层与基座加载逻辑 (完全自包含)
|
| 36 |
+
# ==============================================================================
|
| 37 |
+
def _frame_stack_length(length: int | torch.Tensor, k: int) -> int | torch.Tensor:
|
| 38 |
+
return (length - k) // k + 1
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def _frame_stack(x: torch.Tensor, k: int) -> torch.Tensor:
|
| 42 |
+
batch, seq, dim = x.shape
|
| 43 |
+
out_len = _frame_stack_length(seq, k)
|
| 44 |
+
return x[:, : out_len * k, :].reshape(batch, out_len, dim * k)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
class MLPAudioProjector(nn.Module):
|
| 48 |
+
def __init__(self, config: object) -> None:
|
| 49 |
+
super().__init__()
|
| 50 |
+
encoder_dim: int = getattr(config, "encoder_dim", 1024)
|
| 51 |
+
llm_dim: int = getattr(config, "llm_dim", 2048)
|
| 52 |
+
self.k: int = getattr(config, "projector_pool_stride", 1)
|
| 53 |
+
|
| 54 |
+
in_dim = encoder_dim * self.k
|
| 55 |
+
hidden_dim: int = getattr(config, "projector_hidden_dim", None) or llm_dim
|
| 56 |
+
self.input_norm = LlamaRMSNorm(in_dim, eps=1e-6)
|
| 57 |
+
self.linear_1 = nn.Linear(in_dim, hidden_dim)
|
| 58 |
+
self.act = nn.GELU()
|
| 59 |
+
self.linear_2 = nn.Linear(hidden_dim, llm_dim)
|
| 60 |
+
|
| 61 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 62 |
+
x = _frame_stack(x, self.k)
|
| 63 |
+
x = self.input_norm(x)
|
| 64 |
+
x = self.linear_1(x)
|
| 65 |
+
x = self.act(x)
|
| 66 |
+
return self.linear_2(x)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
class StandaloneASRModel(nn.Module):
|
| 70 |
+
def __init__(
|
| 71 |
+
self,
|
| 72 |
+
encoder_model_id: str = "ibm-granite/granite-speech-5.0-470m-turboctc-nc",
|
| 73 |
+
decoder_model_id: str = "Qwen/Qwen3.5-0.8B",
|
| 74 |
+
device: str = "cuda" if torch.cuda.is_available() else "cpu",
|
| 75 |
+
dtype: torch.dtype = torch.bfloat16,
|
| 76 |
+
):
|
| 77 |
+
super().__init__()
|
| 78 |
+
self.device = device
|
| 79 |
+
self.dtype = dtype
|
| 80 |
+
|
| 81 |
+
print("1/3 加载音频特征提取器与 Granite-Speech-5.0 编码器...")
|
| 82 |
+
self.feature_extractor = AutoFeatureExtractor.from_pretrained(encoder_model_id)
|
| 83 |
+
self.encoder = GraniteSpeech5Encoder.from_pretrained(
|
| 84 |
+
encoder_model_id,
|
| 85 |
+
torch_dtype=dtype,
|
| 86 |
+
attn_implementation="sdpa",
|
| 87 |
+
low_cpu_mem_usage=True,
|
| 88 |
+
).to(device=device, dtype=dtype)
|
| 89 |
+
self.encoder.eval().requires_grad_(False)
|
| 90 |
+
|
| 91 |
+
print("2/3 加载 Qwen 分词器与文本解码器...")
|
| 92 |
+
self.tokenizer = AutoTokenizer.from_pretrained(decoder_model_id, trust_remote_code=True)
|
| 93 |
+
if self.tokenizer.pad_token is None:
|
| 94 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 95 |
+
|
| 96 |
+
self.decoder = AutoModelForCausalLM.from_pretrained(
|
| 97 |
+
decoder_model_id,
|
| 98 |
+
dtype=dtype,
|
| 99 |
+
attn_implementation="sdpa",
|
| 100 |
+
low_cpu_mem_usage=True,
|
| 101 |
+
trust_remote_code=True,
|
| 102 |
+
).to(device=device, dtype=dtype)
|
| 103 |
+
self.decoder.eval().requires_grad_(False)
|
| 104 |
+
|
| 105 |
+
# 注册 <audio> 特殊标记
|
| 106 |
+
self.audio_token = "<audio>"
|
| 107 |
+
if self.audio_token not in self.tokenizer.get_vocab():
|
| 108 |
+
existing = getattr(self.tokenizer, "additional_special_tokens", None) or []
|
| 109 |
+
self.tokenizer.add_special_tokens(
|
| 110 |
+
{"additional_special_tokens": [*existing, self.audio_token]}
|
| 111 |
+
)
|
| 112 |
+
self.decoder.resize_token_embeddings(len(self.tokenizer), pad_to_multiple_of=128)
|
| 113 |
+
self.audio_token_id = self.tokenizer.convert_tokens_to_ids(self.audio_token)
|
| 114 |
+
|
| 115 |
+
print("3/3 组装 MLP 投影层...")
|
| 116 |
+
class ProjCfg:
|
| 117 |
+
encoder_dim = self.encoder.config.hidden_size
|
| 118 |
+
llm_dim = self.decoder.config.hidden_size
|
| 119 |
+
projector_pool_stride = 1
|
| 120 |
+
projector_hidden_dim = self.decoder.config.hidden_size
|
| 121 |
+
|
| 122 |
+
self.projector = MLPAudioProjector(ProjCfg()).to(device=device, dtype=dtype)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
# ==============================================================================
|
| 126 |
+
# 2. 单例模型管理与转录执行
|
| 127 |
+
# ==============================================================================
|
| 128 |
+
_MODEL = None
|
| 129 |
+
_CKPT = None
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def get_model(checkpoint_dir: str):
|
| 133 |
+
global _MODEL, _CKPT
|
| 134 |
+
resolved = Path(checkpoint_dir).resolve()
|
| 135 |
+
if _MODEL is not None and _CKPT == str(resolved):
|
| 136 |
+
return _MODEL
|
| 137 |
+
|
| 138 |
+
device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
|
| 139 |
+
dtype = torch.bfloat16 if device == "cuda" and torch.cuda.is_bf16_supported() else torch.float32
|
| 140 |
+
|
| 141 |
+
print(f"\n📦 正在初始化基座并加载微调权重: {resolved}")
|
| 142 |
+
merged_decoder_dir = resolved / "decoder"
|
| 143 |
+
decoder_id = str(merged_decoder_dir) if (merged_decoder_dir / "model.safetensors").exists() else "Qwen/Qwen3.5-0.8B"
|
| 144 |
+
model = StandaloneASRModel(decoder_model_id=decoder_id, device=device, dtype=dtype)
|
| 145 |
+
|
| 146 |
+
# 1. 载入 Projector 权重
|
| 147 |
+
proj_pt = resolved / "projector.pt"
|
| 148 |
+
if proj_pt.exists():
|
| 149 |
+
print(f"🔗 载入 Projector 权重: {proj_pt}")
|
| 150 |
+
model.projector.load_state_dict(torch.load(proj_pt, map_location=device))
|
| 151 |
+
else:
|
| 152 |
+
print(f"⚠️ 警告: 未找到 {proj_pt}")
|
| 153 |
+
|
| 154 |
+
# 2. 载入 LoRA 适配层并融合 (若已是融合权重目录则自动跳过)
|
| 155 |
+
adapter_dir = resolved / "decoder_adapter"
|
| 156 |
+
if adapter_dir.exists():
|
| 157 |
+
print(f"🧠 载入 Decoder LoRA 权重: {adapter_dir}")
|
| 158 |
+
model.decoder = PeftModel.from_pretrained(model.decoder, str(adapter_dir))
|
| 159 |
+
if hasattr(model.decoder, "merge_and_unload"):
|
| 160 |
+
model.decoder = model.decoder.merge_and_unload()
|
| 161 |
+
print("🚀 LoRA 权重已成功融合至基座矩阵!")
|
| 162 |
+
elif (merged_decoder_dir / "model.safetensors").exists():
|
| 163 |
+
print("🚀 已直接加载完全融合的独立 Decoder 权重 (无需 LoRA 转换)!")
|
| 164 |
+
|
| 165 |
+
model.eval()
|
| 166 |
+
_MODEL = model
|
| 167 |
+
_CKPT = str(resolved)
|
| 168 |
+
print("✅ 模型就绪!\n")
|
| 169 |
+
return _MODEL
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def transcribe_audio(audio_path: str | None, checkpoint_dir: str, max_new_tokens: int = 128):
|
| 173 |
+
if not audio_path:
|
| 174 |
+
return "⚠️ 请先录制或上传音频!", ""
|
| 175 |
+
|
| 176 |
+
start_t = time.time()
|
| 177 |
+
try:
|
| 178 |
+
model = get_model(checkpoint_dir)
|
| 179 |
+
device = model.device
|
| 180 |
+
|
| 181 |
+
# 1. 读取音频波形并重采样
|
| 182 |
+
speech, sr = sf.read(audio_path)
|
| 183 |
+
if speech.ndim > 1:
|
| 184 |
+
speech = speech.mean(axis=-1)
|
| 185 |
+
if sr != 16000:
|
| 186 |
+
import torchaudio.transforms as T
|
| 187 |
+
speech = T.Resample(sr, 16000)(torch.tensor(speech, dtype=torch.float32)).numpy()
|
| 188 |
+
sr = 16000
|
| 189 |
+
|
| 190 |
+
audio_len = len(speech) / sr
|
| 191 |
+
|
| 192 |
+
# 2. 提取特征
|
| 193 |
+
audio_in = model.feature_extractor(speech, sampling_rate=16000, return_tensors="pt")
|
| 194 |
+
feats = audio_in.input_features.to(device=device, dtype=model.dtype)
|
| 195 |
+
|
| 196 |
+
# 3. 前向提取与特征替换
|
| 197 |
+
with torch.no_grad():
|
| 198 |
+
hidden = model.encoder(input_features=feats).last_hidden_state
|
| 199 |
+
proj_dtype = model.projector.linear_1.weight.dtype
|
| 200 |
+
embeds = model.projector(hidden.to(dtype=proj_dtype))
|
| 201 |
+
num_tokens = embeds.shape[1]
|
| 202 |
+
|
| 203 |
+
prompt = (
|
| 204 |
+
f"<|im_start|>user\n{'<audio>' * num_tokens} Transcribe the speech to text.<|im_end|>\n"
|
| 205 |
+
f"<|im_start|>assistant\n"
|
| 206 |
+
)
|
| 207 |
+
input_ids = model.tokenizer.encode(prompt, return_tensors="pt").to(device)
|
| 208 |
+
inputs_embeds = model.decoder.get_input_embeddings()(input_ids)
|
| 209 |
+
|
| 210 |
+
is_audio = input_ids == model.audio_token_id
|
| 211 |
+
inputs_embeds = inputs_embeds.masked_scatter(is_audio.unsqueeze(-1), embeds.to(inputs_embeds.dtype))
|
| 212 |
+
|
| 213 |
+
# 4. 生成文本
|
| 214 |
+
out = model.decoder.generate(
|
| 215 |
+
inputs_embeds=inputs_embeds,
|
| 216 |
+
max_new_tokens=max_new_tokens,
|
| 217 |
+
do_sample=False,
|
| 218 |
+
eos_token_id=model.tokenizer.eos_token_id,
|
| 219 |
+
pad_token_id=model.tokenizer.pad_token_id,
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
text = model.tokenizer.decode(out[0], skip_special_tokens=True).strip()
|
| 223 |
+
elapsed = time.time() - start_t
|
| 224 |
+
rtf = elapsed / max(audio_len, 0.01)
|
| 225 |
+
|
| 226 |
+
metrics = f"⏱️ **音频时长**: `{audio_len:.2f}s` | ⚡ **推理耗时**: `{elapsed:.2f}s` | 🚀 **实时率 (RTF)**: `{rtf:.2f}x`"
|
| 227 |
+
return text, metrics
|
| 228 |
+
|
| 229 |
+
except Exception as e:
|
| 230 |
+
import traceback
|
| 231 |
+
return f"❌ 运行异常:\n{traceback.format_exc()}", ""
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
# ==============================================================================
|
| 235 |
+
# 3. Gradio Web 界面构建
|
| 236 |
+
# ==============================================================================
|
| 237 |
+
with gr.Blocks(title="Tiny-Audio Standalone ASR") as demo:
|
| 238 |
+
gr.HTML(
|
| 239 |
+
"""
|
| 240 |
+
<div style="text-align: center; margin-bottom: 1.5rem;">
|
| 241 |
+
<h1 style="font-size: 2rem; color: #2563eb;">🎙️ Tiny-Audio 语音转录系统 (独立部署版)</h1>
|
| 242 |
+
<p style="color: #64748b;">单文件自包含运行脚本 · 无缝跨平台兼容 · 支持麦克风与音频文件</p>
|
| 243 |
+
</div>
|
| 244 |
+
"""
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
with gr.Row():
|
| 248 |
+
with gr.Column():
|
| 249 |
+
audio_in = gr.Audio(sources=["microphone", "upload"], type="filepath", label="🎤 麦克风录音 / 上传音频文件")
|
| 250 |
+
with gr.Accordion("⚙️ 模型配置", open=False):
|
| 251 |
+
ckpt_in = gr.Textbox(value=".", label="检查点权重路径")
|
| 252 |
+
tokens_slider = gr.Slider(minimum=32, maximum=512, value=128, step=32, label="Max New Tokens")
|
| 253 |
+
with gr.Row():
|
| 254 |
+
clear_btn = gr.ClearButton([audio_in], value="清空")
|
| 255 |
+
submit_btn = gr.Button("开始识别 (Transcribe)", variant="primary", scale=2)
|
| 256 |
+
|
| 257 |
+
with gr.Column():
|
| 258 |
+
text_out = gr.Textbox(label="📝 识别结果", lines=7, placeholder="识别结果将在此显示...")
|
| 259 |
+
metrics_out = gr.Markdown()
|
| 260 |
+
|
| 261 |
+
submit_btn.click(
|
| 262 |
+
fn=transcribe_audio,
|
| 263 |
+
inputs=[audio_in, ckpt_in, tokens_slider],
|
| 264 |
+
outputs=[text_out, metrics_out],
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
if __name__ == "__main__":
|
| 269 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
decoder/chat_template.jinja
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{%- set image_count = namespace(value=0) %}
|
| 2 |
+
{%- set video_count = namespace(value=0) %}
|
| 3 |
+
{%- macro render_content(content, do_vision_count, is_system_content=false) %}
|
| 4 |
+
{%- if content is string %}
|
| 5 |
+
{{- content }}
|
| 6 |
+
{%- elif content is iterable and content is not mapping %}
|
| 7 |
+
{%- for item in content %}
|
| 8 |
+
{%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
|
| 9 |
+
{%- if is_system_content %}
|
| 10 |
+
{{- raise_exception('System message cannot contain images.') }}
|
| 11 |
+
{%- endif %}
|
| 12 |
+
{%- if do_vision_count %}
|
| 13 |
+
{%- set image_count.value = image_count.value + 1 %}
|
| 14 |
+
{%- endif %}
|
| 15 |
+
{%- if add_vision_id %}
|
| 16 |
+
{{- 'Picture ' ~ image_count.value ~ ': ' }}
|
| 17 |
+
{%- endif %}
|
| 18 |
+
{{- '<|vision_start|><|image_pad|><|vision_end|>' }}
|
| 19 |
+
{%- elif 'video' in item or item.type == 'video' %}
|
| 20 |
+
{%- if is_system_content %}
|
| 21 |
+
{{- raise_exception('System message cannot contain videos.') }}
|
| 22 |
+
{%- endif %}
|
| 23 |
+
{%- if do_vision_count %}
|
| 24 |
+
{%- set video_count.value = video_count.value + 1 %}
|
| 25 |
+
{%- endif %}
|
| 26 |
+
{%- if add_vision_id %}
|
| 27 |
+
{{- 'Video ' ~ video_count.value ~ ': ' }}
|
| 28 |
+
{%- endif %}
|
| 29 |
+
{{- '<|vision_start|><|video_pad|><|vision_end|>' }}
|
| 30 |
+
{%- elif 'text' in item %}
|
| 31 |
+
{{- item.text }}
|
| 32 |
+
{%- else %}
|
| 33 |
+
{{- raise_exception('Unexpected item type in content.') }}
|
| 34 |
+
{%- endif %}
|
| 35 |
+
{%- endfor %}
|
| 36 |
+
{%- elif content is none or content is undefined %}
|
| 37 |
+
{{- '' }}
|
| 38 |
+
{%- else %}
|
| 39 |
+
{{- raise_exception('Unexpected content type.') }}
|
| 40 |
+
{%- endif %}
|
| 41 |
+
{%- endmacro %}
|
| 42 |
+
{%- if not messages %}
|
| 43 |
+
{{- raise_exception('No messages provided.') }}
|
| 44 |
+
{%- endif %}
|
| 45 |
+
{%- if tools and tools is iterable and tools is not mapping %}
|
| 46 |
+
{{- '<|im_start|>system\n' }}
|
| 47 |
+
{{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
|
| 48 |
+
{%- for tool in tools %}
|
| 49 |
+
{{- "\n" }}
|
| 50 |
+
{{- tool | tojson }}
|
| 51 |
+
{%- endfor %}
|
| 52 |
+
{{- "\n</tools>" }}
|
| 53 |
+
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
|
| 54 |
+
{%- if messages[0].role == 'system' %}
|
| 55 |
+
{%- set content = render_content(messages[0].content, false, true)|trim %}
|
| 56 |
+
{%- if content %}
|
| 57 |
+
{{- '\n\n' + content }}
|
| 58 |
+
{%- endif %}
|
| 59 |
+
{%- endif %}
|
| 60 |
+
{{- '<|im_end|>\n' }}
|
| 61 |
+
{%- else %}
|
| 62 |
+
{%- if messages[0].role == 'system' %}
|
| 63 |
+
{%- set content = render_content(messages[0].content, false, true)|trim %}
|
| 64 |
+
{{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
|
| 65 |
+
{%- endif %}
|
| 66 |
+
{%- endif %}
|
| 67 |
+
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
|
| 68 |
+
{%- for message in messages[::-1] %}
|
| 69 |
+
{%- set index = (messages|length - 1) - loop.index0 %}
|
| 70 |
+
{%- if ns.multi_step_tool and message.role == "user" %}
|
| 71 |
+
{%- set content = render_content(message.content, false)|trim %}
|
| 72 |
+
{%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
|
| 73 |
+
{%- set ns.multi_step_tool = false %}
|
| 74 |
+
{%- set ns.last_query_index = index %}
|
| 75 |
+
{%- endif %}
|
| 76 |
+
{%- endif %}
|
| 77 |
+
{%- endfor %}
|
| 78 |
+
{%- if ns.multi_step_tool %}
|
| 79 |
+
{{- raise_exception('No user query found in messages.') }}
|
| 80 |
+
{%- endif %}
|
| 81 |
+
{%- for message in messages %}
|
| 82 |
+
{%- set content = render_content(message.content, true)|trim %}
|
| 83 |
+
{%- if message.role == "system" %}
|
| 84 |
+
{%- if not loop.first %}
|
| 85 |
+
{{- raise_exception('System message must be at the beginning.') }}
|
| 86 |
+
{%- endif %}
|
| 87 |
+
{%- elif message.role == "user" %}
|
| 88 |
+
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
|
| 89 |
+
{%- elif message.role == "assistant" %}
|
| 90 |
+
{%- set reasoning_content = '' %}
|
| 91 |
+
{%- if message.reasoning_content is string %}
|
| 92 |
+
{%- set reasoning_content = message.reasoning_content %}
|
| 93 |
+
{%- else %}
|
| 94 |
+
{%- if '</think>' in content %}
|
| 95 |
+
{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
|
| 96 |
+
{%- set content = content.split('</think>')[-1].lstrip('\n') %}
|
| 97 |
+
{%- endif %}
|
| 98 |
+
{%- endif %}
|
| 99 |
+
{%- set reasoning_content = reasoning_content|trim %}
|
| 100 |
+
{%- if loop.index0 > ns.last_query_index %}
|
| 101 |
+
{{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
|
| 102 |
+
{%- else %}
|
| 103 |
+
{{- '<|im_start|>' + message.role + '\n' + content }}
|
| 104 |
+
{%- endif %}
|
| 105 |
+
{%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
|
| 106 |
+
{%- for tool_call in message.tool_calls %}
|
| 107 |
+
{%- if tool_call.function is defined %}
|
| 108 |
+
{%- set tool_call = tool_call.function %}
|
| 109 |
+
{%- endif %}
|
| 110 |
+
{%- if loop.first %}
|
| 111 |
+
{%- if content|trim %}
|
| 112 |
+
{{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 113 |
+
{%- else %}
|
| 114 |
+
{{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 115 |
+
{%- endif %}
|
| 116 |
+
{%- else %}
|
| 117 |
+
{{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 118 |
+
{%- endif %}
|
| 119 |
+
{%- if tool_call.arguments is defined %}
|
| 120 |
+
{%- for args_name, args_value in tool_call.arguments|items %}
|
| 121 |
+
{{- '<parameter=' + args_name + '>\n' }}
|
| 122 |
+
{%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
|
| 123 |
+
{{- args_value }}
|
| 124 |
+
{{- '\n</parameter>\n' }}
|
| 125 |
+
{%- endfor %}
|
| 126 |
+
{%- endif %}
|
| 127 |
+
{{- '</function>\n</tool_call>' }}
|
| 128 |
+
{%- endfor %}
|
| 129 |
+
{%- endif %}
|
| 130 |
+
{{- '<|im_end|>\n' }}
|
| 131 |
+
{%- elif message.role == "tool" %}
|
| 132 |
+
{%- if loop.previtem and loop.previtem.role != "tool" %}
|
| 133 |
+
{{- '<|im_start|>user' }}
|
| 134 |
+
{%- endif %}
|
| 135 |
+
{{- '\n<tool_response>\n' }}
|
| 136 |
+
{{- content }}
|
| 137 |
+
{{- '\n</tool_response>' }}
|
| 138 |
+
{%- if not loop.last and loop.nextitem.role != "tool" %}
|
| 139 |
+
{{- '<|im_end|>\n' }}
|
| 140 |
+
{%- elif loop.last %}
|
| 141 |
+
{{- '<|im_end|>\n' }}
|
| 142 |
+
{%- endif %}
|
| 143 |
+
{%- else %}
|
| 144 |
+
{{- raise_exception('Unexpected message role.') }}
|
| 145 |
+
{%- endif %}
|
| 146 |
+
{%- endfor %}
|
| 147 |
+
{%- if add_generation_prompt %}
|
| 148 |
+
{{- '<|im_start|>assistant\n' }}
|
| 149 |
+
{%- if enable_thinking is defined and enable_thinking is true %}
|
| 150 |
+
{{- '<think>\n' }}
|
| 151 |
+
{%- else %}
|
| 152 |
+
{{- '<think>\n\n</think>\n\n' }}
|
| 153 |
+
{%- endif %}
|
| 154 |
+
{%- endif %}
|
decoder/config.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"Qwen3_5ForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_bias": false,
|
| 6 |
+
"attention_dropout": 0.0,
|
| 7 |
+
"attn_output_gate": true,
|
| 8 |
+
"bos_token_id": null,
|
| 9 |
+
"dtype": "bfloat16",
|
| 10 |
+
"eos_token_id": 248044,
|
| 11 |
+
"full_attention_interval": 4,
|
| 12 |
+
"head_dim": 256,
|
| 13 |
+
"hidden_act": "silu",
|
| 14 |
+
"hidden_size": 1024,
|
| 15 |
+
"initializer_range": 0.02,
|
| 16 |
+
"intermediate_size": 3584,
|
| 17 |
+
"layer_types": [
|
| 18 |
+
"linear_attention",
|
| 19 |
+
"linear_attention",
|
| 20 |
+
"linear_attention",
|
| 21 |
+
"full_attention",
|
| 22 |
+
"linear_attention",
|
| 23 |
+
"linear_attention",
|
| 24 |
+
"linear_attention",
|
| 25 |
+
"full_attention",
|
| 26 |
+
"linear_attention",
|
| 27 |
+
"linear_attention",
|
| 28 |
+
"linear_attention",
|
| 29 |
+
"full_attention",
|
| 30 |
+
"linear_attention",
|
| 31 |
+
"linear_attention",
|
| 32 |
+
"linear_attention",
|
| 33 |
+
"full_attention",
|
| 34 |
+
"linear_attention",
|
| 35 |
+
"linear_attention",
|
| 36 |
+
"linear_attention",
|
| 37 |
+
"full_attention",
|
| 38 |
+
"linear_attention",
|
| 39 |
+
"linear_attention",
|
| 40 |
+
"linear_attention",
|
| 41 |
+
"full_attention"
|
| 42 |
+
],
|
| 43 |
+
"linear_conv_kernel_dim": 4,
|
| 44 |
+
"linear_key_head_dim": 128,
|
| 45 |
+
"linear_num_key_heads": 16,
|
| 46 |
+
"linear_num_value_heads": 16,
|
| 47 |
+
"linear_value_head_dim": 128,
|
| 48 |
+
"mamba_ssm_dtype": "float32",
|
| 49 |
+
"max_position_embeddings": 262144,
|
| 50 |
+
"mlp_only_layers": [],
|
| 51 |
+
"model_type": "qwen3_5_text",
|
| 52 |
+
"mtp_num_hidden_layers": 1,
|
| 53 |
+
"mtp_use_dedicated_embeddings": false,
|
| 54 |
+
"num_attention_heads": 8,
|
| 55 |
+
"num_hidden_layers": 24,
|
| 56 |
+
"num_key_value_heads": 2,
|
| 57 |
+
"pad_token_id": null,
|
| 58 |
+
"partial_rotary_factor": 0.25,
|
| 59 |
+
"rms_norm_eps": 1e-06,
|
| 60 |
+
"rope_parameters": {
|
| 61 |
+
"mrope_interleaved": true,
|
| 62 |
+
"mrope_section": [
|
| 63 |
+
11,
|
| 64 |
+
11,
|
| 65 |
+
10
|
| 66 |
+
],
|
| 67 |
+
"partial_rotary_factor": 0.25,
|
| 68 |
+
"rope_theta": 10000000,
|
| 69 |
+
"rope_type": "default"
|
| 70 |
+
},
|
| 71 |
+
"tie_word_embeddings": true,
|
| 72 |
+
"transformers_version": "5.19.0.dev0",
|
| 73 |
+
"use_cache": true,
|
| 74 |
+
"vocab_size": 248192
|
| 75 |
+
}
|
decoder/generation_config.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"eos_token_id": 248044,
|
| 4 |
+
"transformers_version": "5.19.0.dev0",
|
| 5 |
+
"use_cache": true
|
| 6 |
+
}
|
decoder/model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:679da87352997d332cd346a590ef1d18fb492d5eaabfd8cbf8f8e3e66c47034a
|
| 3 |
+
size 1504565464
|
decoder/tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:887dad55597b750795f8a6adfa0ee18d35c57d81316e54c8aae82a3f0eb1b522
|
| 3 |
+
size 19989523
|
decoder/tokenizer_config.json
ADDED
|
Binary file (1.2 kB). View file
|
|
|
preprocessor_config.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"delta_win_length": 3,
|
| 3 |
+
"feature_extractor_type": "GraniteSpeech5FeatureExtractor",
|
| 4 |
+
"feature_size": 320,
|
| 5 |
+
"hop_length": 160,
|
| 6 |
+
"logmel_floor_db": 8.0,
|
| 7 |
+
"n_fft": 512,
|
| 8 |
+
"num_mel_bins": 80,
|
| 9 |
+
"padding_side": "right",
|
| 10 |
+
"padding_value": 0.0,
|
| 11 |
+
"return_attention_mask": true,
|
| 12 |
+
"sampling_rate": 16000,
|
| 13 |
+
"win_length": 400
|
| 14 |
+
}
|
projector.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ac6adb6cb73b1b4135b2ca978580b727123547cdf695d12396706f0ecbd239a5
|
| 3 |
+
size 4203311
|
qwen_decoder.gguf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:606ed1f0e4e8603b92f1a9abd68265be66a9da9887b35526fc5d1d754544849d
|
| 3 |
+
size 1516477184
|
qwen_decoder_q8_0.gguf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:12724d4c8ba830527a8d0de22d70a4942766720428805d8b8530501b44811b11
|
| 3 |
+
size 811698944
|
tiny_audio_metadata.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_type": "tiny_audio_asr",
|
| 3 |
+
"audio_encoder": {
|
| 4 |
+
"model_id": "ibm-granite/granite-speech-5.0-470m-turboctc-nc",
|
| 5 |
+
"encoder_conv_layers": [
|
| 6 |
+
[
|
| 7 |
+
0,
|
| 8 |
+
2,
|
| 9 |
+
2
|
| 10 |
+
],
|
| 11 |
+
[
|
| 12 |
+
0,
|
| 13 |
+
2,
|
| 14 |
+
2
|
| 15 |
+
]
|
| 16 |
+
],
|
| 17 |
+
"sample_rate": 16000
|
| 18 |
+
},
|
| 19 |
+
"projector": {
|
| 20 |
+
"type": "MLPAudioProjector",
|
| 21 |
+
"weight_file": "projector.pt",
|
| 22 |
+
"encoder_dim": 1024,
|
| 23 |
+
"llm_dim": 2048,
|
| 24 |
+
"stride": 1
|
| 25 |
+
},
|
| 26 |
+
"decoder": {
|
| 27 |
+
"type": "Qwen3.5-0.8B-Merged",
|
| 28 |
+
"weight_dir": "decoder",
|
| 29 |
+
"is_lora_merged": true
|
| 30 |
+
}
|
| 31 |
+
}
|