Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,37 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
emotion_classifier = pipeline(
|
| 6 |
"text-classification",
|
| 7 |
model="bhadresh-savani/distilbert-base-uncased-emotion",
|
| 8 |
-
top_k=3
|
| 9 |
)
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
urgent_emotions = {"anger", "annoyance", "disgust", "frustration", "sadness"}
|
| 13 |
moderate_emotions = {"confusion", "concern", "nervousness", "fear"}
|
| 14 |
low_emotions = {"neutral", "approval", "excitement", "joy", "curiosity"}
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def assess_priority(emotion):
|
| 17 |
if emotion in urgent_emotions:
|
| 18 |
return "π΄ High", "β
Immediate human support is recommended."
|
|
@@ -21,33 +40,38 @@ def assess_priority(emotion):
|
|
| 21 |
else:
|
| 22 |
return "π’ Low", "β No human support needed. Automated response is sufficient."
|
| 23 |
|
| 24 |
-
# Streamlit
|
| 25 |
-
st.set_page_config(page_title="AI Customer
|
| 26 |
-
st.title("π AI Customer Emotion &
|
| 27 |
|
| 28 |
-
# User input
|
| 29 |
user_input = st.text_area("Please enter the customer's message or conversation:", height=150)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
if st.button("Analyze Emotion"):
|
| 33 |
if user_input.strip() == "":
|
| 34 |
st.warning("Please enter a message to analyze.")
|
| 35 |
else:
|
| 36 |
-
with st.spinner("Analyzing
|
| 37 |
-
|
|
|
|
| 38 |
emotion_results = emotion_classifier(user_input)
|
| 39 |
top_emotion = emotion_results[0][0]
|
| 40 |
emotion_label = top_emotion['label']
|
| 41 |
emotion_score = top_emotion['score']
|
| 42 |
-
|
| 43 |
-
# Determine priority level
|
| 44 |
priority_level, recommendation = assess_priority(emotion_label)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
st.subheader("π Emotion Analysis Results")
|
| 48 |
st.write(f"**Primary Emotion**: {emotion_label}")
|
| 49 |
st.write(f"**Confidence Score**: {emotion_score:.2f}")
|
| 50 |
|
| 51 |
st.subheader("ποΈ Support Priority Recommendation")
|
| 52 |
st.write(f"**Priority Level**: {priority_level}")
|
| 53 |
st.success(recommendation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load models
|
| 5 |
emotion_classifier = pipeline(
|
| 6 |
"text-classification",
|
| 7 |
model="bhadresh-savani/distilbert-base-uncased-emotion",
|
| 8 |
+
top_k=3
|
| 9 |
)
|
| 10 |
|
| 11 |
+
intent_classifier = pipeline(
|
| 12 |
+
"zero-shot-classification",
|
| 13 |
+
model="facebook/bart-large-mnli"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Define emotion priority rules
|
| 17 |
urgent_emotions = {"anger", "annoyance", "disgust", "frustration", "sadness"}
|
| 18 |
moderate_emotions = {"confusion", "concern", "nervousness", "fear"}
|
| 19 |
low_emotions = {"neutral", "approval", "excitement", "joy", "curiosity"}
|
| 20 |
|
| 21 |
+
# Define candidate customer intents
|
| 22 |
+
candidate_tasks = [
|
| 23 |
+
"change data plan",
|
| 24 |
+
"upgrade phone",
|
| 25 |
+
"top up balance",
|
| 26 |
+
"report network issue",
|
| 27 |
+
"ask for billing help",
|
| 28 |
+
"request human support",
|
| 29 |
+
"check account status",
|
| 30 |
+
"suspend service",
|
| 31 |
+
"reactivate number",
|
| 32 |
+
"cancel subscription"
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
def assess_priority(emotion):
|
| 36 |
if emotion in urgent_emotions:
|
| 37 |
return "π΄ High", "β
Immediate human support is recommended."
|
|
|
|
| 40 |
else:
|
| 41 |
return "π’ Low", "β No human support needed. Automated response is sufficient."
|
| 42 |
|
| 43 |
+
# Streamlit App Interface
|
| 44 |
+
st.set_page_config(page_title="AI Customer Support Analyzer", layout="centered")
|
| 45 |
+
st.title("π AI Customer Emotion & Intent Analyzer")
|
| 46 |
|
|
|
|
| 47 |
user_input = st.text_area("Please enter the customer's message or conversation:", height=150)
|
| 48 |
|
| 49 |
+
if st.button("Analyze"):
|
|
|
|
| 50 |
if user_input.strip() == "":
|
| 51 |
st.warning("Please enter a message to analyze.")
|
| 52 |
else:
|
| 53 |
+
with st.spinner("Analyzing..."):
|
| 54 |
+
|
| 55 |
+
# --- Emotion Classification ---
|
| 56 |
emotion_results = emotion_classifier(user_input)
|
| 57 |
top_emotion = emotion_results[0][0]
|
| 58 |
emotion_label = top_emotion['label']
|
| 59 |
emotion_score = top_emotion['score']
|
|
|
|
|
|
|
| 60 |
priority_level, recommendation = assess_priority(emotion_label)
|
| 61 |
|
| 62 |
+
st.subheader("π Emotion Analysis")
|
|
|
|
| 63 |
st.write(f"**Primary Emotion**: {emotion_label}")
|
| 64 |
st.write(f"**Confidence Score**: {emotion_score:.2f}")
|
| 65 |
|
| 66 |
st.subheader("ποΈ Support Priority Recommendation")
|
| 67 |
st.write(f"**Priority Level**: {priority_level}")
|
| 68 |
st.success(recommendation)
|
| 69 |
+
|
| 70 |
+
# --- Intent Detection ---
|
| 71 |
+
task_result = intent_classifier(user_input, candidate_tasks)
|
| 72 |
+
top_tasks = task_result['labels'][:3]
|
| 73 |
+
top_scores = task_result['scores'][:3]
|
| 74 |
+
|
| 75 |
+
st.subheader("βοΈ Detected Possible Customer Intents")
|
| 76 |
+
for label, score in zip(top_tasks, top_scores):
|
| 77 |
+
st.write(f"πΈ **{label}** (confidence: {score:.2f})")
|