File size: 2,498 Bytes
82cb80f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from typing import Any, Dict, Mapping, Tuple

import gradio as gr

from dialect_analysis.pipeline import classify_text


def _format_scores(scores: Mapping[str, float]) -> str:
    ordered = sorted(scores.items(), key=lambda kv: kv[1], reverse=True)
    return "\n".join(f"- {d}: {pct:.1f}%" for d, pct in ordered)


def analyze_text(text: str, strip_diacritics: bool) -> Tuple[str, str, str]:
    """Gradio callback.

    NOTE: Gradio passes inputs positionally, so callback args must not be
    keyword-only.
    """

    try:
        text = (text or "").strip()
        if not text:
            return "", "", ""

        result: Dict[str, Any] = classify_text(text, strip_diacritics=bool(strip_diacritics))
        dialect = str(result.get("dialect", ""))
        confidence = float(result.get("confidence", 0.0) or 0.0) * 100.0
        scores: Mapping[str, float] = result.get("scores", {}) or {}
        explanation = str(result.get("explanation", ""))

        summary_md = f"**Dialect:** {dialect}\n\n**Confidence:** {confidence:.1f}%"
        scores_md = _format_scores(scores)
        explanation_md = f"```\n{explanation}\n```" if explanation else ""

        return summary_md, scores_md, explanation_md
    except Exception as e:
        # Surface errors in the UI instead of failing silently.
        return "", "", f"```\nError: {type(e).__name__}: {e}\n```"


with gr.Blocks(title="Ancient Greek Dialect Classifier") as demo:
    gr.Markdown(
        "# Ancient Greek Dialect Classifier\n"
        "Rule-based, explainable classifier for Attic / Ionic / Doric / Aeolic / Koine.\n"
        "\nPaste Greek text below and click **Analyze**."
    )

    with gr.Row():
        strip = gr.Checkbox(value=True, label="Strip diacritics (recommended)")

    inp = gr.Textbox(
        label="Greek text",
        lines=12,
        placeholder="Paste Ancient Greek text here…",
    )

    btn = gr.Button("Analyze")

    with gr.Row():
        summary = gr.Markdown(label="Summary")

    with gr.Row():
        scores = gr.Markdown(label="Scores")

    explanation = gr.Markdown(label="Explanation")

    btn.click(
        fn=analyze_text,
        inputs=[inp, strip],
        outputs=[summary, scores, explanation],
    )

    # Nice-to-have for Spaces: enable Shift+Enter submit.
    inp.submit(
        fn=analyze_text,
        inputs=[inp, strip],
        outputs=[summary, scores, explanation],
    )


if __name__ == "__main__":
    demo.launch()