Spaces:
No application file
No application file
| # 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()} π") | |