Giter Club home page Giter Club logo

Comments (7)

daniel-jones-deepl avatar daniel-jones-deepl commented on August 21, 2024

Hi @ay2456,
In # 2, it seems you have "Too many non-downloaded documents", so downloading some already-translated documents should resolve the issue.

In # 3, the request timed out. This was probably due to temporary problems with our network, so retrying that document.

In # 1, there was some error during translation on DeepL servers. Unfortunately due to a bug in the DocumentTranslationException stringize function, we cannot see in the log what the underlying cause was. That bug will be fixed soon.
However using the logged document ID and key you can try check the status and retrieve the document, using something like this:

def retrieve_document(id, key, output_path):
    handle = deepl.DocumentHandle(id, key)
    status = translator.translate_document_get_status(handle)
    if status.ok and status.done:
        translator.translate_document_download(handle, output_path)
        return True
    else:
        print("Document translation not finished or error occurred")
        return False

Your code seems fine, but you could consider catching any DocumentTranslationException and recording the document_request that contains the ID and key, so you can retrieve them later or contact support.

from deepl-python.

daniel-jones-deepl avatar daniel-jones-deepl commented on August 21, 2024

v1.4.1 includes the fix I described for # 1.

from deepl-python.

ay2456 avatar ay2456 commented on August 21, 2024

@daniel-jones-deepl Thank you for the quick reply and fix! Appreciated it!

I have 2 follow-up questions:

Q1: Regarding the retrieve_document example you provided above, usually how long do I have to wait until I can consider the translation failed? Will it throw DocumentTranslationException as well?

Q2: In cases like # 1 where something failed on the Deepl server-side, am I getting billed for the request?

from deepl-python.

ay2456 avatar ay2456 commented on August 21, 2024

@daniel-jones-deepl I updated the deepl package to 1.4.1 and ran into the # 1 exception again, this time the error log looked like this:

---------------------------------------------------------------------------
DocumentTranslationException              Traceback (most recent call last)
Input In [15], in <module>
----> 1 dt.translate_batch_docs([pdf_path])

Input In [12], in DocumentTranslator.translate_batch_docs(self, pdfs)
      7 for pdf in pdfs:
      8     translated_pdf = pdf.replace('.pdf', '_translated.pdf')
----> 9     self.translator.translate_document_from_filepath(pdf, translated_pdf, target_lang=self.target_language_code)

File /opt/conda/lib/python3.9/site-packages/deepl/translator.py:792, in Translator.translate_document_from_filepath(self, input_path, output_path, source_lang, target_lang, formality, glossary)
    790 out_file.close()
    791 os.unlink(output_path)
--> 792 raise e

File /opt/conda/lib/python3.9/site-packages/deepl/translator.py:781, in Translator.translate_document_from_filepath(self, input_path, output_path, source_lang, target_lang, formality, glossary)
    779 with open(output_path, "wb") as out_file:
    780     try:
--> 781         self.translate_document(
    782             in_file,
    783             out_file,
    784             target_lang=target_lang,
    785             source_lang=source_lang,
    786             formality=formality,
    787             glossary=glossary,
    788         )
    789     except Exception as e:
    790         out_file.close()

File /opt/conda/lib/python3.9/site-packages/deepl/translator.py:851, in Translator.translate_document(self, input_document, output_document, source_lang, target_lang, formality, glossary)
    848     raise DocumentTranslationException(str(e), handle) from e
    850 if not status.ok:
--> 851     raise DocumentTranslationException(
    852         "Error occurred while translating document", handle
    853     )

DocumentTranslationException: Error occurred while translating document, document request: Document ID: FEC7FDF4AAEEE98***, key: 50FEA198BA72CD7DADFA64F13***

It still doesn't say what was wrong.

Also, is there an easier way to fetch the document_request? I had to parse the error messages to get it right now?

from deepl-python.

daniel-jones-deepl avatar daniel-jones-deepl commented on August 21, 2024

Hi @ay2456, sorry that this reply is so delayed.

Firstly your most recent question: yes, you can fetch the document request by catching the thrown exception, like this:

try:
    self.translator.translate...                       # Call deepl functions
except deepl.DocumentTranslationException as error:
    document_request = error.document_request          # Get the document_request
    document_id = document_request.document_id
    document_key = document_request.document_key
    ...                                                # Save the document ID and key for later use
    raise error                                        # Throw the error onwards

Just a quick extra note: error.document_request will soon be renamed to error.document_handle (to match the class DocumentHandle), but we'll make the change backward-compatible so the above code will still work.

Next, your earlier Q1: no, Translator.translate_document_get_status and Translator.translate_document_download do not throw DocumentTranslationException, and they should only throw if there is a connection error. A document translation error would be indicated in the status as not status.ok. In my experience, a document translation failure will be reported in seconds, but it could vary based on many factors. I can see now the retrieve_document example I gave above squashes the two cases of error and not-done together, we can split them apart to make it clearer:

def retrieve_document(id, key, output_path):
    handle = deepl.DocumentHandle(id, key)
    status = translator.translate_document_get_status(handle)
    if not status.ok:
        print("Document translation failed")  # Maybe you would prefer to raise an exception here?
        return False
    elif status.done:
        translator.translate_document_download(handle, output_path)
        return True
    else:
        print("Document translation not yet finished")
        return False

If your documents have a translation failure, you could contact our support.

Lastly, Q2: no we will not bill you if the document translation failure is on our side.

from deepl-python.

daniel-jones-deepl avatar daniel-jones-deepl commented on August 21, 2024

Ah sorry, I also missed your point about the error message not saying what was wrong. Unfortunately our API does not expose an error message in case of document translation failure. So for that document could you please contact support?

from deepl-python.

ay2456 avatar ay2456 commented on August 21, 2024

@daniel-jones-deepl Thank you for the reply! Let's do more testing. I'll close this issue if everything works properly.

from deepl-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.