本文介绍了googletrans Translate()不适用于Spyder,但适用于Colab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对本地仓库中的脱机数据使用googletrans Translator:

I am using googletrans Translator on offline data in local repo:

translator = Translator()
translations = []
for element in df['myText']:
    translations.append(translator.translate(element).text)
df['translations'] = translations

在Google Colab上,它可以正常工作(20分钟),但在我的计算机上则需要30分钟,并因ReadTimeout错误而停止:

On Google Colab it works fine(20 mins) but on my machine it takes 30 mins and stops with ReadTimeout error:

  File "<ipython-input-9-2209313a9a78>", line 4, in <module>
    translations.append(translator.translate(element).text)

  File "C:\Anaconda3\lib\site-packages\googletrans\client.py", line 182, in translate
    data = self._translate(text, dest, src, kwargs)

  File "C:\Anaconda3\lib\site-packages\googletrans\client.py", line 83, in _translate
    r = self.client.get(url, params=params)

  File "C:\Anaconda3\lib\site-packages\httpx\_client.py", line 763, in get
    timeout=timeout,

  File "C:\Anaconda3\lib\site-packages\httpx\_client.py", line 601, in request
    request, auth=auth, allow_redirects=allow_redirects, timeout=timeout,

  File "C:\Anaconda3\lib\site-packages\httpx\_client.py", line 621, in send
    request, auth=auth, timeout=timeout, allow_redirects=allow_redirects,

  File "C:\Anaconda3\lib\site-packages\httpx\_client.py", line 648, in send_handling_redirects
    request, auth=auth, timeout=timeout, history=history

  File "C:\Anaconda3\lib\site-packages\httpx\_client.py", line 684, in send_handling_auth
    response = self.send_single_request(request, timeout)

  File "C:\Anaconda3\lib\site-packages\httpx\_client.py", line 719, in send_single_request
    timeout=timeout.as_dict(),

  File "C:\Anaconda3\lib\site-packages\httpcore\_sync\connection_pool.py", line 153, in request
    method, url, headers=headers, stream=stream, timeout=timeout

  File "C:\Anaconda3\lib\site-packages\httpcore\_sync\connection.py", line 78, in request
    return self.connection.request(method, url, headers, stream, timeout)

  File "C:\Anaconda3\lib\site-packages\httpcore\_sync\http11.py", line 62, in request
    ) = self._receive_response(timeout)

  File "C:\Anaconda3\lib\site-packages\httpcore\_sync\http11.py", line 115, in _receive_response
    event = self._receive_event(timeout)

  File "C:\Anaconda3\lib\site-packages\httpcore\_sync\http11.py", line 145, in _receive_event
    data = self.socket.read(self.READ_NUM_BYTES, timeout)

  File "C:\Anaconda3\lib\site-packages\httpcore\_backends\sync.py", line 62, in read
    return self.sock.recv(n)

  File "C:\Anaconda3\lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)

  File "C:\Anaconda3\lib\site-packages\httpcore\_exceptions.py", line 12, in map_exceptions
    raise to_exc(exc) from None

ReadTimeout: The read operation timed out

我的机器:16 GB Ram(i5 + NVIDIA);Google Colab RAM:0.87 GB/12.72 GB

My machine: 16 GB Ram (i5 + NVIDIA);Google Colab RAM: 0.87 GB/12.72 GB

# Data Size
len(df) : 1800

不知道为什么它不能在我的本地计算机上运行?我以前处理过较重的数据集.我正在使用Python 3(Spyder 4.0).

Not sure why doesn't it run on my local machine? I have worked on heavier datasets before.I am using Python 3 (Spyder 4.0).

推荐答案

此翻译也存在一些问题...看来,您得到的错误与您的机器无关,但与对API的请求超时.尝试将 Timeout 对象从 httpx 库传递给 Translator 构建器.像这样:

I'm having some problems with this translation as well... It appears that the error you're getting has nothing to do with your machine, but with the request to the API timing out. Try and pass a Timeout object from the httpx library to the Translator builder. Something like this:

import httpx
timeout = httpx.Timeout(5) # 5 seconds timeout
translator = Translator(timeout=timeout)

如果需要,您可以将5更改为另一个值.到目前为止,它已经解决了我的问题.

You can change to 5 to another value, if needed. It has solver the problem for me, so far.

这篇关于googletrans Translate()不适用于Spyder,但适用于Colab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:25