Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# -----------------------------
|
| 8 |
+
# Load pretrained emotion model
|
| 9 |
+
# -----------------------------
|
| 10 |
+
model_name = "superb/hubert-large-superb-er"
|
| 11 |
+
emotion_classifier = pipeline("audio-classification", model=model_name)
|
| 12 |
+
|
| 13 |
+
# Emotion mapping (short label β full name + emoji)
|
| 14 |
+
EMOTION_MAP = {
|
| 15 |
+
"ang": ("Angry", "π‘"),
|
| 16 |
+
"hap": ("Happy", "π"),
|
| 17 |
+
"neu": ("Neutral", "π"),
|
| 18 |
+
"sad": ("Sad", "π’"),
|
| 19 |
+
"exc": ("Excited", "π€©"),
|
| 20 |
+
"fru": ("Frustrated", "π€"),
|
| 21 |
+
"fea": ("Fearful", "π¨"),
|
| 22 |
+
"sur": ("Surprised", "π²"),
|
| 23 |
+
"dis": ("Disgusted", "π€’"),
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# -----------------------------
|
| 27 |
+
# Setup data storage
|
| 28 |
+
# -----------------------------
|
| 29 |
+
os.makedirs("data", exist_ok=True)
|
| 30 |
+
CSV_PATH = "data/customer_complaints_emotion.csv"
|
| 31 |
+
|
| 32 |
+
# Example categories β these can be linked to your delivery/order system
|
| 33 |
+
ORDER_ISSUES = [
|
| 34 |
+
"Late Delivery",
|
| 35 |
+
"Damaged Package",
|
| 36 |
+
"Wrong Product Delivered",
|
| 37 |
+
"Missing Items",
|
| 38 |
+
"Rude Delivery Staff",
|
| 39 |
+
"Payment Issue",
|
| 40 |
+
"Return/Refund Request",
|
| 41 |
+
"Order Cancelled Automatically",
|
| 42 |
+
"Other Complaint"
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
# -----------------------------
|
| 46 |
+
# Emotion analysis function
|
| 47 |
+
# -----------------------------
|
| 48 |
+
def analyze_complaint(audio_file, complaint_type, order_id):
|
| 49 |
+
if audio_file is None or not complaint_type or not order_id:
|
| 50 |
+
return "β οΈ Please upload an audio complaint, select issue type, and enter Order ID."
|
| 51 |
+
|
| 52 |
+
# Run prediction
|
| 53 |
+
results = emotion_classifier(audio_file)
|
| 54 |
+
results = sorted(results, key=lambda x: x['score'], reverse=True)
|
| 55 |
+
top = results[0]
|
| 56 |
+
label, emoji = EMOTION_MAP.get(top['label'], (top['label'], "π"))
|
| 57 |
+
score = round(top['score'] * 100, 2)
|
| 58 |
+
|
| 59 |
+
# -----------------------------
|
| 60 |
+
# Build dashboard text
|
| 61 |
+
# -----------------------------
|
| 62 |
+
dashboard = f"## π Customer Complaint Emotion Dashboard\n"
|
| 63 |
+
dashboard += f"### π― Detected Emotion: {emoji} **{label.upper()} ({score}%)**\n\n"
|
| 64 |
+
dashboard += f"### π¦ Complaint Type: **{complaint_type}**\n"
|
| 65 |
+
dashboard += f"### π§Ύ Order ID: **{order_id}**\n\n"
|
| 66 |
+
dashboard += f"### π Emotion Breakdown\n"
|
| 67 |
+
|
| 68 |
+
for r in results:
|
| 69 |
+
lbl, emo = EMOTION_MAP.get(r['label'], (r['label'], "π"))
|
| 70 |
+
scr = round(r['score'] * 100, 2)
|
| 71 |
+
bar = "β" * int(scr / 5)
|
| 72 |
+
dashboard += f"{emo} **{lbl}**: {scr}% \n{bar}\n\n"
|
| 73 |
+
|
| 74 |
+
# -----------------------------
|
| 75 |
+
# Save results to CSV
|
| 76 |
+
# -----------------------------
|
| 77 |
+
df = pd.DataFrame({
|
| 78 |
+
"datetime": [datetime.now().strftime("%Y-%m-%d %H:%M:%S")],
|
| 79 |
+
"order_id": [order_id],
|
| 80 |
+
"complaint_type": [complaint_type],
|
| 81 |
+
"emotion": [label],
|
| 82 |
+
"score": [score]
|
| 83 |
+
})
|
| 84 |
+
if os.path.exists(CSV_PATH):
|
| 85 |
+
df.to_csv(CSV_PATH, mode="a", header=False, index=False)
|
| 86 |
+
else:
|
| 87 |
+
df.to_csv(CSV_PATH, index=False)
|
| 88 |
+
|
| 89 |
+
return dashboard
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
# -----------------------------
|
| 93 |
+
# Gradio Interface
|
| 94 |
+
# -----------------------------
|
| 95 |
+
app = gr.Interface(
|
| 96 |
+
fn=analyze_complaint,
|
| 97 |
+
inputs=[
|
| 98 |
+
gr.Audio(type="filepath", label="ποΈ Upload Customer Complaint Voice"),
|
| 99 |
+
gr.Dropdown(ORDER_ISSUES, label="π Select Complaint Type"),
|
| 100 |
+
gr.Textbox(label="π¦ Enter Order ID"),
|
| 101 |
+
],
|
| 102 |
+
outputs="markdown",
|
| 103 |
+
title="π§ Customer Service Emotion Analyzer",
|
| 104 |
+
description=(
|
| 105 |
+
"Analyze voice-based customer complaints to detect emotions and "
|
| 106 |
+
"prioritize based on emotional intensity. Ideal for supply chain "
|
| 107 |
+
"and logistics feedback monitoring."
|
| 108 |
+
),
|
| 109 |
+
allow_flagging="never",
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# -----------------------------
|
| 113 |
+
# Run the app
|
| 114 |
+
# -----------------------------
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
app.launch()
|