GLM-4-Voice,智谱开源版“Her”来了!

简介: 智谱 AI 推出并开源端到端语音模型 GLM-4-Voice!GLM-4-Voice 能够直接理解和生成中英文语音,进行实时语音对话,并且能够遵循用户的指令要求改变语音的情感、语调、语速、方言等属性。

今天,智谱 AI 推出并开源端到端语音模型 GLM-4-Voice!GLM-4-Voice 能够直接理解和生成中英文语音,进行实时语音对话,并且能够遵循用户的指令要求改变语音的情感、语调、语速、方言等属性。

模型结构  

image.png

GLM-4-Voice 由三个部分组成:

  • GLM-4-Voice-Tokenizer: 通过在 Whisper 的 Encoder 部分增加 Vector Quantization 并在 ASR 数据上有监督训练,将连续的语音输入转化为离散的 token。每秒音频平均只需要用 12.5 个离散 token 表示。
  • GLM-4-Voice-Decoder: 基于 CosyVoice 的 Flow Matching 模型结构训练的支持流式推理的语音解码器,将离散化的语音 token 转化为连续的语音输出。最少只需要 10 个语音 token 即可开始生成,降低端到端对话延迟。
  • GLM-4-Voice-9B: 在 GLM-4-9B 的基础上进行语音模态的预训练和对齐,从而能够理解和生成离散化的语音 token。

预训练方面,为了攻克模型在语音模态下的智商和合成表现力两个难关,研究团队将 Speech2Speech 任务解耦合为“根据用户音频做出文本回复”和“根据文本回复和用户语音合成回复语音”两个任务,并设计两种预训练目标,分别基于文本预训练数据和无监督音频数据合成语音-文本交错数据以适配这两种任务形式。GLM-4-Voice-9B 在 GLM-4-9B 的基座模型基础之上,经过了数百万小时音频和数千亿 token 的音频文本交错数据预训练,拥有很强的音频理解和建模能力。

对齐方面,为了支持高质量的语音对话,研究团队设计了一套流式思考架构:根据用户语音,GLM-4-Voice 可以流式交替输出文本和语音两个模态的内容,其中语音模态以文本作为参照保证回复内容的高质量,并根据用户的语音指令要求做出相应的声音变化,在最大程度保留语言模型智商的情况下仍然具有端到端建模的能力,同时具备低延迟性,最低只需要输出 20 个 token 便可以合成语音。

模型体验  

模型链接

GLM-4-Voice-Tokenizer:

https://modelscope.cn/models/ZhipuAI/glm-4-voice-tokenizer

GLM-4-Voice-9B:

https://modelscope.cn/models/ZhipuAI/glm-4-voice-9b

GLM-4-Voice-Decoder:

https://modelscope.cn/models/ZhipuAI/glm-4-voice-decoder

模型效果体验

体验链接:

https://modelscope.cn/studios/ZhipuAI/GLM-4-Voice-Demo/summary

小程序体验:

image.png

模型实战实践  

模型推理

环境准备:

git clone https://github.com/THUDM/GLM-4-Voice.git
 
pip install matcha-tts torchaudio hyperpyyaml
cd GLM-4-Voice
# 如果出现环境问题,可以运行以下命令
pip install -r requirements.txt

然后进去`GLM-4-Voice`的目录下运行以下代码:

