当前位置: 首页 > news >正文

阿里声音项目Qwen2-Audio的部署安装,在服务器Ubuntu22.04系统——点动科技

阿里声音项目Qwen2-Audio的部署安装,在服务器Ubuntu22.04系统——点动科技

  • 一、ubuntu22.04基本环境配置
      • 1.1 更换清华Ubuntu镜像源
      • 1.2 更新包列表:
      • 2. 安装英伟达显卡驱动
          • 2.1 使用wget在命令行下载驱动包
          • 2.2 更新软件列表和安装必要软件、依赖
          • 2.2 卸载原有驱动
          • 2.3 安装驱动
          • 2.4 安装CUDA
          • 2.5 环境变量配置
  • 二、安装miniconda环境
      • 1. 下载miniconda3
      • 2. 安装miniconda3
      • 3. 切换到bin文件夹
      • 4. 输入pwd获取路径
      • 5. 打开用户环境编辑页面
      • 6. 重新加载用户环境变量
      • 7. 初始化conda
      • 8.验证是否安装成功
      • 9.conda配置
  • 三、安装Qwen2-Audio
      • 1.克隆仓库
          • 1.1 github克隆
          • 1.2 国内github镜像克隆
          • 1.3. 进入目录
      • 2.创建虚拟环境
          • 2.1 进入虚拟环境
      • 3. 安装依赖
          • 3.1设置清华源、更新pip
          • 3.2安装torch 12.4cuda版本
          • 3.3安装依赖文件
          • 3.4安装webui界面及其他未安装依赖
          • 3.5安装魔搭库准备下载模型文件:
          • 3.6下载相关模型
          • 3.7执行代码,启动webui界面
          • 3.8加入声音驱动,实现真正的语音聊天
  • 四、成功实现语音交互
        • 4.1 找不到录音机问题

一、ubuntu22.04基本环境配置

1.1 更换清华Ubuntu镜像源

  • 删除原来的文件
rm /etc/apt/sources.list
  • 开始编辑新文件
vim /etc/apt/sources.list
  • 先按i键,粘贴以下内容
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

请添加图片描述

  • 确保内容跟上述图片一致

  • 按esc键,再输入冒号+wq保存

在这里插入图片描述

1.2 更新包列表:

  • 打开终端,输入以下命令:
sudo apt-get update
sudo apt upgrade
  • 更新时间较长,请耐心等待

在这里插入图片描述

2. 安装英伟达显卡驱动

2.1 使用wget在命令行下载驱动包
wget https://cn.download.nvidia.com/XFree86/Linux-x86_64/550.100/NVIDIA-Linux-x86_64-550.100.run

在这里插入图片描述

2.2 更新软件列表和安装必要软件、依赖
sudo apt-get install g++

在这里插入图片描述

在这里插入图片描述
点击回车enter即可
在这里插入图片描述

sudo apt-get install gcc

在这里插入图片描述

sudo apt-get install make

在这里插入图片描述
在这里插入图片描述
点击回车enter即可

成功安装

在这里插入图片描述

2.2 卸载原有驱动
sudo apt-get remove --purge nvidia* 

在这里插入图片描述

  • 1.使用vim修改配置文件
sudo vim /etc/modprobe.d/blacklist.conf
  • 2.按i键进入编辑模式,在文件尾增加两行:
blacklist nouveau
options nouveau modeset=0

在这里插入图片描述

  • 3.按esc键退出编辑模式,输入:wq保存并退出

  • 4.更新文件

sudo update-initramfs -u

这里等待时间较久

在这里插入图片描述

  • 5.重启电脑:
sudo reboot

这里需要等一会才能连上

2.3 安装驱动
  • 1.授予执行权限
sudo chmod 777 NVIDIA-Linux-x86_64-550.100.run
  • 2.执行安装命令
sudo ./NVIDIA-Linux-x86_64-550.100.run

在这里插入图片描述

在这里插入图片描述

这里一直按回车就行,默认选择

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

一直按回车enter键,直到安装成功

在这里插入图片描述

在这里插入图片描述

  • 3.检测显卡驱动是否安装成功
nvidia-smi

在这里插入图片描述

2.4 安装CUDA
wget https://developer.download.nvidia.com/compute/cuda/12.4.0/local_installers/cuda_12.4.0_550.54.14_linux.run

在这里插入图片描述

执行安装命令

sudo sh ./cuda_12.4.0_550.54.14_linux.run
  • 1.输出accept开始安装

在这里插入图片描述

  • 2.然后注意这里要按enter取消勾选第一个选项,因为之前已经安装了驱动

