Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +1,53 @@
|
|
| 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 |
-
" [\"I had an amazing experience, thank you!\"],\n",
|
| 55 |
-
" [\"This is unacceptable, I will report this.\"]\n",
|
| 56 |
-
" ]\n",
|
| 57 |
-
")\n",
|
| 58 |
-
"\n",
|
| 59 |
-
"if __name__ == \"__main__\":\n",
|
| 60 |
-
" iface.launch()\n"
|
| 61 |
-
]
|
| 62 |
-
},
|
| 63 |
-
{
|
| 64 |
-
"cell_type": "code",
|
| 65 |
-
"execution_count": null,
|
| 66 |
-
"id": "4d59bd58-8c46-4707-81b4-bf7780d27812",
|
| 67 |
-
"metadata": {},
|
| 68 |
-
"outputs": [],
|
| 69 |
-
"source": []
|
| 70 |
-
}
|
| 71 |
-
],
|
| 72 |
-
"metadata": {
|
| 73 |
-
"kernelspec": {
|
| 74 |
-
"display_name": "Python 3 (ipykernel)",
|
| 75 |
-
"language": "python",
|
| 76 |
-
"name": "python3"
|
| 77 |
-
},
|
| 78 |
-
"language_info": {
|
| 79 |
-
"codemirror_mode": {
|
| 80 |
-
"name": "ipython",
|
| 81 |
-
"version": 3
|
| 82 |
-
},
|
| 83 |
-
"file_extension": ".py",
|
| 84 |
-
"mimetype": "text/x-python",
|
| 85 |
-
"name": "python",
|
| 86 |
-
"nbconvert_exporter": "python",
|
| 87 |
-
"pygments_lexer": "ipython3",
|
| 88 |
-
"version": "3.13.5"
|
| 89 |
-
}
|
| 90 |
-
},
|
| 91 |
-
"nbformat": 4,
|
| 92 |
-
"nbformat_minor": 5
|
| 93 |
-
}
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import torch
|
| 5 |
+
import scipy
|
| 6 |
+
import sentencepiece
|
| 7 |
+
from google.protobuf import text_format
|
| 8 |
+
import re
|
| 9 |
+
|
| 10 |
+
# Load sentiment analysis pipeline
|
| 11 |
+
sentiment_model = pipeline("sentiment-analysis")
|
| 12 |
+
|
| 13 |
+
# Function to analyze text and calculate risk
|
| 14 |
+
def analyze_text(text):
|
| 15 |
+
result = sentiment_model(text)[0]
|
| 16 |
+
sentiment = result['label']
|
| 17 |
+
score = result['score']
|
| 18 |
+
|
| 19 |
+
# Simple risk scoring logic
|
| 20 |
+
risk_score = score * (1.5 if sentiment == "NEGATIVE" else 1.0)
|
| 21 |
+
escalation = risk_score > 0.7 # threshold
|
| 22 |
+
return sentiment, risk_score, escalation
|
| 23 |
+
|
| 24 |
+
# Process input for Gradio
|
| 25 |
+
def process_input(text):
|
| 26 |
+
sentiment, risk_score, escalation = analyze_text(text)
|
| 27 |
+
return sentiment, round(risk_score, 2), "Yes" if escalation else "No"
|
| 28 |
+
|
| 29 |
+
# Gradio app configuration
|
| 30 |
+
title = "SocialAegis MVP"
|
| 31 |
+
description = """
|
| 32 |
+
A sentiment-based escalation engine to detect emotional volatility in social media posts.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
iface = gr.Interface(
|
| 36 |
+
fn=process_input,
|
| 37 |
+
inputs=gr.Textbox(lines=4, placeholder="Enter social media text here..."),
|
| 38 |
+
outputs=[
|
| 39 |
+
gr.Label(label="Sentiment"),
|
| 40 |
+
gr.Label(label="Risk Score"),
|
| 41 |
+
gr.Label(label="Escalation Triggered")
|
| 42 |
+
],
|
| 43 |
+
title=title,
|
| 44 |
+
description=description,
|
| 45 |
+
examples=[
|
| 46 |
+
["I am so frustrated with this service!"],
|
| 47 |
+
["I had an amazing experience, thank you!"],
|
| 48 |
+
["This is unacceptable, I will report this."]
|
| 49 |
+
]
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|