File size: 11,622 Bytes
bbffd8d
 
 
4bfc9f6
bbffd8d
4bfc9f6
398d44f
 
 
4bfc9f6
 
 
bbffd8d
4bfc9f6
 
 
 
 
bbffd8d
692adce
4bfc9f6
 
 
 
 
 
bbffd8d
398d44f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
692adce
bbffd8d
692adce
bbffd8d
692adce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398d44f
692adce
 
398d44f
692adce
398d44f
692adce
398d44f
692adce
 
398d44f
692adce
 
398d44f
692adce
398d44f
bbffd8d
 
 
692adce
bbffd8d
 
 
 
 
4bfc9f6
bbffd8d
398d44f
bbffd8d
4bfc9f6
 
 
 
 
 
 
 
bbffd8d
4bfc9f6
 
bbffd8d
4bfc9f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398d44f
 
 
4bfc9f6
 
 
 
 
 
 
 
 
 
 
 
 
 
398d44f
 
 
 
 
4bfc9f6
 
 
 
 
398d44f
 
 
 
 
 
 
 
 
 
4bfc9f6
 
398d44f
bbffd8d
 
398d44f
bbffd8d
398d44f
bbffd8d
 
 
 
 
398d44f
bbffd8d
398d44f
 
 
 
 
 
 
 
 
 
bbffd8d
 
 
 
 
 
 
398d44f
bbffd8d
 
398d44f
 
 
 
 
 
 
 
 
 
 
 
bbffd8d
 
 
 
 
398d44f
bbffd8d
 
 
 
398d44f
 
 
bbffd8d
398d44f
bbffd8d
398d44f
 
 
 
 
 
 
 
 
 
 
bbffd8d
 
4bfc9f6
398d44f
 
 
bbffd8d
 
 
 
 
 
 
398d44f
1
2
3
4
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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import gradio as gr
from transformers import pipeline
import pandas as pd
import numpy as np
import os
from datetime import datetime
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

# Initialize the classification pipelines
sentiment_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")

# Define various classification labels
SENTIMENT_LABELS = ["positive", "negative", "neutral"]
URGENCY_LABELS = ["critical", "high", "medium", "low"]
BRAND_IMPACT_LABELS = ["severe", "moderate", "minimal"]
ISSUE_CATEGORIES = ["product", "service", "security", "fraud", "compliance", "technical", "billing", "general"]

# Keywords for critical issue detection
CRITICAL_KEYWORDS = {
    'security': ['hack', 'breach', 'leaked', 'stolen', 'unauthorized', 'privacy'],
    'fraud': ['scam', 'fraud', 'fake', 'unauthorized charge', 'stolen'],
    'compliance': ['lawsuit', 'legal', 'regulation', 'complaint', 'policy violation'],
    'sensitive': ['racist', 'discrimination', 'harassment', 'abuse', 'offensive']
}

def create_charts(df):
    """
    Create visualization charts using Plotly
    """
    # Create subplot figure
    fig = make_subplots(
        rows=2, cols=2,
        subplot_titles=("Urgency Distribution", "Sentiment Analysis",
                       "Brand Impact Assessment", "Critical Issues Breakdown"),
        specs=[[{"type": "pie"}, {"type": "pie"}],
               [{"type": "pie"}, {"type": "bar"}]]
    )

    # 1. Urgency Distribution Pie Chart
    urgency_counts = df['urgency'].value_counts()
    fig.add_trace(
        go.Pie(labels=urgency_counts.index,
               values=urgency_counts.values,
               marker=dict(colors=['#ff0000', '#ff6666', '#ffcccc', '#ffe6e6'])),
        row=1, col=1
    )

    # 2. Sentiment Analysis Pie Chart
    sentiment_counts = df['sentiment'].value_counts()
    fig.add_trace(
        go.Pie(labels=sentiment_counts.index,
               values=sentiment_counts.values,
               marker=dict(colors=['#00cc00', '#ff0000', '#cccccc'])),
        row=1, col=2
    )

    # 3. Brand Impact Pie Chart
    impact_counts = df['brand_impact'].value_counts()
    fig.add_trace(
        go.Pie(labels=impact_counts.index,
               values=impact_counts.values,
               marker=dict(colors=['#ff0000', '#ff9933', '#ffcc00'])),
        row=2, col=1
    )

    # 4. Critical Issues Bar Chart
    critical_issues = df['critical_issues'].str.split('|', expand=True).stack()
    critical_counts = critical_issues[critical_issues != 'none'].value_counts()
    fig.add_trace(
        go.Bar(x=critical_counts.index,
               y=critical_counts.values,
               marker_color='#ff0000'),
        row=2, col=2
    )

    # Update layout
    fig.update_layout(
        height=800,
        showlegend=True,
        title_text="Social Media Analysis Dashboard",
        title_x=0.5,
        title_font_size=20,
        paper_bgcolor='rgba(0,0,0,0)',
        plot_bgcolor='rgba(0,0,0,0)'
    )

    # Save the figure
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    chart_file = f"analysis_dashboard_{timestamp}.html"
    fig.write_html(chart_file)

    return chart_file

