Spaces:
Sleeping
Sleeping
update app.py to use puberta-news
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
|
4 |
|
5 |
MODEL_URL = "https://huggingface.co/dsfsi/PuoBERTa-News"
|
@@ -8,33 +7,45 @@ WEBSITE_URL = "https://www.kodiks.com/ai_solutions.html"
|
|
8 |
tokenizer = AutoTokenizer.from_pretrained("dsfsi/PuoBERTa-News")
|
9 |
model = AutoModelForSequenceClassification.from_pretrained("dsfsi/PuoBERTa-News")
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def prediction(news):
|
12 |
-
# create pipeline
|
13 |
clasifer = pipeline("sentiment-analysis", tokenizer=tokenizer, model=model, return_all_scores=True)
|
14 |
|
15 |
preds = clasifer(news)
|
16 |
|
17 |
-
preds_dict={}
|
18 |
for pred in preds[0]:
|
19 |
-
|
|
|
20 |
|
21 |
return preds_dict
|
22 |
|
23 |
-
|
24 |
gradio_ui = gr.Interface(
|
25 |
fn=prediction,
|
26 |
title="Setswana News Classification",
|
27 |
-
description=f"Enter
|
28 |
examples=[
|
29 |
-
['
|
30 |
-
["
|
31 |
-
["
|
32 |
-
["
|
33 |
],
|
34 |
-
inputs=gr.inputs.Textbox(lines=10, label="Paste some news here"),
|
35 |
outputs=gr.outputs.Label(num_top_classes=5, type="auto", label="News categories probabilities"),
|
36 |
theme="huggingface",
|
37 |
-
article="<p style='text-align: center'>
|
38 |
)
|
39 |
|
40 |
-
gradio_ui.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
|
3 |
|
4 |
MODEL_URL = "https://huggingface.co/dsfsi/PuoBERTa-News"
|
|
|
7 |
tokenizer = AutoTokenizer.from_pretrained("dsfsi/PuoBERTa-News")
|
8 |
model = AutoModelForSequenceClassification.from_pretrained("dsfsi/PuoBERTa-News")
|
9 |
|
10 |
+
categories = {
|
11 |
+
"arts_culture_entertainment_and_media": "Botsweretshi, setso, boitapoloso le bobegakgang",
|
12 |
+
"crime_law_and_justice": "Bosenyi, molao le bosiamisi",
|
13 |
+
"disaster_accident_and_emergency_incident": "Masetlapelo, kotsi le tiragalo ya maemo a tshoganyetso",
|
14 |
+
"economy_business_and_finance": "Ikonomi, tsa kgwebo le tsa ditšhelete",
|
15 |
+
"education": "Thuto",
|
16 |
+
"environment": "Tikologo",
|
17 |
+
"health": "Boitekanelo",
|
18 |
+
"politics": "Dipolotiki",
|
19 |
+
"religion_and_belief": "Bodumedi le tumelo",
|
20 |
+
"society": "Setšhaba"
|
21 |
+
}
|
22 |
+
|
23 |
def prediction(news):
|
|
|
24 |
clasifer = pipeline("sentiment-analysis", tokenizer=tokenizer, model=model, return_all_scores=True)
|
25 |
|
26 |
preds = clasifer(news)
|
27 |
|
28 |
+
preds_dict = {}
|
29 |
for pred in preds[0]:
|
30 |
+
label = categories.get(pred['label'], pred['label'])
|
31 |
+
preds_dict[label] = pred['score']
|
32 |
|
33 |
return preds_dict
|
34 |
|
|
|
35 |
gradio_ui = gr.Interface(
|
36 |
fn=prediction,
|
37 |
title="Setswana News Classification",
|
38 |
+
description=f"Enter Setswana news article to see the category of the news.\n For this classification, the {MODEL_URL} model was used.",
|
39 |
examples=[
|
40 |
+
['Ka Letsatsi la Aforika, Aforika Borwa e tla be e keteka mabaka a boikemelo, le diketso tse di siameng tse e di dirileng go tokafatsa dikamano tsa yona le dinaga tse dingwe tsa Aforika.'],
|
41 |
+
["Thuto ya Setswana ke nngwe ya dithuto tse di botlhokwa mo sekolong se se tlhamaletseng go ruta bana ba ba mo lefatsheng la Botswana."],
|
42 |
+
["Mo kgweding e e fetileng, dipuisano tsa ditheko tsa dijalo di ile tsa tswelela, ka batho ba rekang le barui ba ba ruileng."],
|
43 |
+
["Masole a Aforika Borwa a ne a ya kwa Mozambique go tlisetsa motlakase morago ga maduo a kgatlha."],
|
44 |
],
|
45 |
+
inputs=gr.inputs.Textbox(lines=10, label="Paste some Setswana news here"),
|
46 |
outputs=gr.outputs.Label(num_top_classes=5, type="auto", label="News categories probabilities"),
|
47 |
theme="huggingface",
|
48 |
+
article="<p style='text-align: center'>For our other AI works: <a href='https://www.kodiks.com/ai_solutions.html' target='_blank'>https://www.kodiks.com/ai_solutions.html</a> | <a href='https://twitter.com/KodiksBilisim' target='_blank'>Contact us</a></p>",
|
49 |
)
|
50 |
|
51 |
+
gradio_ui.launch()
|