"""mundart-explorer — Swiss-German (gsw) language-ID + gsw→de alignment demo. Paste a sentence; the Space says whether it reads as written Swiss-German or Standard German (with a confidence) and surfaces the nearest aligned Standard- German rendering from the gsw-eval probe set. All logic lives in :mod:`lid_service`; this file is the thin Gradio UI only. """ from __future__ import annotations import gradio as gr import lid_service as svc _HUB = "https://huggingface.co/datasets/mischeiwiller" _CORPUS_URL = f"{_HUB}/swiss-german-text" _EVAL_URL = f"{_HUB}/gsw-eval" _EXAMPLES = [ "Ich ha hüt es Brötli gässe und bi denn go poschte.", "Mir gönd am Samschtig go bärgsteige, wenn s Wätter schön isch.", "Chasch mir bitte säge, wo de Bahnhof isch?", "Was machsch du hüt z Aabig?", "Was machst du heute Abend?", "Guten Morgen!", ] def analyze(text: str) -> tuple[str, str]: """Run LID + alignment lookup; return (verdict markdown, alignment markdown).""" text = (text or "").strip() if not text: return "_Paste a sentence above to begin._", "" res = svc.identify(text) pct = f"{res['confidence'] * 100:.0f}%" flag = "🇨🇭" if res["label"] == "gsw" else "🇩🇪" verdict = ( f"### {flag} {res['label_long']}\n" f"**Confidence:** {pct} \n" f"gsw {res['probs']['gsw'] * 100:.0f}% · de {res['probs']['de'] * 100:.0f}% " f"— char n-gram Naive Bayes baseline" ) align = svc.nearest_alignment(text) if align is None: alignment = ( "_No close Standard-German alignment in the probe set " f"({svc.probe_size()} gsw→de pairs)._" ) else: sim = f"{align['similarity'] * 100:.0f}%" alignment = ( f"**Nearest aligned pair** (similarity {sim}):\n\n" f"> 🇨🇭 {align['gsw']}\n>\n" f"> 🇩🇪 {align['de']}" ) return verdict, alignment with gr.Blocks(title="mundart-explorer", fill_width=True) as demo: gr.Markdown( "# 🇨🇭 mundart-explorer\n" "Is it written **Swiss-German (gsw)** or **Standard German (de)**? " "Paste a sentence — the demo also surfaces the nearest aligned German " f"rendering from the [`gsw-eval`]({_EVAL_URL}) probe set." ) inp = gr.Textbox( label="Sentence", placeholder="Schrib öppis uf Schwiizerdütsch oder Hochdütsch …", lines=3, autofocus=True, ) btn = gr.Button("Analyze", variant="primary") verdict = gr.Markdown() alignment = gr.Markdown() gr.Examples(examples=_EXAMPLES, inputs=inp) btn.click(analyze, inputs=inp, outputs=[verdict, alignment]) inp.submit(analyze, inputs=inp, outputs=[verdict, alignment]) gr.Markdown( "LID baseline: deterministic char n-gram multinomial Naive Bayes " "(macro-F1 0.63 on the held-out test split). Part of the **Mundart** project — " f"[`swiss-german-text`]({_CORPUS_URL}) corpus + " f"[`gsw-eval`]({_EVAL_URL}) benchmark." ) if __name__ == "__main__": demo.launch()