Spaces:
Sleeping
Sleeping
Download language_detect.py from unixio/bilinguascript-htr: direct link, hf CLI and curl.
- Browser
- Download file 496 Bytes
-
https://huggingface.co/spaces/unixio/bilinguascript-htr/resolve/main/language_detect.py
- Command line
-
hf download hf://spaces/unixio/bilinguascript-htr/language_detect.py
-
curl -L -o language_detect.py https://huggingface.co/spaces/unixio/bilinguascript-htr/resolve/main/language_detect.py
496 Bytes
| """Standalone language detection used by app.py routing logic.""" | |
| from langdetect import detect, DetectorFactory, LangDetectException | |
| DetectorFactory.seed = 42 | |
| SUPPORTED = {"en", "fr"} | |
| def detect_language(text: str, hint: str = "auto") -> str: | |
| if hint in SUPPORTED: | |
| return hint | |
| if not text or not text.strip(): | |
| return "en" | |
| try: | |
| lang = detect(text[:500]) | |
| return lang if lang in SUPPORTED else "en" | |
| except LangDetectException: | |
| return "en" | |