File size: 2,553 Bytes
91f477d
 
 
 
7a6cab0
91f477d
 
 
 
 
 
 
 
7a6cab0
91f477d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a6cab0
91f477d
 
 
7a6cab0
91f477d
 
 
 
 
7a6cab0
91f477d
6e1bde4
91f477d
7a6cab0
91f477d
 
7a6cab0
 
91f477d
7a6cab0
91f477d
 
 
7a6cab0
91f477d
 
7a6cab0
 
91f477d
 
 
 
 
 
 
 
7a6cab0
 
91f477d
 
 
 
 
7a6cab0
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
import streamlit as st
from transformers import pipeline
import pandas as pd


@st.cache_resource
def load_sentiment_model():
    return pipeline("text-classification", model="jinchenliuljc/ecommerce-sentiment-analysis")

@st.cache_resource
def load_ner_model():
    return pipeline("ner", model="jinchenliuljc/ecom_ner_model")


def extract_products(ner_result):
    products = []
    current_product = None
    
    for entity in ner_result:
        if entity['entity'] == 'B-HCCX':
            if current_product is not None:
                products.append(current_product)
            current_product = {
                'start': entity['start'],
                'end': entity['end'],
                'text': entity['word']
            }
        elif entity['entity'] == 'I-HCCX' and current_product is not None:
            current_product['end'] = entity['end']
            current_product['text'] += entity['word']
    
    if current_product is not None:
        products.append(current_product)
    
    return [p['text'] for p in products]

if 'records' not in st.session_state:
    st.session_state.records = []

st.title("DTC Customer Review Analysis System")
col1, col2 = st.columns(2)

with col1:
    user_input = st.text_input("Enter customer review:", key="input")
    
    if user_input:
        sentiment_classifier = load_sentiment_model()
        sentiment_result = sentiment_classifier(user_input)[0]['label']
        

        if sentiment_result == 'LABEL_1':
            st.success("Thank you for your positive feedback! :)")
        else:

            ner_pipe = load_ner_model()
            ner_result = ner_pipe(user_input)

            products = extract_products(ner_result)
            
            # Extract products
            products = extract_products(ner_result)
            
            if products:
                # Add to records
                for product in products:
                    new_record = {
                        'Product Category': product,
                        'Review Content': user_input
                    }
                    st.session_state.records.append(new_record)
with col2:
    if st.session_state.records:
        df = pd.DataFrame(st.session_state.records)
        st.dataframe(
            df,
            column_config={
                "Product Category": "Affected Product",
                "Review Content": "Related Review"
            },
            hide_index=True,
            use_container_width=True
        )
    else:
        st.info("No feedback records yet")