Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
|
| 6 |
+
# Load the tokenizer and model
|
| 7 |
+
tokenizer = BertTokenizer.from_pretrained('indobenchmark/indobert-large-p1')
|
| 8 |
+
model = BertForSequenceClassification.from_pretrained("hendri/nergrit")
|
| 9 |
+
|
| 10 |
+
labels = ["LABEL_0", "LABEL_1", "LABEL_2", "LABEL_3", "LABEL_4", "LABEL_5" , "LABEL_6"]
|
| 11 |
+
|
| 12 |
+
# Map these to your actual labels:
|
| 13 |
+
label_mapping = {
|
| 14 |
+
"LABEL_0": "I-PERSON",
|
| 15 |
+
"LABEL_1": "B-ORGANISATION",
|
| 16 |
+
"LABEL_2": "I-ORGANISATION",
|
| 17 |
+
"LABEL_3": "B-PLACE",
|
| 18 |
+
"LABEL_4": "I-PLACE",
|
| 19 |
+
"LABEL_5": "O",
|
| 20 |
+
"LABEL_6": "B-PERSON"
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
# Define a function to process user input and return predictions
|
| 24 |
+
def classify_emotion(text):
|
| 25 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
outputs = model(**inputs)
|
| 28 |
+
logits = outputs.logits
|
| 29 |
+
probabilities = F.softmax(logits, dim=-1)
|
| 30 |
+
predictions = {label_mapping[labels[i]]: round(float(prob), 4) for i, prob in enumerate(probabilities[0])}
|
| 31 |
+
return predictions
|
| 32 |
+
|
| 33 |
+
# Create the Gradio interface
|
| 34 |
+
interface = gr.Interface(
|
| 35 |
+
fn=classify_emotion,
|
| 36 |
+
inputs=gr.Textbox(label="Enter Text for NER"),
|
| 37 |
+
outputs=gr.Label(label="Predicted NER"),
|
| 38 |
+
title="Emotion Classification",
|
| 39 |
+
description="This application uses an IndoBERT model fine-tuned for NER. Enter a sentence (bahasa Indonesia) to see the predicted NER and their probabilities."
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Launch the Gradio interface
|
| 43 |
+
interface.launch()
|