在这里插入图片描述

  • 3.接着选择Install开始安装

在这里插入图片描述

  • 4.安装完成

在这里插入图片描述

2.5 环境变量配置
  • 1.以vim方式打开配置文件
sudo vim ~/.bashrc
  • 2.按i键进入编辑模式,在文件尾增加下面内容:
export PATH="/usr/local/cuda-12.4/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-12.4/lib64:$LD_LIBRARY_PATH"

在这里插入图片描述

  • 按esc键退出编辑模式,输入:wq保存并退出

  • 3.更新环境变量

source ~/.bashrc
  • 4.检测CUDA是否安装成功
nvcc -V

在这里插入图片描述

二、安装miniconda环境

1. 下载miniconda3

wget https://mirrors.cqupt.edu.cn/anaconda/miniconda/Miniconda3-py310_23.10.0-1-Linux-x86_64.sh

在这里插入图片描述

2. 安装miniconda3

bash Miniconda3-py310_23.10.0-1-Linux-x86_64.sh -u

在这里插入图片描述

直接一直enter键,到输入路径和yes

这边建议路径为:miniconda3

在这里插入图片描述

直接回车enter即可,再次输入yes

在这里插入图片描述

成功安装

在这里插入图片描述

3. 切换到bin文件夹

cd miniconda3/bin/

4. 输入pwd获取路径

pwd

复制这里的路径

5. 打开用户环境编辑页面

vim ~/.bashrc 
  • 点击键盘I键进入编辑模式,在最下方输入以下代码
export PATH="/root/miniconda3/bin:$PATH"

在这里插入图片描述

按esc键退出编辑模式,输入:wq保存并退出

6. 重新加载用户环境变量

source ~/.bashrc

在这里插入图片描述

7. 初始化conda

conda init bash

在这里插入图片描述

8.验证是否安装成功

conda -V

在这里插入图片描述

9.conda配置

  • 1.配置清华镜像源

    代码如下:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

在这里插入图片描述

  • 2.设置搜索时显示通道地址
conda config --set show_channel_urls yes

在这里插入图片描述

  • 3.配置pip 镜像源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

在这里插入图片描述

三、安装Qwen2-Audio

1.克隆仓库

1.1 github克隆
git clone https://github.com/QwenLM/Qwen2-Audio.git
1.2 国内github镜像克隆
git clone https://mirror.ghproxy.com/https://github.com/QwenLM/Qwen2-Audio.git
1.3. 进入目录
cd Qwen2-Audio/

2.创建虚拟环境

conda create -n qwen2 python=3.10

在这里插入图片描述

  • 输入y回车即可
2.1 进入虚拟环境
conda activate qwen2

在这里插入图片描述

3. 安装依赖

3.1设置清华源、更新pip
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simplepython -m pip install --upgrade pip
3.2安装torch 12.4cuda版本
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124

在这里插入图片描述

3.3安装依赖文件
cd demo
pip install -r requirements_web_demo.txt
3.4安装webui界面及其他未安装依赖
pip install librosa
pip install --upgrade "accelerate>=0.21.0"
pip install django
pip install git+https://mirror.ghproxy.com/https://github.com/huggingface/transformers
3.5安装魔搭库准备下载模型文件:
pip install modelscope
3.6下载相关模型
cd ..
modelscope download --model qwen/qwen2-audio-7b-instruct --local_dir './Qwen/Qwen2-Audio-7B-Instruct'
3.7执行代码,启动webui界面
  • 改端口
vim demo/web_demo_audio.py

按i进行编辑,完成后再按esc,冒号,wq退出

在这里插入图片描述

  • 成功进入

在这里插入图片描述

3.8加入声音驱动,实现真正的语音聊天
cd demo
  • 创建一个新的webui界面的文件
touch test_audio.py
vim test_audio.py

输入以下内容

