sitijahrona commited on
Commit
56b62de
·
verified ·
1 Parent(s): 028d15b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+
5
+ # Load model dan tokenizer dari Hugging Face
6
+ model = AutoModelForSequenceClassification.from_pretrained("kelompokjavonlp/sentiment_analysis")
7
+ tokenizer = AutoTokenizer.from_pretrained("kelompokjavonlp/sentiment_analysis")
8
+
9
+ # Update label sesuai model kamu
10
+ labels = ["Very Negative", "Negative", "Neutral", "Positive", "Very Positive"]
11
+
12
+ # Fungsi prediksi
13
+ def predict_sentiment(text):
14
+ inputs = tokenizer(text, return_tensors="pt")
15
+ with torch.no_grad():
16
+ outputs = model(**inputs)
17
+ probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
18
+ pred = torch.argmax(probs).item()
19
+ return {labels[i]: float(probs[0][i]) for i in range(len(labels))}
20
+
21
+ # Interface Gradio
22
+ iface = gr.Interface(
23
+ fn=predict_sentiment,
24
+ inputs=gr.Textbox(label="Masukkan Kalimat"),
25
+ outputs=gr.Label(label="Hasil Sentimen"),
26
+ title="Demo Analisis Sentimen Bahasa Indonesia",
27
+ description="Model klasifikasi sentimen: Very Negative, Negative, Neutral, Positive, Very Positive."
28
+ )
29
+
30
+ iface.launch()