-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
81 lines (60 loc) · 2.65 KB
/
Copy pathapp.py
File metadata and controls
81 lines (60 loc) · 2.65 KB
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
"""Gradio demo: type a sentence in Amharic or Afaan Oromo, see the topic."""
from __future__ import annotations
import json
import sys
from pathlib import Path
import gradio as gr
ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(ROOT / "src"))
import predict # noqa: E402
EXAMPLES_PATH = ROOT / "reports" / "demo_examples.json"
DESCRIPTION = """
News topic classification for **Amharic** and **Afaan Oromo**, two languages
with very little NLP tooling. TF-IDF over character n-grams plus logistic
regression, trained on [MasakhaNEWS](https://huggingface.co/datasets/masakhane/masakhanews).
The language is detected from the script (Ge'ez vs Latin) and routed to the
matching model, so just type — you do not need to pick one. The two languages
have different topic sets: Amharic covers business, health, politics and
sports; Afaan Oromo covers entertainment, health, politics, sports and
technology.
---
Built by **Bekan**.
"""
def load_examples() -> list[list[str]]:
if not EXAMPLES_PATH.exists():
return []
data = json.loads(EXAMPLES_PATH.read_text(encoding="utf-8"))
return [[item["text"]] for entries in data.values() for item in entries]
def classify(text: str) -> tuple[dict, str]:
result = predict.predict(text)
if not result["predictions"]:
return {}, "Type a sentence to see a prediction."
confidences = {p["label"]: p["confidence"] for p in result["predictions"]}
top = result["predictions"][0]
note = f"Detected **{result['language_name']}** from the script."
if top["confidence"] < 0.5:
note += " Confidence is low here — the model is genuinely unsure."
return confidences, note
with gr.Blocks(title="Amharic / Afaan Oromo news topic classifier") as demo:
gr.Markdown("# Amharic / Afaan Oromo news topic classifier")
gr.Markdown(DESCRIPTION)
with gr.Row():
with gr.Column():
text_input = gr.Textbox(
label="Your sentence",
placeholder="Barreeffama afaan Oromoo yookaan Amaariffaa asitti barreessi…",
lines=3,
)
submit = gr.Button("Classify", variant="primary")
with gr.Column():
label_output = gr.Label(label="Predicted topic", num_top_classes=3)
note_output = gr.Markdown()
gr.Examples(
examples=load_examples(),
inputs=text_input,
label="Held-out test headlines to try",
)
submit.click(classify, inputs=text_input, outputs=[label_output, note_output])
text_input.submit(classify, inputs=text_input, outputs=[label_output, note_output])
if __name__ == "__main__":
demo.launch(favicon_path=ROOT / "favicon.svg")