MiniCPM-o 4.5
MiniCPM-o 全模态系列
从一段音频或一张图片开始,跑通首次全模态对话:语音转文本、文本转语音、语音克隆,以及把视觉与语音一起喂给模型的全模态识别。选择与你任务相符的示例,逐步搭建自己的音视频应用。
| 示例 | 输入 → 输出 | 要点 |
|---|---|---|
| 语音转文本 | 音频 → 文本 | ASR / 语音问答,需 librosa 读音频 |
| 文本转语音 | 文本 → 音频 | 需 model.init_tts(),输出 .wav |
| 语音克隆 | 参考音频 + 文本 → 音频 | 用一段参考音复刻音色 |
| 全模态识别 | 图像 + 文本 → 文本 | 物体识别与物体定位 |
加载模型统一使用 AutoModel.from_pretrained(..., trust_remote_code=True),注意力实现请用 sdpa 或 flash_attention_2,不要用 eager。
语音转文本
加载模型
Python
import torch
import librosa
from transformers import AutoModel, AutoTokenizer
model_path = 'openbmb/MiniCPM-o-2_6'
model = AutoModel.from_pretrained(model_path, trust_remote_code=True,
# sdpa 或 flash_attention_2,不要用 eager
attn_implementation='sdpa', torch_dtype=torch.bfloat16)
model = model.eval().cuda()
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model.init_tts()
model.tts.float()
使用示例
Python
audio_input, _ = librosa.load('./assets/recongize.wav', sr=16000, mono=True)
msgs = [{'role': 'user', 'content': [audio_input]}]
res = model.chat(
msgs=msgs,
image=None,
tokenizer=tokenizer,
sampling=True,
max_new_tokens=4096,
use_tts_template=True,
temperature=0.3,
generate_audio=True,
)
print("asr 结果:", res)
示例输出
Plain Text
asr 结果: 你好!我是MiniCPM 3 Omni,由面壁智能科技有限责任公司研发。我今天能为你提供什么帮助?
文本转语音
加载模型
Python
import torch
import librosa
from transformers import AutoModel, AutoTokenizer
model_path = 'openbmb/MiniCPM-o-2_6'
model = AutoModel.from_pretrained(model_path, trust_remote_code=True,
# sdpa 或 flash_attention_2,不要用 eager
attn_implementation='sdpa', torch_dtype=torch.bfloat16)
model = model.eval().cuda()
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model.init_tts()
model.tts.float()
使用示例
Python
text_to_speak = """
男声以较低的音高,较低的音量,充满喜悦并感到深刻的幸福,表现得非常亲切与满足,"I have a dream, 世界会更好。"
"""
ref_audio_path = './assets/male_example.wav'
ref_audio, _ = librosa.load(ref_audio_path, sr=16000, mono=True)
msgs = [{'role': 'user', 'content': [text_to_speak, ref_audio]}]
tts_result = model.chat(
msgs=msgs,
image=None,
tokenizer=tokenizer,
sampling=True,
temperature=0.3,
max_new_tokens=4096,
use_tts_template=True,
generate_audio=True,
output_audio_path='text2speech_output.wav',
ref_audio=ref_audio
)
语音克隆
加载模型
Python
import torch
import librosa
from transformers import AutoModel, AutoTokenizer
model_path = 'openbmb/MiniCPM-o-2_6'
model = AutoModel.from_pretrained(model_path, trust_remote_code=True,
# sdpa 或 flash_attention_2,不要用 eager
attn_implementation='sdpa', torch_dtype=torch.bfloat16)
model = model.eval().cuda()
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model.init_tts()
model.tts.float()
使用示例
Python
prompt = "Please repeat the user's speech, including voice style and speech content."
audio_input, _ = librosa.load('./assets/male_example.wav', sr=16000, mono=True)
msgs = [{'role': 'user', 'content': [prompt, audio_input]}]
res = model.chat(
msgs=msgs,
image=None,
tokenizer=tokenizer,
sampling=True,
max_new_tokens=4096,
use_tts_template=True,
temperature=0.3,
generate_audio=True,
output_audio_path='./voice_clone_output.wav',
)
全模态识别
可以用 MiniCPM-V-4_5 在图片上做物体识别任务。
Python
from PIL import Image
import torch
from transformers import AutoModel, AutoTokenizer
model_path = 'openbmb/MiniCPM-V-4_5'
model = AutoModel.from_pretrained(model_path, trust_remote_code=True,
# sdpa 或 flash_attention_2,不要用 eager
attn_implementation='sdpa', torch_dtype=torch.bfloat16)
model = model.eval().cuda()
tokenizer = AutoTokenizer.from_pretrained(
model_path, trust_remote_code=True)
image = Image.open('./assets/single.png').convert('RGB')
question = "图片中的地貌是什么?"
msgs = [{'role': 'user', 'content': [image, question]}]
answer = model.chat(
msgs=msgs,
image=image,
tokenizer=tokenizer
)
print(answer)
图片:

结果:
Plain Text
The landform in the picture is a mountain range. The image shows steep cliffs, rugged terrain, and a winding path that ascends between two prominent rocky peaks covered with green vegetation. This type of landscape features high elevations and dramatic slopes characteristic of mountains.
物体定位
也可以用 MiniCPM-V-4_5 做物体定位任务。
Python
from PIL import Image
import torch
from transformers import AutoModel, AutoTokenizer
model_path = 'openbmb/MiniCPM-V-4_5'
model = AutoModel.from_pretrained(model_path, trust_remote_code=True,
# sdpa 或 flash_attention_2,不要用 eager
attn_implementation='sdpa', torch_dtype=torch.bfloat16)
model = model.eval().cuda()
tokenizer = AutoTokenizer.from_pretrained(
model_path, trust_remote_code=True)
image = Image.open('./assets/multi_object.png').convert('RGB')
question = "识别图片中的食物,以 JSON 格式返回它们的 bounding box 与中英文名称。"
msgs = [{'role': 'user', 'content': [image, question]}]
answer = model.chat(
msgs=msgs,
image=image,
tokenizer=tokenizer
)
print(answer)
图片:

结果:
JSON
{
"food_1": {
"bounding_box": "<box>520 637 644 782</box>",
"chinese_name": "炒鸡蛋",
"english_name": "Fried eggs"
},
"food_2": {
"bounding_box": "<box>9 329 194 480</box>",
"chinese_name": "蔬菜白米饭",
"english_name": "Vegetable white rice"
},
"food_3": {
"bounding_box": "<box>43 505 310 670</box>",
"chinese_name": "蔬菜白米饭",
"english_name": "Vegetable white rice"
},
"food_4": {
"bounding_box": "<box>15 157 312 327</box>",
"chinese_name": "蔬菜白米饭",
"english_name": "Vegetable white rice"
},
"food_5": {
"bounding_box": "<box>435 20 737 324</box>",
"chinese_name": "鸡汤",
"english_name": "Chicken soup"
},
"food_6": {
"bounding_box": "<box>496 341 951 574</box>",
"chinese_name": "炒青菜",
"english_name": "Stir-fried greens"
},
"food_7": {
"bounding_box": "<box>203 345 542 574</box>",
"chinese_name": "蒜蓉粉丝蒸虾",
"english_name": "Garlic and vermicelli steamed shrimp"
},
"food_8": {
"bounding_box": "<box>729 1 1000 314</box>",
"chinese_name": "圣女果",
"english_name": "Cherry tomatoes"
}
}