import gradio as gr
import modelscope_studio as mgr
import librosa
from transformers import AutoProcessor, Qwen2AudioForConditionalGeneration
from argparse import ArgumentParser
import requests
import os
from django.http import HttpResponse# 默认的模型检查点路径
DEFAULT_CKPT_PATH = 'Qwen/Qwen2-Audio-7B-Instruct'def text_to_speech(text2):data = {"text": text2,"text_language": "zh",}# 注意 URL 中的单引号应该是 URL 的一部分,需要正确转义response = requests.post('http://服务器IP:端口', json=data)if response.status_code == 200:audio_file_path = "/root/project/Qwen2-Audio/demo/output.mp3"with open(audio_file_path, "wb") as f:f.write(response.content)return audio_file_pathelse:print(f"错误:请求失败,状态码为 {response.status_code}")return Nonedef _get_args():"""解析命令行参数,获取运行配置。返回:argparse.Namespace: 包含命令行参数的命名空间对象。"""parser = ArgumentParser()parser.add_argument("-c", "--checkpoint-path", type=str, default=DEFAULT_CKPT_PATH,help="Checkpoint name or path, default to %(default)r")  # 模型检查点路径parser.add_argument("--cpu-only", action="store_true", help="Run demo with CPU only")  # 是否仅使用CPUparser.add_argument("--inbrowser", action="store_true", default=False,help="Automatically launch the interface in a new tab on the default browser.")  # 是否在浏览器中自动打开界面parser.add_argument("--server-port", type=int, default=15110,help="Demo server port.")  # 指定服务器端口parser.add_argument("--server-name", type=str, default="0.0.0.0",help="Demo server name.")  # 指定服务器名称args = parser.parse_args()return argsdef add_text(chatbot, task_history, input):"""将用户输入的文本内容添加到聊天记录中,并更新聊天机器人界面。参数:chatbot (gr.components.Chatbot): 聊天机器人组件。task_history (list): 任务历史记录。input (gr.inputs): 用户输入内容。返回:tuple: 更新后的聊天机器人界面和任务历史记录,以及重置后的用户输入框。"""text_content = input.text  # 获取文本输入内容content = []if len(input.files) > 0:  # 如果用户上传了音频文件for i in input.files:content.append({'type': 'audio', 'audio_url': i.path})  # 将音频文件添加到内容列表中if text_content:  # 如果用户输入了文本content.append({'type': 'text', 'text': text_content})  # 将文本内容添加到内容列表中task_history.append({"role": "user", "content": content})  # 更新任务历史记录# 更新聊天机器人界面,添加用户输入chatbot.append([{"text": input.text,"files": input.files,}, None])return chatbot, task_history, None
'''
def add_file(chatbot, task_history, audio_file_path):"""将音频文件添加到聊天记录中。参数:chatbot (gr.components.Chatbot): 聊天机器人组件。task_history (list): 任务历史记录。audio_file_path (str): 音频文件的路径。返回:tuple: 更新后的聊天机器人界面和任务历史记录。"""# 确保任务历史记录中的音频条目是正确的格式task_history.append({"role": "user", "content": [{"type": "audio", "audio_url": audio_file_path}]})# 更新聊天记录,直接使用 audio_file_path 而不是 gr.Audio 组件chatbot.append((None, {"type": "audio", "audio_url": audio_file_path}))return chatbot, task_history
'''
import osdef add_file(chatbot, task_history, audio_path):if not os.path.isfile(audio_path):print(f"Error: The file {audio_path} does not exist.")return chatbot, task_history# 将音频文件信息添加到任务历史task_history.append({"role": "user","content": [{"type": "audio", "audio_url": audio_path}]})# 假设 chatbot 组件可以接受字典格式的输入chatbot_state = [{"text": f"[Audio file: {os.path.basename(audio_path)}]","files": [audio_path]  # 直接使用文件路径而不是 gr.File}, None]chatbot.append(chatbot_state)  # 更新 chatbot 状态return chatbot, task_historydef reset_user_input():"""重置用户输入字段。返回:gr.update: 将文本框的值重置为空。"""return gr.Textbox.update(value='')def reset_state(task_history):"""重置聊天记录和任务历史。参数:task_history (list): 当前的任务历史记录。返回:tuple: 清空的聊天记录和任务历史。"""return [], []def regenerate(chatbot, task_history):"""重新生成最后的机器人响应。参数:chatbot (gr.components.Chatbot): 聊天机器人组件。task_history (list): 任务历史记录。返回:tuple: 更新后的聊天机器人界面和任务历史记录。"""# 如果最后一条消息是助手生成的,则移除它if task_history and task_history[-1]['role'] == 'assistant':task_history.pop()chatbot.pop()# 如果任务历史记录不为空,重新生成响应if task_history:chatbot, task_history = predict(chatbot, task_history)return chatbot, task_historydef predict(chatbot, task_history):"""根据当前任务历史记录生成模型响应,并将响应转换为音频文件添加到聊天记录中。参数:chatbot (gr.components.Chatbot): 聊天机器人组件。task_history (list): 任务历史记录。返回:tuple: 更新后的聊天机器人界面和任务历史记录。"""print(f"{task_history=}")print(f"{chatbot=}")# 使用处理器将任务历史记录格式化为模型输入text = processor.apply_chat_template(task_history, add_generation_prompt=True, tokenize=False)audios = []# 遍历任务历史,查找音频内容并加载for message in task_history:if isinstance(message["content"], list):for ele in message["content"]:if ele["type"] == "audio":audios.append(librosa.load(ele['audio_url'], sr=processor.feature_extractor.sampling_rate)[0])if len(audios) == 0:  # 如果没有音频,则设置为 Noneaudios = Noneprint(f"{text=}")print(f"{audios=}")# 使用处理器生成模型输入inputs = processor(text=text, audios=audios, return_tensors="pt", padding=True)if not _get_args().cpu_only:  # 如果支持 GPU,则将输入数据移动到 CUDA 设备inputs["input_ids"] = inputs.input_ids.to("cuda")# 生成响应generate_ids = model.generate(**inputs, max_length=256)generate_ids = generate_ids[:, inputs.input_ids.size(1):]# 解码生成的文本响应# 假设其他参数已经正确设置response = processor.batch_decode(generate_ids, skip_special_tokens=True)[0]task_history.append({'role': 'assistant', 'content': response})chatbot.append((None, response))  # 添加文本响应# 将文本响应转换为语音audio_file_path = text_to_speech(response)if audio_file_path:chatbot, task_history = add_file(chatbot, task_history, audio_file_path)return chatbot, task_historydef _launch_demo(args):"""启动Gradio的Web用户界面,展示Qwen2-Audio-Instruct模型的聊天功能。参数:args (argparse.Namespace): 从命令行解析的参数。"""with gr.Blocks() as demo:# 添加页面标题和描述gr.Markdown("""<p align="center"><img src="https://qianwen-res.oss-cn-beijing.aliyuncs.com/assets/blog/qwenaudio/qwen2audio_logo.png" style="height: 80px"/><p>""")gr.Markdown("""<center><font size=8>Qwen2-Audio-Instruct Bot</center>""")gr.Markdown("""\<center><font size=3>This WebUI is based on Qwen2-Audio-Instruct, developed by Alibaba Cloud. \(本WebUI基于Qwen2-Audio-Instruct打造,实现聊天机器人功能。)</center>""")gr.Markdown("""\<center><font size=4>Qwen2-Audio <a href="https://modelscope.cn/models/qwen/Qwen2-Audio-7B">🤖 </a> | <a href="https://huggingface.co/Qwen/Qwen2-Audio-7B">🤗</a>&nbsp | Qwen2-Audio-Instruct <a href="https://modelscope.cn/models/qwen/Qwen2-Audio-7B-Instruct">🤖 </a> | <a href="https://huggingface.co/Qwen/Qwen2-Audio-7B-Instruct">🤗</a>&nbsp | &nbsp<a href="https://github.com/QwenLM/Qwen2-Audio">Github</a></center>""")# 创建聊天机器人组件chatbot = mgr.Chatbot(label='Qwen2-Audio-7B-Instruct', elem_classes="control-height", height=750)# 创建用户输入组件,支持文本、麦克风和文件上传user_input = mgr.MultimodalInput(interactive=True,sources=['microphone', 'upload'],submit_button_props=dict(value="🚀 Submit (发送)"),upload_button_props=dict(value="📁 Upload (上传文件)", show_progress=True),)task_history = gr.State([])  # 初始化任务历史状态with gr.Row():  # 创建清除历史和重试按钮empty_bin = gr.Button("🧹 Clear History (清除历史)")regen_btn = gr.Button("🤔️ Regenerate (重试)")# 当用户提交输入时,调用add_text函数,然后调用predict函数生成响应user_input.submit(fn=add_text,inputs=[chatbot, task_history, user_input],outputs=[chatbot, task_history, user_input]).then(predict, [chatbot, task_history], [chatbot, task_history], show_progress=True)# 清除历史按钮的点击事件处理,重置聊天记录和任务历史empty_bin.click(reset_state, outputs=[chatbot, task_history], show_progress=True)# 重试按钮的点击事件处理,重新生成最后的响应regen_btn.click(regenerate, [chatbot, task_history], [chatbot, task_history], show_progress=True)# 启动Gradio界面demo.queue().launch(share=False,  # 不共享URLinbrowser=args.inbrowser,  # 是否自动在浏览器中打开server_port=args.server_port,  # 指定服务器端口server_name=args.server_name,  # 指定服务器名称ssl_certfile="/root/project/cert.pem", ssl_keyfile="/root/project/key.pem", ssl_verify=False)if __name__ == "__main__":args = _get_args()  # 获取命令行参数if args.cpu_only:device_map = "cpu"  # 如果指定了仅使用CPU,设置设备映射为CPUelse:device_map = "auto"  # 否则自动选择设备# 加载模型model = Qwen2AudioForConditionalGeneration.from_pretrained(args.checkpoint_path,torch_dtype="auto",  # 自动选择数据类型device_map=device_map,  # 设置设备映射resume_download=True,  # 断点续传).eval()model.generation_config.max_new_tokens = 2048  # 设置最大生成token数,用于长对话print("generation_config", model.generation_config)processor = AutoProcessor.from_pretrained(args.checkpoint_path, resume_download=True)  # 加载处理器_launch_demo(args)  # 启动演示界面
  • 运行上面代码之前先到4.1配置ssl

