|
import torch |
|
import torch.nn as nn |
|
from transformers import AutoTokenizer, AutoModel |
|
import gradio as gr |
|
|
|
|
|
class MultiTaskModel(nn.Module): |
|
def __init__(self, base_model_name, num_topic_classes, num_sentiment_classes): |
|
super(MultiTaskModel, self).__init__() |
|
self.encoder = AutoModel.from_pretrained(base_model_name) |
|
hidden_size = self.encoder.config.hidden_size |
|
self.topic_classifier = nn.Linear(hidden_size, num_topic_classes) |
|
self.sentiment_classifier = nn.Linear(hidden_size, num_sentiment_classes) |
|
|
|
def forward(self, input_ids, attention_mask): |
|
outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask) |
|
pooled_output = outputs.last_hidden_state[:, 0] |
|
topic_logits = self.topic_classifier(pooled_output) |
|
sentiment_logits = self.sentiment_classifier(pooled_output) |
|
return topic_logits, sentiment_logits |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("tokenizer") |
|
model = MultiTaskModel("indobenchmark/indobert-base-p1", num_topic_classes=4, num_sentiment_classes=3) |
|
model.load_state_dict(torch.load("model.pt", map_location=torch.device("cpu"))) |
|
model.eval() |
|
|
|
|
|
topic_labels = ["Produk", "Layanan", "Pengiriman", "Lainnya"] |
|
sentiment_labels = ["Negatif", "Netral", "Positif"] |
|
|
|
|
|
def klasifikasi(text): |
|
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True) |
|
with torch.no_grad(): |
|
topic_logits, sentiment_logits = model(**inputs) |
|
topic_probs = torch.softmax(topic_logits, dim=-1).squeeze() |
|
sentiment_probs = torch.softmax(sentiment_logits, dim=-1).squeeze() |
|
|
|
topic_result = {label: float(prob) for label, prob in zip(topic_labels, topic_probs)} |
|
sentiment_result = {label: float(prob) for label, prob in zip(sentiment_labels, sentiment_probs)} |
|
return {"Topik": topic_result, "Sentimen": sentiment_result} |
|
|
|
|
|
demo = gr.Interface(fn=klasifikasi, inputs="text", outputs="json", title="Klasifikasi Topik dan Sentimen Pelanggan") |
|
demo.launch() |
|
|