haujetzhao--capswriter-offline
137 行
4.7 KiB
Plaintext
137 行
4.7 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "0cde0648",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"\n",
|
|
"\n",
|
|
"import argparse\n",
|
|
"import sys\n",
|
|
"from pathlib import Path\n",
|
|
"\n",
|
|
"import soundfile as sf\n",
|
|
"import sherpa_onnx\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"def create_recognizer(provider=\"cpu\") -> sherpa_onnx.OfflineRecognizer:\n",
|
|
"\n",
|
|
" model_dir = Path(\"./FireRed/sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16\")\n",
|
|
" encoder = model_dir / 'encoder.int8.onnx'\n",
|
|
" decoder = model_dir / 'decoder.int8.onnx'\n",
|
|
" tokens = model_dir / \"tokens.txt\"\n",
|
|
" itn = Path(\"./ITN/itn_zh_number.fst\")\n",
|
|
" \n",
|
|
" return sherpa_onnx.OfflineRecognizer.from_fire_red_asr(\n",
|
|
" encoder=encoder.as_posix(),\n",
|
|
" decoder=decoder.as_posix(),\n",
|
|
" tokens = tokens.as_posix(),\n",
|
|
" debug=True,\n",
|
|
" num_threads=4,\n",
|
|
" provider=provider,\n",
|
|
" rule_fsts=itn.as_posix(),\n",
|
|
" )\n",
|
|
"\n",
|
|
"def decode_file(\n",
|
|
" recognizer: sherpa_onnx.OfflineRecognizer,\n",
|
|
" filename: str,\n",
|
|
"):\n",
|
|
" \"\"\"Decode a single audio file.\"\"\"\n",
|
|
" audio, sample_rate = sf.read(filename, dtype=\"float32\", always_2d=True)\n",
|
|
" audio = audio[:, 0] # only use the first channel\n",
|
|
"\n",
|
|
" stream = recognizer.create_stream()\n",
|
|
" stream.accept_waveform(sample_rate, audio)\n",
|
|
" recognizer.decode_stream(stream)\n",
|
|
" result = stream.result\n",
|
|
" return result\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "b89b6afa",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"recognizer_gpu = create_recognizer('cuda')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "fee418fb",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"{\"lang\": \"\", \"emotion\": \"\", \"event\": \"\", \"text\": \"财联社1月7日电港股午间收盘恒生指数跌百分之1点01恒生科技指数跌百分之1点65大型科技股疲软阿里巴巴跌逾百分之4美团网易跌超百分之2\", \"timestamps\": [], \"durations\": [], \"tokens\":[\"财\", \"联\", \"社\", \"一\", \"月\", \"七\", \"日\", \"电\", \"港\", \"股\", \"午\", \"间\", \"收\", \"盘\", \"恒\", \"生\", \"指\", \"数\", \"跌\", \"百\", \"分\", \"之\", \"一\", \"点\", \"零\", \"一\", \"恒\", \"生\", \"科\", \"技\", \"指\", \"数\", \"跌\", \"百\", \"分\", \"之\", \"一\", \"点\", \"六\", \"五\", \"大\", \"型\", \"科\", \"技\", \"股\", \"疲\", \"软\", \"阿\", \"里\", \"巴\", \"巴\", \"跌\", \"逾\", \"百\", \"分\", \"之\", \"四\", \"美\", \"团\", \"网\", \"易\", \"跌\", \"超\", \"百\", \"分\", \"之\", \"二\"], \"ys_log_probs\": [], \"words\": []}\n",
|
|
"财联社1月7日电港股午间收盘恒生指数跌百分之1点01恒生科技指数跌百分之1点65大型科技股疲软阿里巴巴跌逾百分之4美团网易跌超百分之2\n",
|
|
"[]\n",
|
|
"['财', '联', '社', '一', '月', '七', '日', '电', '港', '股', '午', '间', '收', '盘', '恒', '生', '指', '数', '跌', '百', '分', '之', '一', '点', '零', '一', '恒', '生', '科', '技', '指', '数', '跌', '百', '分', '之', '一', '点', '六', '五', '大', '型', '科', '技', '股', '疲', '软', '阿', '里', '巴', '巴', '跌', '逾', '百', '分', '之', '四', '美', '团', '网', '易', '跌', '超', '百', '分', '之', '二']\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"\n",
|
|
"wav_path = f\"./demo4.mp3\"\n",
|
|
"result = decode_file(recognizer_gpu, wav_path)\n",
|
|
"print(result)\n",
|
|
"print(result.text)\n",
|
|
"print(result.timestamps)\n",
|
|
"print(result.tokens)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "51339c12",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# recognizer_cpu = create_recognizer('cpu')\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "65807bd6",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# wav_path = f\"../demo.mp3\"\n",
|
|
"# result = decode_file(recognizer_cpu, wav_path)\n",
|
|
"# print(result)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "capswriter",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.10.19"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|