def classify_text(text, labels, classifier):
    """
    Perform zero-shot classification on text
    """
    result = classifier(text, labels)
    return result['labels'][0], result['scores'][0]

def detect_critical_issues(text):
    """
    Detect critical issues based on keywords
    """
    text_lower = text.lower()
    issues = []

    for category, keywords in CRITICAL_KEYWORDS.items():
        if any(keyword in text_lower for keyword in keywords):
            issues.append(category)

    return issues

def determine_urgency(text, sentiment, critical_issues):
    """
    Determine urgency level based on content, sentiment, and critical issues
    """
    if critical_issues:
        return "critical"
    elif "!" in text or "?" in text or any(word in text.lower() for word in ['urgent', 'asap', 'immediately']):
        return "high"
    elif sentiment == "negative":
        return "medium"
    else:
        return "low"

def analyze_brand_impact(text, sentiment, critical_issues):
    """
    Analyze potential brand impact
    """
    if critical_issues or sentiment == "negative" and ("share" in text.lower() or "viral" in text.lower()):
        return "severe"
    elif sentiment == "negative":
        return "moderate"
    else:
        return "minimal"

def generate_recommendations(row):
    """
    Generate actionable recommendations based on analysis
    """
    recommendations = []

    if row['urgency'] == 'critical':
        recommendations.append("🚨 IMMEDIATE ESCALATION REQUIRED - Route to crisis management team")

    if 'security' in row['critical_issues']:
        recommendations.append("πŸ”’ Engage security team for immediate investigation")
    elif 'fraud' in row['critical_issues']:
        recommendations.append("⚠️ Route to fraud prevention team for investigation")
    elif 'compliance' in row['critical_issues']:
        recommendations.append("πŸ“œ Escalate to legal/compliance team for review")

    if row['brand_impact'] == 'severe':
        recommendations.append("πŸ“’ Engage PR team for reputation management strategy")

    if row['sentiment'] == 'negative':
        recommendations.append("πŸ”₯ Priority customer outreach needed for resolution")

    return ' | '.join(recommendations) if recommendations else "βœ… Standard response protocol"

