zyddnys / manga-image-translator

Translate manga/image 一键翻译各类图片内文字 https://cotrans.touhou.ai/
https://cotrans.touhou.ai/
GNU General Public License v3.0
5.4k stars 558 forks source link

[Bug]: colab 上面的chatgpt 無法使用 #653

Open upright2003 opened 5 months ago

upright2003 commented 5 months ago

Issue

https://colab.research.google.com/drive/1jhLfJdY3uPzlfw1MtLX6WJhB9TKshmYQ?usp=sharing

我在 translators/key.py 14行 輸入 sk- 開頭的api 但是無法翻譯, 使用google 翻譯可以翻譯,但是日文翻成中文有點怪怪的 比如弟弟變成哥哥啥的 請問該如何使用

Command Line Arguments

#使用命令行批量翻译 GPT
!python -m manga_translator --verbose --mode batch --use-gpu --translator=gpt3.5 --target-lang=CHS -i "/content/manga-image-translator/demo"

Console logs

[batch] Namespace(mode='batch', input=['/content/manga-image-translator/demo'], dest='', target_lang='CHS', verbose=True, format=None, attempts=0, ignore_errors=False, overwrite=False, skip_no_text=False, model_dir=None, skip_lang=None, use_gpu=True, use_gpu_limited=False, detector='default', ocr='48px', use_mocr_merge=False, inpainter='lama_large', upscaler='esrgan', upscale_ratio=None, colorizer=None, translator='gpt3.5', translator_chain=None, selective_translation=None, revert_upscaling=False, detection_size=1536, det_rotate=False, det_auto_rotate=False, det_invert=False, det_gamma_correct=False, unclip_ratio=2.3, box_threshold=0.7, text_threshold=0.5, min_text_length=0, no_text_lang_skip=False, inpainting_size=2048, inpainting_precision='fp32', colorization_size=576, denoise_sigma=30, mask_dilation_offset=0, font_size=None, font_size_offset=0, font_size_minimum=-1, font_color=None, line_spacing=None, force_horizontal=False, force_vertical=False, align_left=False, align_center=False, align_right=False, uppercase=False, lowercase=False, no_hyphenation=False, manga2eng=False, gpt_config=None, use_mtpe=False, save_text=False, save_text_file='', filter_text=None, prep_manual=False, font_path='', gimp_font='Sans-serif', host='127.0.0.1', port=5003, nonce='', ws_url='ws://localhost:5000', save_quality=100, ignore_bubble=0, kernel_size=3)
[batch] Running in batch mode
[batch] Skipping as already translated: "/content/manga-image-translator/demo-translated/00000.jpg". Use --overwrite to overwrite existing translations.
[batch] Skipping as already translated: "/content/manga-image-translator/demo-translated/00001.jpg". Use --overwrite to overwrite existing translations.
[batch] Translating: "/content/manga-image-translator/demo/00002.jpg"
[batch] Loading models
ERROR: [batch] AttributeError: module 'openai' has no attribute 'AsyncOpenAI'
Traceback (most recent call last):
  File "/content/manga-image-translator/manga_translator/__main__.py", line 73, in <module>
    loop.run_until_complete(dispatch(args))
  File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/content/manga-image-translator/manga_translator/__main__.py", line 42, in dispatch
    await translator.translate_path(path, dest, args_dict)
  File "/content/manga-image-translator/manga_translator/manga_translator.py", line 178, in translate_path
    if await self.translate_file(file_path, output_dest, params):
  File "/content/manga-image-translator/manga_translator/manga_translator.py", line 205, in translate_file
    return await self._translate_file(path, dest, ctx)
  File "/content/manga-image-translator/manga_translator/manga_translator.py", line 248, in _translate_file
    ctx = await self.translate(img, ctx)
  File "/content/manga-image-translator/manga_translator/manga_translator.py", line 301, in translate
    await prepare_translation(ctx.translator)
  File "/content/manga-image-translator/manga_translator/translators/__init__.py", line 92, in prepare
    translator = get_translator(key)
  File "/content/manga-image-translator/manga_translator/translators/__init__.py", line 54, in get_translator
    translator_cache[key] = translator(*args, **kwargs)
  File "/content/manga-image-translator/manga_translator/translators/chatgpt.py", line 57, in __init__
    self.client = openai.AsyncOpenAI()
AttributeError: module 'openai' has no attribute 'AsyncOpenAI'
Temp530 commented 5 months ago

add this code in manga_translator.py line 678

if 'gpt' in key or 'sakura' in key:
                continue

like this

async def _init_connection(self):
        available_translators = []
        from .translators import MissingAPIKeyException, get_translator
        for key in TRANSLATORS:
            #like this
            # ===
            if 'gpt' in key or 'sakura' in key:
                continue
            # ===
            try:
                get_translator(key)
                available_translators.append(key)
            except MissingAPIKeyException:
                pass

        data = {
            'nonce': self.nonce,
            'capabilities': {
                'translators': available_translators,
            },
        }
        requests.post(f'http://{self.host}:{self.port}/connect-internal', json=data)

you can not use gpt and sakura but you can use other models at least

arrancarr commented 5 months ago

卸载当前的openai package,然后安装最新版本的openai package

pip uninstall openai pip install openai --upgrade

在chatgpt.py内, 修改以下的代码

try:
    import openai
    from openai import AsyncOpenAI
except ImportError:
    AsyncOpenAI = None
def __init__(self, check_openai_key = True):
        super().__init__()
        self.client = AsyncOpenAI()

参考链接:https://pypi.org/project/openai/

jokester commented 2 months ago

https://github.com/zyddnys/manga-image-translator/pull/666 此PR之後 pip也會安裝新版的openai包

upright2003 commented 2 months ago

今天重新測試gpt3.5 測試下發現可能是 您的積分已用完或已達到每月最高支出。 算了 改成papago或是其他翻譯就好了 目前發現google不能用了

!python -m manga_translator --verbose --use-gpu --translator=gpt3.5 --target-lang=CHS -i /content/00003.jpg

openai.RateLimitError: 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'}}

另外我想問問其他人那些免費翻譯沒有門檻的api比較容易申請的? deep似乎還要先拿信用卡登記我怕會被盜刷