Spaces:
Sleeping
Sleeping
Added urgency, brand impact and issue categories
Browse files
app.py
CHANGED
|
@@ -4,12 +4,7 @@ import pandas as pd
|
|
| 4 |
import numpy as np
|
| 5 |
import os
|
| 6 |
from datetime import datetime
|
| 7 |
-
import plotly.graph_objects as go
|
| 8 |
-
from plotly.subplots import make_subplots
|
| 9 |
-
import base64
|
| 10 |
-
import io
|
| 11 |
|
| 12 |
-
# [Previous imports and constants remain the same]
|
| 13 |
# Initialize the classification pipelines
|
| 14 |
sentiment_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 15 |
|
|
@@ -19,7 +14,7 @@ URGENCY_LABELS = ["critical", "high", "medium", "low"]
|
|
| 19 |
BRAND_IMPACT_LABELS = ["severe", "moderate", "minimal"]
|
| 20 |
ISSUE_CATEGORIES = ["product", "service", "security", "fraud", "compliance", "technical", "billing", "general"]
|
| 21 |
|
| 22 |
-
#
|
| 23 |
CRITICAL_KEYWORDS = {
|
| 24 |
'security': ['hack', 'breach', 'leaked', 'stolen', 'unauthorized', 'privacy'],
|
| 25 |
'fraud': ['scam', 'fraud', 'fake', 'unauthorized charge', 'stolen'],
|
|
@@ -27,65 +22,77 @@ CRITICAL_KEYWORDS = {
|
|
| 27 |
'sensitive': ['racist', 'discrimination', 'harassment', 'abuse', 'offensive']
|
| 28 |
}
|
| 29 |
|
| 30 |
-
def
|
| 31 |
"""
|
| 32 |
-
|
| 33 |
"""
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
def process_csv(file):
|
| 87 |
"""
|
| 88 |
-
Process posts from CSV file with enhanced analysis
|
| 89 |
"""
|
| 90 |
try:
|
| 91 |
# Read the input CSV file
|
|
@@ -135,12 +142,6 @@ def process_csv(file):
|
|
| 135 |
# Save results
|
| 136 |
results_df.to_csv(output_file, index=False)
|
| 137 |
|
| 138 |
-
# Generate pie charts
|
| 139 |
-
charts_buf = create_pie_charts(results_df)
|
| 140 |
-
charts_file = f"analysis_charts_{timestamp}.png"
|
| 141 |
-
with open(charts_file, 'wb') as f:
|
| 142 |
-
f.write(charts_buf.getvalue())
|
| 143 |
-
|
| 144 |
# Generate summary statistics
|
| 145 |
total_posts = len(results_df)
|
| 146 |
critical_posts = len(results_df[results_df['urgency'] == 'critical'])
|
|
@@ -156,15 +157,14 @@ Negative Sentiment Posts: {negative_sentiment}
|
|
| 156 |
Severe Brand Impact Posts: {severe_impact}
|
| 157 |
|
| 158 |
The detailed analysis has been saved to: {output_file}
|
| 159 |
-
Charts have been saved to: {charts_file}
|
| 160 |
"""
|
| 161 |
|
| 162 |
-
return
|
| 163 |
|
| 164 |
except Exception as e:
|
| 165 |
-
return
|
| 166 |
|
| 167 |
-
#
|
| 168 |
def create_example_file():
|
| 169 |
"""
|
| 170 |
Create an example CSV file for demonstration
|
|
@@ -197,7 +197,7 @@ iface = gr.Interface(
|
|
| 197 |
)
|
| 198 |
],
|
| 199 |
outputs=[
|
| 200 |
-
gr.File(label="Download Analysis
|
| 201 |
gr.Textbox(label="Analysis Summary", max_lines=10)
|
| 202 |
],
|
| 203 |
title="Enhanced Social Media Analysis System",
|
|
@@ -208,7 +208,6 @@ iface = gr.Interface(
|
|
| 208 |
- Brand Impact Assessment
|
| 209 |
- Critical Issue Detection
|
| 210 |
- Actionable Recommendations
|
| 211 |
-
- Visual Analytics
|
| 212 |
|
| 213 |
Input CSV format:
|
| 214 |
- Required columns: 'post_id', 'text'
|
|
@@ -224,4 +223,4 @@ if __name__ == "__main__":
|
|
| 224 |
iface.launch()
|
| 225 |
finally:
|
| 226 |
if os.path.exists(example_file):
|
| 227 |
-
os.remove(example_file)
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import os
|
| 6 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
|
|
|
| 8 |
# Initialize the classification pipelines
|
| 9 |
sentiment_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 10 |
|
|
|
|
| 14 |
BRAND_IMPACT_LABELS = ["severe", "moderate", "minimal"]
|
| 15 |
ISSUE_CATEGORIES = ["product", "service", "security", "fraud", "compliance", "technical", "billing", "general"]
|
| 16 |
|
| 17 |
+
# Keywords for critical issue detection
|
| 18 |
CRITICAL_KEYWORDS = {
|
| 19 |
'security': ['hack', 'breach', 'leaked', 'stolen', 'unauthorized', 'privacy'],
|
| 20 |
'fraud': ['scam', 'fraud', 'fake', 'unauthorized charge', 'stolen'],
|
|
|
|
| 22 |
'sensitive': ['racist', 'discrimination', 'harassment', 'abuse', 'offensive']
|
| 23 |
}
|
| 24 |
|
| 25 |
+
def classify_text(text, labels, classifier):
|
| 26 |
"""
|
| 27 |
+
Perform zero-shot classification on text
|
| 28 |
"""
|
| 29 |
+
result = classifier(text, labels)
|
| 30 |
+
return result['labels'][0], result['scores'][0]
|
| 31 |
+
|
| 32 |
+
def detect_critical_issues(text):
|
| 33 |
+
"""
|
| 34 |
+
Detect critical issues based on keywords
|
| 35 |
+
"""
|
| 36 |
+
text_lower = text.lower()
|
| 37 |
+
issues = []
|
| 38 |
+
|
| 39 |
+
for category, keywords in CRITICAL_KEYWORDS.items():
|
| 40 |
+
if any(keyword in text_lower for keyword in keywords):
|
| 41 |
+
issues.append(category)
|
| 42 |
+
|
| 43 |
+
return issues
|
| 44 |
+
|
| 45 |
+
def determine_urgency(text, sentiment, critical_issues):
|
| 46 |
+
"""
|
| 47 |
+
Determine urgency level based on content, sentiment, and critical issues
|
| 48 |
+
"""
|
| 49 |
+
if critical_issues:
|
| 50 |
+
return "critical"
|
| 51 |
+
elif "!" in text or "?" in text or any(word in text.lower() for word in ['urgent', 'asap', 'immediately']):
|
| 52 |
+
return "high"
|
| 53 |
+
elif sentiment == "negative":
|
| 54 |
+
return "medium"
|
| 55 |
+
else:
|
| 56 |
+
return "low"
|
| 57 |
+
|
| 58 |
+
def analyze_brand_impact(text, sentiment, critical_issues):
|
| 59 |
+
"""
|
| 60 |
+
Analyze potential brand impact
|
| 61 |
+
"""
|
| 62 |
+
if critical_issues or sentiment == "negative" and ("share" in text.lower() or "viral" in text.lower()):
|
| 63 |
+
return "severe"
|
| 64 |
+
elif sentiment == "negative":
|
| 65 |
+
return "moderate"
|
| 66 |
+
else:
|
| 67 |
+
return "minimal"
|
| 68 |
+
|
| 69 |
+
def generate_recommendations(row):
|
| 70 |
+
"""
|
| 71 |
+
Generate actionable recommendations based on analysis
|
| 72 |
+
"""
|
| 73 |
+
recommendations = []
|
| 74 |
+
|
| 75 |
+
if row['urgency'] == 'critical':
|
| 76 |
+
recommendations.append("Immediate escalation required - Route to crisis management team")
|
| 77 |
+
|
| 78 |
+
if 'security' in row['critical_issues']:
|
| 79 |
+
recommendations.append("Engage security team for immediate investigation")
|
| 80 |
+
elif 'fraud' in row['critical_issues']:
|
| 81 |
+
recommendations.append("Route to fraud prevention team for investigation")
|
| 82 |
+
elif 'compliance' in row['critical_issues']:
|
| 83 |
+
recommendations.append("Escalate to legal/compliance team for review")
|
| 84 |
+
|
| 85 |
+
if row['brand_impact'] == 'severe':
|
| 86 |
+
recommendations.append("Engage PR team for reputation management strategy")
|
| 87 |
+
|
| 88 |
+
if row['sentiment'] == 'negative':
|
| 89 |
+
recommendations.append("Priority customer outreach needed for resolution")
|
| 90 |
+
|
| 91 |
+
return ' | '.join(recommendations) if recommendations else "Standard response protocol"
|
| 92 |
|
| 93 |
def process_csv(file):
|
| 94 |
"""
|
| 95 |
+
Process posts from CSV file with enhanced analysis
|
| 96 |
"""
|
| 97 |
try:
|
| 98 |
# Read the input CSV file
|
|
|
|
| 142 |
# Save results
|
| 143 |
results_df.to_csv(output_file, index=False)
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
# Generate summary statistics
|
| 146 |
total_posts = len(results_df)
|
| 147 |
critical_posts = len(results_df[results_df['urgency'] == 'critical'])
|
|
|
|
| 157 |
Severe Brand Impact Posts: {severe_impact}
|
| 158 |
|
| 159 |
The detailed analysis has been saved to: {output_file}
|
|
|
|
| 160 |
"""
|
| 161 |
|
| 162 |
+
return output_file, summary
|
| 163 |
|
| 164 |
except Exception as e:
|
| 165 |
+
return None, f"Error processing CSV: {str(e)}"
|
| 166 |
|
| 167 |
+
# Create example CSV file
|
| 168 |
def create_example_file():
|
| 169 |
"""
|
| 170 |
Create an example CSV file for demonstration
|
|
|
|
| 197 |
)
|
| 198 |
],
|
| 199 |
outputs=[
|
| 200 |
+
gr.File(label="Download Detailed Analysis"),
|
| 201 |
gr.Textbox(label="Analysis Summary", max_lines=10)
|
| 202 |
],
|
| 203 |
title="Enhanced Social Media Analysis System",
|
|
|
|
| 208 |
- Brand Impact Assessment
|
| 209 |
- Critical Issue Detection
|
| 210 |
- Actionable Recommendations
|
|
|
|
| 211 |
|
| 212 |
Input CSV format:
|
| 213 |
- Required columns: 'post_id', 'text'
|
|
|
|
| 223 |
iface.launch()
|
| 224 |
finally:
|
| 225 |
if os.path.exists(example_file):
|
| 226 |
+
os.remove(example_file)
|