def process_csv(file):
    """
    Process posts from CSV file with enhanced analysis
    """
    try:
        # Read the input CSV file
        df = pd.read_csv(file.name)

        # Verify required columns
        if 'post_id' not in df.columns or 'text' not in df.columns:
            return None, None, "Error: CSV must contain 'post_id' and 'text' columns"

        # Perform comprehensive analysis
        analysis_results = []

        for _, row in df.iterrows():
            text = row['text']

            # Basic sentiment analysis
            sentiment, sentiment_score = classify_text(text, SENTIMENT_LABELS, sentiment_classifier)

            # Detect critical issues
            critical_issues = detect_critical_issues(text)

            # Determine urgency and brand impact
            urgency = determine_urgency(text, sentiment, critical_issues)
            brand_impact = analyze_brand_impact(text, sentiment, critical_issues)

            # Store results
            analysis_results.append({
                'post_id': row['post_id'],
                'text': text,
                'sentiment': sentiment,
                'sentiment_confidence': round(sentiment_score, 3),
                'urgency': urgency,
                'brand_impact': brand_impact,
                'critical_issues': '|'.join(critical_issues) if critical_issues else 'none',
            })

        # Create results DataFrame
        results_df = pd.DataFrame(analysis_results)

        # Generate recommendations
        results_df['recommendations'] = results_df.apply(generate_recommendations, axis=1)

        # Create visualization dashboard
        dashboard_file = create_charts(results_df)

        # Add analysis timestamp
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        output_file = f"social_media_analysis_{timestamp}.csv"

        # Save results
        results_df.to_csv(output_file, index=False)

        # Generate summary statistics
        total_posts = len(results_df)
        critical_posts = len(results_df[results_df['urgency'] == 'critical'])
        negative_sentiment = len(results_df[results_df['sentiment'] == 'negative'])
        severe_impact = len(results_df[results_df['brand_impact'] == 'severe'])

        summary = f"""
🎯 Real-Time Social Media Intelligence Report
==========================================

πŸ“Š Key Metrics:
-------------
Total Posts Analyzed: {total_posts}
Critical Issues Requiring Immediate Attention: {critical_posts}
Negative Sentiment Posts: {negative_sentiment}
Severe Brand Impact Posts: {severe_impact}

⚑ Quick Actions Required:
----------------------
- {critical_posts} posts need immediate escalation
- {severe_impact} posts require PR team intervention
- {negative_sentiment} posts need customer satisfaction follow-up

πŸ’‘ AI-Powered Analysis Complete:
----------------------------
Detailed analysis saved to: {output_file}
Interactive dashboard saved to: {dashboard_file}
        """

        return output_file, dashboard_file, summary

    except Exception as e:
        return None, None, f"Error processing CSV: {str(e)}"

# Create example CSV file with more diverse cases
def create_example_file():
    """
    Create an example CSV file for demonstration
    """
    example_data = {
        'post_id': range(1, 11),
        'text': [
            "Just experienced a major security breach! My account was hacked and sensitive data leaked. This is unacceptable! #cybersecurity #breach",
            "Thank you for the amazing customer service! The team went above and beyond to help me. Truly impressed! 🌟",
            "Your latest app update is constantly crashing. Can't access my account for 3 days now. Fix this ASAP!",
            "Noticed some suspicious charges on my account. Possible fraud? Need immediate assistance! 🚨",
            "Love the new features you've added! Makes my work so much easier. Keep innovating! πŸ‘",
            "Planning to file a legal complaint due to repeated policy violations. This needs attention.",
            "System down again? This is the third time this week. Considering switching to your competitor.",
            "Your product has completely transformed our business operations. Best investment ever! πŸš€",
            "Experiencing discrimination from your staff. This is unacceptable and I'm reporting it.",
            "Warning to others: Potential scam detected in recent transactions. Be careful!"
        ]
    }
    df = pd.DataFrame(example_data)
    example_file = "example_input.csv"
    df.to_csv(example_file, index=False)
    return example_file

# Create the example file
example_file = create_example_file()

# Create Gradio interface with custom theme
theme = gr.themes.Base(
    primary_hue="red",
    secondary_hue="red",
)

css = """
.gradio-container {
    background: linear-gradient(to bottom right, #ffffff, #ffecec);
}
"""

# Create Gradio interface
iface = gr.Interface(
    fn=process_csv,
    inputs=[
        gr.File(
            label="Upload CSV File πŸ“",
            file_types=[".csv"]
        )
    ],
    outputs=[
        gr.File(label="Download Detailed Analysis Report πŸ“Š"),
        gr.File(label="Download Interactive Dashboard πŸ“ˆ"),
        gr.Textbox(label="Real-Time Analysis Summary πŸ“±", max_lines=15)
    ],
    title="πŸš€ NoCode Ninjas: AI-Powered Social Media Intelligence Platform",
    description="""
    ### Enterprise-Grade Social Media Analytics with Advanced AI

    Transform your social media monitoring with our cutting-edge AI analysis platform:

    🎯 **Real-Time Sentiment Analysis**
    πŸ” **Urgent Issue Detection**
    ⚑ **Instant Crisis Alerts**
    πŸ“Š **Brand Impact Assessment**
    πŸ€– **AI-Driven Recommendations**

    *Trusted by leading brands for proactive social media management and crisis prevention.*
    """,
    examples=[
        [example_file]
    ],
    theme=theme,
    css=css
)

if __name__ == "__main__":
    try:
        iface.launch()
    finally:
        if os.path.exists(example_file):
            os.remove(example_file)