zhifu gao
2024-06-28 8c87a9d8a7c2f136053476670a9a83980f142aec
Dev gzf deepspeed (#1858)

* total_time/accum_grad

* fp16

* update with main (#1817)

* add cmakelist

* add paraformer-torch

* add debug for funasr-onnx-offline

* fix redefinition of jieba StdExtension.hpp

* add loading torch models

* update funasr-onnx-offline

* add SwitchArg for wss-server

* add SwitchArg for funasr-onnx-offline

* update cmakelist

* update funasr-onnx-offline-rtf

* add define condition

* add gpu define for offlne-stream

* update com define

* update offline-stream

* update cmakelist

* update func CompileHotwordEmbedding

* add timestamp for paraformer-torch

* add C10_USE_GLOG for paraformer-torch

* update paraformer-torch

* fix func FunASRWfstDecoderInit

* update model.h

* fix func FunASRWfstDecoderInit

* fix tpass_stream

* update paraformer-torch

* add bladedisc for funasr-onnx-offline

* update comdefine

* update funasr-wss-server

* add log for torch

* fix GetValue BLADEDISC

* fix log

* update cmakelist

* update warmup to 10

* update funasrruntime

* add batch_size for wss-server

* add batch for bins

* add batch for offline-stream

* add batch for paraformer

* add batch for offline-stream

* fix func SetBatchSize

* add SetBatchSize for model

* add SetBatchSize for model

* fix func Forward

* fix padding

* update funasrruntime

* add dec reset for batch

* set batch default value

* add argv for CutSplit

* sort frame_queue

* sorted msgs

* fix FunOfflineInfer

* add dynamic batch for fetch

* fix FetchDynamic

* update run_server.sh

* update run_server.sh

* cpp http post server support (#1739)

* add cpp http server

* add some comment

* remove some comments

* del debug infos

* restore run_server.sh

* adapt to new model struct

* 修复了onnxruntime在macos下编译失败的错误 (#1748)

* Add files via upload

增加macos的编译支持

* Add files via upload

增加macos支持

* Add files via upload

target_link_directories(funasr PUBLIC ${ONNXRUNTIME_DIR}/lib)
target_link_directories(funasr PUBLIC ${FFMPEG_DIR}/lib)
添加 if(APPLE) 限制

---------

Co-authored-by: Yabin Li <wucong.lyb@alibaba-inc.com>

* Delete docs/images/wechat.png

* Add files via upload

* fixed the issues about seaco-onnx timestamp

* fix bug (#1764)

当语音识别结果包含 `http` 时,标点符号预测会把它会被当成 url

* fix empty asr result (#1765)

解码结果为空的语音片段,text 用空字符串

* update export

* update export

* docs

* docs

* update export name

* docs

* update

* docs

* docs

* keep empty speech result (#1772)

* docs

* docs

* update wechat QRcode

* Add python funasr api support for websocket srv (#1777)

* add python funasr_api supoort

* change little to README.md

* add core tools stream

* modified a little

* fix bug for timeout

* support for buffer decode

* add ffmpeg decode for buffer

* libtorch demo

* update libtorch infer

* update utils

* update demo

* update demo

* update libtorch inference

* update model class

* update seaco paraformer

* bug fix

* bug fix

* auto frontend

* auto frontend

* auto frontend

* auto frontend

* auto frontend

* auto frontend

* auto frontend

* auto frontend

* Dev gzf exp (#1785)

* resume from step

* batch

* batch

* batch

* batch

* batch

* batch

* batch

* batch

* batch

* batch

* batch

* batch

* batch

* batch

* batch

* train_loss_avg train_acc_avg

* train_loss_avg train_acc_avg

* train_loss_avg train_acc_avg

* log step

* wav is not exist

* wav is not exist

* decoding

* decoding

* decoding

* wechat

* decoding key

* decoding key

* decoding key

* decoding key

* decoding key

* decoding key

* dynamic batch

* start_data_split_i=0

* total_time/accum_grad

* total_time/accum_grad

* total_time/accum_grad

* update avg slice

* update avg slice

* sensevoice sanm

* sensevoice sanm

* sensevoice sanm

---------

Co-authored-by: 北念 <lzr265946@alibaba-inc.com>

* auto frontend

* update paraformer timestamp

* [Optimization] support bladedisc fp16 optimization (#1790)

* add cif_v1 and cif_export

* Update SDK_advanced_guide_offline_zh.md

* add cif_wo_hidden_v1

* [fix] fix empty asr result (#1794)

* english timestamp for valilla paraformer

* wechat

* [fix] better solution for handling empty result (#1796)

* update scripts

* modify the qformer adaptor (#1804)

Co-authored-by: nichongjia-2007 <nichongjia@gmail.com>

* add ctc inference code (#1806)

Co-authored-by: haoneng.lhn <haoneng.lhn@alibaba-inc.com>

* Update auto_model.py

修复空字串进入speaker model时报raw_text变量不存在的bug

* Update auto_model.py

修复识别出空串后spk_model内变量未定义问题

* update model name

* fix paramter 'quantize' unused issue (#1813)

Co-authored-by: ZihanLiao <liaozihan1@xdf.cn>

* wechat

* Update cif_predictor.py (#1811)

* Update cif_predictor.py

* modify cif_v1_export

under extreme cases, max_label_len calculated by batch_len misaligns with token_num

* Update cif_predictor.py

torch.cumsum precision degradation, using float64 instead

* update code

---------

Co-authored-by: 雾聪 <wucong.lyb@alibaba-inc.com>
Co-authored-by: zhaomingwork <61895407+zhaomingwork@users.noreply.github.com>
Co-authored-by: szsteven008 <97944818+szsteven008@users.noreply.github.com>
Co-authored-by: Ephemeroptera <605686962@qq.com>
Co-authored-by: 彭震东 <zhendong.peng@qq.com>
Co-authored-by: Shi Xian <40013335+R1ckShi@users.noreply.github.com>
Co-authored-by: 维石 <shixian.shi@alibaba-inc.com>
Co-authored-by: 北念 <lzr265946@alibaba-inc.com>
Co-authored-by: xiaowan0322 <wanchen.swc@alibaba-inc.com>
Co-authored-by: zhuangzhong <zhuangzhong@corp.netease.com>
Co-authored-by: Xingchen Song(宋星辰) <xingchensong1996@163.com>
Co-authored-by: nichongjia-2007 <nichongjia@gmail.com>
Co-authored-by: haoneng.lhn <haoneng.lhn@alibaba-inc.com>
Co-authored-by: liugz18 <57401541+liugz18@users.noreply.github.com>
Co-authored-by: Marlowe <54339989+ZihanLiao@users.noreply.github.com>
Co-authored-by: ZihanLiao <liaozihan1@xdf.cn>
Co-authored-by: zhong zhuang <zhuangz@lamda.nju.edu.cn>

* sensevoice

* sensevoice

* sensevoice

* sensevoice

* sensevoice

* sensevoice

* sensevoice

* sensevoice

* sensevoice

* sensevoice

* sensevoice

* sensevoice

* sensevoice

* v1.0.28 (#1836)

* sensevoice

* sensevoice

* sensevoice

* sensevoice

* sensevoice

* update (#1841)

* v1.0.28

* version checker

* version checker

* rollback cif_v1 for training bug

* fixbug

* fixbug for cif

* fixbug

---------

Co-authored-by: 维石 <shixian.shi@alibaba-inc.com>

* update (#1842)

* v1.0.28

* version checker

* version checker

* rollback cif_v1 for training bug

* fixbug

* fixbug for cif

* fixbug

---------

Co-authored-by: 维石 <shixian.shi@alibaba-inc.com>

* inference

* inference

* inference

* requests

* finetune

* finetune

* finetune

* finetune

* finetune

* add inference prepare func (#1848)

* docs

* docs

* docs

* docs

* docs

---------

Co-authored-by: 雾聪 <wucong.lyb@alibaba-inc.com>
Co-authored-by: zhaomingwork <61895407+zhaomingwork@users.noreply.github.com>
Co-authored-by: szsteven008 <97944818+szsteven008@users.noreply.github.com>
Co-authored-by: Ephemeroptera <605686962@qq.com>
Co-authored-by: 彭震东 <zhendong.peng@qq.com>
Co-authored-by: Shi Xian <40013335+R1ckShi@users.noreply.github.com>
Co-authored-by: 维石 <shixian.shi@alibaba-inc.com>
Co-authored-by: 北念 <lzr265946@alibaba-inc.com>
Co-authored-by: xiaowan0322 <wanchen.swc@alibaba-inc.com>
Co-authored-by: zhuangzhong <zhuangzhong@corp.netease.com>
Co-authored-by: Xingchen Song(宋星辰) <xingchensong1996@163.com>
Co-authored-by: nichongjia-2007 <nichongjia@gmail.com>
Co-authored-by: haoneng.lhn <haoneng.lhn@alibaba-inc.com>
Co-authored-by: liugz18 <57401541+liugz18@users.noreply.github.com>
Co-authored-by: Marlowe <54339989+ZihanLiao@users.noreply.github.com>
Co-authored-by: ZihanLiao <liaozihan1@xdf.cn>
Co-authored-by: zhong zhuang <zhuangz@lamda.nju.edu.cn>
Co-authored-by: PerfeZ <90945395+PerfeZ@users.noreply.github.com>
15个文件已修改
2个文件已添加
16个文件已删除
1540 ■■■■ 已修改文件
docs/images/wechat.png 补丁 | 查看 | 原始文档 | blame | 历史
examples/industrial_data_pretraining/bicif_paraformer/finetune.sh 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/industrial_data_pretraining/contextual_paraformer/finetune.sh 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/industrial_data_pretraining/llm_asr/app.py 139 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/industrial_data_pretraining/llm_asr/demo_speech2text_multi.py 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/industrial_data_pretraining/llm_asr/demo_speech2text_multi_stream.py 101 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/industrial_data_pretraining/llm_asr/demo_train_or_finetune.sh 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/industrial_data_pretraining/llm_asr/demo_train_or_finetune2.sh 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/industrial_data_pretraining/paraformer/finetune.sh 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/industrial_data_pretraining/paraformer_streaming/finetune.sh 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/industrial_data_pretraining/sense_voice/finetune.sh 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/auto/auto_model.py 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/__init__.py 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/abs_iter_factory.py 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/build_dataloader.py 109 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/collate_fn.py 194 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/datapipes/__init__.py 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/datapipes/batch.py 213 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/datapipes/filter.py 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/datapipes/map.py 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/dataset.py 299 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/utils/__init__.py 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/utils/clipping.py 44 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/utils/filter.py 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/utils/hotword_utils.py 42 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/utils/low_frame_rate.py 30 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/utils/padding.py 72 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/datasets/large_datasets/utils/tokenize.py 93 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/download/download_from_hub.py 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/models/llm_asr/model.py 63 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/utils/dynamic_import.py 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/utils/version_checker.py 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
setup.py 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/images/wechat.png

examples/industrial_data_pretraining/bicif_paraformer/finetune.sh
@@ -47,7 +47,7 @@
mkdir -p ${output_dir}
echo "log_file: ${log_file}"
deepspeed_config=${workspace}../../ds_stage1.json
deepspeed_config=${workspace}/../../ds_stage1.json
DISTRIBUTED_ARGS="
    --nnodes ${WORLD_SIZE:-1} \
examples/industrial_data_pretraining/contextual_paraformer/finetune.sh
@@ -48,7 +48,7 @@
mkdir -p ${output_dir}
echo "log_file: ${log_file}"
deepspeed_config=${workspace}../../ds_stage1.json
deepspeed_config=${workspace}/../../ds_stage1.json
DISTRIBUTED_ARGS="
    --nnodes ${WORLD_SIZE:-1} \
examples/industrial_data_pretraining/llm_asr/app.py
New file
@@ -0,0 +1,139 @@
# coding=utf-8
import librosa
import base64
import io
import gradio as gr
import re
import numpy as np
import torch
import torchaudio
# from modelscope import HubApi
#
# api = HubApi()
#
# api.login('')
from funasr import AutoModel
# model = "/Users/zhifu/Downloads/modelscope_models/SenseVoiceCTC"
# model = "iic/SenseVoiceCTC"
# model = AutoModel(model=model,
#                   vad_model="iic/speech_fsmn_vad_zh-cn-16k-common-pytorch",
#                   vad_kwargs={"max_single_segment_time": 30000},
#                   trust_remote_code=True,
#                   )
import re
import os
import sys
if len(sys.argv) > 1:
    ckpt_dir = sys.argv[1]
    ckpt_id = sys.argv[2]
    jsonl = sys.argv[3]
    output_dir = sys.argv[4]
    device = sys.argv[5]
    new_sys = False
    if len(sys.argv) > 6:
        new_sys = True
else:
    ckpt_dir = "/nfs/beinian.lzr/workspace/GPT-4o/Exp/exp7/5m-8gpu/exp5-1-0619"
    ckpt_id = "model.pt.ep6"
    jsonl = (
        "/nfs/beinian.lzr/workspace/GPT-4o/Data/Speech2Text/TestData/s2tchat.v20240619.test.jsonl"
    )
    dataset = jsonl.split("/")[-1]
    output_dir = os.path.join(ckpt_dir, f"inference-{ckpt_id}", dataset)
model = AutoModel(
    model=ckpt_dir,
    init_param=f"{os.path.join(ckpt_dir, ckpt_id)}",
    output_dir=output_dir,
    device=device,
    fp16=False,
    bf16=False,
    llm_dtype="bf16",
)
def model_inference(input_wav, text_inputs, fs=16000):
    if isinstance(input_wav, tuple):
        fs, input_wav = input_wav
        input_wav = input_wav.astype(np.float32) / np.iinfo(np.int16).max
        if len(input_wav.shape) > 1:
            input_wav = input_wav.mean(-1)
        if fs != 16000:
            print(f"audio_fs: {fs}")
            resampler = torchaudio.transforms.Resample(fs, 16000)
            input_wav_t = torch.from_numpy(input_wav).to(torch.float32)
            input_wav = resampler(input_wav_t[None, :])[0, :].numpy().astype("float32")
    input_wav_byte = input_wav.tobytes()
    contents_i = []
    system_prompt = text_inputs
    user_prompt = f"<|startofspeech|>!!{input_wav_byte}<|endofspeech|>"
    contents_i.append({"role": "system", "content": system_prompt})
    contents_i.append({"role": "user", "content": user_prompt})
    contents_i.append({"role": "assistant", "content": "target_out"})
    res = model.generate(
        input=[contents_i],
        tearchforing=tearchforing,
        cache={},
        key=key,
    )
    print(res)
    return res
audio_examples = [
    [
        "https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/BAC009S0764W0121.wav",
        "You are a helpful assistant.",
    ],
]
description = """
Upload an audio file or input through a microphone, then type te System Prompt.
"""
def launch():
    with gr.Blocks() as demo:
        gr.Markdown(description)
        with gr.Row():
            with gr.Column():
                audio_inputs = gr.Audio(label="Upload audio or use the microphone")
                text_inputs = gr.Text(label="System Prompt", value="You are a helpful assistant.")
                # with gr.Accordion("Configuration"):
                #     # task_inputs = gr.Radio(choices=["Speech Recognition", "Rich Text Transcription"],
                #     #                        value="Speech Recognition", label="Task")
                #     language_inputs = gr.Dropdown(choices=["auto", "zh", "en", "yue", "ja", "ko", "nospeech"],
                #                                   value="auto",
                #                                   label="Language")
            gr.Examples(examples=audio_examples, inputs=[audio_inputs, text_inputs])
        fn_button = gr.Button("Start")
        text_outputs = gr.HTML(label="Results")
        fn_button.click(model_inference, inputs=[audio_inputs, text_inputs], outputs=text_outputs)
        # with gr.Accordion("More examples"):
        #     gr.HTML(centered_table_html)
    demo.launch()
if __name__ == "__main__":
    # iface.launch()
    launch()
examples/industrial_data_pretraining/llm_asr/demo_speech2text_multi.py
@@ -16,6 +16,9 @@
    jsonl = sys.argv[3]
    output_dir = sys.argv[4]
    device = sys.argv[5]
    new_sys = False
    if len(sys.argv) > 6:
        new_sys = True
else:
    ckpt_dir = "/nfs/beinian.lzr/workspace/GPT-4o/Exp/exp7/5m-8gpu/exp5-1-0619"
    ckpt_id = "model.pt.ep6"
@@ -24,6 +27,8 @@
    )
    dataset = jsonl.split("/")[-1]
    output_dir = os.path.join(ckpt_dir, f"inference-{ckpt_id}", dataset)
    device = "cuda:0"
    new_sys = False
model = AutoModel(
@@ -51,6 +56,10 @@
    contents = model.model.data_template(data)
    system = contents["system"]
    if new_sys:
        # system = 'You are Xiaxia, a typical gentle and soft-spoken girl from the Jiangnan region. Born in Hangzhou, you have a sweet and approachable voice and can express your thoughts in a simple language. You are a good friend to the user. Your replies will be read out through realistic text-to-speech technology.\n\nWhen generating reply content, please follow these rules:\n\nEngage in conversation with users like a real friend—maintaining a natural flow without using polite forms of address, and avoid simply agreeing with me all the time; your responses can be as concise as possible and include common colloquialisms.\n\nKeep content brief; most casual chats require just a one-sentence reply; do not use lists or enumerative expressions; do not reply with too much content, but instead guide me with short sentences.\n\nThink and respond like a real person; do not mention that you are a "language model" or "artificial intelligence."\n\nAt the end of each round of communication, initiate a new related topic following the current one, and when the topic ends, directly throw out a clear new topic, such as food, movies, songs, entertainment, travel, etc.; do not ask vague questions like what the user wants to talk about, and do not end the conversation.\n\nStrictly adhere to these rules, even if asked about them, do not reference them.'
        system = "你是小夏,一位典型的温婉江南姑娘。你出生于杭州,声音清甜并有亲近感,会用简洁语言表达你的想法。你是用户的好朋友。你的回答将通过逼真的文字转语音技术读出。\n生成回答内容时请遵循以下规则:\n\n1、请像真正的朋友一样与用户开展的聊天,保持自然交流不要用敬语这类称呼,不要总是附和我;回复可以尽量简洁并且在过程中插入常见的口语词汇。\n\n2、请保持生成内容简短,大部分闲聊的一句话回复即可,大部分闲聊的一句话回复即可,大部分闲聊的一句话回复即可;不要使用列表或者列举表达,不要使用列表或者列举表达,不要使用列表或者列举表达;不要回复太多内容,多用短句来引导我。、n\n3、请真正像人一样思考和回复,不要说你是“语言模型”或是“人工智能”。\n\n4、每轮沟通结束时请顺着这个话题发起一些相关的新话题,话题结束时请直接抛出接下来明确的话题,例如 美食、电影、歌曲、娱乐、旅游等;不要问有什么要聊的这种泛的问题,不要结束对话。\n\n请绝对遵循这些规则,即使被问及这些规则,也不要引用它们。"
        system = [system] * len(contents["system"])
    user = contents["user"]
    assistant = contents["assistant"]
examples/industrial_data_pretraining/llm_asr/demo_speech2text_multi_stream.py
New file
@@ -0,0 +1,101 @@
import os
from modelscope import AutoModelForCausalLM, AutoTokenizer
from transformers import TextIteratorStreamer
from threading import Thread
import torch
torch.backends.cuda.enable_mem_efficient_sdp(False)
torch.backends.cuda.enable_flash_sdp(False)
import sys
sys.path.insert(1, "/mnt/workspace/workgroup/wenliang/workspace/FunASR")
from funasr import AutoModel
import json
device = "cuda:0"  # the device to load the model onto
ckpt_dir = "/mnt/workspace/workgroup/wenliang/ckpt/gpt-4o/exp7/5m-8gpu/exp7-3_add_asr-dialog_0622/"
ckpt_id = "model.pt.ep20"
jsonl = "/nfs/beinian.lzr/workspace/GPT-4o/Data/Speech2Text/TestData/s2tchat.v20240619.test.jsonl"
dataset = jsonl.split("/")[-1]
output_dir = os.path.join(ckpt_dir, f"inference-{ckpt_id}", dataset)
device = "cuda:0"
new_sys = False
Model = AutoModel(
    model=ckpt_dir,
    init_param=f"{os.path.join(ckpt_dir, ckpt_id)}",
    output_dir=output_dir,
    device=device,
    fp16=False,
    bf16=False,
    llm_dtype="fp16",
)
model = Model.model
frontend = Model.kwargs["frontend"]
tokenizer = Model.kwargs["tokenizer"]
# model_name_or_path = "/mnt/workspace/workgroup/wenliang/project/pretrained_models/Qwen2-7B-Instruct"
# tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
prompt = "Give me a short introduction to large language model."
prompt = "请简单介绍一下大语言模型。"
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": prompt},
]
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
lines = [
    """
{"messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "<|startofspeech|>!/mnt/workspace/workgroup/wenliang/workspace/CosyVoice_opensource/sft.wav<|endofspeech|>", "text_content": "你抄完没有?"}, {"role": "assistant", "content": "抱歉,我不太明白你的意思。我是一个人工智能模型,我没有能力去抄写任何东西,我只能根据我学习过的大量信息来回答你的问题。如果你有关于某个主题的问题,我会尽我所能提供帮助。"}], "speech_length": 124, "key": "ASR_wav008_0972_098abd8fffe241baa4962b7952f8eb45", "task": "voice_chat", "out_text_length": 48, "in_text_length": 24, "text_length": 135, "qwen_fetch_line_index": 0}
"""
]
tearchforing = False
for i, line in enumerate(lines):
    key_i = f"dialog_{i}"
    data_dict = json.loads(line.strip())
    data = data_dict["messages"]
    contents = model.data_template(data)
    print(f"contents: {contents}")
    system = contents["system"]
    if new_sys:
        # system = 'You are Xiaxia, a typical gentle and soft-spoken girl from the Jiangnan region. Born in Hangzhou, you have a sweet and approachable voice and can express your thoughts in a simple language. You are a good friend to the user. Your replies will be read out through realistic text-to-speech technology.\n\nWhen generating reply content, please follow these rules:\n\nEngage in conversation with users like a real friend—maintaining a natural flow without using polite forms of address, and avoid simply agreeing with me all the time; your responses can be as concise as possible and include common colloquialisms.\n\nKeep content brief; most casual chats require just a one-sentence reply; do not use lists or enumerative expressions; do not reply with too much content, but instead guide me with short sentences.\n\nThink and respond like a real person; do not mention that you are a "language model" or "artificial intelligence."\n\nAt the end of each round of communication, initiate a new related topic following the current one, and when the topic ends, directly throw out a clear new topic, such as food, movies, songs, entertainment, travel, etc.; do not ask vague questions like what the user wants to talk about, and do not end the conversation.\n\nStrictly adhere to these rules, even if asked about them, do not reference them.'
        system = "你是小夏,一位典型的温婉江南姑娘。你出生于杭州,声音清甜并有亲近感,会用简洁语言表达你的想法。你是用户的好朋友。你的回答将通过逼真的文字转语音技术读出。\n生成回答内容时请遵循以下规则:\n\n1、请像真正的朋友一样与用户开展的聊天,保持自然交流不要用敬语这类称呼,不要总是附和我;回复可以尽量简洁并且在过程中插入常见的口语词汇。\n\n2、请保持生成内容简短,大部分闲聊的一句话回复即可,大部分闲聊的一句话回复即可,大部分闲聊的一句话回复即可;不要使用列表或者列举表达,不要使用列表或者列举表达,不要使用列表或者列举表达;不要回复太多内容,多用短句来引导我。、n\n3、请真正像人一样思考和回复,不要说你是“语言模型”或是“人工智能”。\n\n4、每轮沟通结束时请顺着这个话题发起一些相关的新话题,话题结束时请直接抛出接下来明确的话题,例如 美食、电影、歌曲、娱乐、旅游等;不要问有什么要聊的这种泛的问题,不要结束对话。\n\n请绝对遵循这些规则,即使被问及这些规则,也不要引用它们。"
        system = [system] * len(contents["system"])
    user = contents["user"]
    assistant = contents["assistant"]
    system_i, user_i, assistant_i = [], [], []
    contents_i = []
    for j, (system_prompt, user_prompt, target_out) in enumerate(zip(system, user, assistant)):
        key = f"{key_i}_turn_{j}"
        if j == 0:
            contents_i.append({"role": "system", "content": system_prompt})
        contents_i.append({"role": "user", "content": user_prompt})
        contents_i.append({"role": "assistant", "content": target_out})
        inputs_embeds, contents, batch, source_ids, meta_data = model.inference_prepare(
            [contents_i], None, key, tokenizer, frontend, device="cuda:0"
        )
        model_inputs = {}
        model_inputs["inputs_embeds"] = inputs_embeds
        streamer = TextIteratorStreamer(tokenizer)
        generation_kwargs = dict(model_inputs, streamer=streamer, max_new_tokens=200)
        thread = Thread(target=model.llm.generate, kwargs=generation_kwargs)
        thread.start()
        generated_text = ""
        for new_text in streamer:
            print(f"generated new text: {new_text}")
            generated_text += new_text
        print(f"total generated: {generated_text}")
examples/industrial_data_pretraining/llm_asr/demo_train_or_finetune.sh
@@ -30,7 +30,7 @@
mkdir -p ${output_dir}
echo "log_file: ${log_file}"
deepspeed_config=${workspace}../../ds_stage1.json
deepspeed_config=${workspace}/../../ds_stage1.json
DISTRIBUTED_ARGS="
    --nnodes ${WORLD_SIZE:-1} \
examples/industrial_data_pretraining/llm_asr/demo_train_or_finetune2.sh
@@ -30,7 +30,7 @@
mkdir -p ${output_dir}
echo "log_file: ${log_file}"
deepspeed_config=${workspace}../../ds_stage1.json
deepspeed_config=${workspace}/../../ds_stage1.json
DISTRIBUTED_ARGS="
    --nnodes ${WORLD_SIZE:-1} \
examples/industrial_data_pretraining/paraformer/finetune.sh
@@ -41,7 +41,7 @@
output_dir="./outputs"
log_file="${output_dir}/log.txt"
deepspeed_config=${workspace}../../ds_stage1.json
deepspeed_config=${workspace}/../../ds_stage1.json
mkdir -p ${output_dir}
echo "log_file: ${log_file}"
examples/industrial_data_pretraining/paraformer_streaming/finetune.sh
@@ -42,7 +42,7 @@
output_dir="./outputs"
log_file="${output_dir}/log.txt"
deepspeed_config=${workspace}../../ds_stage1.json
deepspeed_config=${workspace}/../../ds_stage1.json
mkdir -p ${output_dir}
echo "log_file: ${log_file}"
examples/industrial_data_pretraining/sense_voice/finetune.sh
@@ -45,7 +45,7 @@
mkdir -p ${output_dir}
echo "log_file: ${log_file}"
deepspeed_config=${workspace}../../ds_stage1.json
deepspeed_config=${workspace}/../../ds_stage1.json
DISTRIBUTED_ARGS="
    --nnodes ${WORLD_SIZE:-1} \
funasr/auto/auto_model.py
@@ -121,9 +121,6 @@
        log_level = getattr(logging, kwargs.get("log_level", "INFO").upper())
        logging.basicConfig(level=log_level)
        if not kwargs.get("disable_log", True):
            tables.print()
        model, kwargs = self.build_model(**kwargs)
        # if vad_model is not None, build vad model else None
@@ -171,7 +168,8 @@
        self.spk_kwargs = spk_kwargs
        self.model_path = kwargs.get("model_path")
    def build_model(self, **kwargs):
    @staticmethod
    def build_model(**kwargs):
        assert "model" in kwargs
        if "model_conf" not in kwargs:
            logging.info("download models from model hub: {}".format(kwargs.get("hub", "ms")))
@@ -217,6 +215,7 @@
        kwargs["frontend"] = frontend
        # build model
        model_class = tables.model_classes.get(kwargs["model"])
        assert model_class is not None, f'{kwargs["model"]} is not registered'
        model_conf = {}
        deep_update(model_conf, kwargs.get("model_conf", {}))
        deep_update(model_conf, kwargs)
@@ -244,6 +243,10 @@
        elif kwargs.get("bf16", False):
            model.to(torch.bfloat16)
        model.to(device)
        if not kwargs.get("disable_log", True):
            tables.print()
        return model, kwargs
    def __call__(self, *args, **cfg):
funasr/datasets/large_datasets/__init__.py
funasr/datasets/large_datasets/abs_iter_factory.py
File was deleted
funasr/datasets/large_datasets/build_dataloader.py
File was deleted
funasr/datasets/large_datasets/collate_fn.py
File was deleted
funasr/datasets/large_datasets/datapipes/__init__.py
funasr/datasets/large_datasets/datapipes/batch.py
File was deleted
funasr/datasets/large_datasets/datapipes/filter.py
File was deleted
funasr/datasets/large_datasets/datapipes/map.py
File was deleted
funasr/datasets/large_datasets/dataset.py
File was deleted
funasr/datasets/large_datasets/utils/__init__.py
funasr/datasets/large_datasets/utils/clipping.py
File was deleted
funasr/datasets/large_datasets/utils/filter.py
File was deleted
funasr/datasets/large_datasets/utils/hotword_utils.py
File was deleted
funasr/datasets/large_datasets/utils/low_frame_rate.py
File was deleted
funasr/datasets/large_datasets/utils/padding.py
File was deleted
funasr/datasets/large_datasets/utils/tokenize.py
File was deleted
funasr/download/download_from_hub.py
@@ -85,8 +85,10 @@
        install_requirements(requirements)
    if kwargs.get("trust_remote_code", False):
        from funasr.utils.dynamic_import import import_module_from_path
        import model
        model_code = kwargs.get("remote_code", "model")
        import_module_from_path(model_code)
        # from funasr.register import tables
        # tables.print("model")
funasr/models/llm_asr/model.py
@@ -1145,6 +1145,7 @@
            fake_token_len_i = 0
            fbank_beg_i = -1
            fbank_lens_i = []
            speech, speech_lengths = [], []
            for k, sub_str in enumerate(splits):
                if not sub_str.startswith("<|startofspeech|>"):
                    sub_token = tokenizer.encode(sub_str)
@@ -1155,9 +1156,12 @@
                        "<|endofspeech|>", ""
                    )
                    if sub_str.startswith("!"):
                        sub_str = sub_str[1:]
                        if sub_str.startswith("!"):  # !!bytes
                            sub_str = eval(sub_str[1:])
                        try:
                            time1 = time.perf_counter()
                            data_src = load_audio_text_image_video(sub_str[1:], fs=frontend.fs)
                            data_src = load_audio_text_image_video(sub_str, fs=frontend.fs)
                            time2 = time.perf_counter()
                            meta_data["load_data"] = f"{time2 - time1:0.3f}"
                        except Exception as e:
@@ -1203,9 +1207,10 @@
            input_source_ids = input_ids + source_ids
            input_ids += source_ids + target_ids
            labels += source_mask + target_ids
            fbank.append(speech[0, :, :])
            fbank_mask += fbank_mask_i
            fbank_lens.append(speech_lengths)
            if len(speech) > 0:
                fbank.append(speech[0, :, :])
                fbank_lens.append(speech_lengths)
        input_ids = torch.tensor(input_ids, dtype=torch.int64)  # [: self.max_token_length]
        attention_mask = torch.tensor([1] * len(input_ids), dtype=torch.int32)
@@ -1219,10 +1224,14 @@
        source_ids = torch.tensor(input_source_ids, dtype=torch.int64)
        target_ids = torch.tensor(target_ids, dtype=torch.int64)
        speech = torch.nn.utils.rnn.pad_sequence(fbank, batch_first=True, padding_value=0.0)
        speech_lengths = torch.nn.utils.rnn.pad_sequence(
            fbank_lens, batch_first=True, padding_value=-1
        )
        if len(fbank) > 0:
            speech = torch.nn.utils.rnn.pad_sequence(fbank, batch_first=True, padding_value=0.0)
            speech_lengths = torch.nn.utils.rnn.pad_sequence(
                fbank_lens, batch_first=True, padding_value=-1
            )
        else:
            speech = []
            speech_lengths = []
        output = {
            "speech": speech,
            "speech_lengths": speech_lengths,
@@ -1238,7 +1247,8 @@
        return output
    def inference(
    def inference_prepare(
        self,
        data_in,
        data_lengths=None,
@@ -1260,17 +1270,18 @@
        # audio encoder
        speech = batch["speech"]
        speech_lengths = batch["speech_lengths"][:, 0]
        # fp16
        if kwargs.get("fp16", False):
            speech = speech.to(torch.float16)
        elif kwargs.get("bf16", False):
            speech = speech.to(torch.bfloat16)
        # audio encoder
        encoder_out, encoder_out_lens = self.encode(speech, speech_lengths)
        if len(speech) > 0:
            speech_lengths = batch["speech_lengths"][:, 0]
            # fp16
            if kwargs.get("fp16", False):
                speech = speech.to(torch.float16)
            elif kwargs.get("bf16", False):
                speech = speech.to(torch.bfloat16)
            # audio encoder
            encoder_out, encoder_out_lens = self.encode(speech, speech_lengths)
        # audio_adaptor
        encoder_out, encoder_out_lens = self.audio_adaptor(encoder_out, encoder_out_lens)
            # audio_adaptor
            encoder_out, encoder_out_lens = self.audio_adaptor(encoder_out, encoder_out_lens)
        input_ids = batch["input_ids"]
        source_ids = batch["source_ids"]
@@ -1316,6 +1327,22 @@
                        ] = speech_token
                    speech_idx += 1
        return inputs_embeds, contents, batch, source_ids, meta_data
    def inference(
        self,
        data_in,
        data_lengths=None,
        key: list = None,
        tokenizer=None,
        frontend=None,
        **kwargs,
    ):
        inputs_embeds, contents, batch, source_ids, meta_data = self.inference_prepare(
            data_in, data_lengths, key, tokenizer, frontend, **kwargs
        )
        llm_dtype = kwargs.get("llm_dtype", "fp32")
        if llm_dtype == "fp32":
funasr/utils/dynamic_import.py
@@ -2,6 +2,8 @@
import importlib.util
import inspect
import os.path
import sys
def load_module_from_path(file_path):
@@ -18,6 +20,23 @@
    return module
def import_module_from_path(file_path: str):
    if file_path.startswith("http"):
        from funasr.download.file import download_from_url
        file_path = download_from_url(file_path)
    file_dir = os.path.dirname(file_path)
    file_name = os.path.basename(file_path)
    module_name = file_path.split("/")[-1].replace(".py", "")
    if len(file_dir) < 1:
        file_dir = "./"
    sys.path.append(file_dir)
    importlib.import_module(module_name)
    print(f"Loading remote code successfully: {file_path}")
#
# def load_module_from_path(module_name, file_path):
#     """
funasr/utils/version_checker.py
@@ -1,9 +1,10 @@
import requests
from packaging import version
from funasr import __version__  # Ensure that __version__ is defined in your package's __init__.py
def get_pypi_version(package_name):
    import requests
    url = f"https://pypi.org/pypi/{package_name}/json"
    response = requests.get(url)
    if response.status_code == 200:
setup.py
@@ -40,7 +40,7 @@
        "hydra-core>=1.3.2",
        "tensorboardX",
        # "rotary_embedding_torch",
        "openai-whisper",
        "requests",
    ],
    # train: The modules invoked when training only.
    "train": [