nidhaloff / deep-translator

A flexible free and unlimited python tool to translate between different languages in a simple way using multiple translators.
https://deep-translator.readthedocs.io/en/latest/?badge=latest
Apache License 2.0
1.61k stars 185 forks source link

đŸ”ļ Add asyncIO feature for optimization of batch_translate #202

Open jiaulislam opened 1 year ago

jiaulislam commented 1 year ago

Description

At current version of deep-translator(1.10.1) batch_translate is doing a linear loop of same function of single translate function. So in that context request is creating new session every time for requesting url to translate. Which is causing translator slow. Also It's working synchronously as each time request is blocking so until that request is finished translator is waiting idle. It can be optimized with asyncio feature also adding a dependency of aiohttp package and further for duplicate request we can utilize lru_cache system. Theres still long way to go for every translator to add the asyncio.

Changes Made

Changes are going to be non-breaking. Maybe we could add different function for async versions. But new dependency should be added aside of request we need to add aiohttp for async http calls.

Checklist:

Proof Of Concept

image

Usage


from deep_translator.google import GoogleTranslator
import asyncio

def medium_batch_bengali():
    return [
        "āĻ†āĻŽāĻŋ āĻ­āĻžāĻ˛ā§‹ āĻ†āĻ›āĻŋāĨ¤ āĻ¤ā§āĻŽāĻŋ āĻ•ā§‡āĻŽāĻ¨ āĻ†āĻ›ā§‹?",
        "āĻ†āĻŽāĻžāĻ° āĻ¸ā§āĻ•ā§āĻ˛ā§‡ āĻŦāĻŋāĻļāĻžāĻ˛ āĻ˛āĻžāĻ‡āĻŦā§āĻ°ā§‡āĻ°āĻŋ āĻ†āĻ›ā§‡āĨ¤",
        "āĻ†āĻŽāĻ°āĻž āĻŦāĻžāĻ‚āĻ˛āĻžāĻĻā§‡āĻļā§‡āĻ° āĻœāĻ¨ā§āĻ¯ āĻ¸ā§‡āĻ°āĻž āĻ•āĻ°ā§‡ āĻšā§‡āĻˇā§āĻŸāĻž āĻ•āĻ°āĻ›āĻŋāĨ¤",
        "āĻ†āĻŽāĻ°āĻž āĻĻā§€āĻ°ā§āĻ˜ āĻ¸āĻŽāĻ¯āĻŧ āĻ§āĻ°ā§‡ āĻāĻ•āĻ¸āĻŽāĻ¯āĻŧ āĻ¸āĻŦāĻžāĻ‡ āĻĒā§āĻ°āĻ¤ā§āĻ¯ā§‡āĻ•ā§‡āĻ° āĻ¸āĻžāĻĨā§‡ āĻ¯ā§āĻ•ā§āĻ¤ āĻĨāĻžāĻ•āĻ¤ā§‡ āĻšāĻžāĻ‡āĨ¤",
    ]

async def main():
    translator = GoogleTranslator(target="en")
    translated_texts = await translator.async_translate_batch(medium_batch_bengali())

if __name__== "__main__":
    asyncio.run(main())
jiaulislam commented 1 year ago

@nidhaloff , It would be great if you provide some of observations here.