2024年3月18日 星期一

Python: googletrans 與 bilingual_book_maker

2024年3月18日 星期一

2023年3月應朋友的要求,將開源的專案「bilingual_book_maker」可以在 Colab 中使用(參見:雄:OpenAI : ChatGPT 應用 : 製作雙語電子書),利用它可以把 EPUB 格式的電子書變成「雙語」的內容。前一陣子陸續有人反應有問題,不過,因為我根本沒在使用,加上有時問題會在一陣子後又消息了,所以沒有積極地去查原因。前天換有在使用的朋友跟我說了,哈!開始認真地找解方。

底下記錄一下排除問題的關鍵。

httpx 版本的問題

googletrans 已經滿久沒更新了,而 openai 持續地在更新,但是兩者都有用到 httpx,一個要舊版的,一個要新版的,因此安裝完 googletrans 以後,出現了「dependency conflicts」的錯誤訊息:

  Building wheel for googletrans (setup.py) ... done
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
openai 1.14.1 requires httpx<1,>=0.23.0, but you have httpx 0.13.3 which is incompatible.

httpx 版本的衝突,由訊息看來,因為 googletrans 是後安裝的,所以最後 httpx 用的版本是 0.13.3 ,而不是 openai 要的版本( >=0.23.0 )。這也導致開始翻譯時,出現了這些訊息:

Traceback (most recent call last):
  File "/usr/local/bin/bbook_maker", line 5, in <module>
    from book_maker.cli import main
  File "/usr/local/lib/python3.10/dist-packages/book_maker/cli.py", line 7, in <module>
    from book_maker.translator import MODEL_DICT
  File "/usr/local/lib/python3.10/dist-packages/book_maker/translator/__init__.py", line 2, in <module>
    from book_maker.translator.chatgptapi_translator import ChatGPTAPI
  File "/usr/local/lib/python3.10/dist-packages/book_maker/translator/chatgptapi_translator.py", line 7, in <module>
    from openai import AzureOpenAI, OpenAI, RateLimitError
  File "/usr/local/lib/python3.10/dist-packages/openai/__init__.py", line 8, in <module>
    from . import types
  File "/usr/local/lib/python3.10/dist-packages/openai/types/__init__.py", line 5, in <module>
    from .image import Image as Image
  File "/usr/local/lib/python3.10/dist-packages/openai/types/image.py", line 5, in <module>
    from .._models import BaseModel
  File "/usr/local/lib/python3.10/dist-packages/openai/_models.py", line 23, in <module>
    from ._types import (
  File "/usr/local/lib/python3.10/dist-packages/openai/_types.py", line 23, in <module>
    from httpx import URL, Proxy, Timeout, Response, BaseTransport, AsyncBaseTransport
ImportError: cannot import name 'BaseTransport' from 'httpx' (/usr/local/lib/python3.10/dist-packages/httpx/__init__.py)

openai 比較重要,試著以它為主,讓 googletrans 使用較新版本的 httpx 看看,也就是安裝 googletrans 時,不要檢查 dependency,將原本的安裝指令:

!pip -q install  googletrans==4.0.0-rc1

多加「--no-dependencies」改為:

!pip -q install --no-dependencies googletrans==4.0.0-rc1

httpcore 的問題

前面雖然解決了 googletrans 安裝時的問題,執行時,換 httpcore 有問題,出現了這樣的錯誤訊息:

AttributeError: module 'httpcore' has no attribute 'SyncHTTPTransport'

查了一下,可以在使用 googletrans 前,先執行:

import httpcore
setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy')

利用這兩行來 patch,只要比 googletrans 先執行,就不會出現 AtributeError。

httpx[http2]

本以為可以順利執行 google 翻譯了吧!結果又出現這樣的訊息:

Using http2=True, but the 'h2' package is not installed. Make sure to install httpx using `pip
install httpx[http2]`.

不過,這看起來比較好解決,就照提示,安裝 httpx[https] 就好,所以在安裝 googletrans 之前,先執行:

!pip -q install httpx[http2]

經過以上的修正,終於可以讓 googletrans 順利地和 bilingual_book_maker 搭配,以 google 翻譯來進行翻譯。

不過,為了讓使用更有彈性來選擇是否以 googletrans 幫 bilingual_book_maker 執行 google 翻譯的功能,我加上了「install_googletrans」的設定參數,如果勾選了,就使用 googletrans ,否則使用 bilingual_book_maker 專案內建的功能。這樣子,必要的時候,可以選擇關閉我亂改的部份,直接執行 bilingual_book_maker,也方便除錯。

這個雖然是自己沒在使用的工具,累積了除錯及解決問題的能力,時間沒有白花,收穫還挺多的,相信它是一個善的循環。

相關文章


沒有留言:

張貼留言

 
雄::gsyan © 2009. Design by Pocket