模型简介
模型特点
模型能力
使用案例
🚀 MiniCPM-o 2.6:适用于手机端的视觉、语音和多模态直播的GPT - 4o级多模态大语言模型
MiniCPM-o 2.6是一款强大的多模态大语言模型,可在手机端高效运行,具备卓越的视觉、语音处理能力以及多模态直播功能,为用户带来全新的交互体验。
🔔 最新消息
- [2025.03.01] 🚀🚀🚀 MiniCPM-o的对齐技术RLAIF - V被CVPR 2025接收!代码、数据集、论文均已开源!
- [2025.01.24] 📢📢📢 MiniCPM-o 2.6技术报告发布!点击查看。
- [2025.01.19] ⭐️⭐️⭐️ MiniCPM-o登上GitHub趋势榜,并在Hugging Face趋势榜上排名第二!
✨ 主要特性
🔥 领先的视觉能力
MiniCPM-o 2.6在OpenCompass上的综合评估中平均得分达到70.2分,该评估涵盖了8个流行的基准测试。仅80亿参数的情况下,在单图像理解方面超越了广泛使用的专有模型,如GPT - 4o - 202405、Gemini 1.5 Pro和Claude 3.5 Sonnet。在多图像和视频理解方面也优于GPT - 4V和Claude 3.5 Sonnet,并展现出了出色的上下文学习能力。
🎙 先进的语音能力
MiniCPM-o 2.6支持中英双语实时语音对话,并可配置语音。在音频理解任务(如ASR和STT翻译)上优于GPT - 4o - realtime,在开源社区的语义和声学评估中,语音对话表现达到了先进水平。还支持诸如情感/速度/风格控制、端到端语音克隆、角色扮演等有趣功能。
🎬 强大的多模态直播能力
作为一项新特性,MiniCPM-o 2.6可以独立于用户查询接受连续的视频和音频流,并支持实时语音交互。在StreamingBench(一个用于实时视频理解、全源(视频和音频)理解和多模态上下文理解的综合基准测试)上,优于GPT - 4o - 202408和Claude 3.5 Sonnet,在开源社区中表现出先进水平。
💪 强大的OCR能力及其他
继承了MiniCPM - V系列流行的视觉能力,MiniCPM-o 2.6可以处理任意宽高比、高达180万像素(如1344x1344)的图像。在OCRBench上,对于250亿参数以下的模型,达到了先进水平,超越了GPT - 4o - 202405等专有模型。基于最新的RLAIF - V和VisCPM技术,具有可靠的行为表现,在MMHal - Bench上优于GPT - 4o和Claude 3.5 Sonnet,并支持30多种语言的多语言能力。
🚀 卓越的效率
除了友好的模型规模外,MiniCPM-o 2.6还展现出了先进的令牌密度(即每个视觉令牌编码的像素数)。处理180万像素的图像时仅产生640个令牌,比大多数模型少75%。这直接提高了推理速度、首令牌延迟、内存使用效率和功耗。因此,MiniCPM-o 2.6可以在iPad等终端设备上高效支持多模态直播。
💫 易于使用
MiniCPM-o 2.6可以通过多种方式轻松使用:
- llama.cpp支持在本地设备上进行高效的CPU推理。
- int4和GGUF格式的量化模型,有16种尺寸可供选择。
- vLLM支持高吞吐量和内存高效的推理。
- 使用LLaMA - Factory在新领域和任务上进行微调。
- 使用Gradio快速设置本地WebUI演示。
- 在服务器上进行在线Web演示。
📚 详细文档
模型架构
- 端到端全模态架构:不同模态的编码器/解码器以端到端的方式连接和训练,充分利用丰富的多模态知识。
- 全模态直播机制:
- 将离线模态编码器/解码器转换为在线编码器/解码器,以处理流式输入/输出。
- 在大语言模型主干中设计了一种时分复用(TDM)机制,用于全模态流式处理。它将并行的全模态流在小的周期性时间片内划分为顺序信息。
- 可配置语音建模设计:设计了一个多模态系统提示,包括传统的文本系统提示和一个新的音频系统提示,用于确定助手语音。这使得在推理时可以灵活配置语音,也便于端到端语音克隆和基于描述的语音创建。

评估

