Comments (13)
@daniel-jones-deepl if this library would use httpx, it could support both sync and async easily. It has API most similar to requests
, so it wouldn't be that hard to rewrite at first
from deepl-python.
Hi everyone, thanks for adding your interest on this feature. (Note: this is @daniel-jones-deepl, I've migrated to my personal account).
I had a quick look into this, aiohttp
looks like a good choice.
Regarding only changing the file http_client.py: it would be an option. Another user implemented a DeepL Python library using something similar. However it doesn't allow proper typing support.
I think the best way to support async would be add a new class TranslatorAsync
with the same interface as Translator
, except that most functions (translate_text
etc.) are labelled async
.
On Friday I have some time to work on that more, I'll post another update then.
from deepl-python.
I've pushed some WIP changes to the async branch, and I'd be interested in feedback.
Lots of things are broken (see below), but the following should work with aiohttp
installed:
async def async_func():
async with deepl.TranslatorAsync(auth_key) as async_translator:
text_result = await async_translator.translate_text(
"Hello, world!", target_lang="de"
)
print(text_result.text)
asyncio.run(async_func())
Currently only translate_text
works (also the synchronous version), the other functions are broken. Also proxies and SSL-verification are broken.
Anyway, feedback is welcome!
from deepl-python.
Hi @Nereg, I did make some progress on it a few weeks back, but I forgot to push those changes. I've pushed them to the async branch now.
The changes fix all the existing tests for the sync implementation, and the async translate_text function works too. The async tests are incomplete and failing in the CI because asyncio is not installed.
Some remaining work to be done:
- implement remaining async functionality - mostly trivial aside from the document download/upload functions.
- complete async tests - I wanted to try a more elegant way to test both the sync and async implementation without duplicating test code. Maybe a possibility is to rewrite all tests to be async, and wrap the sync implementation to behave as async, then parameterise all tests with a sync and async translator.
- proxies and SSL verification
- documentation
from deepl-python.
Just wanna add, it seems like a single file, the http client file, that needs to be updated. Shouldn't be that hard for anyone who wants this and has some time. (this is directed towards non-maintainers looking at this)
from deepl-python.
Hi @nfnfgo, that is right, currently only synchronous calls are possible because the library is based on requests
.
We would like to add asynchronous functionality to this library in future.
from deepl-python.
I think the fastest solution for now could be to generate async client based on available OpenAPI specification. But yes, it's nice to have officially supported "async" version
from deepl-python.
Are there any further updates on if this will be happening/the current status?
from deepl-python.
+1 for async support. aiohttp or httpx
from deepl-python.
+1
from deepl-python.
@daniel-jones-dev Heck yeah! This will make lots of Discord bot developers (who like accessibility through readily available translations) happy
from deepl-python.
Hi! The WIP async version works for me, but I see there is not a lot of progress here. Maybe I could contribute some code to help with it. The proxy and SSL verification should be easy enough to implement, and then I could do the user-facing APIs. I wonder how the test will be adapted to test async implementation too.
from deepl-python.
It seems that the WIP async branch has broken sync implementation! I don't know (but assume) that it is fine in the main branch. So I will need to find the issue with sync first - they use the same code to process the results from the network. Here is the code to reproduce:
import asyncio
import traceback
import deepl
async def main():
trans = deepl.Translator(
"KEY",
# proxy={"https": "https://localhost:8080"},
# verify_ssl=False,
)
trans_async = deepl.TranslatorAsync("KEY")
try:
print(trans.translate_text("test", source_lang="EN", target_lang="RU"))
except Exception as e:
traceback.print_exception(e)
print("translation results: " + await trans_async.translate_text("test", source_lang="EN", target_lang="RU"))
await trans_async.close()
asyncio.run(main())
and the output on my side:
Traceback (most recent call last):
File "***\deepl-python-testing\test.py", line 16, in main
print(trans.translate_text("test", source_lang="EN", target_lang="RU"))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "***\.venv\Lib\site-packages\deepl\translator.py", line 230, in translate_text
return self._translate_text_post(response, base_context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "***\Lib\site-packages\deepl\translator_base.py", line 384, in _translate_text_post
translations = response.json.get("translations", [])
^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'get'
translation results: тест
So the async version works, while sync doesn't.
from deepl-python.
Related Issues (20)
- [Bug] DeepL Write adds sentences describing chosen style
- DeepL documentation states ZH is supported as target for translate_docs but on execution it errors HOT 2
- translate_document_from_filepath fails to recognise XML files HOT 1
- Error: Bad request, message: Document is empty HOT 4
- [Feature Request] Make auto-retry toggleable HOT 3
- can't use my auth key, and can't sign up to the API plan for free trial HOT 4
- preserve_formatting missing in translate_document() function HOT 5
- translator.create_glossary() forces to remove regional variant HOT 3
- Convenient request max retries setup HOT 4
- Encoding problem when translating to french. HOT 2
- New feature : customize timeout duration HOT 2
- Glossaries HOT 1
- deepl.Formality.MORE results in untranslated text HOT 1
- Mismatch in the placement of ignore tags and lack of translation HOT 1
- The deepl.Formality is ignored
- Abnormal strings in translation results: ãã£ã£ãç§ã¡ã¡åã HOT 5
- `translate_document()` should `seek(0, 0)` the output file after being done
- Disambiguate homonyms in glossaries / provide alternative translations? HOT 3
- When translating long English fields into Chinese, the last paragraph was lost
- Glossary flexible search
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deepl-python.