Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,10 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
import time
|
4 |
|
5 |
st.set_page_config(
|
6 |
-
page_title="Cosmetic Review
|
7 |
layout="wide",
|
8 |
initial_sidebar_state="expanded",
|
9 |
)
|
@@ -36,9 +37,9 @@ def load_css():
|
|
36 |
@st.cache_resource(show_spinner=False)
|
37 |
def load_models():
|
38 |
summarizer = pipeline(
|
39 |
-
"
|
40 |
-
model="
|
41 |
-
max_length=
|
42 |
temperature=0.7
|
43 |
)
|
44 |
|
@@ -52,36 +53,13 @@ def load_models():
|
|
52 |
def main():
|
53 |
load_css()
|
54 |
st.title("💄 Cosmetic Review AI Analyst")
|
55 |
-
st.
|
56 |
-
|
57 |
-
with st.sidebar:
|
58 |
-
st.header("Configuration Parameters")
|
59 |
-
|
60 |
-
with st.container(border=True):
|
61 |
-
st.markdown("""
|
62 |
-
**Adjust parameters before analysis:**
|
63 |
-
- `Max Summary Length`: Control generated summary's word count (50-200)
|
64 |
-
- `Confidence Threshold`: Set minimum confidence for positive classification
|
65 |
-
""")
|
66 |
-
|
67 |
-
st.markdown("### 🔧 Summary Settings")
|
68 |
-
max_length = st.slider(
|
69 |
-
"Max Summary Length",
|
70 |
-
50, 200, 120,
|
71 |
-
help="Maximum number of tokens in generated summary"
|
72 |
-
)
|
73 |
-
|
74 |
-
st.markdown("### 🎚️ Sentiment Threshold")
|
75 |
-
confidence_threshold = st.slider(
|
76 |
-
"Sentiment Confidence Threshold",
|
77 |
-
0.5, 1.0, 0.8,
|
78 |
-
help="Minimum confidence score for positive sentiment classification"
|
79 |
-
)
|
80 |
|
81 |
user_input = st.text_area(
|
82 |
-
"Input cosmetic product review (
|
83 |
height=200,
|
84 |
-
placeholder="Example: This serum
|
|
|
85 |
)
|
86 |
|
87 |
if st.button("Start Analysis", use_container_width=True):
|
@@ -89,47 +67,50 @@ def main():
|
|
89 |
st.error("⚠️ Please input valid review content")
|
90 |
return
|
91 |
|
92 |
-
with st.spinner('🔍
|
93 |
-
|
94 |
summarizer, classifier = load_models()
|
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 |
</div>
|
131 |
-
|
132 |
-
|
|
|
|
|
133 |
|
134 |
if __name__ == "__main__":
|
135 |
-
main()
|
|
|
1 |
+
# app.py
|
2 |
import streamlit as st
|
3 |
from transformers import pipeline
|
4 |
import time
|
5 |
|
6 |
st.set_page_config(
|
7 |
+
page_title="Cosmetic Review Analyst",
|
8 |
layout="wide",
|
9 |
initial_sidebar_state="expanded",
|
10 |
)
|
|
|
37 |
@st.cache_resource(show_spinner=False)
|
38 |
def load_models():
|
39 |
summarizer = pipeline(
|
40 |
+
"summarization",
|
41 |
+
model="Falconsai/text_summarization",
|
42 |
+
max_length=200,
|
43 |
temperature=0.7
|
44 |
)
|
45 |
|
|
|
53 |
def main():
|
54 |
load_css()
|
55 |
st.title("💄 Cosmetic Review AI Analyst")
|
56 |
+
st.warning("⚠️ Please keep reviews under 200 words for optimal analysis")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
user_input = st.text_area(
|
59 |
+
"Input cosmetic product review (Chinese/English supported)",
|
60 |
height=200,
|
61 |
+
placeholder="Example: This serum transformed my skin in just 3 days...",
|
62 |
+
help="Maximum 200 characters recommended"
|
63 |
)
|
64 |
|
65 |
if st.button("Start Analysis", use_container_width=True):
|
|
|
67 |
st.error("⚠️ Please input valid review content")
|
68 |
return
|
69 |
|
70 |
+
with st.spinner('🔍 Analyzing...'):
|
71 |
+
try:
|
72 |
summarizer, classifier = load_models()
|
73 |
+
|
74 |
+
with st.expander("Original Review", expanded=True):
|
75 |
+
st.write(user_input)
|
76 |
+
|
77 |
+
# Text summarization
|
78 |
+
summary = summarizer(user_input, max_length=200)[0]['summary_text']
|
79 |
+
with st.container():
|
80 |
+
col1, col2 = st.columns([1, 3])
|
81 |
+
with col1:
|
82 |
+
st.subheader("📝 Summary")
|
83 |
+
with col2:
|
84 |
+
st.markdown(f"```\n{summary}\n```")
|
85 |
+
|
86 |
+
# Sentiment analysis
|
87 |
+
results = classifier(summary)
|
88 |
+
positive_score = results[0][1]['score']
|
89 |
+
label = "Positive 👍" if positive_score > 0.5 else "Negative 👎"
|
90 |
+
|
91 |
+
with st.container():
|
92 |
+
st.subheader("📊 Sentiment Analysis")
|
93 |
+
col1, col2 = st.columns(2)
|
94 |
+
with col1:
|
95 |
+
st.metric("Verdict", label)
|
96 |
+
st.write(f"Confidence: {positive_score:.2%}")
|
97 |
+
with col2:
|
98 |
+
progress_color = "#4CAF50" if label=="Positive 👍" else "#FF5252"
|
99 |
+
st.markdown(f"""
|
100 |
+
<div style="
|
101 |
+
background: {progress_color}10;
|
102 |
+
border-radius: 10px;
|
103 |
+
padding: 15px;
|
104 |
+
">
|
105 |
+
<div style="font-size: 14px; color: {progress_color}; margin-bottom: 8px;">Intensity</div>
|
106 |
+
<div style="height: 8px; background: #eee; border-radius: 4px;">
|
107 |
+
<div style="width: {positive_score*100}%; height: 100%; background: {progress_color}; border-radius: 4px;"></div>
|
108 |
+
</div>
|
109 |
</div>
|
110 |
+
""", unsafe_allow_html=True)
|
111 |
+
|
112 |
+
except Exception as e:
|
113 |
+
st.error(f"Analysis failed: {str(e)}")
|
114 |
|
115 |
if __name__ == "__main__":
|
116 |
+
main()
|