Try   HackMD

《用創客玩 ChatGPT x Python AI 語音大應用》服務專區

雖然沒有官方正式宣告, 但是 OpenAI 似乎已經取消新帳戶 5 美元免費額度的政策, 要使用 OpenAI API 都必須付費。

範例程式下載點

按此下載範例程式

補充說明:
範例程式中每章的 Chat_Module.py 檔皆不太一樣, 直至最終章 CH11 的 Chat_Module.py 才是完全版

下載 ffmpeg 或 ffprobe 教學資源

按此前往

注意事項

flask 請安裝 3.0.0 版

在套件搜尋畫面中請按下方的 按鈕後選取 3.0.0 版再安裝:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

安裝其他的程式庫

本書 ch04 及 ch08 需要使用 pydub 以及 requests 程式庫, 請自行透過管理套件工具安裝。

各項問題除錯

ESP32 端遇到 [Errno 113] 錯誤

當 ESP32 執行程式時遇到如下的狀況, 初步判定為與伺服器斷線:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

以下是可能的問題及解決方法:

1 伺服器端並未啟動

解決方式:請在伺服器端中重新執行伺服器程式, 並記錄伺服器網址 (紅框處, 每台電腦的伺服器網址皆不相同)

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →


2 伺服器端自動斷線

如果有一台以上的電腦同時在同個 Wi-Fi 下啟用伺服器端, 並且建立在同個端口時就會發生此情況。

同個端口:伺服器程式中會有以下這一行的程式, 5000 就是本機的通訊埠號

app.run(host='0.0.0.0', port=5000)

解決方式:請嘗試重啟伺服器, 如果依舊錯誤, 請更改通訊埠號為 80 或 443

app.run(host='0.0.0.0', port=80)

更改後如果需要使用 ngrok 那麼建立通道時也需要連接到新的通訊埠號。


Lab03 一直錄到雜訊聲音

若持續錄到雜音, 沒辦法聽清楚人聲時, 可以嘗試以下兩個步驟:

  1. 確認麥克風接線是正常的, 且麥克風無異常發熱。
  2. 嘗試更換杜邦線, 有時可能是線材問題。

如果上述都確認無誤,但就只會錄到雜音,可以嘗試變更接線:

麥克風腳位 ESP32 腳位
SCK 14
SD 32
WS 25

並請記得修改程式碼中的腳位設定:

...
mic_sample_rate = 4000
chunk_size = mic_sample_rate * 2

audio_in = I2S(0,
#                sck=Pin(25),
#                ws=Pin(27),
#                sd=Pin(26),
               sck=Pin(14),
               ws=Pin(25),
               sd=Pin(32),
               mode=I2S.RX,
               bits=16,
               format=I2S.MONO,
               rate=mic_sample_rate,
               ibuf=chunk_size
               )
...

兩種接法的測試影片請參考:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

最後若仍然無法解決, 請聯繫創客粉專。


伺服器端出現 couldn't find ffmpeg or

當伺服器端出現以下狀況時, 代表執行此伺服器程式的相同路徑下並沒有 ffmpeg 執行檔

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

下圖是未放入 ffmpeg 檔

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

這時候只需要將 ffmpeg 執行檔複製到此路徑下即可解決問題

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

ps. ffprobe 檔案也請跟著放入喔


macOS 下載網址出現錯誤畫面

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

因為 ffmpeg 官網將下載網址改掉了, 請直接至以下教學下載 ffmpeg 與 ffprobe

下載教學


ESP32 顯示 OSError 28

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

會出現 error 28 是因為 ESP32 空間不足, 這時可能是 ESP32 存了其他的 WAV 檔, 請刪除 WAV 檔之後就可以正常使用了。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →


一直出現「無法辨識語音」或「Audio file is too short.」只錄製到雜音

遇到這情況會比較複雜, 有以下三種可能:

  1. 錄音設備無法正常錄音

  2. 沒有修改 SERVER 端的 ENV 檔中的 OpenAI API KEY,會看到如下的訊息:

    ​​​​'ascii' codec can't encode characters in position 7-8: ordinal not in range(128)
    ​​​​無法識別語音
    ​​​​Error_info: Audio file is too short.
    

    如果是你的 OpenAI API KEY 錯誤,會看到如下的錯誤訊息:

    ​​​​Error code: 401 - {'error': {'message': 'Incorrect API key provided: sk-proj-******************************************************************************************************************************************************a8UH. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}
    ​​​​無法識別語音
    ​​​​Error_info: Audio file is too short.
    
  3. 你的 OpenAI 已經沒有額度了, 需要去儲值才能使用 API,這會看到以下的錯誤訊息:

    ​​​​Error code: 429 - {'error': {'message': 'You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.', 'type': 'insufficient_quota', 'param': None, 'code': 'insufficient_quota'}}
    ​​​​無法識別語音
    ​​​​Error_info: Audio file is too short.
    

我們針對以上三種問題作排除與解決:

  1. 錄音設備無法正常錄音
    首先確認麥克風的接線是否都有按照手冊線路接好, 可以直接執行 LAB03 的程式測試錄音狀況是否正常。(正常是指可以聽清楚說話的聲音, 錄製時可以大聲一點會較清楚)
    若不正常則可能是麥克風損壞或杜邦線有問題。
  2. 沒修改 OpenAI API KEY
    直接到 SERVER 端的 ENV 檔中直接修改即可。
  3. OpenAI api 額度已用完
    若以上兩種都確定沒問題時, 就會是 api 額度用完了, 請參考第五章的步驟完成儲值, 就可以繼續使用。

ESP32 端出現記憶體不足

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

如果在錄製聲音時發現 ESP32 報以上錯誤, 就是 ESP32 本身的記憶體不足了。
成因: 錄製的時常超過限制
在錄製過程中會將音訊資料站存在 buffer 中, 而這是很吃記憶體的過程, 所以錄製時請確認是否超過限制的時間。

出現 Unsupported protocol: http:

S__16089091

出現以上錯誤就是 url 的地方打錯了, 造成他無法支援此網址。
請看一下 ESP32 端的 url。

S__16089092

可以發現上面的 http 左邊有一個空格 「 http」, 將空格刪除後就不會有問題了。

使用 pytubefix 替換 pytube

由於原本書上使用的 pytube 沒有持續更新,請改用 pytubefix。

Lab23 錄完音後出現 Error 202

如果你看到像是以下的畫面:

image

檢查一下第 8 行 url 設定的最後在英文雙引號前是不是有多了空格或其他字元,導致網址錯誤無法上傳錄音檔案?如果是的話,請移除多於字元再重新測試,應該就可以正確運作了。