Vinay115 commited on
Commit
126f0cf
·
verified ·
1 Parent(s): 5224131

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -93
app.py CHANGED
@@ -1,93 +1,53 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": null,
6
- "id": "1f0fddef-d467-4a41-b0d3-0107c8393190",
7
- "metadata": {},
8
- "outputs": [],
9
- "source": [
10
- "import gradio as gr\n",
11
- "from transformers import pipeline\n",
12
- "import torch\n",
13
- "import scipy\n",
14
- "import sentencepiece\n",
15
- "from google.protobuf import text_format\n",
16
- "import re\n",
17
- "\n",
18
- "# Load sentiment analysis pipeline\n",
19
- "sentiment_model = pipeline(\"sentiment-analysis\")\n",
20
- "\n",
21
- "# Function to analyze text and calculate risk\n",
22
- "def analyze_text(text):\n",
23
- " result = sentiment_model(text)[0]\n",
24
- " sentiment = result['label']\n",
25
- " score = result['score']\n",
26
- "\n",
27
- " # Simple risk scoring logic\n",
28
- " risk_score = score * (1.5 if sentiment == \"NEGATIVE\" else 1.0)\n",
29
- " escalation = risk_score > 0.7 # threshold\n",
30
- " return sentiment, risk_score, escalation\n",
31
- "\n",
32
- "# Process input for Gradio\n",
33
- "def process_input(text):\n",
34
- " sentiment, risk_score, escalation = analyze_text(text)\n",
35
- " return sentiment, round(risk_score, 2), \"Yes\" if escalation else \"No\"\n",
36
- "\n",
37
- "title = \"SocialAegis MVP\"\n",
38
- "description = \"\"\"\n",
39
- "A sentiment-based escalation engine to detect emotional volatility in social media posts.\n",
40
- "\"\"\"\n",
41
- "\n",
42
- "iface = gr.Interface(\n",
43
- " fn=process_input,\n",
44
- " inputs=gr.Textbox(lines=4, placeholder=\"Enter social media text here...\"),\n",
45
- " outputs=[\n",
46
- " gr.Label(label=\"Sentiment\"),\n",
47
- " gr.Label(label=\"Risk Score\"),\n",
48
- " gr.Label(label=\"Escalation Triggered\")\n",
49
- " ],\n",
50
- " title=title,\n",
51
- " description=description,\n",
52
- " examples=[\n",
53
- " [\"I am so frustrated with this service!\"],\n",
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()