ahmeterenodaci / easygoogletranslate

Easy Google Translate: Unofficial Google Translate API
99 stars 14 forks source link

Read timed out #3

Closed rumbleFTW closed 1 year ago

rumbleFTW commented 1 year ago

Having this issue for translating Indic languages to english:

raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='translate.google.com', port=443): Read timed out.

Traceback:

Traceback (most recent call last):
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/site-packages/urllib3/response.py", line 444, in _error_catcher
    yield
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/site-packages/urllib3/response.py", line 828, in read_chunked
    self._update_chunk_length()
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/site-packages/urllib3/response.py", line 758, in _update_chunk_length
    line = self._fp.fp.readline()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/socket.py", line 706, in readinto
    return self._sock.recv_into(b)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/ssl.py", line 1311, in recv_into
    return self.read(nbytes, buffer)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/ssl.py", line 1167, in read
    return self._sslobj.read(len, buffer)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TimeoutError: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/site-packages/requests/models.py", line 816, in generate
    yield from self.raw.stream(chunk_size, decode_content=True)
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/site-packages/urllib3/response.py", line 624, in stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/site-packages/urllib3/response.py", line 816, in read_chunked
    with self._error_catcher():
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/site-packages/urllib3/response.py", line 449, in _error_catcher
    raise ReadTimeoutError(self._pool, None, "Read timed out.")
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='translate.google.com', port=443): Read timed out.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/rumbleftw/Documents/Codes/vartapratikriya-cron-job/src/cron_job.py", line 137, in <module>
    job.run()
  File "/home/rumbleftw/Documents/Codes/vartapratikriya-cron-job/src/cron_job.py", line 106, in run
    categorised_dict = self.generate_sentiment(data=categorised, type="categorised")
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rumbleftw/Documents/Codes/vartapratikriya-cron-job/src/cron_job.py", line 71, in generate_sentiment
    sentiment = self.analyser(article["description"])
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rumbleftw/Documents/Codes/vartapratikriya-cron-job/src/sentiment_analyser.py", line 16, in __call__
    return self.pipe(self.translator.translate(text=string, target_language="en"))[
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/site-packages/easygoogletranslate/easygoogletranslate.py", line 81, in translate
    return self.make_request(target_language, source_language, text, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/site-packages/easygoogletranslate/easygoogletranslate.py", line 55, in make_request
    response = requests.get(url, timeout=timeout)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/site-packages/requests/api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/site-packages/requests/sessions.py", line 747, in send
    r.content
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/site-packages/requests/models.py", line 899, in content
    self._content = b"".join(self.iter_content(CONTENT_CHUNK_SIZE)) or b""
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rumbleftw/anaconda3/envs/vartapratikriya/lib/python3.11/site-packages/requests/models.py", line 822, in generate
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='translate.google.com', port=443): Read timed out.
ahmeterenodaci commented 1 year ago

Can you provide an example input? @rumbleFTW

rumbleFTW commented 1 year ago

Actually I'm not sure. I have a cron-job that uses easygoogletranslate to translate multiple texts, before feeding them to a sentiment analysis-model, about 200+ per day. Here's the code:

from easygoogletranslate import EasyGoogleTranslate
from transformers import pipeline

from typing import *

class SentimentAnalyser:
    def __init__(self) -> None:
        self.pipe = pipeline(
            "text-classification",
            model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis",
        )
        self.translator = EasyGoogleTranslate()

    def __call__(self, string: str) -> Dict:
        return self.pipe(self.translator.translate(text=string, target_language="en"))[
            0
        ]
ahmeterenodaci commented 1 year ago

You can specify the timeout duration. The default timeout duration is 5 seconds, and it seems like it's not enough time to translate your text input. You can override the default timeout duration by defining it like this:

 EasyGoogleTranslate(
    target_language='en',
    timeout=20
)

@rumbleFTW let me know if you have further problems.

rumbleFTW commented 1 year ago

@ahmeterenodaci Thanks a lot. Word like a charm! Cheers :)

ahmeterenodaci commented 1 year ago

You're welcome :)