googletrans Translate() not working on Spyder but works on Colab

别等时光非礼了梦想. 提交于 2021-01-28 21:32:06

问题


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

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

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

# Data Size
len(df) : 1800

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).


回答1:


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)

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



来源:https://stackoverflow.com/questions/62516992/googletrans-translate-not-working-on-spyder-but-works-on-colab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!