Dhrubob commited on
Commit
a1edd2e
Β·
verified Β·
1 Parent(s): 7182ef0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -0
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()