-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
285 lines (228 loc) Β· 12.3 KB
/
app.py
File metadata and controls
285 lines (228 loc) Β· 12.3 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""
Project: Review Sentiment Analyzer
Author: Silvio Christian, Joe
Description:
This Streamlit application performs sentiment analysis on English and Indonesian text.
It leverages Hugging Face Transformer models (RoBERTa) for high-accuracy classification.
Features include batch processing (CSV/Excel), interactive visualization (Plotly),
and N-gram analysis for insight extraction.
"""
import pandas as pd
import numpy as np
import streamlit as st
import streamlit.components.v1 as components
from transformers import pipeline
import plotly.express as px
from utils import get_top_n_words_en, get_top_n_words_id, convert_for_download
from lime.lime_text import LimeTextExplainer
import torch
import re, time
# ==========================================
# 1. Language Selection & Session Management
# ==========================================
bahasa = st.selectbox("Choose Language: ", ["English", "Indonesia"])
# Initialize session state to track language changes
if "last_lang" not in st.session_state:
st.session_state.last_lang = bahasa
# Detect if language has changed. If yes, clear memory/cache and reload UI.
if bahasa != st.session_state.last_lang:
st.session_state.clear()
st.session_state.bahasa = bahasa
st.rerun()
# ==========================================
# 2. Main UI Layout (Tabs)
# ==========================================
tab_file, tab_teks = st.tabs(["File", "Text"])
# ==========================================
# 3. Logic: English Analysis
# ==========================================
if bahasa == "English":
# Load Pre-trained Model: RoBERTa optimized for Sentiment Analysis
nlp = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
with tab_file:
# File Uploader Widget
file_table_en = st.file_uploader("Please upload yoru data here!", type=["csv", "xlsx"], key=f"data {bahasa}")
if file_table_en is not None:
# Data Loading: Handle both CSV and Excel formats
if file_table_en.name.endswith(".csv"):
df = pd.read_csv(file_table_en)
elif file_table_en.name.endswith(".xlsx"):
df = pd.read_excel(file_table_en)
st.toast("π Data successfully loaded. Preparing for processing...")
st.write("π Preview Data:")
st.dataframe(df.head())
# Batch Inference: Apply model to the entire column if not already processed
if "Sentiment" not in df.columns:
df['Sentiment'] = df['komentar'].apply(lambda x: nlp(x)[0]["label"])
if "Confidence" not in df.columns:
df['Confidence'] = df['komentar'].apply(lambda x: nlp(x)[0]["score"])
st.write("π Result:")
st.dataframe(df.head())
# Prepare CSV for Download
csv = convert_for_download(df)
st.download_button(
label="Download CSV",
data=csv,
file_name="sentiment_english.csv",
mime="text/csv",
icon=":material/download:",
)
# Visualization: Sentiment Distribution Pie Chart
data = df["Sentiment"].value_counts()
fig = px.pie(data, names=data.index, values=data.values, title="How Many?")
st.plotly_chart(fig, use_container_width=True)
# N-Gram Analysis (Word Frequency)
num = st.number_input("How Many K?", min_value = 1, max_value=10, value=5)
sentiment = st.selectbox("Choose sentiment: ", ["positive", "negative", "neutral"])
ngram = st.slider("Select a range of values", 1, 3, (1, 1))
# Helper function call to extract top keywords
result = get_top_n_words_en(corpus = df[df["Sentiment"] == sentiment]["komentar"], n=num, ngram_range=ngram)
result_df = pd.DataFrame(result, columns=["Word", "Jumlah"])
st.dataframe(result_df)
st.bar_chart(result_df, x="Word", y="Jumlah", x_label="Kata", y_label="Banyaknya kemunculan")
# Feature Engineering: Analyze Text Complexity (Sentence & Word Length)
df['Panjang Kalimat'] = df["komentar"].apply(lambda x: len([x for x in re.split(r'[.!?]+', x) if x.strip()]))
df['Panjang Kata'] = df["komentar"].apply(lambda x: len(x.split()))
# Statistical Aggregation
data_kalimat = df.groupby("Sentiment")["Panjang Kalimat"].mean().sort_values().reset_index()
st.dataframe(data_kalimat)
data_kata = df.groupby("Sentiment")["Panjang Kata"].mean().sort_values().reset_index()
st.dataframe(data_kata)
st.toast("π Data processing completed successfully.")
else:
st.write("β οΈ Belum ada file yang di-upload")
with tab_teks:
text = st.text_area("Write Your Sentiment Here")
if st.button("Send"):
if text:
# Perform Single Inference
sentiment = nlp(text)[0]["label"]
conf = nlp(text)[0]["score"]
st.info(f"""
**Sentiment:** {sentiment}
**Confidence:** {conf:.2f}
""")
def predict_function(texts):
if isinstance(texts, np.ndarray):
texts = texts.tolist()
if isinstance(texts, str):
texts = [texts]
validated_text = []
for text in texts:
if not text or text == "":
validated_text.append(".")
else:
validated_text.append(text)
predictions = nlp(validated_text, top_k=None)
scores = []
for prediction in predictions:
sorted_pred = sorted(prediction, key=lambda x: x['label'])
items = [item['score'] for item in sorted_pred]
scores.append(items)
return np.array(scores)
with st.spinner("Analyzing with LIME..."):
explainer = LimeTextExplainer(class_names=['negative', 'neutral', 'positive'])
exp = explainer.explain_instance(
text_instance=text,
classifier_fn=predict_function,
num_features=5 # Top 5 Words
)
html_data = exp.as_html()
components.html(html_data, height=800, scrolling=True)
# ==========================================
# 4. Logic: Indonesian Analysis
# ==========================================
elif bahasa == "Indonesia":
# Load Pre-trained Model: Indo-RoBERTa for Indonesian Sentiment
nlp = pipeline("sentiment-analysis", model="w11wo/indonesian-roberta-base-sentiment-classifier")
with tab_file:
# File Uploader for Indonesian Data
file_table_id = st.file_uploader("Please upload yoru data here!", type=["csv", "xlsx"], key=f"data {bahasa}")
if file_table_id is not None:
if file_table_id.name.endswith(".csv"):
df = pd.read_csv(file_table_id)
elif file_table_id.name.endswith(".xlsx"):
df = pd.read_excel(file_table_id)
st.toast("π Data successfully loaded. Preparing for processing...")
st.write("π Preview Data:")
st.dataframe(df.head())
# Apply Inference Row-by-Row
if "Sentiment" not in df.columns:
df['Sentiment'] = df['komentar'].apply(lambda x: nlp(x)[0]["label"])
if "Confidence" not in df.columns:
df['Confidence'] = df['komentar'].apply(lambda x: nlp(x)[0]["score"])
st.write("π Result:")
st.dataframe(df.head())
# Prepare Download
csv = convert_for_download(df)
st.download_button(
label="Download CSV",
data=csv,
file_name="sentiment_indo.csv",
mime="text/csv",
icon=":material/download:",
)
# Visualization: Pie Chart
data = df["Sentiment"].value_counts()
fig = px.pie(data, names=data.index, values=data.values, title="How Many?")
st.plotly_chart(fig, use_container_width=True)
# N-Gram Analysis Configuration
num = st.number_input("How Many K?", min_value = 1, max_value=10, value=5)
sentiment = st.selectbox("Choose sentiment: ", ["positive", "negative", "neutral"])
ngram = st.slider("Select a range of values", 1, 3, (1, 1))
# Extract Top Words (Indonesian Helper Function)
result = get_top_n_words_id(corpus = df[df["Sentiment"] == sentiment]["komentar"], n=num, ngram_range=ngram)
result_df = pd.DataFrame(result, columns=["Word", "Jumlah"])
st.dataframe(result_df)
st.bar_chart(result_df, x="Word", y="Jumlah", x_label="Kata", y_label="Banyaknya kemunculan")
# Feature Engineering: Text Statistics
df['Panjang Kalimat'] = df["komentar"].apply(lambda x: len([x for x in re.split(r'[.!?]+', x) if x.strip()]))
df['Panjang Kata'] = df["komentar"].apply(lambda x: len(x.split()))
data_kalimat = df.groupby("Sentiment")["Panjang Kalimat"].mean().sort_values().reset_index()
st.dataframe(data_kalimat)
data_kata = df.groupby("Sentiment")["Panjang Kata"].mean().sort_values().reset_index()
st.dataframe(data_kata)
st.toast("π Data processing completed successfully.")
else:
st.write("β οΈ Belum ada file yang di-upload")
with tab_teks:
# Chat Interface for Indonesian
if "messages_indo" not in st.session_state:
st.session_state.messages_indo = []
text = st.text_area("Write Your Sentiment Here")
if st.button("Send"):
if text:
# Perform Single Inference
sentiment = nlp(text)[0]["label"]
conf = nlp(text)[0]["score"]
st.info(f"""
**Sentiment:** {sentiment}
**Confidence:** {conf:.2f}
""")
def predict_function(text):
if isinstance(texts, np.ndarray):
texts = texts.tolist()
if isinstance(texts, str):
texts = [texts]
validated_text = []
for text in texts:
if not text or text == "":
validated_text.append(".")
else:
validated_text.append(text)
predictions = nlp(validated_text, top_k=None)
scores = []
for prediction in predictions:
sorted_pred = sorted(prediction, key=lambda x: x['label'])
items = [item['score'] for item in sorted_pred]
scores.append(items)
return np.array(scores)
with st.spinner("Analyzing with LIME..."):
explainer = LimeTextExplainer(class_names=['negative', 'neutral', 'positive'])
exp = explainer.explain_instance(
text_instance=text,
classifier_fn=predict_function,
num_features=5 # Top 5 Words
)
html_data = exp.as_html()
components.html(html_data, height=800, scrolling=True)