import os
import uuid
from typing import List, Optional, Tuple
import torch
import torchaudio
from flow_inference import AudioDecoder
from modelscope import snapshot_download
from speech_tokenizer.modeling_whisper import WhisperVQEncoder
from speech_tokenizer.utils import extract_speech_token
from transformers import AutoModel, AutoTokenizer, GenerationConfig, WhisperFeatureExtractor
class GLM4Voice:
    def _prepare_model(self):
        model_path = snapshot_download('ZhipuAI/glm-4-voice-9b')
        decoder_path = snapshot_download('ZhipuAI/glm-4-voice-decoder')
        tokenizer_path = snapshot_download('ZhipuAI/glm-4-voice-tokenizer')
        flow_config = os.path.join(decoder_path, 'config.yaml')
        flow_checkpoint = os.path.join(decoder_path, 'flow.pt')
        hift_checkpoint = os.path.join(decoder_path, 'hift.pt')
        # GLM
        self.model = AutoModel.from_pretrained(model_path, trust_remote_code=True, device_map=self.device).eval()
        self.tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True, use_fast=False)
        # Flow & Hift
        self.audio_decoder = AudioDecoder(
            config_path=flow_config, flow_ckpt_path=flow_checkpoint, hift_ckpt_path=hift_checkpoint, device=self.device)
        # Speech tokenizer
        self.whisper_model = WhisperVQEncoder.from_pretrained(tokenizer_path).eval().to(self.device)
        self.feature_extractor = WhisperFeatureExtractor.from_pretrained(tokenizer_path)
    def clear(self):
        self.previous_tokens = ''
    def __init__(self, generation_config=None):
        if generation_config is None:
            generation_config = GenerationConfig(top_p=0.8, temperature=0.2, max_new_tokens=2000, do_sample=True)
        self.generation_config = generation_config
        self.device = 'cuda:0'
        self._prepare_model()
        self.audio_offset = self.tokenizer.convert_tokens_to_ids('<|audio_0|>')
        self.end_token_id = self.tokenizer.convert_tokens_to_ids('<|user|>')
        self.clear()
    def infer(self, audio_path: Optional[str] = None, text: Optional[str] = None) -> Tuple[str, str]:
        if audio_path is not None:
            audio_tokens = extract_speech_token(self.whisper_model, self.feature_extractor, [audio_path])[0]
            audio_tokens = ''.join([f'<|audio_{x}|>' for x in audio_tokens])
            audio_tokens = '<|begin_of_audio|>' + audio_tokens + '<|end_of_audio|>'
            user_input = audio_tokens
            system_prompt = 'User will provide you with a speech instruction. Do it step by step. First, think about the instruction and respond in a interleaved manner, with 13 text token followed by 26 audio tokens. '
        else:
            user_input = text
            system_prompt = 'User will provide you with a text instruction. Do it step by step. First, think about the instruction and respond in a interleaved manner, with 13 text token followed by 26 audio tokens.'
        text = self.previous_tokens
        text = text.strip()
        if '<|system|>' not in text:
            text += f'<|system|>\n{system_prompt}'
        text += f'<|user|>\n{user_input}<|assistant|>streaming_transcription\n'
        inputs = self.tokenizer([text], return_tensors='pt').to(self.device)
        generate_ids = self.model.generate(**inputs, generation_config=self.generation_config)[0]
        generate_ids = generate_ids[inputs['input_ids'].shape[1]:]
        self.previous_tokens += text + self.tokenizer.decode(generate_ids, spaces_between_special_tokens=False)
        return self._parse_generate_ids(generate_ids)
    def _parse_generate_ids(self, generate_ids: List[int]) -> Tuple[str, str]:
        text_tokens, audio_tokens = [], []
        this_uuid = str(uuid.uuid4())
        for token_id in generate_ids.tolist():
            if token_id >= self.audio_offset:
                audio_tokens.append(token_id - self.audio_offset)
            elif token_id != self.end_token_id:
                text_tokens.append(token_id)
        audio_tokens_pt = torch.tensor(audio_tokens, device=self.device)[None]
        tts_speech, _ = self.audio_decoder.token2wav(audio_tokens_pt, uuid=this_uuid, finalize=True)
        audio_path = f'{this_uuid}.wav'
        with open(audio_path, 'wb') as f:
            torchaudio.save(f, tts_speech.cpu(), 22050, format='wav')
        response = self.tokenizer.decode(text_tokens)
        return response, audio_path
if __name__ == '__main__':
    generation_config = GenerationConfig(top_p=0.8, temperature=0.2, max_new_tokens=2000, do_sample=True)
    glm_voice = GLM4Voice(generation_config=generation_config)
    audio_path = 'http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/weather.wav'
    response, output_path = glm_voice.infer(audio_path=audio_path)
    print(f'response: {response}\noutput_path: {output_path}')
    text = '请用英文回答'
    response, output_path = glm_voice.infer(text=text)
    print(f'response: {response}\noutput_path: {output_path}')
    glm_voice.clear()  # 清空历史
    text = '请用英文回答'
    response, output_path = glm_voice.infer(text=text)
    print(f'response: {response}\noutput_path: {output_path}')
"""
response: 是啊,阳光明媚的,真是个出门走走的好日子!你今天有什么计划吗?
output_path: 7f146cb5-4c1f-4c2c-85d0-0a8c985c90c0.wav
response: Sure! Today's weather is really nice, isn't it? It's a great day to go out and enjoy some fresh air. Do you have any plans for today?
output_path: 9326df35-aeec-4292-856b-5c0b1688e3f8.wav
response: Sure, I'll answer in English. What would you like to know?
output_path: e6e7c94b-7532-475f-bea7-e41566a954b6.wav
"""

显存占用:

image.png

模型Web Demo体验

智谱提供了可以直接启动的 Web Demo。用户可以输入语音或文本,模型会同时给出语音和文字回复。

首先下载仓库

git clone --recurse-submodules https://github.com/THUDM/GLM-4-Voice
cd GLM-4-Voice

然后安装依赖。

pip install -r requirements.txt

由于 Decoder 模型不支持通过 transformers 初始化,因此 checkpoint 需要单独下载,建议单独下载三个模型。

# 使用ModelScope CLI下载
modelscope download --model=ZhipuAI/glm-4-voice-9b --local_dir ./glm-4-voice-9b
# 建议其他模型也单独下载
modelscope download --model=ZhipuAI/glm-4-voice-decoder --local_dir ./glm-4-voice-decoder
modelscope download --model=ZhipuAI/glm-4-voice-tokenizer --local_dir ./glm-4-voice-tokenizer

发布WebDemo

首先启动模型服务,指定文件路径

python model_server.py --model-path glm-4-voice-9b

然后启动 web 服务

python web_demo.py --tokenizer-path glm-4-voice-tokenizer --model-path glm-4-voice-9b

