Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +338 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,340 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
import re
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Page configuration
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="DistilBERT Sentiment Analyzer",
|
| 11 |
+
page_icon="π",
|
| 12 |
+
layout="wide"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Title and description
|
| 16 |
+
st.title("π DistilBERT Sentiment Analysis with AI Responses")
|
| 17 |
+
st.markdown("**5-Class Amazon Review Sentiment Analysis + AI-Generated Customer Support Responses**")
|
| 18 |
+
st.markdown("*Powered by DistilBERT & GitHub Models API*")
|
| 19 |
+
st.markdown("---")
|
| 20 |
+
|
| 21 |
+
# Sidebar for API configuration
|
| 22 |
+
st.sidebar.header("π API Configuration")
|
| 23 |
+
github_token = st.sidebar.text_input(
|
| 24 |
+
"GitHub Models API Token:",
|
| 25 |
+
type="password",
|
| 26 |
+
help="Get your free token from GitHub Models marketplace"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
if not github_token:
|
| 30 |
+
st.sidebar.warning("β οΈ Enter GitHub token to enable AI responses!")
|
| 31 |
+
|
| 32 |
+
# Load model
|
| 33 |
+
@st.cache_resource
|
| 34 |
+
def load_sentiment_model():
|
| 35 |
+
"""Load the fine-tuned DistilBERT model"""
|
| 36 |
+
try:
|
| 37 |
+
# Replace with your actual model path from Hugging Face Hub
|
| 38 |
+
model_name = "your-username/distilbert-amazon-sentiment" # UPDATE THIS
|
| 39 |
+
|
| 40 |
+
return pipeline(
|
| 41 |
+
"text-classification",
|
| 42 |
+
model=model_name,
|
| 43 |
+
tokenizer=model_name,
|
| 44 |
+
return_all_scores=True,
|
| 45 |
+
device=0 if torch.cuda.is_available() else -1
|
| 46 |
+
)
|
| 47 |
+
except Exception as e:
|
| 48 |
+
st.error(f"Error loading model: {str(e)}")
|
| 49 |
+
# Fallback to a generic model
|
| 50 |
+
return pipeline(
|
| 51 |
+
"text-classification",
|
| 52 |
+
model="cardiffnlp/twitter-roberta-base-sentiment-latest",
|
| 53 |
+
return_all_scores=True
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
def load_llm_client(token):
|
| 57 |
+
"""Initialize GitHub Models client"""
|
| 58 |
+
if not token:
|
| 59 |
+
return None
|
| 60 |
+
try:
|
| 61 |
+
return OpenAI(
|
| 62 |
+
api_key=token,
|
| 63 |
+
base_url="https://models.inference.ai.azure.com/"
|
| 64 |
+
)
|
| 65 |
+
except Exception as e:
|
| 66 |
+
st.error(f"Failed to initialize LLM client: {str(e)}")
|
| 67 |
+
return None
|
| 68 |
+
|
| 69 |
+
# Load models
|
| 70 |
+
with st.spinner("Loading DistilBERT model..."):
|
| 71 |
+
sentiment_pipeline = load_sentiment_model()
|
| 72 |
+
st.success("β
Model loaded successfully!")
|
| 73 |
+
|
| 74 |
+
# Initialize LLM client
|
| 75 |
+
llm_client = None
|
| 76 |
+
if github_token:
|
| 77 |
+
llm_client = load_llm_client(github_token)
|
| 78 |
+
if llm_client:
|
| 79 |
+
st.sidebar.success("β
GitHub Models API connected!")
|
| 80 |
+
|
| 81 |
+
def predict_sentiment_enhanced(text):
|
| 82 |
+
"""Enhanced sentiment prediction with confidence scores for 5 classes"""
|
| 83 |
+
if not text.strip():
|
| 84 |
+
return "Average", 0.20, {}
|
| 85 |
+
|
| 86 |
+
try:
|
| 87 |
+
# Get predictions
|
| 88 |
+
results = sentiment_pipeline(text)
|
| 89 |
+
if isinstance(results[0], list):
|
| 90 |
+
results = results[0]
|
| 91 |
+
|
| 92 |
+
best_result = max(results, key=lambda x: x['score'])
|
| 93 |
+
|
| 94 |
+
# Map labels to readable format
|
| 95 |
+
label_map = {
|
| 96 |
+
'LABEL_0': 'Very Bad',
|
| 97 |
+
'LABEL_1': 'Bad',
|
| 98 |
+
'LABEL_2': 'Average',
|
| 99 |
+
'LABEL_3': 'Good',
|
| 100 |
+
'LABEL_4': 'Very Good',
|
| 101 |
+
'NEGATIVE': 'Bad',
|
| 102 |
+
'NEUTRAL': 'Average',
|
| 103 |
+
'POSITIVE': 'Good'
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
sentiment = label_map.get(best_result['label'], best_result['label'])
|
| 107 |
+
confidence = best_result['score']
|
| 108 |
+
|
| 109 |
+
# Get all scores
|
| 110 |
+
all_scores = {}
|
| 111 |
+
for result in results:
|
| 112 |
+
mapped_label = label_map.get(result['label'], result['label'])
|
| 113 |
+
all_scores[mapped_label] = result['score']
|
| 114 |
+
|
| 115 |
+
return sentiment, confidence, all_scores
|
| 116 |
+
|
| 117 |
+
except Exception as e:
|
| 118 |
+
st.error(f"Error in prediction: {str(e)}")
|
| 119 |
+
return "Average", 0.5, {"Average": 0.5}
|
| 120 |
+
|
| 121 |
+
def generate_llm_response(review_text, sentiment):
|
| 122 |
+
"""Generate AI-powered customer support response"""
|
| 123 |
+
if not llm_client:
|
| 124 |
+
return "β οΈ GitHub API token required for AI response generation."
|
| 125 |
+
|
| 126 |
+
# Enhanced prompts for different sentiment levels
|
| 127 |
+
prompts = {
|
| 128 |
+
'Very Bad': f"""You are a professional customer service manager. A customer left this review: "{review_text}"
|
| 129 |
+
|
| 130 |
+
Their sentiment is very negative. Provide a professional response that:
|
| 131 |
+
1. Shows genuine empathy and takes responsibility
|
| 132 |
+
2. Offers concrete solutions: refund, replacement, customer support contact
|
| 133 |
+
3. Provides next steps and assures resolution
|
| 134 |
+
4. Don't mention star ratings
|
| 135 |
+
|
| 136 |
+
Keep it professional and solution-focused (2-3 sentences).
|
| 137 |
+
Response:""",
|
| 138 |
+
|
| 139 |
+
'Bad': f"""You are a customer service representative. A customer wrote: "{review_text}"
|
| 140 |
+
|
| 141 |
+
Their experience was negative. Provide a response that:
|
| 142 |
+
1. Acknowledges their concerns and validates feedback
|
| 143 |
+
2. Offers solutions: replacement, troubleshooting, partial refund
|
| 144 |
+
3. Asks for suggestions to improve
|
| 145 |
+
4. Shows commitment to making it right
|
| 146 |
+
|
| 147 |
+
Keep it constructive (2-3 sentences). Don't mention star ratings.
|
| 148 |
+
Response:""",
|
| 149 |
+
|
| 150 |
+
'Average': f"""You are responding to a customer who wrote: "{review_text}"
|
| 151 |
+
|
| 152 |
+
They had a mixed experience. Provide a response that:
|
| 153 |
+
1. Thanks them for honest feedback
|
| 154 |
+
2. Acknowledges both positives and areas for improvement
|
| 155 |
+
3. Offers assistance and support
|
| 156 |
+
4. Invites suggestions for enhancement
|
| 157 |
+
|
| 158 |
+
Keep it balanced and appreciative (2-3 sentences). Don't mention star ratings.
|
| 159 |
+
Response:""",
|
| 160 |
+
|
| 161 |
+
'Good': f"""You are responding to a satisfied customer: "{review_text}"
|
| 162 |
+
|
| 163 |
+
They had a positive experience. Provide a response that:
|
| 164 |
+
1. Thanks them genuinely for positive feedback
|
| 165 |
+
2. Acknowledges specific aspects they liked
|
| 166 |
+
3. Shows commitment to maintaining quality
|
| 167 |
+
4. Offers continued support
|
| 168 |
+
|
| 169 |
+
Keep it appreciative (2-3 sentences). Don't mention star ratings.
|
| 170 |
+
Response:""",
|
| 171 |
+
|
| 172 |
+
'Very Good': f"""You are responding to a delighted customer: "{review_text}"
|
| 173 |
+
|
| 174 |
+
They had an excellent experience. Provide a response that:
|
| 175 |
+
1. Expresses genuine gratitude for amazing feedback
|
| 176 |
+
2. Celebrates their positive experience
|
| 177 |
+
3. Shows this motivates your team
|
| 178 |
+
4. Invites them to share their experience
|
| 179 |
+
|
| 180 |
+
Keep it enthusiastic yet professional (2-3 sentences). Don't mention star ratings.
|
| 181 |
+
Response:"""
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
prompt = prompts.get(sentiment, prompts['Average'])
|
| 185 |
+
|
| 186 |
+
try:
|
| 187 |
+
response = llm_client.chat.completions.create(
|
| 188 |
+
model="gpt-4o-mini",
|
| 189 |
+
messages=[{"role": "user", "content": prompt}],
|
| 190 |
+
max_tokens=150,
|
| 191 |
+
temperature=0.7
|
| 192 |
+
)
|
| 193 |
+
return response.choices[0].message.content.strip()
|
| 194 |
+
except Exception as e:
|
| 195 |
+
# Fallback responses
|
| 196 |
+
fallbacks = {
|
| 197 |
+
'Very Bad': "We sincerely apologize for this disappointing experience. Please contact our customer support immediately so we can arrange a full refund or replacement.",
|
| 198 |
+
'Bad': "Thank you for bringing these concerns to our attention. We'd like to work together to find a solution that meets your needs.",
|
| 199 |
+
'Average': "We appreciate your honest feedback and suggestions. Your input helps us continue improving our products and services.",
|
| 200 |
+
'Good': "Thank you for your positive feedback! We're delighted you had a good experience and appreciate your support.",
|
| 201 |
+
'Very Good': "Wow, thank you for this fantastic review! Your enthusiasm means the world to our team."
|
| 202 |
+
}
|
| 203 |
+
return fallbacks.get(sentiment, "Thank you for your valuable feedback!")
|
| 204 |
+
|
| 205 |
+
# Main interface
|
| 206 |
+
col1, col2 = st.columns([2, 1])
|
| 207 |
+
|
| 208 |
+
with col1:
|
| 209 |
+
st.subheader("π Enter Product Review")
|
| 210 |
+
review_text = st.text_area(
|
| 211 |
+
"Type or paste a product review here:",
|
| 212 |
+
height=150,
|
| 213 |
+
placeholder="Example: This product broke after just two days of use. Very disappointed with the quality and delivery was delayed too."
|
| 214 |
+
)
|
| 215 |
+
|
| 216 |
+
analyze_button = st.button("π Analyze & Generate Response", type="primary")
|
| 217 |
+
|
| 218 |
+
with col2:
|
| 219 |
+
st.subheader("π Analysis Results")
|
| 220 |
+
|
| 221 |
+
if analyze_button and review_text.strip():
|
| 222 |
+
with st.spinner("Analyzing sentiment..."):
|
| 223 |
+
sentiment, confidence, all_scores = predict_sentiment_enhanced(review_text)
|
| 224 |
+
|
| 225 |
+
# Display results
|
| 226 |
+
color_map = {
|
| 227 |
+
'Very Good': 'green',
|
| 228 |
+
'Good': 'blue',
|
| 229 |
+
'Average': 'orange',
|
| 230 |
+
'Bad': 'red',
|
| 231 |
+
'Very Bad': 'violet'
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
emoji_map = {
|
| 235 |
+
'Very Bad': 'π‘',
|
| 236 |
+
'Bad': 'π',
|
| 237 |
+
'Average': 'π',
|
| 238 |
+
'Good': 'π',
|
| 239 |
+
'Very Good': 'π€©'
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
color = color_map.get(sentiment, 'blue')
|
| 243 |
+
emoji = emoji_map.get(sentiment, 'π')
|
| 244 |
+
|
| 245 |
+
st.markdown(f"**Sentiment:** :{color}[{sentiment}] {emoji}")
|
| 246 |
+
st.progress(confidence)
|
| 247 |
+
st.caption(f"Confidence: {confidence:.2%}")
|
| 248 |
+
|
| 249 |
+
# Show all predictions
|
| 250 |
+
st.subheader("π― All Predictions")
|
| 251 |
+
for class_name, score in sorted(all_scores.items(), key=lambda x: x[1], reverse=True):
|
| 252 |
+
emoji_display = emoji_map.get(class_name, 'π')
|
| 253 |
+
st.write(f"{emoji_display} {class_name}: {score:.1%}")
|
| 254 |
+
|
| 255 |
+
st.markdown("---")
|
| 256 |
+
|
| 257 |
+
with st.spinner("Generating AI response..."):
|
| 258 |
+
ai_response = generate_llm_response(review_text, sentiment)
|
| 259 |
+
|
| 260 |
+
st.subheader("π€ AI Customer Support Response")
|
| 261 |
+
if ai_response.startswith("β οΈ"):
|
| 262 |
+
st.warning(ai_response)
|
| 263 |
+
else:
|
| 264 |
+
st.info(ai_response)
|
| 265 |
+
|
| 266 |
+
# Show strategy
|
| 267 |
+
strategies = {
|
| 268 |
+
'Very Bad': "π Crisis Management: Immediate resolution",
|
| 269 |
+
'Bad': "π§ Problem Resolution: Solutions & improvements",
|
| 270 |
+
'Average': "βοΈ Balanced: Acknowledge & enhance",
|
| 271 |
+
'Good': "π Appreciation: Maintain quality",
|
| 272 |
+
'Very Good': "π Celebration: Encourage sharing"
|
| 273 |
+
}
|
| 274 |
+
st.caption(f"**Strategy:** {strategies.get(sentiment, 'βοΈ Balanced')}")
|
| 275 |
+
|
| 276 |
+
elif analyze_button and not review_text.strip():
|
| 277 |
+
st.warning("β οΈ Please enter a review to analyze!")
|
| 278 |
+
|
| 279 |
+
# Examples section
|
| 280 |
+
st.markdown("---")
|
| 281 |
+
st.subheader("π‘ Try These Examples")
|
| 282 |
+
|
| 283 |
+
examples = [
|
| 284 |
+
"This product completely broke on the first day! Terrible quality and customer service was unhelpful.",
|
| 285 |
+
"The product works but has some issues. Build quality could be better and delivery took longer than expected.",
|
| 286 |
+
"Decent product overall. Does what it's supposed to do but nothing exceptional. Good value for the price.",
|
| 287 |
+
"Really happy with this purchase! Good quality, fast delivery, and works perfectly. Would recommend.",
|
| 288 |
+
"Outstanding product! Exceeded all my expectations. Amazing quality, perfect packaging, incredible service!"
|
| 289 |
+
]
|
| 290 |
+
|
| 291 |
+
cols = st.columns(5)
|
| 292 |
+
sentiments = ['Very Bad', 'Bad', 'Average', 'Good', 'Very Good']
|
| 293 |
+
emojis = ['π‘', 'π', 'π', 'π', 'π€©']
|
| 294 |
+
|
| 295 |
+
for i, (example, sentiment, emoji) in enumerate(zip(examples, sentiments, emojis)):
|
| 296 |
+
with cols[i]:
|
| 297 |
+
if st.button(f"{emoji} {sentiment}", key=f"ex_{i}"):
|
| 298 |
+
st.session_state.example_review = example
|
| 299 |
+
|
| 300 |
+
if 'example_review' in st.session_state:
|
| 301 |
+
st.text_area("Selected example:", value=st.session_state.example_review, key="example_display")
|
| 302 |
+
|
| 303 |
+
# Instructions
|
| 304 |
+
st.markdown("---")
|
| 305 |
+
st.subheader("π How to Use")
|
| 306 |
+
|
| 307 |
+
col_a, col_b = st.columns(2)
|
| 308 |
+
|
| 309 |
+
with col_a:
|
| 310 |
+
st.markdown("""
|
| 311 |
+
**π§ Setup:**
|
| 312 |
+
1. Get free GitHub Models API token
|
| 313 |
+
2. Enter token in sidebar
|
| 314 |
+
3. Start analyzing reviews!
|
| 315 |
+
|
| 316 |
+
**π― Features:**
|
| 317 |
+
- 5-class sentiment analysis
|
| 318 |
+
- Confidence scores for all classes
|
| 319 |
+
- Professional AI responses
|
| 320 |
+
- Solution-oriented strategies
|
| 321 |
+
""")
|
| 322 |
+
|
| 323 |
+
with col_b:
|
| 324 |
+
st.markdown("""
|
| 325 |
+
**πΌ Business Use Cases:**
|
| 326 |
+
- Customer service automation
|
| 327 |
+
- Review response generation
|
| 328 |
+
- Quality assurance monitoring
|
| 329 |
+
- Brand reputation management
|
| 330 |
+
|
| 331 |
+
**π Model Info:**
|
| 332 |
+
- Based on DistilBERT
|
| 333 |
+
- 92%+ accuracy on reviews
|
| 334 |
+
- Real-time processing
|
| 335 |
+
- Memory efficient
|
| 336 |
+
""")
|
| 337 |
+
|
| 338 |
+
# Footer
|
| 339 |
+
st.markdown("---")
|
| 340 |
+
st.caption("Built with Streamlit β’ Powered by DistilBERT & GitHub Models β’ Deployed on Hugging Face Spaces")
|