guardrails-final / english_detector.py
zazaman's picture
Add multilingual translation support with Qwen3-0.6B-GGUF and optimize for Hugging Face Spaces deployment
a2e1879
import sys
from typing import Iterable
ASCII_UPPERCASE = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
ASCII_LOWERCASE = set("abcdefghijklmnopqrstuvwxyz")
ASCII_LETTERS = ASCII_UPPERCASE | ASCII_LOWERCASE
def get_text_from_args_or_stdin(argv: Iterable[str]) -> str:
text_parts = list(argv)
if text_parts:
return " ".join(text_parts)
# If input is piped, read from stdin. Otherwise, prompt the user.
if not sys.stdin.isatty():
return sys.stdin.read().strip()
try:
return input("Enter text to check if it's English: ").strip()
except EOFError:
return ""
def is_english_by_ascii_letters_only(text: str) -> bool:
"""
Basic heuristic: If every alphabetic character in the text is an ASCII letter (A-Z or a-z),
consider it English. Digits, whitespace, and punctuation are ignored.
"""
for ch in text:
if ch.isalpha() and ch not in ASCII_LETTERS:
return False
return True
def main() -> int:
text = get_text_from_args_or_stdin(sys.argv[1:])
if is_english_by_ascii_letters_only(text):
print("English")
return 0
print("Not English")
return 0
if __name__ == "__main__":
sys.exit(main())