Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
|
5 |
+
sentiment_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
6 |
+
flagged_words = ["hate", "stupid", "ugly", "bad"]
|
7 |
+
positive_flags = ["love", "amazing", "great"]
|
8 |
+
negative_flags = ["awful", "terrible", "worst"]
|
9 |
+
|
10 |
+
def analyze_message(message, custom_flagged_words, custom_positive_flags, custom_negative_flags):
|
11 |
+
flagged_list = custom_flagged_words.split(",") if custom_flagged_words else []
|
12 |
+
positive_list = custom_positive_flags.split(",") if custom_positive_flags else []
|
13 |
+
negative_list = custom_negative_flags.split(",") if custom_negative_flags else []
|
14 |
+
|
15 |
+
sentiment_result = sentiment_pipeline(message)
|
16 |
+
sentiment_label = sentiment_result[0]['label']
|
17 |
+
flagged_terms = [word for word in flagged_list if word in message.lower()]
|
18 |
+
positive_terms = [word for word in positive_list if word in message.lower()]
|
19 |
+
negative_terms = [word for word in negative_list if word in message.lower()]
|
20 |
+
|
21 |
+
flagged_status = "Yes" if flagged_terms else "No"
|
22 |
+
|
23 |
+
# Rule-based flagging
|
24 |
+
if sentiment_label == "POSITIVE" and positive_terms:
|
25 |
+
sentiment_label = "FLAGGED POSITIVE"
|
26 |
+
elif sentiment_label == "NEGATIVE" and negative_terms:
|
27 |
+
sentiment_label = "FLAGGED NEGATIVE"
|
28 |
+
|
29 |
+
|
30 |
+
return sentiment_label, ", ".join(flagged_terms) if flagged_terms else "None"
|
31 |
+
|
32 |
+
with gr.Blocks() as demo:
|
33 |
+
gr.Markdown("## Messaging Sentiment analysis")
|
34 |
+
|
35 |
+
with gr.Row():
|
36 |
+
input_text = gr.Textbox(label="Enter Message")
|
37 |
+
submit_btn = gr.Button("submit")
|
38 |
+
|
39 |
+
sentiment_output = gr.Textbox(label="Sentiment Category", interactive=False)
|
40 |
+
flagged_words_output = gr.Textbox(label="Flagged Words", interactive=False)
|
41 |
+
|
42 |
+
gr.Markdown("### Customize Flagging Rules")
|
43 |
+
custom_flagged_words = gr.Textbox(label="Custom Flagged Words", value=", ".join(flagged_words))
|
44 |
+
custom_positive_flags = gr.Textbox(label="Words to Flag Positive Messages", value=", ".join(positive_flags))
|
45 |
+
custom_negative_flags = gr.Textbox(label="Words to Flag Negative Messages", value=", ".join(negative_flags))
|
46 |
+
|
47 |
+
submit_btn.click(analyze_message,
|
48 |
+
inputs=[input_text, custom_flagged_words, custom_positive_flags, custom_negative_flags],
|
49 |
+
outputs=[sentiment_output, flagged_words_output])
|
50 |
+
|
51 |
+
demo.launch()
|