clb5114 commited on
Commit
ad54b59
·
verified ·
1 Parent(s): c34b0c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -71
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 AI Analyst",
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
- "text-generation",
40
- model="Lin0He/text-summary-gpt2-short",
41
- max_length=150,
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.subheader("AI-Powered E-commerce Review Insight Engine")
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 (Supports both Chinese/English)",
83
  height=200,
84
- placeholder="Example: This serum is amazing! After one week of use, my skin looks visibly brighter..."
 
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('🔍 AI is analyzing...'):
93
- with st.spinner('🚀 Loading AI models...'):
94
  summarizer, classifier = load_models()
95
-
96
- st.toast('Models ready!', icon='✅')
97
-
98
- with st.expander("Original Review", expanded=True):
99
- st.write(user_input)
100
-
101
- summary = summarizer(user_input, max_length=max_length)[0]['generated_text']
102
- with st.container():
103
- col1, col2 = st.columns([1, 3])
104
- with col1:
105
- st.subheader("📝 Smart Summary")
106
- with col2:
107
- st.markdown(f"```\n{summary}\n```")
108
-
109
- results = classifier(summary)
110
- positive_score = results[0][1]['score']
111
- label = "Positive 👍" if positive_score > confidence_threshold else "Negative 👎"
112
-
113
- with st.container():
114
- st.subheader("📊 Sentiment Report")
115
- col1, col2 = st.columns(2)
116
- with col1:
117
- st.metric("Sentiment", label)
118
- st.write(f"Confidence: {positive_score:.2%}")
119
- with col2:
120
- progress_color = "#4CAF50" if label=="Positive 👍" else "#FF5252"
121
- st.markdown(f"""
122
- <div style="
123
- background: {progress_color}10;
124
- border-radius: 10px;
125
- padding: 15px;
126
- ">
127
- <div style="font-size: 14px; color: {progress_color}; margin-bottom: 8px;">Sentiment Intensity</div>
128
- <div style="height: 8px; background: #eee; border-radius: 4px;">
129
- <div style="width: {positive_score*100}%; height: 100%; background: {progress_color}; border-radius: 4px;"></div>
 
130
  </div>
131
- </div>
132
- """, unsafe_allow_html=True)
 
 
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()