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