maxsi commited on
Commit
692adce
·
1 Parent(s): 4bfc9f6

Added urgency, brand impact and issue categories

Browse files
Files changed (1) hide show
  1. app.py +72 -73
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
- # [Previous keyword definitions and helper functions remain the same]
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 create_pie_charts(df):
31
  """
32
- Create pie charts for Urgency, Brand Impact, and Critical Issues
33
  """
34
- # Create a figure with subplots
35
- fig = make_subplots(
36
- rows=1, cols=3,
37
- subplot_titles=('Urgency Distribution', 'Brand Impact Distribution', 'Critical Issues Distribution'),
38
- specs=[[{'type':'pie'}, {'type':'pie'}, {'type':'pie'}]]
39
- )
40
-
41
- # Urgency Pie Chart
42
- urgency_counts = df['urgency'].value_counts()
43
- fig.add_trace(
44
- go.Pie(labels=urgency_counts.index, values=urgency_counts.values,
45
- marker_colors=['#FF0000', '#FFA500', '#FFFF00', '#90EE90']),
46
- row=1, col=1
47
- )
48
-
49
- # Brand Impact Pie Chart
50
- # impact_counts = df['brand_impact'].value_counts()
51
- # fig.add_trace(
52
- # go.Pie(labels=impact_counts.index, values=impact_counts.values,
53
- # marker_colors=['#FF0000', '#FFA500', '#90EE90']),
54
- # row=1, col=2
55
- # )
56
-
57
- # Critical Issues Pie Chart
58
- # Split the combined issues and count unique occurrences
59
- all_issues = df[df['critical_issues'] != 'none']['critical_issues'].str.split('|').explode()
60
- # if len(all_issues) == 0:
61
- # issue_counts = pd.Series({'No Critical Issues': 1})
62
- # else:
63
- # issue_counts = all_issues.value_counts()
64
-
65
- # fig.add_trace(
66
- # go.Pie(labels=issue_counts.index, values=issue_counts.values,
67
- # marker_colors=['#FF0000', '#FFA500', '#FFFF00', '#90EE90', '#4169E1']),
68
- # row=1, col=3
69
- # )
70
-
71
- # Update layout
72
- fig.update_layout(
73
- height=400,
74
- width=1200,
75
- showlegend=True,
76
- title_text="Analysis Distribution",
77
- )
78
-
79
- # Save the plot to a bytes buffer
80
- buf = io.BytesIO()
81
- fig.write_image(buf, format='png')
82
- buf.seek(0)
83
-
84
- return buf
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  def process_csv(file):
87
  """
88
- Process posts from CSV file with enhanced analysis and visualizations
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 [output_file, charts_file], summary
163
 
164
  except Exception as e:
165
- return [None, None], f"Error processing CSV: {str(e)}"
166
 
167
- # [Previous example file creation function remains the same]
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 Files", file_count="multiple"),
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)