File size: 11,451 Bytes
d1d306c
c1bd4b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd46b3c
c1bd4b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1d306c
c1bd4b3
 
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import streamlit as st
import time
import re
import numpy as np
import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModelForSequenceClassification

# ==========================================
# βš™οΈ 1. PAGE SETUP
# ==========================================
st.set_page_config(page_title="Spotify ABSA Analyzer", page_icon="🎡", layout="wide")

# Custom CSS for clear visualization
st.markdown(
    """
    <style>
    .main { background-color: #f8f9fa; }
    
    /* Segment Box Styling */
    .segment-box {
        background-color: white;
        padding: 20px;
        border-radius: 10px;
        box-shadow: 0 2px 5px rgba(0,0,0,0.05);
        margin-bottom: 15px;
        border-left: 6px solid #ccc;
    }
    
    /* Text inside the box */
    .segment-text {
        font-size: 1.15em;
        font-family: sans-serif;
        color: #212529;
        margin-bottom: 12px;
    }

    /* Sentiment Badges */
    .badge-pos {
        background-color: #d4edda;
        color: #155724;
        padding: 4px 8px;
        border-radius: 4px;
        font-weight: bold;
        font-size: 0.85em;
        border: 1px solid #c3e6cb;
    }
    .badge-neg {
        background-color: #f8d7da;
        color: #721c24;
        padding: 4px 8px;
        border-radius: 4px;
        font-weight: bold;
        font-size: 0.85em;
        border: 1px solid #f5c6cb;
    }

    /* Aspect Trigger Badges */
    .trigger-badge {
        display: inline-block;
        background-color: #e2e6ea;
        color: #495057;
        padding: 4px 10px;
        border-radius: 15px;
        font-size: 0.85em;
        margin-right: 8px;
        margin-bottom: 5px;
        border: 1px solid #ced4da;
    }
    .trigger-word {
        background-color: #fff3cd;
        padding: 0 3px;
        border-radius: 3px;
        font-weight: bold;
        border-bottom: 1px solid #ffeeba;
    }

    /* Border Colors */
    .border-pos { border-left-color: #28a745 !important; }
    .border-neg { border-left-color: #dc3545 !important; }
    </style>
""",
    unsafe_allow_html=True,
)

# ==========================================
# 🧠 2. ASPECT DICTIONARY (Standard Structure)
# ==========================================

ASPECT_KEYWORDS = {
    "en": {
        "Audio Quality": [
            "audio",
            "sound",
            "bass",
            "treble",
            "voice",
            "music quality",
            "volume",
            "noise",
            "hifi",
            "dolby",
        ],
        "Price & Premium": [
            "price",
            "cost",
            "premium",
            "subscription",
            "expensive",
            "cheap",
            "worth",
            "pay",
            "money",
            "billing",
        ],
        "Ads (Iklan)": [
            "ads",
            "advertisement",
            "commercial",
            "interrupt",
            "sponsor",
            "unskippable",
        ],
        "App Stability": [
            "crash",
            "bug",
            "error",
            "slow",
            "loading",
            "lag",
            "force close",
            "glitch",
            "stuck",
            "freeze",
        ],
        "Content/Library": [
            "song",
            "playlist",
            "library",
            "genre",
            "podcast",
            "lyrics",
            "collection",
            "track",
            "album",
        ],
    },
    "id": {
        "Audio Quality": [
            "suara",
            "audio",
            "bass",
            "bunyi",
            "kualitas",
            "jernih",
            "cempreng",
            "kresek",
            "volume",
            "vokal",
            "dolby",
        ],
        "Price & Premium": [
            "harga",
            "bayar",
            "mahal",
            "murah",
            "premium",
            "langganan",
            "boros",
            "tagihan",
            "uang",
            "beli",
            "berbayar",
        ],
        "Ads (Iklan)": ["iklan", "ads", "promosi", "tonton", "komersial", "ganggu"],
        "App Stability": [
            "crash",
            "bug",
            "error",
            "lemot",
            "keluar sendiri",
            "macet",
            "lag",
            "lelet",
            "berat",
            "rusak",
            "gagal",
            "force close",
        ],
        "Content/Library": [
            "lagu",
            "musik",
            "playlist",
            "koleksi",
            "podcast",
            "lirik",
            "genre",
            "album",
            "artis",
            "katalog",
        ],
    },
}


def get_aspects_detailed(text, lang="en"):
    """
    Scans text for keywords.
    Returns list: [{'aspect': 'Audio', 'trigger': 'bass'}, ...]
    """
    found_details = []
    text_lower = text.lower()
    keywords = ASPECT_KEYWORDS.get(lang, ASPECT_KEYWORDS["en"])

    for aspect_category, keyword_list in keywords.items():
        for key in keyword_list:
            # Word boundary check (\b) to avoid partial matches
            if re.search(r"\b" + re.escape(key) + r"\b", text_lower):
                found_details.append({"aspect": aspect_category, "trigger": key})

    return found_details


# ==========================================
# πŸ› οΈ 3. MODEL LOADER
# ==========================================


