Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
import torch
|
2 |
import torch.nn as nn
|
3 |
-
from transformers import AutoTokenizer, AutoModel
|
4 |
import gradio as gr
|
5 |
|
|
|
6 |
class MultiTaskModel(nn.Module):
|
7 |
def __init__(self, base_model_name, num_topic_classes, num_sentiment_classes):
|
8 |
super(MultiTaskModel, self).__init__()
|
@@ -22,15 +23,21 @@ class MultiTaskModel(nn.Module):
|
|
22 |
sentimen_logits = self.sentiment_classifier(pooled_output)
|
23 |
return topik_logits, sentimen_logits
|
24 |
|
|
|
25 |
tokenizer = AutoTokenizer.from_pretrained("tokenizer")
|
26 |
model = MultiTaskModel("indobenchmark/indobert-base-p1", num_topic_classes=5, num_sentiment_classes=3)
|
27 |
model.load_state_dict(torch.load("model.pt", map_location=torch.device("cpu")))
|
28 |
model.eval()
|
29 |
|
|
|
|
|
|
|
|
|
30 |
topik_labels = ["Produk", "Layanan", "Pengiriman", "Pembatalan", "Lainnya"]
|
31 |
sentimen_labels = ["Negatif", "Netral", "Positif"]
|
32 |
|
33 |
def klasifikasi(text):
|
|
|
34 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
35 |
with torch.no_grad():
|
36 |
topik_logits, sentimen_logits = model(**inputs)
|
@@ -39,7 +46,11 @@ def klasifikasi(text):
|
|
39 |
|
40 |
topik = topik_labels[topik_idx]
|
41 |
sentimen = sentimen_labels[sentimen_idx]
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
|
44 |
return f"HASIL ANALISIS\nTopik: {topik}\nSentimen: {sentimen}\nRingkasan: {ringkasan}"
|
45 |
|
|
|
1 |
import torch
|
2 |
import torch.nn as nn
|
3 |
+
from transformers import AutoTokenizer, AutoModel, AutoModelForSeq2SeqLM
|
4 |
import gradio as gr
|
5 |
|
6 |
+
# Model klasifikasi multitugas
|
7 |
class MultiTaskModel(nn.Module):
|
8 |
def __init__(self, base_model_name, num_topic_classes, num_sentiment_classes):
|
9 |
super(MultiTaskModel, self).__init__()
|
|
|
23 |
sentimen_logits = self.sentiment_classifier(pooled_output)
|
24 |
return topik_logits, sentimen_logits
|
25 |
|
26 |
+
# Load model klasifikasi
|
27 |
tokenizer = AutoTokenizer.from_pretrained("tokenizer")
|
28 |
model = MultiTaskModel("indobenchmark/indobert-base-p1", num_topic_classes=5, num_sentiment_classes=3)
|
29 |
model.load_state_dict(torch.load("model.pt", map_location=torch.device("cpu")))
|
30 |
model.eval()
|
31 |
|
32 |
+
# Load model summarization
|
33 |
+
sum_tokenizer = AutoTokenizer.from_pretrained("cahya/bart-base-indonesian-summarization")
|
34 |
+
sum_model = AutoModelForSeq2SeqLM.from_pretrained("cahya/bart-base-indonesian-summarization")
|
35 |
+
|
36 |
topik_labels = ["Produk", "Layanan", "Pengiriman", "Pembatalan", "Lainnya"]
|
37 |
sentimen_labels = ["Negatif", "Netral", "Positif"]
|
38 |
|
39 |
def klasifikasi(text):
|
40 |
+
# Klasifikasi
|
41 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
42 |
with torch.no_grad():
|
43 |
topik_logits, sentimen_logits = model(**inputs)
|
|
|
46 |
|
47 |
topik = topik_labels[topik_idx]
|
48 |
sentimen = sentimen_labels[sentimen_idx]
|
49 |
+
|
50 |
+
# Ringkasan
|
51 |
+
sum_inputs = sum_tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
|
52 |
+
summary_ids = sum_model.generate(**sum_inputs, max_length=40, min_length=10, do_sample=False)
|
53 |
+
ringkasan = sum_tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
54 |
|
55 |
return f"HASIL ANALISIS\nTopik: {topik}\nSentimen: {sentimen}\nRingkasan: {ringkasan}"
|
56 |
|