Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
3 |
+
|
4 |
+
model_id = "xlm-roberta-base"
|
5 |
+
peft_model_id = "rasyosef/xlm-roberta-base-lora-amharic-news-classification"
|
6 |
+
|
7 |
+
categories = ['แแแญ แ แแ แแ', 'แแแแ', 'แตแแญแต', 'แขแแแต', 'แแแ แ แแ แแ', 'แแแฒแซ']
|
8 |
+
id2label = {i: lbl for i, lbl in enumerate(categories)}
|
9 |
+
label2id = {lbl: i for i, lbl in enumerate(categories)}
|
10 |
+
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
12 |
+
|
13 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
14 |
+
model_id,
|
15 |
+
num_labels=len(categories), # 6
|
16 |
+
id2label=id2label,
|
17 |
+
label2id=label2id
|
18 |
+
)
|
19 |
+
|
20 |
+
model.load_adapter(peft_model_id)
|
21 |
+
|
22 |
+
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
23 |
+
|
24 |
+
def predict(text):
|
25 |
+
return classifier([text])[0]
|
26 |
+
|
27 |
+
|
28 |
+
with gr.Blocks() as demo:
|
29 |
+
gr.Markdown(
|
30 |
+
"""
|
31 |
+
# Amharic News Article Classification
|
32 |
+
This is A finetuned RoBERTa model (xlm-roberta-base) that classifies amharic news articles into one of 6 categories.
|
33 |
+
- แแแญ แ แแ แแ (Local News)
|
34 |
+
- แแแแ (Entertainment)
|
35 |
+
- แตแแญแต (Sports)
|
36 |
+
- แขแแแต (Business)
|
37 |
+
- แแแ แ แแ แแ (International News)
|
38 |
+
- แแแฒแซ (Politics)
|
39 |
+
"""
|
40 |
+
)
|
41 |
+
|
42 |
+
with gr.Row():
|
43 |
+
with gr.Column():
|
44 |
+
input = gr.Textbox(label="Amharic text")
|
45 |
+
classify_btn = gr.Button(value="Classify")
|
46 |
+
with gr.Column():
|
47 |
+
output = gr.Textbox(label="Predicted class")
|
48 |
+
|
49 |
+
classify_btn.click(predict, inputs=input, outputs=output)
|
50 |
+
examples = gr.Examples(
|
51 |
+
examples=[
|
52 |
+
"""แ แ แ แดแแตแฎแต แจแแแตแ แฆแณ แฐแจแตแ แแชแซแ แฐแแแฎ แจแแแ แจแแแจแ แแแฃแณ แฐแ แแแ แตแซ แฅแแฒแแแญ แแแชแแฝ แ แญแแแแข แแแ แแณแแแต แแแซ แซแแแฃแต แจแขแตแฎแตแซ แ แแตแแต แจแณแแแฃแต แณแแแ แ แ แดแแตแฎแต แจแแแแณแธแ แ แแต แฐแแฝ แแคแ แงแแต แจแแฃแ แฆแณ แตแ แ แตแญแแ แฐแแฝ แแค แแแแตแ แฃแแแแต แจแแแตแ แฆแณแ แฅแแแฝ แแแฐ แแแตแต แแขแตแฎแตแซ แฐแฅแแ แจแแแฑแฃแต แฆแณ แแตแข"""
|
53 |
+
],
|
54 |
+
inputs=[input],
|
55 |
+
)
|
56 |
+
|
57 |
+
demo.launch()
|