即可在 http://127.0.0.1:8888 访问 web demo。可以手动下载之后通过 --tokenizer-path 和 --model-path  指定本地的路径。

image.png

显存占用:

image.png

点击链接👇,直达模型体验

https://modelscope.cn/studios/ZhipuAI/GLM-4-Voice-Demo/summary

相关文章
|
9月前
|
人工智能 API
重磅!阿里云Coding Plan全面上线Qwen3.5、GLM-5、MiniMax M2.5、Kimi K2.5
阿里云Coding Plan上线Qwen3.5、GLM-5、M2.5、K2.5四大顶尖开源模型,支持Qwen Code等工具自由切换。Lite/Pro套餐首月仅7.9元/39.9元,分别享1.8万/9万次请求。Qwen3.5以397B总参、17B激活参数实现高性价比,全面优化编程与Agent能力。
|
8月前
|
人工智能 弹性计算 自然语言处理
“养小龙虾”上阿里云!OpenClaw一键部署,两步拥有专属AI助理!
“养小龙虾”火了!但此“虾”非水产,而是开源AI智能体框架OpenClaw——为AI装上“手脚”,支持本地部署、多工具调用与自然语言驱动执行。阿里云提供一键部署方案,5分钟零代码拥有专属数字员工,办公、开发、生活全场景自动化。
1614 1
|
人工智能 弹性计算 编解码
技术分享 | 如何基于阿里云AIACC加速Stable-Diffusion AI绘画
AIACC是基于阿里云IaaS资源推出的神龙AI性能加速套件,用于优化基于AI主流计算框架搭建的模型,能显著提升训练和推理性能。AIACC神龙AI推理加速套件由AIACC-Torch(Pytorch推理加速引擎)、AIACC-MLIR(MLIR推理加速引擎)、AIACC-HRT(AIACC算子深度加速引擎)等组件构成。AIACC神龙AI性能加速套件能够为Stable-Diffusion提供加速优化支持,在512x512分辨率下,可将单张图片的延迟从2秒降低至0.88秒,吞吐提速至原来的2.2倍。
技术分享 | 如何基于阿里云AIACC加速Stable-Diffusion AI绘画
|
10月前
|
安全 API 开发者
手把手带你使用无影 AgentBay + AgentScope 完成一站式智能体开发部署
阿里云无影 AgentBay 作为一个面向 AI 智能体开发的云端 GUI 沙箱服务,已集成至阿里巴巴通义实验室开源的 AgentScope 框架,助力开发者快速构建安全、高效的智能体应用。
2841 1
|
7月前
|
人工智能 自然语言处理 安全
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
本文介绍了Claude Code终端AI助手的使用指南,主要内容包括:1)常用命令如版本查看、项目启动和更新;2)三种工作模式切换及界面说明;3)核心功能指令速查表,包含初始化、压缩对话、清除历史等操作;4)详细解析了/init、/help、/clear、/compact、/memory等关键命令的使用场景和语法。文章通过丰富的界面截图和场景示例,帮助开发者快速掌握如何通过命令行和交互界面高效使用Claude Code进行项目开发,特别强调了CLAUDE.md文件作为项目知识库的核心作用。
52693 73
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
|
IDE 开发工具 开发者
仓颉开发语言入门教程:搭建开发环境
仓颉开发语言是华为为鸿蒙系统自主研发的编程语言,肩负重要使命。本文介绍如何从零搭建仓颉开发环境,重点演示在DevEco Studio中安装插件、配置项目及运行Hello World示例,助力开发者快速上手。
|
11月前
|
存储 安全 数据管理
没听过冷数据?一文带你读懂冷数据
冷数据指长期不用但需合规保存的历史数据,如旧订单、合同等。它虽不常用,却关乎成本、安全与合规。管理不当将导致存储浪费、系统变慢、审计风险。应通过分类、分级存储、自动归档与索引管理,确保“用时能查”,实现数据治理的精细化与可持续化。
|
数据可视化 搜索推荐 数据处理
Dify Agent + AntV 实战:从 0 到 1 打造数据可视化解决方案
本文介绍如何结合Dify Agent与AntV,打造高效、灵活的数据可视化解决方案。通过低代码构建数据流程,调用AntV丰富图表工具,实现从数据到多样可视化图表的快速生成,助力开发者兼顾效率与个性化需求。
1981 0
Dify Agent + AntV 实战:从 0 到 1 打造数据可视化解决方案
|
数据采集 自然语言处理 Java
Playwright 多语言一体化——Python/Java/.NET 全栈采集实战
本文以反面教材形式,剖析了在使用 Playwright 爬取懂车帝车友圈问答数据时常见的配置错误(如未设置代理、Cookie 和 User-Agent),并提供了 Python、Java 和 .NET 三种语言的修复代码示例。通过错误示例 → 问题剖析 → 修复过程 → 总结教训的完整流程,帮助读者掌握如何正确配置爬虫代理及其它必要参数,避免 IP 封禁和反爬检测,实现高效数据采集与分析。
1081 3
Playwright 多语言一体化——Python/Java/.NET 全栈采集实战

热门文章

最新文章