APIS

Caution

This document is outdated, please do not refer to it.

Table of Content

The present project supports two types of APIs, All methods need the Redis;


Python

As pdf2zh is an installed module in Python, we expose two methods for other programs to call in any Python scripts.

For example, if you want translate a document from English to Chinese using Google Translate, you may use the following code:

from pdf2zh_next import translate, translate_stream

params = {
    'lang_in': 'en',
    'lang_out': 'zh',
    'service': 'google',
    'thread': 4,
}
Translate with files:
(file_mono, file_dual) = translate(files=['example.pdf'], **params)[0]
Translate with stream:
with open('example.pdf', 'rb') as f:
    (stream_mono, stream_dual) = translate_stream(stream=f.read(), **params)

⬆️ Back to top


HTTP

In a more flexible way, you can communicate with the program using HTTP protocols, if:

  1. Install and run backend
pip install pdf2zh_next[backend]
pdf2zh_next --flask
pdf2zh_next --celery worker
  1. Using HTTP protocols as follows:

  2. Submit translate task

    curl http://localhost:11008/v1/translate -F "[email protected]" -F "data={\"lang_in\":\"en\",\"lang_out\":\"zh\",\"service\":\"google\",\"thread\":4}"
    {"id":"d9894125-2f4e-45ea-9d93-1a9068d2045a"}
    
  3. Check Progress

    curl http://localhost:11008/v1/translate/d9894125-2f4e-45ea-9d93-1a9068d2045a
    {"info":{"n":13,"total":506},"state":"PROGRESS"}
    
  4. Check Progress (if finished)

    curl http://localhost:11008/v1/translate/d9894125-2f4e-45ea-9d93-1a9068d2045a
    {"state":"SUCCESS"}
    
  5. Save monolingual file

    curl http://localhost:11008/v1/translate/d9894125-2f4e-45ea-9d93-1a9068d2045a/mono --output example-mono.pdf
    
  6. Save bilingual file

    curl http://localhost:11008/v1/translate/d9894125-2f4e-45ea-9d93-1a9068d2045a/dual --output example-dual.pdf
    
  7. Interrupt if running and delete the task

    curl http://localhost:11008/v1/translate/d9894125-2f4e-45ea-9d93-1a9068d2045a -X DELETE
    

⬆️ Back to top