File size: 10,669 Bytes
023e017 c350a60 023e017 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
import os
import json
import gradio as gr
import fasttext
from google.cloud import translate_v2 as translate
from transformers import pipeline
from dotenv import load_dotenv
import subprocess
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MODEL_PATH = os.path.join(BASE_DIR, "models", "lid.176.bin")
fasttext_model = fasttext.load_model(MODEL_PATH)
# model = fasttext.load_model("models\lid.176.bin")
# print(model.predict("Hello world"))
# --- Setup FastText model (download if missing) ---
# MODEL_PATH = "C:/_Prep/_code/Python/language-detection-compare-models/models/lid.176.bin"
# os.makedirs("models", exist_ok=True)
# if not os.path.exists(MODEL_PATH):
# os.system(
# f"wget -O {MODEL_PATH} https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.bin"
# )
try:
fasttext_model = fasttext.load_model(MODEL_PATH)
except ValueError:
raise RuntimeError("FastText model file could not be loaded.")
# --- Setup Google Translate Client ---
# google_creds = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
# if google_creds:
# with open("google_creds.json", "w") as f:
# f.write(google_creds)
# os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "google_creds.json"
# translate_client = translate.Client()
# else:
# translate_client = None
#print("Current working directory:", os.getcwd())
#load_dotenv(dotenv_path=r"C:\_Prep\_code\Python\language-detection-compare-models\.env") # If needed
#C:\_Prep\_code\Python\language-detection-compare-models\.env
google_creds_path = os.getenv("GOOGLE_APPLICATION_CREDENTIAL")
#print("Resolved GOOGLE_APPLICATION_CREDENTIALS:", google_creds_path)
# load_dotenv()
# google_creds_path = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
#google_creds_path = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
if google_creds_path and os.path.isfile(google_creds_path):
os.environ["GOOGLE_APPLICATION_CREDENTIAL"] = google_creds_path # redundant but explicit
from google.cloud import translate_v2 as translate
translate_client = translate.Client()
else:
translate_client = None
# --- Setup Hugging Face pipeline ---
HF_MODEL_NAME = "papluca/xlm-roberta-base-language-detection"
hf_lang_detector = pipeline("text-classification", model=HF_MODEL_NAME)
# --- Mapping ISO 639-1 language codes to countries with flag emojis ---
# Source: filtered and truncated for top 5 countries (edit as needed)
LANGUAGE_TO_COUNTRIES = {
"en": ["US", "GB", "CA", "AU", "IN"],
"fr": ["FR", "BE", "CA", "CH", "LU"],
"es": ["ES", "MX", "CO", "AR", "PE"],
"de": ["DE", "AT", "CH", "LU", "BE"],
"ar": ["EG", "SA", "IQ", "DZ", "MA"],
"hi": ["IN", "FJ", "MU", "NP", "SG"],
"zh": ["CN", "SG", "MY", "TW", "HK"],
"ru": ["RU", "BY", "KZ", "UA", "KG"],
"pt": ["PT", "BR", "AO", "MZ", "GW"],
"ja": ["JP"],
"ko": ["KR"],
}
def flag_emoji(country_code):
return "".join(chr(0x1F1E6 + ord(c) - ord('A')) for c in country_code)
def render_result(model_name, lang_code, score):
flags = LANGUAGE_TO_COUNTRIES.get(lang_code, [])
if flags:
flag_str = " ".join(flag_emoji(c) for c in flags[:5])
etc = "<br>...etc" if len(flags) > 5 else ""
else:
flag_str = "🌐"
etc = ""
return f"<b>{model_name}:</b> <code>{lang_code}</code> ({score})<br>{flag_str}{etc}"
# def detect_languages(text, hf_model_path=None):
# # FastText
# try:
# ft_label, ft_score = fasttext_model.predict(text, k=1)
# ft_lang = ft_label[0].replace("__label__", "")
# ft_score = round(ft_score[0], 3)
# except Exception:
# ft_lang, ft_score = "Error", 0
# # Google Translate
# if translate_client:
# try:
# result = translate_client.detect_language(text)
# google_lang = result.get("language", "N/A")
# google_conf = round(result.get("confidence", 0), 3)
# except Exception:
# google_lang, google_conf = "Error", 0
# else:
# google_lang, google_conf = "NotConfigured", 0
# # Hugging Face
# try:
# model = (
# pipeline("text-classification", model=hf_model_path)
# if hf_model_path and hf_model_path.strip()
# else hf_lang_detector
# )
# hf_results = model(text)
# hf_lang = hf_results[0]["label"].lower()
# hf_score = round(hf_results[0]["score"], 3)
# except Exception:
# hf_lang, hf_score = "Error", 0
# return (
# render_result("FastText", ft_lang, ft_score),
# render_result("Google", google_lang, google_conf),
# render_result("HuggingFace", hf_lang, hf_score)
# )
from langcodes import Language
# Maps language code to top 5 countries where it's predominantly spoken
LANG_COUNTRY_MAP = {
'af': ['ZA', 'NA'],
'am': ['ET'],
'ar': ['SA', 'EG', 'IQ', 'MA', 'DZ', 'SD', 'SY', 'YE', 'JO', 'LB', 'TN', 'AE', 'OM', 'KW', 'BH', 'QA', 'LY'],
'az': ['AZ'],
'be': ['BY'],
'bg': ['BG'],
'bn': ['BD', 'IN'],
'bs': ['BA'],
'ca': ['ES', 'AD'],
'ceb': ['PH'],
'cs': ['CZ'],
'cy': ['GB'],
'da': ['DK'],
'de': ['DE', 'AT', 'CH', 'LU', 'BE', 'LI'],
'el': ['GR', 'CY'],
'en': ['US', 'GB', 'CA', 'AU', 'NZ', 'IE', 'ZA', 'IN', 'PH', 'NG', 'KE', 'UG'],
'eo': ['PL', 'FR', 'DE', 'US'],
'es': ['ES', 'MX', 'CO', 'AR', 'PE', 'VE', 'CL', 'EC', 'GT', 'CU', 'BO', 'DO', 'HN', 'PY', 'SV', 'NI', 'CR', 'PA', 'UY'],
'et': ['EE'],
'eu': ['ES', 'FR'],
'fa': ['IR', 'AF', 'TJ'],
'fi': ['FI'],
'fil': ['PH'],
'fj': ['FJ'],
'fr': ['FR', 'BE', 'CA', 'CH', 'LU', 'CI', 'SN', 'ML', 'CM', 'HT', 'MG', 'NE', 'TG', 'GA', 'CD', 'BF', 'TD'],
'fy': ['NL'],
'ga': ['IE'],
'gd': ['GB'],
'gl': ['ES'],
'gu': ['IN'],
'ha': ['NG', 'NE', 'GH'],
'haw': ['US'],
'he': ['IL'],
'hi': ['IN', 'FJ', 'MU', 'NP', 'SG'],
'hmn': ['US'],
'hr': ['HR', 'BA'],
'ht': ['HT'],
'hu': ['HU'],
'hy': ['AM'],
'id': ['ID'],
'ig': ['NG'],
'is': ['IS'],
'it': ['IT', 'CH', 'SM'],
'ja': ['JP'],
'jv': ['ID'],
'ka': ['GE'],
'kk': ['KZ'],
'km': ['KH'],
'kn': ['IN'],
'ko': ['KR', 'KP'],
'ku': ['IQ', 'TR', 'SY', 'IR'],
'ky': ['KG'],
'la': ['VA'],
'lb': ['LU'],
'lo': ['LA'],
'lt': ['LT'],
'lv': ['LV'],
'mg': ['MG'],
'mi': ['NZ'],
'mk': ['MK'],
'ml': ['IN'],
'mn': ['MN'],
'mr': ['IN'],
'ms': ['MY', 'BN', 'SG'],
'mt': ['MT'],
'my': ['MM'],
'ne': ['NP'],
'nl': ['NL', 'BE', 'SR', 'AW', 'CW'],
'no': ['NO'],
'ny': ['MW', 'ZM', 'ZW'],
'pa': ['IN', 'PK'],
'pl': ['PL'],
'ps': ['AF'],
'pt': ['PT', 'BR', 'AO', 'MZ', 'GW', 'ST', 'CV'],
'ro': ['RO', 'MD'],
'ru': ['RU', 'BY', 'KZ', 'KG', 'UA'],
'rw': ['RW'],
'sd': ['PK'],
'si': ['LK'],
'sk': ['SK'],
'sl': ['SI'],
'sm': ['WS'],
'sn': ['ZW'],
'so': ['SO'],
'sq': ['AL', 'XK', 'MK'],
'sr': ['RS', 'BA', 'ME'],
'st': ['LS'],
'su': ['ID'],
'sv': ['SE', 'FI'],
'sw': ['KE', 'TZ', 'UG'],
'ta': ['IN', 'LK', 'SG', 'MY'],
'te': ['IN'],
'tg': ['TJ'],
'th': ['TH'],
'ti': ['ET', 'ER'],
'tk': ['TM'],
'tl': ['PH'],
'tr': ['TR', 'CY'],
'tt': ['RU'],
'ug': ['CN'],
'uk': ['UA'],
'ur': ['PK', 'IN'],
'uz': ['UZ'],
'vi': ['VN'],
'xh': ['ZA'],
'yi': ['US', 'IL'],
'yo': ['NG'],
'zh': ['CN', 'SG', 'MY', 'TW'],
'zu': ['ZA'],
}
def country_flag_img(country_code):
#return f"<img src='https://flagcdn.com/w40/{country_code.lower()}.png' height='20' style='margin-right:4px'/><br/>"
return f"<img src='https://flagcdn.com/w40/{country_code.lower()}.png' title='{LANG_COUNTRY_MAP.get(country_code, country_code)}' height='20' style='margin-right:4px'/><br/>"
def format_with_flags(lang_code):
countries = LANG_COUNTRY_MAP.get(lang_code, [])
flags_html = ''.join([country_flag_img(c) for c in countries[:5]])
if len(countries) > 5:
flags_html += "<span style='margin-left:4px;'>etc...</span>"
return flags_html
def detect_languages(text, hf_model_path=None):
ft_label, ft_score = fasttext_model.predict(text, k=1)
ft_lang = ft_label[0].replace("__label__", "")
ft_score = round(ft_score[0], 3)
if translate_client:
try:
result = translate_client.detect_language(text)
google_lang = result.get("language", "N/A")
google_conf = round(result.get("confidence", 0), 3)
except Exception:
google_lang = "Error"
google_conf = 0
else:
google_lang = "Not Configured"
google_conf = 0
if hf_model_path and hf_model_path.strip() != "":
try:
custom_detector = pipeline("text-classification", model=hf_model_path)
hf_results = custom_detector(text)
except Exception:
hf_results = [{"label": "Error", "score": 0}]
else:
hf_results = hf_lang_detector(text)
hf_label = hf_results[0]["label"].lower()
hf_score = round(hf_results[0]["score"], 3)
return (
f"FastText: {ft_lang} ({ft_score})<br>{format_with_flags(ft_lang)}",
f"Google API: {google_lang} ({google_conf})<br>{format_with_flags(google_lang)}",
f"HuggingFace: {hf_label} ({hf_score})<br>{format_with_flags(hf_label)}"
)
with gr.Blocks() as demo:
gr.Markdown("## 🌍 Language Detection Comparison")
with gr.Row():
input_text = gr.TextArea(label="Enter text", lines=4, placeholder="Type text to detect language...", value="Die Renaissance war eine kulturelle und intellektuelle Bewegung, die im 14. Jahrhundert in Italien begann und sich bis ins 17. Jahrhundert über Europa ausbreitete. Sie markierte eine Wiederbelebung der klassischen Kunst, Literatur und Wissenschaft, die den Humanismus, die wissenschaftliche Forschung und den individuellen Ausdruck betonte. Zu den Schlüsselpersonen gehören Leonardo da Vinci, Michelangelo und Galileo.")
with gr.Row():
hf_model_path = gr.Textbox(label="HuggingFace Model Path (optional)", value="papluca/xlm-roberta-base-language-detection", placeholder="e.g. papluca/xlm-roberta-base-language-detection")
detect_btn = gr.Button("Detect Language")
with gr.Row():
fasttext_out = gr.HTML(label="FastText")
google_out = gr.HTML(label="Google")
hf_out = gr.HTML(label="Hugging Face")
detect_btn.click(
detect_languages,
inputs=[input_text, hf_model_path],
outputs=[fasttext_out, google_out, hf_out]
)
if __name__ == "__main__":
demo.launch()
|