naufalnashif commited on
Commit
0b0a15c
·
1 Parent(s): 7e56286

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -64
app.py CHANGED
@@ -110,6 +110,7 @@ tfidf_vectorizer = joblib.load(tfidf_model_path)
110
  #---------------------------------------------------Milih Model----------------------------------------------------------------------
111
 
112
  # Fungsi untuk memilih model berdasarkan pilihan pengguna
 
113
  def select_sentiment_model(selected_model):
114
  if selected_model == "Ensemble":
115
  model_path = 'ensemble_clf_soft_smote.joblib'
@@ -135,10 +136,10 @@ def predict_sentiment(text, _model, _tfidf_vectorizer, slang_dict):
135
  norm_slang_text = normalize_slang(cleaned_text, slang_dict)
136
 
137
  # Tahap-2: Ekstraksi fitur TF-IDF
138
- tfidf_matrix = tfidf_vectorizer.transform([norm_slang_text])
139
 
140
  # Tahap-3: Lakukan prediksi sentimen
141
- sentiment = model.predict(tfidf_matrix)
142
 
143
  # Tahap-4: Menggantikan indeks dengan label sentimen
144
  labels = {0: "Negatif", 1: "Netral", 2: "Positif"}
@@ -275,71 +276,73 @@ with st.expander ("Preference Settings :"):
275
  # Analisis sentimen
276
  results = []
277
  analisis = False
278
- if st.button("Analysis") and input_option == "Teks Manual" and user_input:
279
- # Pisahkan teks yang dimasukkan pengguna menjadi baris-baris terpisah
280
- user_texts = user_input.split('\n')
281
- for text in user_texts:
282
- sentiment_label = predict_sentiment(text, sentiment_model, tfidf_vectorizer, lookp_dict)
283
- emoticon = get_emoticon(sentiment_label)
284
- cleaned_text = clean_text(text)
285
- norm_slang_text = normalize_slang(cleaned_text, lookp_dict)
286
- tanpa_stopwords = remove_stopwords(norm_slang_text, stop_words)
287
-
288
- results.append({
289
- 'Text': text,
290
- 'cleaned-text' : cleaned_text,
291
- 'normalisasi-text' : norm_slang_text,
292
- 'stopwords-remove' : tanpa_stopwords,
293
- 'label' : sentiment_label,
294
- 'emotikon' : emoticon,
295
- })
296
- analisis = True
 
297
 
298
  elif input_option == "Unggah Berkas XLSX" and uploaded_file is not None:
299
- if 'Text' in df.columns:
300
- if 'Date' in df.columns:
301
- for text, date in zip(texts, df['Date']):
302
- sentiment_label = predict_sentiment(text, sentiment_model, tfidf_vectorizer, lookp_dict)
303
- emoticon = get_emoticon(sentiment_label)
304
- cleaned_text = clean_text(text)
305
- norm_slang_text = normalize_slang(cleaned_text, lookp_dict)
306
- tanpa_stopwords = remove_stopwords(norm_slang_text, stop_words)
307
-
308
- result_entry = {
309
- 'Text': text,
310
- 'cleaned-text': cleaned_text,
311
- 'normalisasi-text': norm_slang_text,
312
- 'stopwords-remove': tanpa_stopwords,
313
- 'label': sentiment_label,
314
- 'emotikon': emoticon,
315
- 'Date': date,
316
- }
317
-
318
- results.append(result_entry)
319
-
320
- analisis = True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321
  else:
322
- for text in texts:
323
- sentiment_label = predict_sentiment(text, sentiment_model, tfidf_vectorizer, lookp_dict)
324
- emoticon = get_emoticon(sentiment_label)
325
- cleaned_text = clean_text(text)
326
- norm_slang_text = normalize_slang(cleaned_text, lookp_dict)
327
- tanpa_stopwords = remove_stopwords(norm_slang_text, stop_words)
328
-
329
- result_entry = {
330
- 'Text': text,
331
- 'cleaned-text': cleaned_text,
332
- 'normalisasi-text': norm_slang_text,
333
- 'stopwords-remove': tanpa_stopwords,
334
- 'label': sentiment_label,
335
- 'emotikon': emoticon,
336
- }
337
-
338
- results.append(result_entry)
339
-
340
- analisis = True
341
- else:
342
- st.warning("Berkas XLSX harus memiliki kolom bernama 'Text' untuk analisis sentimen.")
343
 
344
 
