DeepL Haystack Integration
==========================
| | |
|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| CI/CD | [![Tests](https://github.com/dribia/deepl-haystack/actions/workflows/test.yml/badge.svg)](https://github.com/dribia/deepl-haystack/actions/workflows/test.yml) [![Coverage Status](https://img.shields.io/codecov/c/github/dribia/driconfig)](https://codecov.io/gh/dribia/driconfig) [![Tests](https://github.com/dribia/deepl-haystack/actions/workflows/lint.yml/badge.svg)](https://github.com/dribia/deepl-haystack/actions/workflows/lint.yml) [![types - Mypy](https://img.shields.io/badge/types-Mypy-blue.svg)](https://github.com/python/mypy) [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) |
| Package | [![PyPI](https://img.shields.io/pypi/v/deepl-haystack)](https://pypi.org/project/deepl-haystack/) ![PyPI - Downloads](https://img.shields.io/pypi/dm/deepl-haystack?color=blue&logo=pypi&logoColor=gold) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/deepl-haystack?logo=python&logoColor=gold) [![GitHub](https://img.shields.io/github/license/dribia/deepl-haystack?color=blue)](LICENSE) |
---
## Installation
This project resides in the Python Package Index (PyPI), so it can easily be installed with `pip`:
```console
pip install deepl-haystack
```
## Usage
The DeepL Haystack integration provides two Haystack components: `DeepLTextTranslator`
and `DeepLDocumentTranslator`. These components can be used to translate text and documents,
respectively, using the DeepL API.
## Examples
To run these examples you'll need a working DeepL API key.
You can get one by signing up at the [DeepL API website](https://www.deepl.com/pro#developer).
### Standalone Text Translation
```python
from haystack.utils import Secret
from deepl_haystack import DeepLTextTranslator
translator = DeepLTextTranslator(
api_key=Secret.from_token("your_api_key_here"), source_lang="EN", target_lang="ES"
)
translated_text = translator.run("Hello, world!")
print(translated_text)
# {'translation': '¡Hola, mundo!', 'meta': {'source_lang': 'EN', 'target_lang': 'ES'}}
```
### Standalone Document Translation
```python
from haystack.dataclasses import Document
from haystack.utils import Secret
from deepl_haystack import DeepLDocumentTranslator
translator = DeepLDocumentTranslator(
api_key=Secret.from_token("your_api_key_here"), source_lang="EN", target_lang="ES"
)
documents_to_translate = [
Document(content="Hello, world!"),
Document(content="Goodbye, Joe!", meta={"name": "Joe"}),
]
translated_documents = translator.run(documents_to_translate)
print("\n".join([f"{doc.content}, {doc.meta}" for doc in translated_documents]))
# ¡Hola, mundo!, {'source_lang': 'EN', 'target_lang': 'ES'}
# ¡Adiós, Joe!, {'name': 'Joe', 'source_lang': 'EN', 'target_lang': 'ES'}
```
### Haystack Pipeline Integration
```python
from haystack import Pipeline
from haystack.components.converters import TextFileToDocument
from haystack.components.writers import DocumentWriter
from haystack.dataclasses.byte_stream import ByteStream
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.utils import Secret
from deepl_haystack import DeepLDocumentTranslator
document_store = InMemoryDocumentStore()
pipeline = Pipeline()
pipeline.add_component(instance=TextFileToDocument(), name="converter")
pipeline.add_component(
instance=DeepLDocumentTranslator(
api_key=Secret.from_token("your_api_key_here"),
target_lang="ES",
),
name="translator",
)
pipeline.add_component(
instance=DocumentWriter(document_store=document_store), name="document_store"
)
pipeline.connect("converter", "translator")
pipeline.connect("translator", "document_store")
pipeline.run({"converter": {"sources": [ByteStream.from_string("Hello world!")]}})
print(document_store.filter_documents())
# [Document(id=..., content: '¡Hola, mundo!', meta: {'source_lang': 'EN', 'language': 'ES'})]
```
## Contributing
[Poetry](https://python-poetry.org) is the best way to interact with this project, to install it,
follow the official [Poetry installation guide](https://python-poetry.org/docs/#installation).
With `poetry` installed, one can install the project dependencies with:
```shell
poetry install
```
Then, to run the project unit tests:
```shell
make test-unit
```
To run the linters (`ruff` and `mypy`):
```shell
make lint
```
To apply all code formatting:
```shell
make format
```
And finally, to run the project integration tests (which actually use the DeepL API),
you should either have the `DEEPL_API_KEY` environment variable set,
or create a `.env` file:
```dotenv
DEEPL_API_KEY=your_api_key_here
```
And run:
```shell
make test-integration
```
## License
`deepl-haystack` is distributed under the terms of the
[MIT](https://opensource.org/license/mit) license.
Check the [LICENSE](./LICENSE) file for further details.
Raw data
{
"_id": null,
"home_page": "https://github.com/dribia/deepl-haystack",
"name": "deepl-haystack",
"maintainer": "Albert Iribarne",
"docs_url": null,
"requires_python": "<3.13,>=3.9",
"maintainer_email": "iribarne@dribia.com",
"keywords": "haystack, deepl, translation, nlp",
"author": "Albert Iribarne",
"author_email": "iribarne@dribia.com",
"download_url": "https://files.pythonhosted.org/packages/ac/18/1836b110e3a7b4fc45ff3364129b260824ff223aac9247717a1baaa4409d/deepl_haystack-0.2.2.tar.gz",
"platform": null,
"description": "DeepL Haystack Integration\n==========================\n\n| | |\n|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| CI/CD | [![Tests](https://github.com/dribia/deepl-haystack/actions/workflows/test.yml/badge.svg)](https://github.com/dribia/deepl-haystack/actions/workflows/test.yml) [![Coverage Status](https://img.shields.io/codecov/c/github/dribia/driconfig)](https://codecov.io/gh/dribia/driconfig) [![Tests](https://github.com/dribia/deepl-haystack/actions/workflows/lint.yml/badge.svg)](https://github.com/dribia/deepl-haystack/actions/workflows/lint.yml) [![types - Mypy](https://img.shields.io/badge/types-Mypy-blue.svg)](https://github.com/python/mypy) [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) |\n| Package | [![PyPI](https://img.shields.io/pypi/v/deepl-haystack)](https://pypi.org/project/deepl-haystack/) ![PyPI - Downloads](https://img.shields.io/pypi/dm/deepl-haystack?color=blue&logo=pypi&logoColor=gold) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/deepl-haystack?logo=python&logoColor=gold) [![GitHub](https://img.shields.io/github/license/dribia/deepl-haystack?color=blue)](LICENSE) |\n---\n\n## Installation\n\nThis project resides in the Python Package Index (PyPI), so it can easily be installed with `pip`:\n\n```console\npip install deepl-haystack\n```\n\n## Usage\n\nThe DeepL Haystack integration provides two Haystack components: `DeepLTextTranslator`\nand `DeepLDocumentTranslator`. These components can be used to translate text and documents,\nrespectively, using the DeepL API.\n\n## Examples\n\nTo run these examples you'll need a working DeepL API key.\nYou can get one by signing up at the [DeepL API website](https://www.deepl.com/pro#developer).\n\n### Standalone Text Translation\n\n```python\nfrom haystack.utils import Secret\n\nfrom deepl_haystack import DeepLTextTranslator\n\ntranslator = DeepLTextTranslator(\n api_key=Secret.from_token(\"your_api_key_here\"), source_lang=\"EN\", target_lang=\"ES\"\n)\n\ntranslated_text = translator.run(\"Hello, world!\")\nprint(translated_text)\n# {'translation': '\u00a1Hola, mundo!', 'meta': {'source_lang': 'EN', 'target_lang': 'ES'}}\n```\n\n### Standalone Document Translation\n\n```python\nfrom haystack.dataclasses import Document\nfrom haystack.utils import Secret\n\nfrom deepl_haystack import DeepLDocumentTranslator\n\ntranslator = DeepLDocumentTranslator(\n api_key=Secret.from_token(\"your_api_key_here\"), source_lang=\"EN\", target_lang=\"ES\"\n)\n\ndocuments_to_translate = [\n Document(content=\"Hello, world!\"),\n Document(content=\"Goodbye, Joe!\", meta={\"name\": \"Joe\"}),\n]\n\ntranslated_documents = translator.run(documents_to_translate)\nprint(\"\\n\".join([f\"{doc.content}, {doc.meta}\" for doc in translated_documents]))\n# \u00a1Hola, mundo!, {'source_lang': 'EN', 'target_lang': 'ES'}\n# \u00a1Adi\u00f3s, Joe!, {'name': 'Joe', 'source_lang': 'EN', 'target_lang': 'ES'}\n```\n\n### Haystack Pipeline Integration\n\n```python\nfrom haystack import Pipeline\nfrom haystack.components.converters import TextFileToDocument\nfrom haystack.components.writers import DocumentWriter\nfrom haystack.dataclasses.byte_stream import ByteStream\nfrom haystack.document_stores.in_memory import InMemoryDocumentStore\nfrom haystack.utils import Secret\n\nfrom deepl_haystack import DeepLDocumentTranslator\n\ndocument_store = InMemoryDocumentStore()\n\npipeline = Pipeline()\npipeline.add_component(instance=TextFileToDocument(), name=\"converter\")\npipeline.add_component(\n instance=DeepLDocumentTranslator(\n api_key=Secret.from_token(\"your_api_key_here\"),\n target_lang=\"ES\",\n ),\n name=\"translator\",\n)\npipeline.add_component(\n instance=DocumentWriter(document_store=document_store), name=\"document_store\"\n)\npipeline.connect(\"converter\", \"translator\")\npipeline.connect(\"translator\", \"document_store\")\npipeline.run({\"converter\": {\"sources\": [ByteStream.from_string(\"Hello world!\")]}})\nprint(document_store.filter_documents())\n# [Document(id=..., content: '\u00a1Hola, mundo!', meta: {'source_lang': 'EN', 'language': 'ES'})]\n```\n\n## Contributing\n\n[Poetry](https://python-poetry.org) is the best way to interact with this project, to install it,\nfollow the official [Poetry installation guide](https://python-poetry.org/docs/#installation).\n\nWith `poetry` installed, one can install the project dependencies with:\n\n```shell\npoetry install\n```\n\nThen, to run the project unit tests:\n\n```shell\nmake test-unit\n```\n\nTo run the linters (`ruff` and `mypy`):\n\n```shell\nmake lint\n```\n\nTo apply all code formatting:\n\n```shell\nmake format\n```\n\nAnd finally, to run the project integration tests (which actually use the DeepL API),\nyou should either have the `DEEPL_API_KEY` environment variable set,\nor create a `.env` file:\n\n```dotenv\nDEEPL_API_KEY=your_api_key_here\n```\n\nAnd run:\n\n```shell\nmake test-integration\n```\n\n## License\n\n`deepl-haystack` is distributed under the terms of the\n[MIT](https://opensource.org/license/mit) license.\nCheck the [LICENSE](./LICENSE) file for further details.\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "Haystack integration with DeepL translation services provider.",
"version": "0.2.2",
"project_urls": {
"Bug Tracker": "https://github.com/dribia/deepl-haystack/issues",
"Homepage": "https://github.com/dribia/deepl-haystack",
"Repository": "https://github.com/dribia/deepl-haystack"
},
"split_keywords": [
"haystack",
" deepl",
" translation",
" nlp"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "af185a5f215f5b729abdbdec5803d46cbc1551a3e8f55bf5b9aed224b06dfc55",
"md5": "53ac9757b5b91827cfe95a27c3bc5e1e",
"sha256": "71aa4f4ae9ece70d4d56639aee0c5d27e8c523ba00b933454eabbf2558ff7f72"
},
"downloads": -1,
"filename": "deepl_haystack-0.2.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "53ac9757b5b91827cfe95a27c3bc5e1e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<3.13,>=3.9",
"size": 5875,
"upload_time": "2024-07-29T07:19:19",
"upload_time_iso_8601": "2024-07-29T07:19:19.178783Z",
"url": "https://files.pythonhosted.org/packages/af/18/5a5f215f5b729abdbdec5803d46cbc1551a3e8f55bf5b9aed224b06dfc55/deepl_haystack-0.2.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ac181836b110e3a7b4fc45ff3364129b260824ff223aac9247717a1baaa4409d",
"md5": "89322caf1da87e5450b4640474d314dc",
"sha256": "e7c74a2a46ce8d865a980417762b20ac3f52457ff861eed503442018e1a30be9"
},
"downloads": -1,
"filename": "deepl_haystack-0.2.2.tar.gz",
"has_sig": false,
"md5_digest": "89322caf1da87e5450b4640474d314dc",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<3.13,>=3.9",
"size": 5786,
"upload_time": "2024-07-29T07:19:20",
"upload_time_iso_8601": "2024-07-29T07:19:20.612830Z",
"url": "https://files.pythonhosted.org/packages/ac/18/1836b110e3a7b4fc45ff3364129b260824ff223aac9247717a1baaa4409d/deepl_haystack-0.2.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-29 07:19:20",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "dribia",
"github_project": "deepl-haystack",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "deepl-haystack"
}