Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions deep_translator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from deep_translator.qcri import QcriTranslator
from deep_translator.tencent import TencentTranslator
from deep_translator.yandex import YandexTranslator
from deep_translator.ollama import OllamaTranslator

__author__ = """Nidhal Baccouri"""
__email__ = "[email protected]"
Expand All @@ -35,6 +36,7 @@
"ChatGptTranslator",
"TencentTranslator",
"BaiduTranslator",
"OllamaTranslator",
"single_detection",
"batch_detection",
]
59 changes: 59 additions & 0 deletions deep_translator/ollama.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
__copyright__ = "Copyright (C) 2025 Brivaldo Junior"

from typing import List, Optional
from deep_translator.base import BaseTranslator


class OllamaTranslator(BaseTranslator):
"""
class that wraps functions, which use the Ollama
under the hood to translate word(s)
"""

def __init__(
self,
source: str = "auto",
target: str = "english",
model: Optional[str] = "llama3.2:latest",
**kwargs,
):
"""
@param source: source language
@param target: target language
"""
self.model = model

super().__init__(source=source, target=target, **kwargs)

def translate(self, text: str, **kwargs) -> str:
"""
@param text: text to translate
@return: translated text
"""
import ollama


prompt = f"Translate the text below into {self.target}.\n"
prompt += f'Text: "{text}"'

response = ollama.chat(
model=self.model,
messages=[
{
"role": "user",
"content": prompt,
}
],
)

return response.message.content

def translate_file(self, path: str, **kwargs) -> str:
return self._translate_file(path, **kwargs)

def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
"""
@param batch: list of texts to translate
@return: list of translations
"""
return self._translate_batch(batch, **kwargs)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ requests = "^2.23.0"
docx2txt = {version = "^0.8", optional = true}
pypdf = {version = "^3.3.0", optional = true}
openai = {version = "^0.27.6", python = "^3.7.1", optional = true}
ollama = {version = "^0.4.7", python = "^3.7.1", optional = true}

[tool.poetry.extras]
docx = ["docx2txt"]
Expand Down