abdullahzunorain commited on
Commit
ee04ce9
·
verified ·
1 Parent(s): 5940fce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -36
app.py CHANGED
@@ -1,41 +1,47 @@
1
  # app.py
2
 
3
  import torch
4
- import streamlit as st
5
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
6
 
7
- # ---- Load model and tokenizer ----
8
- @st.cache_resource
9
- def load_model():
10
- model_name = "taufiqdp/indonesian-sentiment"
11
- tokenizer = AutoTokenizer.from_pretrained(model_name)
12
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
13
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
- model.to(device)
15
- return tokenizer, model, device
16
-
17
- tokenizer, model, device = load_model()
18
-
19
- # ---- Streamlit App ----
20
- st.set_page_config(page_title="Indonesian Sentiment Analyzer", page_icon="🇮🇩", layout="centered")
21
-
22
- st.title("🇮🇩 Indonesian Sentiment Analyzer")
23
- st.markdown("### Analyze the sentiment of any Indonesian text — Positive, Neutral, or Negative.")
24
-
25
- text_input = st.text_area("Masukkan teks Bahasa Indonesia:", placeholder="Contoh: Pelayanan lama dan tidak ramah", height=120)
26
-
27
- if st.button("Analyze Sentiment"):
28
- if not text_input.strip():
29
- st.warning("⚠️ Mohon masukkan teks terlebih dahulu.")
30
- else:
31
- with st.spinner("Menganalisis..."):
32
- inputs = tokenizer(text_input, return_tensors='pt', truncation=True, padding=True).to(device)
33
- with torch.inference_mode():
34
- logits = model(**inputs).logits
35
- predicted_class = torch.argmax(logits, dim=1).item()
36
-
37
- labels = ['Negatif', 'Netral', 'Positif']
38
- sentiment = labels[predicted_class]
39
-
40
- colors = {"Negatif": "red", "Netral": "orange", "Positif": "green"}
41
- st.markdown(f"### 🔍 **Sentimen:** <span style='color:{colors[sentiment]};'>{sentiment}</span>", unsafe_allow_html=True)
 
 
 
 
 
 
 
1
  # app.py
2
 
3
  import torch
4
+ import gradio as gr
5
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
6
 
7
+ # --- Load model and tokenizer ---
8
+ model_name = "taufiqdp/indonesian-sentiment"
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
11
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
+ model.to(device)
13
+
14
+ # --- Prediction function ---
15
+ def predict_sentiment(text):
16
+ if not text.strip():
17
+ return "⚠️ Mohon masukkan teks."
18
+
19
+ inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True).to(device)
20
+ with torch.inference_mode():
21
+ logits = model(**inputs).logits
22
+ pred_class = torch.argmax(logits, dim=1).item()
23
+
24
+ labels = ['Negatif', 'Netral', 'Positif']
25
+ sentiment = labels[pred_class]
26
+ return f"**Hasil Analisis Sentimen:** {sentiment}"
27
+
28
+ # --- Build Gradio UI ---
29
+ title = "🇮🇩 Indonesian Sentiment Analyzer"
30
+ description = "Masukkan teks Bahasa Indonesia untuk mengetahui apakah sentimennya **Positif**, **Netral**, atau **Negatif**."
31
+
32
+ interface = gr.Interface(
33
+ fn=predict_sentiment,
34
+ inputs=gr.Textbox(lines=4, placeholder="Contoh: Pelayanan cepat dan memuaskan"),
35
+ outputs="markdown",
36
+ title=title,
37
+ description=description,
38
+ theme="gradio/soft",
39
+ examples=[
40
+ ["Makanan di restoran ini sangat enak dan lezat."],
41
+ ["Pelayanan biasa saja, tidak buruk tapi tidak istimewa."],
42
+ ["Saya kecewa dengan produk ini."]
43
+ ]
44
+ )
45
+
46
+ if __name__ == "__main__":
47
+ interface.launch()