File size: 1,833 Bytes
126f0cf
 
 
 
 
 
 
 
 
373fb15
 
 
 
 
 
 
126f0cf
 
 
 
 
 
 
 
373fb15
 
126f0cf
 
 
 
 
 
 
 
 
 
 
 
373fb15
126f0cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app.py
import gradio as gr
from transformers import pipeline
import torch
import scipy
import sentencepiece
from google.protobuf import text_format
import re

# Load multilingual social media sentiment analysis model
sentiment_model = pipeline(
    "text-classification",
    model="cardiffnlp/twitter-xlm-roberta-base-sentiment",
    tokenizer="cardiffnlp/twitter-xlm-roberta-base-sentiment",
    return_all_scores=False
)

# Function to analyze text and calculate risk
def analyze_text(text):
    result = sentiment_model(text)[0]
    sentiment = result['label']
    score = result['score']

    # Simple risk scoring logic
    # Adjusted to also flag high-intensity "negative" and "neutral" sentiment
    risk_score = score * (1.5 if sentiment.upper() in ["NEGATIVE", "LABEL_0"] else 1.0)
    escalation = risk_score > 0.7  # threshold
    return sentiment, risk_score, escalation

# Process input for Gradio
def process_input(text):
    sentiment, risk_score, escalation = analyze_text(text)
    return sentiment, round(risk_score, 2), "Yes" if escalation else "No"

# Gradio app configuration
title = "SocialAegis MVP"
description = """
A sentiment-based escalation engine to detect emotional volatility in social media posts.
Now using a multilingual, social-media-trained model.
"""

iface = gr.Interface(
    fn=process_input,
    inputs=gr.Textbox(lines=4, placeholder="Enter social media text here..."),
    outputs=[
        gr.Label(label="Sentiment"),
        gr.Label(label="Risk Score"),
        gr.Label(label="Escalation Triggered")
    ],
    title=title,
    description=description,
    examples=[
        ["I am so frustrated with this service!"],
        ["I had an amazing experience, thank you!"],
        ["This is unacceptable, I will report this."]
    ]
)

if __name__ == "__main__":
    iface.launch()