back
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from transformers import BertTokenizer, BertModel
|
4 |
import torch.nn.functional as F
|
5 |
|
6 |
# Load model and tokenizer from Hugging Face
|
@@ -10,7 +10,7 @@ tokenizer = BertTokenizer.from_pretrained(model_name)
|
|
10 |
class IndoBERTMultiTaskClassifier(torch.nn.Module):
|
11 |
def __init__(self, bert_model_name, num_labels_task1, num_labels_task2, dropout_rate=0.3):
|
12 |
super(IndoBERTMultiTaskClassifier, self).__init__()
|
13 |
-
self.bert = BertModel.from_pretrained(bert_model_name)
|
14 |
self.dropout = torch.nn.Dropout(dropout_rate)
|
15 |
self.classifier_task1 = torch.nn.Linear(self.bert.config.hidden_size, num_labels_task1)
|
16 |
self.classifier_task2 = torch.nn.Linear(self.bert.config.hidden_size, num_labels_task2)
|
@@ -25,13 +25,12 @@ class IndoBERTMultiTaskClassifier(torch.nn.Module):
|
|
25 |
|
26 |
return logits_task1, logits_task2
|
27 |
|
28 |
-
# Load
|
29 |
model = IndoBERTMultiTaskClassifier(
|
30 |
bert_model_name=model_name,
|
31 |
num_labels_task1=3, # Adjust with your task1 classes
|
32 |
num_labels_task2=3 # Adjust with your task2 classes
|
33 |
)
|
34 |
-
model.load_state_dict(torch.load("pytorch_model.bin", map_location=torch.device('cpu')))
|
35 |
model.eval()
|
36 |
|
37 |
# Define label mappings
|
@@ -52,27 +51,26 @@ def classify(text):
|
|
52 |
probs_task1 = F.softmax(logits_task1, dim=1).cpu().numpy()[0] # Extract the first batch item
|
53 |
probs_task2 = F.softmax(logits_task2, dim=1).cpu().numpy()[0] # Extract the first batch item
|
54 |
|
55 |
-
#
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
return result_task1, result_task2
|
60 |
|
61 |
-
# Gradio Interface
|
62 |
-
iface = gr.Interface(
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
],
|
69 |
-
title="Multitask IndoBERT: Fake Review & Sentiment Classification",
|
70 |
-
description="Enter a skincare product review in Indonesian and the model will classify it as fake or trusted, and determine the sentiment.",
|
71 |
-
examples=[
|
72 |
-
["Jokowi sangat kecewa dengan POLRI atas kerusuhan yang terjadi di Malang"],
|
73 |
-
["Lesti marah terhadap perlakuan KDRT yang dilakukan oleh Bilar"],
|
74 |
-
["Ungkapan rasa bahagia diutarakan oleh Coki Pardede karena kebebasannya dari penjara"]
|
75 |
-
]
|
76 |
-
)
|
77 |
|
78 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from transformers import BertTokenizer, BertModel
|
4 |
import torch.nn.functional as F
|
5 |
|
6 |
# Load model and tokenizer from Hugging Face
|
|
|
10 |
class IndoBERTMultiTaskClassifier(torch.nn.Module):
|
11 |
def __init__(self, bert_model_name, num_labels_task1, num_labels_task2, dropout_rate=0.3):
|
12 |
super(IndoBERTMultiTaskClassifier, self).__init__()
|
13 |
+
self.bert = BertModel.from_pretrained(bert_model_name)
|
14 |
self.dropout = torch.nn.Dropout(dropout_rate)
|
15 |
self.classifier_task1 = torch.nn.Linear(self.bert.config.hidden_size, num_labels_task1)
|
16 |
self.classifier_task2 = torch.nn.Linear(self.bert.config.hidden_size, num_labels_task2)
|
|
|
25 |
|
26 |
return logits_task1, logits_task2
|
27 |
|
28 |
+
# Load model directly from Hugging Face
|
29 |
model = IndoBERTMultiTaskClassifier(
|
30 |
bert_model_name=model_name,
|
31 |
num_labels_task1=3, # Adjust with your task1 classes
|
32 |
num_labels_task2=3 # Adjust with your task2 classes
|
33 |
)
|
|
|
34 |
model.eval()
|
35 |
|
36 |
# Define label mappings
|
|
|
51 |
probs_task1 = F.softmax(logits_task1, dim=1).cpu().numpy()[0] # Extract the first batch item
|
52 |
probs_task2 = F.softmax(logits_task2, dim=1).cpu().numpy()[0] # Extract the first batch item
|
53 |
|
54 |
+
# Predict label with highest probability
|
55 |
+
pred_task1 = label_mapping_task1[probs_task1.argmax()]
|
56 |
+
pred_task2 = label_mapping_task2[probs_task2.argmax()]
|
57 |
+
|
58 |
+
# Format probabilities as percentages
|
59 |
+
probs_task1_str = ", ".join([f"{label}: {prob*100:.2f}%" for label, prob in zip(label_mapping_task1, probs_task1)])
|
60 |
+
probs_task2_str = ", ".join([f"{label}: {prob*100:.2f}%" for label, prob in zip(label_mapping_task2, probs_task2)])
|
61 |
+
|
62 |
+
# Combine label predictions with their probabilities
|
63 |
+
result_task1 = f"{pred_task1} ({probs_task1_str})"
|
64 |
+
result_task2 = f"{pred_task2} ({probs_task2_str})"
|
65 |
|
66 |
return result_task1, result_task2
|
67 |
|
68 |
+
# Gradio Interface
|
69 |
+
iface = gr.Interface(fn=classify,
|
70 |
+
inputs="text",
|
71 |
+
outputs=[gr.Label(label="Fake Review Detection"),
|
72 |
+
gr.Label(label="Sentiment Classification")],
|
73 |
+
title="Multitask IndoBERT: Fake Review & Sentiment Classification",
|
74 |
+
description="Enter a skincare product review in Indonesian and the model will classify it as fake or trusted, and determine the sentiment.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
iface.launch()
|