视觉理解结果
图像理解:
模型 | 规模 | 令牌密度+ | OpenCompass | OCRBench | MathVista mini | ChartQA | MMVet | MMStar | MME | MMB1.1 test | AI2D | MMMU val | HallusionBench | TextVQA val | DocVQA test | MathVerse mini | MathVision | MMHal Score |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
专有模型 | ||||||||||||||||||
GPT - 4o - 20240513 | - | 1088 | 69.9 | 736 | 61.3 | 85.7 | 69.1 | 63.9 | 2328.7 | 82.2 | 84.6 | 69.2 | 55.0 | - | 92.8 | 50.2 | 30.4 | 3.6 |
Claude3.5 - Sonnet | - | 750 | 67.9 | 788 | 61.6 | 90.8 | 66.0 | 62.2 | 1920.0 | 78.5 | 80.2 | 65.9 | 49.9 | - | 95.2 | - | - | 3.4 |
Gemini 1.5 Pro | - | - | 64.4 | 754 | 57.7 | 81.3 | 64.0 | 59.1 | 2110.6 | 73.9 | 79.1 | 60.6 | 45.6 | 73.5 | 86.5 | - | 19.2 | - |
GPT - 4o - mini - 20240718 | - | 1088 | 64.1 | 785 | 52.4 | - | 66.9 | 54.8 | 2003.4 | 76.0 | 77.8 | 60.0 | 46.1 | - | - | - | - | 3.3 |
开源模型 | ||||||||||||||||||
Cambrian - 34B | 34B | 1820 | 58.3 | 591 | 50.3 | 75.6 | 53.2 | 54.2 | 2049.9 | 77.8 | 79.5 | 50.4 | 41.6 | 76.7 | 75.5 | - | - | - |
GLM - 4V - 9B | 13B | 784 | 59.1 | 776 | 51.1 | - | 58.0 | 54.8 | 2018.8 | 67.9 | 71.2 | 46.9 | 45.0 | - | - | - | - | - |
Pixtral - 12B | 12B | 256 | 61.0 | 685 | 56.9 | 81.8 | 58.5 | 54.5 | - | 72.7 | 79.0 | 51.1 | 47.0 | 75.7 | 90.7 | - | - | - |
DeepSeek - VL2 - 27B (4B) | 27B | 672 | 66.4 | 809 | 63.9 | 86.0 | 60.0 | 61.9 | 2253.0 | 81.2 | 83.8 | 54.0 | 45.3 | 84.2 | 93.3 | - | - | 3.0 |
Qwen2 - VL - 7B | 8B | 784 | 67.1 | 866 | 58.2 | 83.0 | 62.0 | 60.7 | 2326.0 | 81.8 | 83.0 | 54.1 | 50.6 | 84.3 | 94.5 | 31.9 | 16.3 | 3.2 |
LLaVA - OneVision - 72B | 72B | 182 | 68.1 | 741 | 67.5 | 83.7 | 60.6 | 65.8 | 2261.0 | 85.0 | 85.6 | 56.8 | 49.0 | 80.5 | 91.3 | 39.1 | - | 3.5 |
InternVL2.5 - 8B | 8B | 706 | 68.3 | 822 | 64.4 | 84.8 | 62.8 | 62.8 | 2344.0 | 83.6 | 84.5 | 56.0 | 50.1 | 79.1 | 93.0 | 39.5 | 19.7 | 3.4 |
MiniCPM - V 2.6 | 8B | 2822 | 65.2 | 852* | 60.6 | 79.4 | 60.0 | 57.5 | 2348.4* | 78.0 | 82.1 | 49.8* | 48.1* | 80.1 | 90.8 | 25.7 | 18.3 | 3.6 |
MiniCPM - o 2.6 | 8B | 2822 | 70.2 | 897* | 71.9* | 86.9* | 67.5 | 64.0 | 2372.0* | 80.5 | 85.8 | 50.4* | 51.9 | 82.0 | 93.5 | 41.4* | 23.1* | 3.8 |
+ 令牌密度:在最大分辨率下,每个视觉令牌编码的像素数,即最大分辨率下的像素数/视觉令牌数。
注意:对于专有模型,我们根据官方API文档中定义的图像编码收费策略计算令牌密度,这提供了一个上限估计。
多图像和视频理解:
点击查看
模型 | 规模 | BLINK val | Mantis Eval | MIRB | Video - MME (wo / w subs) |
---|---|---|---|---|---|
专有模型 | |||||
GPT - 4o - 20240513 | - | 68.0 | - | - | 71.9/77.2 |
GPT4V | - | 54.6 | 62.7 | 53.1 | 59.9/63.3 |
开源模型 | |||||
LLaVA - NeXT - Interleave 14B | 14B | 52.6 | 66.4 | 30.2 | - |
LLaVA - OneVision - 72B | 72B | 55.4 | 77.6 | - | 66.2/69.5 |
MANTIS 8B | 8B | 49.1 | 59.5 | 34.8 | - |
Qwen2 - VL - 7B | 8B | 53.2 | 69.6* | 67.6* | 63.3/69.0 |
InternVL2.5 - 8B | 8B | 54.8 | 67.7 | 52.5 | 64.2/66.9 |
MiniCPM - V 2.6 | 8B | 53.0 | 69.1 | 53.8 | 60.9/63.6 |
MiniCPM - o 2.6 | 8B | 56.7 | 71.9 | 58.6 | 63.9/67.9 |
音频理解和语音对话结果
音频理解:
任务 | 规模 | ASR (中文) | ASR (英文) | AST | 情感识别 | |||||
---|---|---|---|---|---|---|---|---|---|---|
指标 | CER↓ | WER↓ | BLEU↑ | ACC↑ | ||||||
数据集 | AISHELL - 1 | Fleurs中文 | WenetSpeech test - net | LibriSpeech test - clean | GigaSpeech | TED - LIUM | CoVoST英文转中文 | CoVoST中文转英文 | MELD情感 | |
专有模型 | ||||||||||
GPT - 4o - Realtime | - | 7.3* | 5.4* | 28.9* | 2.6* | 12.9* | 4.8* | 37.1* | 15.7* | 33.2* |
Gemini 1.5 Pro | - | 4.5* | 5.9* | 14.3* | 2.9* | 10.6* | 3.0* | 47.3* | 22.6* | 48.4* |
开源模型 | ||||||||||
Qwen2 - Audio - 7B | 8B | - | 7.5 | - | 1.6 | - | - | 45.2 | 24.4 | 55.3 |
Qwen2 - Audio - 7B - Instruct | 8B | 2.6* | 6.9* | 10.3* | 3.1* | 9.7* | 5.9* | 39.5* | 22.9* | 17.4* |
GLM - 4 - Voice - Base | 9B | 2.5 | - | - | 2.8 | - | - | - | - | |
MiniCPM - o 2.6 | 8B | 1.6 | 4.4 | 6.9 | 1.7 | 8.7 | 3.0 | 48.2 | 27.2 | 52.4 |
语音生成:
任务 | 规模 | 语音问答 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
指标 | ACC↑ | G - Eval (10分制)↑ | 语义ELO得分↑ | 声学ELO得分↑ | 总体ELO得分↑ | UTMOS↑ | ASR - WER↓ | |||
数据集 | 语音Llama问答 | 语音网络问答 | 语音琐事问答 | 语音AlpacaEval | AudioArena | |||||
专有模型 | ||||||||||
GPT - 4o - Realtime | 71.7 | 51.6 | 69.7 | 7.4 | 1157 | 1203 | 1200 | 4.2 | 2.3 | |
开源模型 | ||||||||||
GLM - 4 - Voice | 9B | 50.0 | 32.0 | 36.4 | 5.1 | 999 | 1147 | 1035 | 4.1 | 11.7 |
Llama - Omni | 8B | 45.3 | 22.9 | 10.7 | 3.9 | 960 | 878 | 897 | 3.2 | 24.3 |
Moshi | 7B | 43.7 | 23.8 | 16.7 | 2.4 | 871 | 808 | 875 | 2.8 | 8.2 |
Mini - Omni | 1B | 22.0 | 12.8 | 6.9 | 2.5 | 926 | 803 | 865 | 3.4 | 10.0 |
MiniCPM - o 2.6 | 8B | 61.0 | 40.0 | 40.2 | 5.1 | 1088 | 1163 | 1131 | 4.2 | 9.8 |
端到端语音克隆
任务 | 语音克隆 | |
---|---|---|
指标 | SIMO↑ | SIMO↑ |
数据集 | Seed - TTS测试中文 | Seed - TTS测试英文 |
F5 - TTS | 76 | 67 |
CosyVoice | 75 | 64 |
FireRedTTS | 63 | 46 |
MiniCPM - o 2.6 | 57 | 47 |
多模态直播结果
多模态直播:StreamingBench上的结果
模型 | 规模 | 实时视频理解 | 全源理解 | 上下文理解 | 总体 | |||
---|---|---|---|---|---|---|---|---|
专有模型 | ||||||||
Gemini 1.5 Pro | - | 77.4 | 67.8 | 51.1 | 70.3 | |||
GPT - 4o - 202408 | - | 74.5 | 51.0 | 48.0 | 64.1 | |||
Claude - 3.5 - Sonnet | - | 74.0 | 41.4 | 37.8 | 59.7 | |||
开源模型 | ||||||||
VILA - 1.5 | 8B | 61.5 | 37.5 | 26.7 | 49.5 | |||
LongVA | 7B | 63.1 | 35.9 | 30.2 | 50.7 | |||
LLaVA - Next - Video - 34B | 34B | 69.8 | 41.7 | 34.3 | 56.7 | |||
Qwen2 - VL - 7B | 8B | 71.2 | 40.7 | 33.1 | 57.0 | |||
InternVL2 - 8B | 8B | 70.1 | 42.7 | 34.1 | 57.0 | |||
VITA - 1.5 | 8B | 70.9 | 40.8 | 35.8 | 57.4 | |||
LLaVA - OneVision - 7B | 8B | 74.3 | 40.8 | 31.0 | 58.4 | |||
InternLM - XC2.5 - OL - 7B | 8B | 75.4 | 46.2 | 33.6 | 60.8 | |||
MiniCPM - V 2.6 | 8B | 72.4 | 40.2 | 33.4 | 57.7 | |||
MiniCPM - o 2.6 | 8B | 79.9 | 53.4 | 38.5 | 66.0 |
示例
我们将MiniCPM-o 2.6部署在终端设备上。演示视频是在iPad Pro和Web演示上的原始速度录制。



