MCPcopy Create free account
hub / github.com/FunAudioLLM/Fun-Audio-Chat / infer_example

Function infer_example

examples/infer_s2t.py:32–71  ·  view source on GitHub ↗

推理示例函数(仅生成文本,不生成语音) Args: model_path: 模型路径 audio_path: 输入音频路径 instruction: 输入音频指令,可选

(model_path, audio_path, instruction: Optional[str] = None)

Source from the content-addressed store, hash-verified

30device = "cuda:0" if torch.cuda.is_available() else "cpu"
31
32def infer_example(model_path, audio_path, instruction: Optional[str] = None):
33 """
34 推理示例函数(仅生成文本,不生成语音)
35
36 Args:
37 model_path: 模型路径
38 audio_path: 输入音频路径
39 instruction: 输入音频指令,可选
40 """
41 config = AutoConfig.from_pretrained(model_path)
42 processor = AutoProcessor.from_pretrained(model_path)
43 model = AutoModelForSeq2SeqLM.from_pretrained(model_path, config=config, torch_dtype=torch.bfloat16, device_map=device)
44
45 # 生成参数
46 model.sp_gen_kwargs.update({
47 'text_greedy': True,
48 'disable_speech': True,
49 })
50
51 # 构建audio样例
52 audio = [librosa.load(audio_path, sr=16000)[0]]
53
54 if instruction is None:
55 conversation = [
56 {"role": "system", "content": DEFAULT_S2T_PROMPT},
57 {"role": "user", "content": AUDIO_TEMPLATE},
58 ]
59 else:
60 conversation = [
61 {"role": "system", "content": DEFAULT_S2T_PROMPT},
62 {"role": "user", "content": AUDIO_TEMPLATE + "\n" + instruction},
63 ]
64
65 text = processor.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False)
66 inputs = processor(text=text, audio=audio, return_tensors="pt", return_token_type_ids=False).to(model.device)
67 generate_ids, _ = model.generate(**inputs)
68 generate_ids = generate_ids[:, inputs.input_ids.size(1):]
69 generate_text = processor.decode(generate_ids[0], skip_special_tokens=True)
70
71 print("generate_text: ", generate_text)
72
73
74def infer_function_calling_example(model_path, audio_path):

Callers 1

infer_s2t.pyFile · 0.70

Calls 1

decodeMethod · 0.80

Tested by

no test coverage detected