四、成功实现语音交互

在这里插入图片描述

4.1 找不到录音机问题
  • 创建ssl
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -sha256 -days 365 -nodes

一直按enter键默认选择即可

  • 修改launch
vim demo/web_demo_audio.py

加入ssl参数,注意替换路径

demo.queue().launch(share=False,  # 不共享URLinbrowser=args.inbrowser,  # 是否自动在浏览器中打开server_port=args.server_port,  # 指定服务器端口server_name=args.server_name,  # 指定服务器名称ssl_certfile="/root/project/cert.pem", ssl_keyfile="/root/project/key.pem", ssl_verify=False)

在这里插入图片描述

  • 注意用https访问
https://服务器ip:端口

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 依赖倒置原则(Dependency Inversion Principle, DIP)
  • SQL - 创建 表和数据库
  • k8s综合项目
  • 【秋招笔试】8.18大疆秋招(第二套)-开发岗-三语言题解
  • Spring模块详解Ⅱ
  • 华为HCIP证书好考吗?详解HCIP证书考试难易程度及备考策略!
  • [HDCTF 2023]Welcome To HDCTF 2023
  • C++11:包装器
  • Java 多线程练习2 (抽奖比较Runnable写法)
  • 【Qt】 编辑框 | 按钮 的 初步了解
  • potplayer播放m2ts格式,截图
  • [python][代码]定义了一个用于AES加密和解密的工具类
  • 快速批量替换图片名称为指定名称(附代码)
  • 我是如何克服编程学习中的挫折感的
  • uniapp在线下载安装包更新app
  • JavaScript新鲜事·第5期
  • mac修复ab及siege安装
  • php面试题 汇集2
  • Python3爬取英雄联盟英雄皮肤大图
  • vue 配置sass、scss全局变量
  • 从tcpdump抓包看TCP/IP协议
  • 目录与文件属性:编写ls
  • ​MySQL主从复制一致性检测
  • ​zookeeper集群配置与启动
  • ​数据结构之初始二叉树(3)
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • (2)(2.4) TerraRanger Tower/Tower EVO(360度)
  • (2015)JS ES6 必知的十个 特性
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (翻译)Quartz官方教程——第一课:Quartz入门
  • (附源码)springboot高校宿舍交电费系统 毕业设计031552
  • (附源码)springboot家庭财务分析系统 毕业设计641323
  • (附源码)小程序 交通违法举报系统 毕业设计 242045
  • (免费领源码)Java#Springboot#mysql农产品销售管理系统47627-计算机毕业设计项目选题推荐
  • (转贴)用VML开发工作流设计器 UCML.NET工作流管理系统
  • (转载)深入super,看Python如何解决钻石继承难题
  • .env.development、.env.production、.env.staging
  • .htaccess 强制https 单独排除某个目录
  • .NET CORE Aws S3 使用
  • .NET core 自定义过滤器 Filter 实现webapi RestFul 统一接口数据返回格式
  • .NET 实现 NTFS 文件系统的硬链接 mklink /J(Junction)
  • .NET开源纪元:穿越封闭的迷雾,拥抱开放的星辰
  • .NET连接MongoDB数据库实例教程
  • .net专家(高海东的专栏)
  • @ConditionalOnProperty注解使用说明
  • @Query中countQuery的介绍
  • [ IOS ] iOS-控制器View的创建和生命周期
  • [ 隧道技术 ] 反弹shell的集中常见方式(四)python反弹shell
  • [1181]linux两台服务器之间传输文件和文件夹
  • [2]十道算法题【Java实现】
  • [20180224]expdp query 写法问题.txt
  • [4]CUDA中的向量计算与并行通信模式
  • [ACTF2020 新生赛]Include
  • [Bada开发]初步入口函数介绍
  • [C++基础]-入门知识