在线演示
点击此处尝试MiniCPM-o 2.6的在线演示。
💻 使用示例
基础用法
在NVIDIA GPU上使用Huggingface transformers进行推理。请确保安装了transformers==4.44.2
,因为其他版本可能存在兼容性问题。我们正在调查此问题。在Python 3.10上测试的依赖如下:
Pillow==10.1.0
torch==2.3.1
torchaudio==2.3.1
torchvision==0.18.1
transformers==4.44.2
librosa==0.9.0
soundfile==0.12.1
vector-quantize-pytorch==1.18.5
vocos==0.1.0
decord
moviepy
模型初始化
import torch
from PIL import Image
from transformers import AutoModel, AutoTokenizer
# 加载全模态模型默认设置,默认init_vision/init_audio/init_tts为True
# 如果加载仅视觉模型,请设置init_audio=False和init_tts=False
# 如果加载仅音频模型,请设置init_vision=False
model = AutoModel.from_pretrained(
'openbmb/MiniCPM-o-2_6',
trust_remote_code=True,
attn_implementation='sdpa', # sdpa或flash_attention_2
torch_dtype=torch.bfloat16,
init_vision=True,
init_audio=True,
init_tts=True
)
model = model.eval().cuda()
tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-o-2_6', trust_remote_code=True)
# 除了仅视觉模式,还需要初始化tts处理器和vocos
model.init_tts()
如果使用较旧版本的PyTorch,可能会遇到 "weight_norm_fwd_first_dim_kernel" not implemented for 'BFloat16'
问题,请将TTS转换为float32类型。
model.tts.float()
高级用法
全模态模式
我们提供两种推理模式:聊天和流式推理。
聊天推理
import math
import numpy as np
from PIL import Image
from moviepy.editor import VideoFileClip
import tempfile
import librosa
import soundfile as sf
def get_video_chunk_content(video_path, flatten=True):
video = VideoFileClip(video_path)
print('video_duration:', video.duration)
with tempfile.NamedTemporaryFile(suffix=".wav", delete=True) as temp_audio_file:
temp_audio_file_path = temp_audio_file.name
video.audio.write_audiofile(temp_audio_file_path, codec="pcm_s16le", fps=16000)
audio_np, sr = librosa.load(temp_audio_file_path, sr=16000, mono=True)
num_units = math.ceil(video.duration)
# 1帧 + 1秒音频块
contents= []
for i in range(num_units):
frame = video.get_frame(i+1)
image = Image.fromarray((frame).astype(np.uint8))
audio = audio_np[sr*i:sr*(i+1)]
if flatten:
contents.extend(["<unit>", image, audio])
else:
contents.append(["<unit>", image, audio])
return contents
video_path="assets/Skiing.mp4"
# 如果使用语音克隆提示,请设置ref_audio
ref_audio_path = 'assets/demo.wav'
ref_audio, _ = librosa.load(ref_audio_path, sr=16000, mono=True)
sys_msg = model.get_sys_prompt(ref_audio=ref_audio, mode='omni', language='en')
# 或使用默认提示
# sys_msg = model.get_sys_prompt(mode='omni', language='en')
contents = get_video_chunk_content(video_path)
msg = {"role":"user", "content": contents}
msgs = [sys_msg, msg]
# 请设置generate_audio=True和output_audio_path以保存tts结果
generate_audio = True
output_audio_path = 'output.wav'
res = model.chat(
msgs=msgs,
tokenizer=tokenizer,
sampling=True,
temperature=0.5,
max_new_tokens=4096,
omni_input=True, # 全模态推理时请设置omni_input=True
use_tts_template=True,
generate_audio=generate_audio,
output_audio_path=output_audio_path,
max_slice_nums=1,
use_image_id=False,
return_dict=True
)
print(res)
## 你将得到答案:The person in the picture is skiing down a snowy slope.
# import IPython
# IPython.display.Audio('output.wav')
流式推理
# 新对话需要先重置会话,这将重置kv缓存
model.reset_session()
contents = get_video_chunk_content(video_path, flatten=False)
session_id = '123'
generate_audio = True
# 1. 预填充系统提示
res = model.streaming_prefill(
session_id=session_id,
msgs=[sys_msg],
tokenizer=tokenizer
)
# 2. 预填充视频/音频块
for content in contents:
msgs = [{"role":"user", "content": content}]
res = model.streaming_prefill(
session_id=session_id,
msgs=msgs,
tokenizer=tokenizer
)
# 3. 生成
res = model.streaming_generate(
session_id=session_id,
tokenizer=tokenizer,
temperature=0.5,
generate_audio=generate_audio
)
audios = []
text = ""
if generate_audio:
for r in res:
audio_wav = r.audio_wav
sampling_rate = r.sampling_rate
txt = r.text
audios.append(audio_wav)
text += txt
res = np.concatenate(audios)
sf.write("output.wav", res, samplerate=sampling_rate)
print("text:", text)
print("音频保存到output.wav")
else:
for r in res:
text += r['text']
print("text:", text)
语音和音频模式
模型初始化
import torch
import librosa
from transformers import AutoModel, AutoTokenizer
model = AutoModel.from_pretrained('openbmb/MiniCPM-o-2_6', trust_remote_code=True,
attn_implementation='sdpa', torch_dtype=torch.bfloat16) # sdpa或flash_attention_2,无急切模式
model = model.eval().cuda()
tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-o-2_6', trust_remote_code=True)
model.init_tts()
model.tts.float()
模仿任务
模仿
任务反映了模型的端到端语音建模能力。模型接收音频输入,输出ASR转录,然后以高度相似性重构原始音频。重构音频与原始音频的相似度越高,模型在端到端语音建模方面的基础能力就越强。
mimick_prompt = "请重复每个用户的语音,包括语音风格和语音内容。"
audio_input, _ = librosa.load('./assets/input_examples/Trump_WEF_2018_10s.mp3', sr=16000, mono=True) # 加载要模仿的音频
# 也可以尝试 `./assets/input_examples/cxk_original.wav`,
# `./assets/input_examples/fast-pace.wav`,
# `./assets/input_examples/chi-english-1.wav`
# `./assets/input_examples/exciting-emotion.wav`
# 以测试语音相关的不同特征。
msgs = [{'role': 'user', 'content': [mimick_prompt, audio_input]}]
res = model.chat(
msgs=msgs,
tokenizer=tokenizer,
sampling=True,
max_new_tokens=128,
use_tts_template=True,
temperature=0.3,
generate_audio=True,
output_audio_path='output_mimick.wav', # 将tts结果保存到output_audio_path
)
可配置语音的通用语音对话
MiniCPM-o 2.6
的一个常见使用场景是根据音频提示扮演特定角色。它会在一定程度上模仿角色的语音,并在文本中表现得像该角色,包括语言风格。在这种模式下,MiniCPM-o 2.6
听起来更加自然和人性化。可以使用自定义音频提示以端到端的方式自定义角色的语音。
ref_audio, _ = librosa.load('./assets/input_examples/icl_20.wav', sr=16000, mono=True) # 加载参考音频
sys_prompt = model.get_sys_prompt(ref_audio=ref_audio, mode='audio_roleplay', language='en')
# 第一轮
user_question = {'role': 'user', 'content': [librosa.load('xxx.wav', sr=16000, mono=True)[0]]}
msgs = [sys_prompt, user_question]
res = model.chat(
msgs=msgs,
tokenizer=tokenizer,
sampling=True,
max_new_tokens=128,
use_tts_template=True,
generate_audio=True,
temperature=0.3,
output_audio_path='result_roleplay_round_1.wav',
)
# 第二轮
history = msgs.append({'role': 'assistant', 'content': res})
user_question = {'role': 'user', 'content': [librosa.load('xxx.wav', sr=16000, mono=True)[0]]}
msgs = history.append(user_question)
res = model.chat(
msgs=msgs,
tokenizer=tokenizer,
sampling=True,
max_new_tokens=128,
use_tts_template=True,
generate_audio=True,
temperature=0.3,
output_audio_path='result_roleplay_round_2.wav',
)
print(res)
作为AI助手的语音对话
MiniCPM-o 2.6
的一个增强功能是作为AI助手,但语音选择有限。在这种模式下,MiniCPM-o 2.6
不太像人类,更像语音助手。在这种模式下,模型更遵循指令。演示时,建议使用assistant_female_voice
、assistant_male_voice
和assistant_default_female_voice
。其他语音可能有效,但不如默认语音稳定。
请注意,assistant_female_voice
和assistant_male_voice
更稳定,但听起来像机器人,而assistant_default_female_voice
更像人类,但不稳定,其语音在多轮对话中经常变化。建议尝试稳定的语音assistant_female_voice
和assistant_male_voice
。
ref_audio, _ = librosa.load('./assets/input_examples/assistant_female_voice.wav', sr=16000, mono=True) # 或使用 `./assets/input_examples/assistant_male_voice.wav`
sys_prompt = model.get_sys_prompt(ref_audio=ref_audio, mode='audio_assistant', language='en')
user_question = {'role': 'user', 'content': [librosa.load('xxx.wav', sr=16000, mono=True)[0]]} # 加载用户的音频问题
# 第一轮
msgs = [sys_prompt, user_question]
res = model.chat(
msgs=msgs,
tokenizer=tokenizer,
sampling=True,
max_new_tokens=128,
use_tts_template=True,
generate_audio=True,
temperature=0.3,
output_audio_path='result_assistant_round_1.wav',
)
# 第二轮
history = msgs.append({'role': 'assistant', 'content': res})
user_question = {'role': 'user', 'content': [librosa.load('xxx.wav', sr=16000, mono=True)[0]]}
msgs = history.append(user_question)
res = model.chat(
msgs=msgs,
tokenizer=tokenizer,
sampling=True,
max_new_tokens=128,
use_tts_template=True,
generate_audio=True,
temperature=0.3,
output_audio_path='result_assistant_round_2.wav',
)
print(res)
指令到语音
MiniCPM-o 2.6
还可以进行指令到语音转换,即语音创建。可以详细描述一种语音,模型将生成符合描述的语音。有关更多指令到语音的示例指令,请参考VoxInstruct。
instruction = '像一位有魅力的男性巨星一样说话,每一个字都散发着自信和风格。'
msgs = [{'role': 'user', 'content': [instruction]}]
res = model.chat(
msgs=msgs,
tokenizer=tokenizer,
sampling=True,
max_new_tokens=128,
use_tts_template=True,
generate_audio=True,
temperature=0.3,
output_audio_path='result_voice_creation.wav',
)
语音克隆
MiniCPM-o 2.6
还可以进行零样本文本到语音转换,即语音克隆。在这种模式下,模型将像TTS模型一样工作。
ref_audio, _ = librosa.load('./assets/input_examples/icl_20.wav', sr=16000, mono=True) # 加载参考音频
sys_prompt = model.get_sys_prompt(ref_audio=ref_audio, mode='voice_cloning', language='en')
text_prompt = f"请朗读以下文本。"
user_question = {'role': 'user', 'content': [text_prompt, "你想要朗读的内容"]}
msgs = [sys_prompt, user_question]
res = model.chat(
msgs=msgs,
tokenizer=tokenizer,
sampling=True,
max_new_tokens=128,
use_tts_template=True,
generate_audio=True,
temperature=0.3,
output_audio_path='result_voice_cloning.wav',
)
处理各种音频理解任务
MiniCPM-o 2.6
还可用于处理各种音频理解任务,如ASR、说话人分析、通用音频字幕和声音场景标记。
对于音频到文本任务,可以使用以下提示:
- 中文ASR(与英文到中文AST相同):
请仔细听这段音频片段,并将其内容逐字记录。
- 英文ASR(与中文到英文AST相同):
Please listen to the audio snippet carefully and transcribe the content.
- 说话人分析:
Based on the speaker's content, speculate on their gender, condition, age range, and health status.
- 通用音频字幕:
Summarize the main content of the audio.
- 通用声音场景标记:
Utilize one keyword to convey the audio's content or the associated scene.
task_prompt = "Please listen to the audio snippet carefully and transcribe the content." + "\n" # 可以更改为其他提示。
audio_input, _ = librosa.load('./assets/input_examples/audio_understanding.mp3', sr=16000, mono=True) # 加载要加字幕的音频
msgs = [{'role': 'user', 'content': [task_prompt, audio_input]}]
res = model.chat(
msgs=msgs,
tokenizer=tokenizer,
sampling=True,
max_new_tokens=128,
use_tts_template=True,
generate_audio=True,
temperature=0.3,
output_audio_path='result_audio_understanding.wav',
)
print(res)
仅视觉模式
MiniCPM-o-2_6
的推理方法与MiniCPM-V-2_6
相同。
单图像聊天
# test.py
image = Image.open('xx.jpg').convert('RGB')
question = 'What is in the image?'
msgs = [{'role': 'user', 'content': [image, question]}]
res = model.chat(
image=None,
msgs=msgs,
tokenizer=tokenizer
)
print(res)
## 如果你想使用流式推理,请确保sampling=True和stream=True
## model.chat将返回一个生成器
res = model.chat(
msgs=msgs,
tokenizer=tokenizer,
sampling=True,
stream=True
)
generated_text = ""
for new_text in res:
generated_text += new_text
print(new_text, flush=True, end='')
多图像聊天
点击查看使用多图像输入运行MiniCPM-o 2.6的Python代码。
image1 = Image.open('image1.jpg').convert('RGB')
image2 = Image.open('image2.jpg').convert('RGB')
question = 'Compare image 1 and image 2, tell me about the differences between image 1 and image 2.'
msgs = [{'role': 'user', 'content': [image1, image2, question]}]
answer = model.chat(
msgs=msgs,
tokenizer=tokenizer
)
print(answer)
上下文少样本学习
点击查看使用少样本输入运行MiniCPM-o 2.6的Python代码。
question = "production date"
image1 = Image.open('example1.jpg').convert('RGB')
answer1 = "2023.08.04"
image2 = Image.open('example2.jpg').convert('RGB')
answer2 = "2007.04.24"
image_test = Image.open('test.jpg').convert('RGB')
msgs = [
{'role': 'user', 'content': [image1, question]}, {'role': 'assistant', 'content': [answer1]},
{'role': 'user', 'content': [image2, question]}, {'role': 'assistant', 'content': [answer2]},
{'role': 'user', 'content': [image_test, question]}
]
answer = model.chat(
msgs=msgs,
tokenizer=tokenizer
)
print(answer)
视频聊天
点击查看使用视频输入运行MiniCPM-o 2.6的Python代码。
MAX_NUM_FRAMES=64 # 如果cuda内存不足,请设置较小的数字
def encode_video(video_path):
def uniform_sample(l, n):
gap = len(l) / n
idxs = [int(i * gap + gap / 2) for i in range(n)]
return [l[i] for i in idxs]
vr = VideoReader(video_path, ctx=cpu(0))
sample_fps = round(vr.get_avg_fps() / 1) # FPS
frame_idx = [i for i in range(0, len(vr), sample_fps)]
if len(frame_idx) > MAX_NUM_FRAMES:
frame_idx = uniform_sample(frame_idx, MAX_NUM_FRAMES)









