""" Lead.AI Review Sentinel — Live Demo Sentiment analysis and complaint detection for business reviews. Visit https://www.lead-ai.us for a custom deployment. """ import gradio as gr from transformers import pipeline import re # ── Load sentiment model ────────────────────────────────────────────────────── sentiment_pipe = pipeline( "sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english", truncation=True, max_length=512, ) COMPLAINT_KEYWORDS = { "Service": ["rude", "unhelpful", "ignored", "waited", "no response", "terrible service", "never again", "staff", "employee", "customer service", "support"], "Quality": ["broken", "defective", "cheap", "poor quality", "fell apart", "stopped working", "doesn't work", "damaged", "faulty", "malfunction"], "Delivery": ["late", "delayed", "never arrived", "missing", "wrong item", "shipping", "package", "delivery", "tracking"], "Billing": ["overcharged", "refund", "charged", "billing error", "scam", "fraud", "unauthorized", "dispute", "price"], "Experience": ["disappointing", "waste of money", "regret", "horrible", "awful", "worst", "not worth", "misleading", "false advertising"], } POSITIVE_KEYWORDS = ["amazing", "excellent", "fantastic", "love", "great", "perfect", "recommend", "wonderful", "outstanding", "impressed", "best", "happy"] def suggest_response(sentiment: str, categories: list, text: str) -> str: if sentiment == "POSITIVE": return ( "**Suggested Response:**\n\n" "Thank you so much for your kind words! We're thrilled to hear you had a great experience. " "We look forward to serving you again soon. 😊" ) if "Billing" in categories: return ( "**Suggested Response:**\n\n" "We sincerely apologize for the billing issue you experienced. " "Please contact us directly at hello@yourbusiness.com so we can resolve this immediately. " "We take billing accuracy very seriously and want to make this right for you." ) if "Service" in categories: return ( "**Suggested Response:**\n\n" "We're very sorry to hear about your experience with our team. " "This is not the standard we hold ourselves to. Please reach out directly " "so we can address this personally and ensure it doesn't happen again." ) return ( "**Suggested Response:**\n\n" "Thank you for your feedback. We're sorry your experience didn't meet your expectations. " "Please reach out to us directly at hello@yourbusiness.com and we'll make it right." ) def analyze_review(review_text: str, star_rating: int): if not review_text.strip(): return "Please enter a review to analyze.", "", "" # Sentiment result = sentiment_pipe(review_text[:512])[0] sentiment = result["label"] confidence = round(result["score"] * 100, 1) # Adjust by star rating if star_rating <= 2 and sentiment == "POSITIVE": sentiment = "NEGATIVE" confidence = max(60.0, confidence) elif star_rating >= 4 and sentiment == "NEGATIVE": sentiment = "POSITIVE" confidence = max(60.0, confidence) # Detect complaint categories text_lower = review_text.lower() flagged_categories = [ cat for cat, keywords in COMPLAINT_KEYWORDS.items() if any(kw in text_lower for kw in keywords) ] positive_signals = [kw for kw in POSITIVE_KEYWORDS if kw in text_lower] # Urgency is_urgent = (star_rating <= 2 and len(flagged_categories) >= 2) or \ any(kw in text_lower for kw in ["scam", "fraud", "lawsuit", "never again", "report"]) # Build output if sentiment == "POSITIVE": emoji, label, action = "🟢", "POSITIVE", "Monitor & thank publicly" elif confidence >= 80: emoji, label, action = "🔴", "NEGATIVE — HIGH PRIORITY", "Respond within 2 hours" else: emoji, label, action = "🟡", "MIXED / NEUTRAL", "Respond within 24 hours" if is_urgent: action = "🚨 URGENT — Respond immediately" report = f"## {emoji} Sentiment: {label}\n\n" report += f"**Confidence:** {confidence}% | **Stars:** {'⭐' * star_rating}\n\n" report += f"**Recommended Action:** {action}\n\n" report += "---\n\n" if flagged_categories: report += "### 🚨 Complaint Categories Detected\n\n" for cat in flagged_categories: matched = [kw for kw in COMPLAINT_KEYWORDS[cat] if kw in text_lower] report += f"**{cat}** — triggered by: _{', '.join(matched[:3])}_\n\n" report += "\n" if positive_signals: report += "### ✅ Positive Signals Found\n\n" report += ", ".join(f"_{w}_" for w in positive_signals) + "\n\n" response_suggestion = suggest_response(sentiment, flagged_categories, review_text) report += "---\n\n" report += response_suggestion report += "\n\n---\n\n" report += "> ⚠️ Demo model. Your production Lead.AI Review Sentinel monitors your " report += "real reviews across Google, Yelp, and Trustpilot automatically.\n\n" report += "**[→ Get Review Sentinel for Your Business](https://www.lead-ai.us)**" urgency_label = "🚨 URGENT — ACT NOW" if is_urgent else ("🟢 Low urgency" if sentiment == "POSITIVE" else "🟡 Respond within 24h") return report, urgency_label, f"Categories: {', '.join(flagged_categories) if flagged_categories else 'None detected'}" with gr.Blocks( title="Lead.AI Review Sentinel", theme=gr.themes.Soft(primary_hue="yellow"), css=".footer { text-align:center; margin-top:20px; color:#666; }" ) as demo: gr.Markdown(""" # ⭐ Lead.AI Review Sentinel ### Catch Reputation Damage Before It Spreads Paste any customer review below. The AI reads the sentiment, identifies complaint categories, and suggests a professional response — in seconds. > 💼 This is a live proof-of-concept. [Request a custom system →](https://www.lead-ai.us) """) with gr.Row(): with gr.Column(): gr.Markdown("### Paste Review") review_text = gr.Textbox( lines=6, placeholder='e.g. "I waited 3 weeks for my order and when it finally arrived, the item was broken. Customer service never responded to my emails. Absolutely terrible experience."', label="Customer Review Text", ) star_rating = gr.Slider(1, 5, value=3, step=1, label="Star Rating") btn = gr.Button("🔍 Analyze Review", variant="primary", size="lg") with gr.Column(): gr.Markdown("### AI Analysis") urgency_out = gr.Textbox(label="Urgency Alert", lines=1) categories_out = gr.Textbox(label="Complaint Categories", lines=1) report_out = gr.Markdown() btn.click( fn=analyze_review, inputs=[review_text, star_rating], outputs=[report_out, urgency_out, categories_out], ) gr.Examples( examples=[ ["I waited 3 weeks and my order arrived broken. Customer service ignored my emails completely.", 1], ["Absolutely amazing experience! The team was so helpful and my order arrived the next day. Will definitely recommend!", 5], ["The product is okay but shipping took longer than expected. I got a partial refund which helped.", 3], ["I was charged twice for the same order. Still waiting for my refund after 2 weeks. This is fraud.", 1], ], inputs=[review_text, star_rating], label="Try These Example Reviews", ) gr.Markdown(""" ---
""") if __name__ == "__main__": demo.launch()