Download src/translation.py from WizardCoder2007/social_media_analyzer: direct link, hf CLI and curl.
- Browser
- Download file 586 Bytes
-
https://huggingface.co/spaces/WizardCoder2007/social_media_analyzer/resolve/main/src/translation.py
- Command line
-
hf download hf://spaces/WizardCoder2007/social_media_analyzer/src/translation.py
-
curl -L -o translation.py https://huggingface.co/spaces/WizardCoder2007/social_media_analyzer/resolve/main/src/translation.py
586 Bytes
| from deep_translator import GoogleTranslator | |
| import time | |
| def translate_to_english(text: str, source="auto") -> str: | |
| """ | |
| Translates input text to English using Google Translator. | |
| Retries once on failure. | |
| """ | |
| try: | |
| # Use simple 'auto' detection or specific source | |
| translator = GoogleTranslator(source=source, target='en') | |
| translated = translator.translate(text) | |
| return translated | |
| except Exception as e: | |
| print(f"[WARNING] Translation failed: {e}") | |
| # Fallback to original text if translation fails | |
| return text | |