Download src/models/translation.py from dubbingAI/DubAI: direct link, hf CLI and curl.
- Browser
- Download file 795 Bytes
-
https://huggingface.co/spaces/dubbingAI/DubAI/resolve/main/src/models/translation.py
- Command line
-
hf download hf://spaces/dubbingAI/DubAI/src/models/translation.py
-
curl -L -o translation.py https://huggingface.co/spaces/dubbingAI/DubAI/resolve/main/src/models/translation.py
795 Bytes
| import deepl | |
| import os | |
| from typing import Optional | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| class TranslationService: | |
| def __init__(self): | |
| self.api_key = os.getenv("DEEPL_API_KEY") | |
| if not self.api_key: | |
| raise ValueError("Zmienna środowiskowa DEEPL_API_KEY nie jest ustawiona!") | |
| self.translator = deepl.Translator(self.api_key) | |
| def translate( | |
| self, | |
| text: str, | |
| source_lang: str = "PL", | |
| target_lang: str = "EN-US" | |
| ) -> str: | |
| """Tłumaczenie tekstu""" | |
| result = self.translator.translate_text( | |
| text, | |
| source_lang=source_lang, | |
| target_lang=target_lang | |
| ) | |
| return result.text.replace(".", ",") | |
| # Singleton pattern | |
| TRANSLATOR = TranslationService() |