Download utils/lang_utils.py from Alpha108/GenerativeEngineOptimization: direct link, hf CLI and curl.
- Browser
- Download file 446 Bytes
-
https://huggingface.co/spaces/Alpha108/GenerativeEngineOptimization/resolve/main/utils/lang_utils.py
- Command line
-
hf download hf://spaces/Alpha108/GenerativeEngineOptimization/utils/lang_utils.py
-
curl -L -o lang_utils.py https://huggingface.co/spaces/Alpha108/GenerativeEngineOptimization/resolve/main/utils/lang_utils.py
446 Bytes
| # utils/lang_utils.py | |
| from langdetect import detect | |
| from deep_translator import GoogleTranslator | |
| def detect_language(text: str) -> str: | |
| try: | |
| return detect(text) | |
| except Exception: | |
| return "unknown" | |
| def translate_text(text: str, target_lang: str = "en") -> str: | |
| try: | |
| return GoogleTranslator(source='auto', target=target_lang).translate(text) | |
| except Exception as e: | |
| return f"Translation failed: {e}" | |