from langdetect import detect_langs # Detect the language of the input text def detect_language(text): try: if not text.strip() or len(text) < 3: # Return auto-detect if text is empty or too short return [("Auto-detect", 1.0, "Auto-detect")] lang_detections = detect_langs(text) native_lang_map = { "en": ("English", "English"), "fr": ("Français", "French"), "es": ("Español", "Spanish"), "de": ("Deutsch", "German"), "hi": ("हिन्दी", "Hindi"), "zh": ("中文", "Chinese"), "ar": ("العربية", "Arabic"), "ru": ("Русский", "Russian"), "ja": ("日本語", "Japanese") } detected_options = [(native_lang_map.get(lang.lang, ("Auto-detect", "Auto-detect"))[0], lang.prob, native_lang_map.get(lang.lang, ("Auto-detect", "Auto-detect"))[1]) for lang in lang_detections if lang.prob >= 0.2] return detected_options[:1] if detected_options else [("Auto-detect", 0.5, "Auto-detect")] except Exception as e: st.error(f"Language detection error: {e}") return [("Auto-detect", 0.5, "Auto-detect")]