intanrly commited on
Commit
c34bf2c
·
verified ·
1 Parent(s): 42ae44f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
+
5
+ # Load model sekali saja
6
+ MODEL_NAME = "taufiqdp/indonesian-sentiment"
7
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
8
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
9
+
10
+ class_names = ['negatif', 'netral', 'positif']
11
+
12
+ def predict_sentiment(text):
13
+ if not text or text.strip() == "":
14
+ return "Teks kosong"
15
+
16
+ tokenized = tokenizer(text, return_tensors="pt")
17
+ with torch.inference_mode():
18
+ logits = model(**tokenized).logits
19
+
20
+ pred_id = logits.argmax(dim=1).item()
21
+ sentiment = class_names[pred_id]
22
+ confidence = torch.softmax(logits, dim=1)[0][pred_id].item()
23
+
24
+ return f"{sentiment} ({confidence:.2%})"
25
+
26
+ # Gradio interface
27
+ demo = gr.Interface(
28
+ fn=predict_sentiment,
29
+ inputs=gr.Textbox(label="Masukkan teks"),
30
+ outputs=gr.Textbox(label="Prediksi Sentimen"),
31
+ title="Indonesian Sentiment Analysis",
32
+ description="Model klasifikasi sentimen bahasa Indonesia (negatif, netral, positif)."
33
+ )
34
+
35
+ if __name__ == "__main__":
36
+ demo.launch()