SocialAegis.org / app.py
Vinay115's picture
Update app.py
373fb15 verified
# 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()