Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +94 -139
src/streamlit_app.py
CHANGED
|
@@ -18,112 +18,128 @@ st.markdown("**5-Class Amazon Review Sentiment Analysis + AI-Generated Customer
|
|
| 18 |
st.markdown("*Powered by DistilBERT & GitHub Models API*")
|
| 19 |
st.markdown("---")
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 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 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 38 |
-
model_name = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
return pipeline(
|
| 41 |
"text-classification",
|
| 42 |
-
model=
|
| 43 |
-
tokenizer=
|
| 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 |
-
|
| 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 |
-
#
|
| 75 |
-
llm_client =
|
| 76 |
-
if
|
| 77 |
-
|
| 78 |
-
if llm_client:
|
| 79 |
-
st.sidebar.success("β
GitHub Models API connected!")
|
| 80 |
|
| 81 |
def predict_sentiment_enhanced(text):
|
| 82 |
-
"""Enhanced sentiment prediction
|
| 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 |
-
|
| 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 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
except Exception as e:
|
| 118 |
st.error(f"Error in prediction: {str(e)}")
|
| 119 |
-
return "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 "
|
| 125 |
|
| 126 |
-
#
|
| 127 |
prompts = {
|
| 128 |
'Very Bad': f"""You are a professional customer service manager. A customer left this review: "{review_text}"
|
| 129 |
|
|
@@ -192,7 +208,6 @@ Response:"""
|
|
| 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.",
|
|
@@ -246,9 +261,12 @@ with col2:
|
|
| 246 |
st.progress(confidence)
|
| 247 |
st.caption(f"Confidence: {confidence:.2%}")
|
| 248 |
|
| 249 |
-
# Show
|
| 250 |
-
st.subheader("π― All Predictions")
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
| 252 |
emoji_display = emoji_map.get(class_name, 'π')
|
| 253 |
st.write(f"{emoji_display} {class_name}: {score:.1%}")
|
| 254 |
|
|
@@ -258,83 +276,20 @@ with col2:
|
|
| 258 |
ai_response = generate_llm_response(review_text, sentiment)
|
| 259 |
|
| 260 |
st.subheader("π€ AI Customer Support Response")
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 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 |
-
#
|
| 280 |
-
|
| 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")
|
|
|
|
| 18 |
st.markdown("*Powered by DistilBERT & GitHub Models API*")
|
| 19 |
st.markdown("---")
|
| 20 |
|
| 21 |
+
# HARDCODE YOUR GITHUB API KEY HERE
|
| 22 |
+
GITHUB_TOKEN = "your_actual_github_api_key_here" # REPLACE WITH YOUR REAL KEY
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Initialize LLM client with your key
|
| 25 |
+
@st.cache_resource
|
| 26 |
+
def load_llm_client():
|
| 27 |
+
"""Initialize GitHub Models client with hardcoded key"""
|
| 28 |
+
try:
|
| 29 |
+
return OpenAI(
|
| 30 |
+
api_key=GITHUB_TOKEN,
|
| 31 |
+
base_url="https://models.inference.ai.azure.com/"
|
| 32 |
+
)
|
| 33 |
+
except Exception as e:
|
| 34 |
+
st.error(f"Failed to initialize LLM client: {str(e)}")
|
| 35 |
+
return None
|
| 36 |
|
| 37 |
# Load model
|
| 38 |
@st.cache_resource
|
| 39 |
def load_sentiment_model():
|
| 40 |
+
"""Load the fine-tuned DistilBERT model with proper 5-class handling"""
|
| 41 |
try:
|
| 42 |
+
# EDIT THIS: Replace with your actual model path
|
| 43 |
+
model_name = "your-username/distilbert-amazon-sentiment" # UPDATE THIS
|
| 44 |
+
|
| 45 |
+
# Load with explicit configuration
|
| 46 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 47 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 48 |
+
|
| 49 |
+
# Verify model has 5 classes
|
| 50 |
+
if model.config.num_labels == 5:
|
| 51 |
+
st.success("β
5-class DistilBERT model loaded successfully!")
|
| 52 |
+
else:
|
| 53 |
+
st.warning(f"β οΈ Model has {model.config.num_labels} classes, expected 5")
|
| 54 |
|
| 55 |
return pipeline(
|
| 56 |
"text-classification",
|
| 57 |
+
model=model,
|
| 58 |
+
tokenizer=tokenizer,
|
| 59 |
return_all_scores=True,
|
| 60 |
device=0 if torch.cuda.is_available() else -1
|
| 61 |
)
|
| 62 |
except Exception as e:
|
| 63 |
+
st.error(f"Error loading custom model: {str(e)}")
|
| 64 |
+
st.warning("β οΈ Using fallback model - this will only show 3 classes")
|
| 65 |
return pipeline(
|
| 66 |
"text-classification",
|
| 67 |
model="cardiffnlp/twitter-roberta-base-sentiment-latest",
|
| 68 |
return_all_scores=True
|
| 69 |
)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
# Load models
|
| 72 |
with st.spinner("Loading DistilBERT model..."):
|
| 73 |
sentiment_pipeline = load_sentiment_model()
|
|
|
|
| 74 |
|
| 75 |
+
# Load LLM client automatically
|
| 76 |
+
llm_client = load_llm_client()
|
| 77 |
+
if llm_client:
|
| 78 |
+
st.success("β
GitHub Models API connected automatically!")
|
|
|
|
|
|
|
| 79 |
|
| 80 |
def predict_sentiment_enhanced(text):
|
| 81 |
+
"""Enhanced sentiment prediction ensuring 5-class output"""
|
| 82 |
if not text.strip():
|
| 83 |
+
return "Average", 0.20, {
|
| 84 |
+
'Very Bad': 0.10,
|
| 85 |
+
'Bad': 0.15,
|
| 86 |
+
'Average': 0.50,
|
| 87 |
+
'Good': 0.15,
|
| 88 |
+
'Very Good': 0.10
|
| 89 |
+
}
|
| 90 |
|
| 91 |
try:
|
|
|
|
| 92 |
results = sentiment_pipeline(text)
|
| 93 |
if isinstance(results[0], list):
|
| 94 |
results = results[0]
|
| 95 |
|
| 96 |
+
# Enhanced label mapping for 5-class model
|
|
|
|
|
|
|
| 97 |
label_map = {
|
| 98 |
'LABEL_0': 'Very Bad',
|
| 99 |
'LABEL_1': 'Bad',
|
| 100 |
'LABEL_2': 'Average',
|
| 101 |
'LABEL_3': 'Good',
|
| 102 |
+
'LABEL_4': 'Very Good'
|
|
|
|
|
|
|
|
|
|
| 103 |
}
|
| 104 |
|
| 105 |
+
# Check if we have 5 classes (correct model)
|
| 106 |
+
if len(results) == 5:
|
| 107 |
+
best_result = max(results, key=lambda x: x['score'])
|
| 108 |
+
sentiment = label_map.get(best_result['label'], best_result['label'])
|
| 109 |
+
confidence = best_result['score']
|
| 110 |
+
|
| 111 |
+
all_scores = {}
|
| 112 |
+
for result in results:
|
| 113 |
+
mapped_label = label_map.get(result['label'], result['label'])
|
| 114 |
+
all_scores[mapped_label] = result['score']
|
| 115 |
+
|
| 116 |
+
return sentiment, confidence, all_scores
|
| 117 |
|
| 118 |
+
# Fallback for 3-class model
|
| 119 |
+
else:
|
| 120 |
+
fallback_map = {'NEGATIVE': 'Bad', 'NEUTRAL': 'Average', 'POSITIVE': 'Good'}
|
| 121 |
+
best_result = max(results, key=lambda x: x['score'])
|
| 122 |
+
sentiment = fallback_map.get(best_result['label'], 'Average')
|
| 123 |
+
confidence = best_result['score']
|
| 124 |
+
|
| 125 |
+
# Create approximated 5-class scores
|
| 126 |
+
all_scores = {'Very Bad': 0.0, 'Bad': 0.0, 'Average': 0.0, 'Good': 0.0, 'Very Good': 0.0}
|
| 127 |
+
for result in results:
|
| 128 |
+
mapped = fallback_map.get(result['label'], 'Average')
|
| 129 |
+
all_scores[mapped] = result['score']
|
| 130 |
+
|
| 131 |
+
return sentiment, confidence, all_scores
|
| 132 |
|
| 133 |
except Exception as e:
|
| 134 |
st.error(f"Error in prediction: {str(e)}")
|
| 135 |
+
return "Average", 0.5, {'Average': 0.5}
|
| 136 |
|
| 137 |
def generate_llm_response(review_text, sentiment):
|
| 138 |
+
"""Generate AI-powered customer support response using your API key"""
|
| 139 |
if not llm_client:
|
| 140 |
+
return "β GitHub Models API not available. Please check your API key."
|
| 141 |
|
| 142 |
+
# Same prompts as before...
|
| 143 |
prompts = {
|
| 144 |
'Very Bad': f"""You are a professional customer service manager. A customer left this review: "{review_text}"
|
| 145 |
|
|
|
|
| 208 |
)
|
| 209 |
return response.choices[0].message.content.strip()
|
| 210 |
except Exception as e:
|
|
|
|
| 211 |
fallbacks = {
|
| 212 |
'Very Bad': "We sincerely apologize for this disappointing experience. Please contact our customer support immediately so we can arrange a full refund or replacement.",
|
| 213 |
'Bad': "Thank you for bringing these concerns to our attention. We'd like to work together to find a solution that meets your needs.",
|
|
|
|
| 261 |
st.progress(confidence)
|
| 262 |
st.caption(f"Confidence: {confidence:.2%}")
|
| 263 |
|
| 264 |
+
# Show ALL 5 predictions
|
| 265 |
+
st.subheader("π― All 5 Class Predictions")
|
| 266 |
+
# Ensure we always show all 5 classes in order
|
| 267 |
+
class_order = ['Very Bad', 'Bad', 'Average', 'Good', 'Very Good']
|
| 268 |
+
for class_name in class_order:
|
| 269 |
+
score = all_scores.get(class_name, 0.0)
|
| 270 |
emoji_display = emoji_map.get(class_name, 'π')
|
| 271 |
st.write(f"{emoji_display} {class_name}: {score:.1%}")
|
| 272 |
|
|
|
|
| 276 |
ai_response = generate_llm_response(review_text, sentiment)
|
| 277 |
|
| 278 |
st.subheader("π€ AI Customer Support Response")
|
| 279 |
+
st.info(ai_response)
|
| 280 |
+
|
| 281 |
+
# Show strategy
|
| 282 |
+
strategies = {
|
| 283 |
+
'Very Bad': "π Crisis Management: Immediate resolution",
|
| 284 |
+
'Bad': "π§ Problem Resolution: Solutions & improvements",
|
| 285 |
+
'Average': "βοΈ Balanced: Acknowledge & enhance",
|
| 286 |
+
'Good': "π Appreciation: Maintain quality",
|
| 287 |
+
'Very Good': "π Celebration: Encourage sharing"
|
| 288 |
+
}
|
| 289 |
+
st.caption(f"**Strategy:** {strategies.get(sentiment, 'βοΈ Balanced')}")
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
elif analyze_button and not review_text.strip():
|
| 292 |
st.warning("β οΈ Please enter a review to analyze!")
|
| 293 |
|
| 294 |
+
# Rest of your app (examples section, etc.) remains the same...
|
| 295 |
+
# [Include the examples section, instructions, and footer from the previous code]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|