Spaces:
No application file
No application file
Upload 4 files
Browse files- app.py +53 -0
- label_encoder.pkl +3 -0
- model_sentiment.pkl +3 -0
- vectorizer.pkl +3 -0
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pickle
|
| 4 |
+
import re
|
| 5 |
+
from nltk.tokenize import word_tokenize
|
| 6 |
+
from nltk.corpus import stopwords
|
| 7 |
+
import nltk
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
nltk.download('punkt')
|
| 11 |
+
nltk.download('stopwords')
|
| 12 |
+
|
| 13 |
+
# Load model dan tools
|
| 14 |
+
with open("model_sentiment.pkl", "rb") as f:
|
| 15 |
+
model = pickle.load(f)
|
| 16 |
+
|
| 17 |
+
with open("vectorizer.pkl", "rb") as f:
|
| 18 |
+
vectorizer = pickle.load(f)
|
| 19 |
+
|
| 20 |
+
with open("label_encoder.pkl", "rb") as f:
|
| 21 |
+
le = pickle.load(f)
|
| 22 |
+
|
| 23 |
+
stop_words = set(stopwords.words('indonesian'))
|
| 24 |
+
|
| 25 |
+
# Preprocessing function
|
| 26 |
+
def preprocess(text):
|
| 27 |
+
text = text.lower()
|
| 28 |
+
text = re.sub(r'[^a-zA-Z\s]', '', text)
|
| 29 |
+
tokens = word_tokenize(text)
|
| 30 |
+
tokens = [t for t in tokens if t not in stop_words]
|
| 31 |
+
return ' '.join(tokens)
|
| 32 |
+
|
| 33 |
+
# UI
|
| 34 |
+
st.title("π§ Sentiment Analysis Komentar Bahasa Indonesia")
|
| 35 |
+
st.markdown("Masukkan komentar di bawah ini:")
|
| 36 |
+
|
| 37 |
+
user_input = st.text_area("π¬ Komentar")
|
| 38 |
+
|
| 39 |
+
if st.button("Prediksi Sentimen"):
|
| 40 |
+
if user_input.strip() == "":
|
| 41 |
+
st.warning("Komentar tidak boleh kosong!")
|
| 42 |
+
else:
|
| 43 |
+
cleaned = preprocess(user_input)
|
| 44 |
+
vec = vectorizer.transform([cleaned])
|
| 45 |
+
pred = model.predict(vec)
|
| 46 |
+
label = le.inverse_transform(pred)[0]
|
| 47 |
+
|
| 48 |
+
if label == "positif":
|
| 49 |
+
st.success(f"Hasil: {label.capitalize()} π")
|
| 50 |
+
elif label == "negatif":
|
| 51 |
+
st.error(f"Hasil: {label.capitalize()} π")
|
| 52 |
+
else:
|
| 53 |
+
st.info(f"Hasil: {label.capitalize()} π")
|
label_encoder.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b982ce52466d824af64291d28cd1be7a5a88dfa3cc59936b87106f71057e0da5
|
| 3 |
+
size 271
|
model_sentiment.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ac424f45fc03607c49b71742c31eadf1169dfd9eeb92d2af84f7dbdbbbf8ac7e
|
| 3 |
+
size 53572
|
vectorizer.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:de3c9cf2898b6cee95d9041515b68b4427343bd921af825811ab5ca5b47a4695
|
| 3 |
+
size 30783
|