環(huán)境
python:3+
ffmpeg:用于處理視頻和語音
gradio:UI界面和讀取語音
概述
我們的目的是做一個語音智能助手
下面我們開始
準備工作
下載Visual Studio Code
Visual Studio Code
因為需要寫python代碼,用Visual Studio Code
比較方便。
安裝python
python官網(wǎng)
執(zhí)行下載好的exe文件,可以建議選擇自定義安裝,這樣可以修改安裝路徑,只要別安裝到C盤就行。
記得勾選添加到環(huán)境變量
添加環(huán)境變量
假設(shè)在安裝時候,忘記了勾選添加到環(huán)境變量,那么我們可以自己配置
修改subprocess.py文件
安裝ffmpeg
https://ffmpeg.org/
將下載好的安裝包,進行解壓,然后將解壓后的文件夾放到平時自己喜歡的安裝目錄里。
比如:我的是D盤:D:\Program Files\ffmpeg
。
WINDOWS系統(tǒng)文字轉(zhuǎn)語音WSAY
https://github.com/p-groarke/wsay/releases/tag/v1.5.0
點擊下圖進行下載:
使用GRADIO建立用戶界面
先創(chuàng)建一個項目文件夾,比如我的:E:\openai\project\Chatbot
然后打開我們之前下載好的vsCode,并打開相關(guān)目錄。
參照gradio官網(wǎng)寫測試代碼
https://gradio.app/quickstart/
測試代碼1:頁面
import gradio as gr
def greet(name):
return "Hello " + name + "!"
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch()
執(zhí)行代碼:
PS E:\openai\project\Chatbot> python ui.py
可以看到控制臺會打印如下信息:
Running on local URL: http://127.0.0.1:7860
然后瀏覽器打開網(wǎng)址http://127.0.0.1:7860
:
但是我們是需要做成語音的,所以我們需要調(diào)整下;
在官網(wǎng)找到:
https://gradio.app/docs/#audio
測試代碼2:
import gradio as gr
def transcribe(audio):
print(audio)
return "這里顯示音頻"
demo = gr.Interface(
fn=transcribe,
inputs=gr.Audio(source="microphone"),
outputs="text")
demo.launch()
執(zhí)行命令:py uimp.py
瀏覽器刷新地址如下頁面:
接入openAI:
測試代碼3:
執(zhí)行命令:py uifinish.py
# 最終稿:
import gradio as gr
import openai, subprocess
from pathlib import Path
# 換成你自己的api_key
openai.api_key = "XXXXXXXXXXXXXXXXXXXXXX"
messages = [{"role": "system", "content": '你是一名知識淵博,樂于助人的智能聊天機器人.你的任務是陪我聊天,請用簡短的對話方式,用中文講一段話,每次回答不超過50個字!'}]
def transcribe(audio):
global messages
myfile=Path(audio)
myfile=myfile.rename(myfile.with_suffix('.wav'))
audio_file = open(myfile,"rb")
transcript = openai.Audio.transcribe("whisper-1", audio_file)
messages.append({"role": "user", "content": transcript["text"]})
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
system_message = response["choices"][0]["message"]
# print(response)
messages.append(system_message)
subprocess.call(["wsay", system_message['content']])
chat_transcript = ""
for message in messages:
if message['role'] != 'system':
chat_transcript += message['role'] + ": " + message['content'] + "\n\n"
return chat_transcript
ui = gr.Interface(fn=transcribe, inputs=gr.Audio(source="microphone", type="filepath"), outputs="text")
ui.launch()
效果如下:
總結(jié)
- ffmpeg 來處理語音
- 利用WSAY,將文字轉(zhuǎn)成語音
- 利用gradio,來生成UI頁面,并提供語音輸入
參考地址:
https://updayday.notion.site/Chat-GPT-WHISPER-API-GPT-3-5-TURBO-2af2630c857a4f0da92abcc763b4fd48
Whisper API cannot read files correctly
Renaming file extension using pathlib (python 3)文章來源:http://www.zghlxwxcb.cn/news/detail-414778.html
Path not found in Python文章來源地址http://www.zghlxwxcb.cn/news/detail-414778.html
到了這里,關(guān)于ChatGPT搭建語音智能助手的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!