File size: 2,593 Bytes
62d0c8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75cb516
62d0c8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from utils.inference import predict_theme

EXAMPLES = {
    "Broken on arrival": "barang sampai dalam keadaan pecah, packing asal asalan",
    "Slow delivery": "pengiriman lama banget, sudah seminggu barang belum sampai juga",
    "Happy customer": "barangnya bagus banget, pengiriman cepat, seller ramah, recommended",
    "Wrong item": "warna yang dikirim tidak sesuai dengan yang saya pesan",
}


@st.cache_resource(show_spinner=False)
def _warm():
    predict_theme("warm up")   # load the model once so the first real prediction is fast
    return True


def run():
    st.header("๐Ÿ”ฎ Predict a Review's Theme")
    st.write(
        "Paste an Indonesian product review. The model embeds it, finds the closest theme among "
        "the topics it learned from Tokopedia reviews, and tells you whether it reads as "
        "**praise** or a **complaint**."
    )

    with st.spinner("Loading the language model..."):
        _warm()

    pick = st.selectbox("Try an example (optional)", ["-- type my own --", *EXAMPLES.keys()])
    default = "" if pick == "-- type my own --" else EXAMPLES[pick]
    text = st.text_area("Customer review", value=default, height=130,
                        placeholder="contoh: barang sampai pecah, packing asal-asalan...")

    if st.button("Predict theme", type="primary"):
        if not text.strip():
            st.warning("Please paste a review first.")
            return
        with st.spinner("Analyzing..."):
            cleaned, ranked = predict_theme(text)

        if not ranked:
            st.error("No usable text after cleaning - try a longer review.")
            return

        side, theme, sim = ranked[0]
        if side == "Positive":
            st.success(f"๐Ÿ‘ **{theme}**  -  reads as a positive review")
        else:
            st.error(f"๐Ÿ‘Ž **{theme}**  -  reads as a negative review")
        st.caption(f"Confidence (cosine similarity): {sim:.2f}")

        if len(ranked) > 1:
            st.markdown("**Other close themes:**")
            for s, th, sm in ranked[1:]:
                tag = "praise" if s == "Positive" else "complaint"
                st.write(f"- {th}  *({tag}, {sm:.2f})*")

        with st.expander("What the model actually read (after cleaning)"):
            st.code(cleaned or "(empty)")

    st.caption(
        "Themes were learned separately for positive and negative reviews; the app picks the "
        "single closest theme across both. Mixed Indonesian-English terms (e.g. 'fast charging') "
        "can occasionally be matched to a delivery/speed theme."
    )