345
  st.info('Tekan "Analysis" kembali jika tampilan menghilang', icon = 'ℹ️')
 
110
  #---------------------------------------------------Milih Model----------------------------------------------------------------------
111
 
112
  # Fungsi untuk memilih model berdasarkan pilihan pengguna
113
+ @st.cache_data
114
  def select_sentiment_model(selected_model):
115
  if selected_model == "Ensemble":
116
  model_path = 'ensemble_clf_soft_smote.joblib'
 
136
  norm_slang_text = normalize_slang(cleaned_text, slang_dict)
137
 
138
  # Tahap-2: Ekstraksi fitur TF-IDF
139
+ tfidf_matrix = _tfidf_vectorizer.transform([norm_slang_text])
140
 
141
  # Tahap-3: Lakukan prediksi sentimen
142
+ sentiment = _model.predict(tfidf_matrix)
143
 
144
  # Tahap-4: Menggantikan indeks dengan label sentimen
145
  labels = {0: "Negatif", 1: "Netral", 2: "Positif"}
 
276
  # Analisis sentimen
277
  results = []
278
  analisis = False
279
+ if input_option == "Teks Manual" and user_input:
280
+ if st.button("Analysis"):
281
+ # Pisahkan teks yang dimasukkan pengguna menjadi baris-baris terpisah
282
+ user_texts = user_input.split('\n')
283
+ for text in user_texts:
284
+ sentiment_label = predict_sentiment(text, sentiment_model, tfidf_vectorizer, lookp_dict)
285
+ emoticon = get_emoticon(sentiment_label)
286
+ cleaned_text = clean_text(text)
287
+ norm_slang_text = normalize_slang(cleaned_text, lookp_dict)
288
+ tanpa_stopwords = remove_stopwords(norm_slang_text, stop_words)
289
+
290
+ results.append({
291
+ 'Text': text,
292
+ 'cleaned-text' : cleaned_text,
293
+ 'normalisasi-text' : norm_slang_text,
294
+ 'stopwords-remove' : tanpa_stopwords,
295
+ 'label' : sentiment_label,
296
+ 'emotikon' : emoticon,
297
+ })
298
+ analisis = True
299
 
300
  elif input_option == "Unggah Berkas XLSX" and uploaded_file is not None:
301
+ if st.button("Analysis"):
302
+ if 'Text' in df.columns:
303
+ if 'Date' in df.columns:
304
+ for text, date in zip(texts, df['Date']):
305
+ sentiment_label = predict_sentiment(text, sentiment_model, tfidf_vectorizer, lookp_dict)
306
+ emoticon = get_emoticon(sentiment_label)
307
+ cleaned_text = clean_text(text)
308
+ norm_slang_text = normalize_slang(cleaned_text, lookp_dict)
309
+ tanpa_stopwords = remove_stopwords(norm_slang_text, stop_words)
310
+
311
+ result_entry = {
312
+ 'Text': text,
313
+ 'cleaned-text': cleaned_text,
314
+ 'normalisasi-text': norm_slang_text,
315
+ 'stopwords-remove': tanpa_stopwords,
316
+ 'label': sentiment_label,
317
+ 'emotikon': emoticon,
318
+ 'Date': date,
319
+ }
320
+
321
+ results.append(result_entry)
322
+
323
+ analisis = True
324
+ else:
325
+ for text in texts:
326
+ sentiment_label = predict_sentiment(text, sentiment_model, tfidf_vectorizer, lookp_dict)
327
+ emoticon = get_emoticon(sentiment_label)
328
+ cleaned_text = clean_text(text)
329
+ norm_slang_text = normalize_slang(cleaned_text, lookp_dict)
330
+ tanpa_stopwords = remove_stopwords(norm_slang_text, stop_words)
331
+
332
+ result_entry = {
333
+ 'Text': text,
334
+ 'cleaned-text': cleaned_text,
335
+ 'normalisasi-text': norm_slang_text,
336
+ 'stopwords-remove': tanpa_stopwords,
337
+ 'label': sentiment_label,
338
+ 'emotikon': emoticon,
339
+ }
340
+
341
+ results.append(result_entry)
342
+
343
+ analisis = True
344
  else:
345
+ st.warning("Berkas XLSX harus memiliki kolom bernama 'Text' untuk analisis sentimen.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
 
347
 
348
  st.info('Tekan "Analysis" kembali jika tampilan menghilang', icon = 'ℹ️')