@st.cache_resource
def load_model_safe(lang_code):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    if lang_code == "en":
        local_path = "Hamusssss12/spotify-absa-english"
        fallback_repo = "nlptown/bert-base-multilingual-uncased-sentiment"
    else:
        local_path = "Hamusssss12/spotify-absa-indonesian"
        fallback_repo = "indobenchmark/indobert-base-p1"

    try:
        # Try loading local fine-tuned model
        tokenizer = AutoTokenizer.from_pretrained(local_path)
        model = AutoModelForSequenceClassification.from_pretrained(local_path).to(
            device
        )
        msg = "Status: Using Local Fine-Tuned Model"
        is_custom = True
    except:
        # Fallback to download from HuggingFace
        tokenizer = AutoTokenizer.from_pretrained(fallback_repo)
        model = AutoModelForSequenceClassification.from_pretrained(fallback_repo).to(
            device
        )
        msg = f"Status: Using Generic Base Model ({fallback_repo})"
        is_custom = False

    return model, tokenizer, device, msg, is_custom


def predict_sentiment(text, model, tokenizer, device):
    inputs = tokenizer(
        text, return_tensors="pt", truncation=True, padding=True, max_length=128
    ).to(device)
    with torch.no_grad():
        logits = model(**inputs).logits

        # Binary Classification
        if logits.shape[1] == 2:
            probs = F.softmax(logits, dim=1).cpu().numpy()[0]
            score = probs[1]  # Probability of Positive
        # Multiclass Fallback (1-5 stars)
        else:
            probs = F.softmax(logits, dim=1).cpu().numpy()[0]
            score = np.sum(probs * np.array([0, 0.25, 0.5, 0.75, 1.0]))

    return score


# ==========================================
# πŸ–₯️ 4. APP UI
# ==========================================


def main():
    st.title("🎡 Spotify Review Inspector")
    st.markdown("Analyze reviews to identify sentiment and aspect triggers.")

    # --- SIDEBAR ---
    with st.sidebar:
        st.header("βš™οΈ Configuration")
        lang = st.selectbox("Select Language", ["Indonesian", "English"], index=0)
        lang_code = "id" if lang == "Indonesian" else "en"

        st.divider()

        # Load Model
        with st.spinner("Initializing AI Engine..."):
            model, tokenizer, device, msg, is_custom = load_model_safe(lang_code)

        if is_custom:
            st.success(msg)
        else:
            st.warning(msg)
            st.caption(
                "Tip: Ensure your `models` folder contains the extracted zip files for best results."
            )

        st.divider()

        # DEBUG SECTION
        with st.expander("πŸ“– View Dictionary (Debug)"):
            st.write(f"**Current Dictionary ({lang_code.upper()}):**")
            st.json(ASPECT_KEYWORDS[lang_code])

    # --- MAIN INPUT ---
    default_text = (
        "Suaranya jernih banget enak didenger, tapi sayang harga premiumnya kemahalan buat pelajar."
        if lang_code == "id"
        else "The audio is crystal clear, but the premium price is too expensive."
    )

    user_input = st.text_area("Enter Review Text:", value=default_text, height=100)

    if st.button("Analyze Sentiment", type="primary"):
        st.markdown("### πŸ“Š Analysis Results")

        # 1. Segmentation
        # Split logic: punctuation or contrast words (but, however, tapi, namun)
        if lang_code == "id":
            split_regex = r"[.!?;]|\btapi\b|\bnamun\b|\bsedangkan\b"
        else:
            split_regex = r"[.!?;]|\bbut\b|\bhowever\b|\bwhile\b"

        raw_segments = re.split(split_regex, user_input)
        segments = [s.strip() for s in raw_segments if s.strip()]
        if not segments:
            segments = [user_input]

        # 2. Process & Render
        for i, segment in enumerate(segments):
            # Predict
            score = predict_sentiment(segment, model, tokenizer, device)
            is_positive = score > 0.55

            # Formatting
            sentiment_label = "POSITIVE" if is_positive else "NEGATIVE"
            border_class = "border-pos" if is_positive else "border-neg"
            badge_class = "badge-pos" if is_positive else "badge-neg"

            # Find Aspect Triggers
            details = get_aspects_detailed(segment, lang_code)

            # --- RENDER CARD ---
            st.markdown(
                f"""
            <div class="segment-box {border_class}">
                <div style="display:flex; align-items:center; margin-bottom:8px;">
                    <strong style="color:#888; margin-right:10px;">Segment {i+1}</strong>
                    <span class="{badge_class}">
                        {sentiment_label} ({score:.1%})
                    </span>
                </div>
                <div class="segment-text">"{segment}"</div>
            """,
                unsafe_allow_html=True,
            )

            # --- RENDER TRIGGERS ---
            if details:
                cols = (
                    st.columns(len(details)) if len(details) > 0 else [st.container()]
                )
                badges_html = ""
                for det in details:
                    badges_html += f"""
                    <div class="trigger-badge">
                        <span>🏷️ {det['aspect']}</span>
                        <span style="font-size:0.8em; color:#666; margin-left:5px;">
                            (trigger: <span class="trigger-word">{det['trigger']}</span>)
                        </span>
                    </div>
                    """
                st.markdown(f"<div>{badges_html}</div>", unsafe_allow_html=True)
            else:
                st.markdown(
                    "<small style='color:#999; font-style:italic;'>No specific aspect keywords detected (General Sentiment)</small>",
                    unsafe_allow_html=True,
                )

            st.markdown("</div>", unsafe_allow_html=True)


if __name__ == "__main__":
    main()