import gradio as gr from transformers import pipeline import pandas as pd from datetime import datetime import os # ----------------------------- # Load pretrained emotion model # ----------------------------- model_name = "superb/hubert-large-superb-er" emotion_classifier = pipeline("audio-classification", model=model_name) # Emotion mapping (short label โ†’ full name + emoji) EMOTION_MAP = { "ang": ("Angry", "๐Ÿ˜ก"), "hap": ("Happy", "๐Ÿ˜„"), "neu": ("Neutral", "๐Ÿ˜"), "sad": ("Sad", "๐Ÿ˜ข"), "exc": ("Excited", "๐Ÿคฉ"), "fru": ("Frustrated", "๐Ÿ˜ค"), "fea": ("Fearful", "๐Ÿ˜จ"), "sur": ("Surprised", "๐Ÿ˜ฒ"), "dis": ("Disgusted", "๐Ÿคข"), } # ----------------------------- # Setup data storage # ----------------------------- os.makedirs("data", exist_ok=True) CSV_PATH = "data/customer_complaints_emotion.csv" # Example categories โ€” these can be linked to your delivery/order system ORDER_ISSUES = [ "Late Delivery", "Damaged Package", "Wrong Product Delivered", "Missing Items", "Rude Delivery Staff", "Payment Issue", "Return/Refund Request", "Order Cancelled Automatically", "Other Complaint" ] # ----------------------------- # Emotion analysis function # ----------------------------- def analyze_complaint(audio_file, complaint_type, order_id): if audio_file is None or not complaint_type or not order_id: return "โš ๏ธ Please upload an audio complaint, select issue type, and enter Order ID." # Run prediction results = emotion_classifier(audio_file) results = sorted(results, key=lambda x: x['score'], reverse=True) top = results[0] label, emoji = EMOTION_MAP.get(top['label'], (top['label'], "๐ŸŽญ")) score = round(top['score'] * 100, 2) # ----------------------------- # Build dashboard text # ----------------------------- dashboard = f"## ๐Ÿ“ž Customer Complaint Emotion Dashboard\n" dashboard += f"### ๐ŸŽฏ Detected Emotion: {emoji} **{label.upper()} ({score}%)**\n\n" dashboard += f"### ๐Ÿ“ฆ Complaint Type: **{complaint_type}**\n" dashboard += f"### ๐Ÿงพ Order ID: **{order_id}**\n\n" dashboard += f"### ๐Ÿ“Š Emotion Breakdown\n" for r in results: lbl, emo = EMOTION_MAP.get(r['label'], (r['label'], "๐ŸŽญ")) scr = round(r['score'] * 100, 2) bar = "โ–ˆ" * int(scr / 5) dashboard += f"{emo} **{lbl}**: {scr}% \n{bar}\n\n" # ----------------------------- # Save results to CSV # ----------------------------- df = pd.DataFrame({ "datetime": [datetime.now().strftime("%Y-%m-%d %H:%M:%S")], "order_id": [order_id], "complaint_type": [complaint_type], "emotion": [label], "score": [score] }) if os.path.exists(CSV_PATH): df.to_csv(CSV_PATH, mode="a", header=False, index=False) else: df.to_csv(CSV_PATH, index=False) return dashboard # ----------------------------- # Gradio Interface # ----------------------------- app = gr.Interface( fn=analyze_complaint, inputs=[ gr.Audio(type="filepath", label="๐ŸŽ™๏ธ Upload Customer Complaint Voice"), gr.Dropdown(ORDER_ISSUES, label="๐Ÿšš Select Complaint Type"), gr.Textbox(label="๐Ÿ“ฆ Enter Order ID"), ], outputs="markdown", title="๐ŸŽง Customer Service Emotion Analyzer", description=( "Analyze voice-based customer complaints to detect emotions and " "prioritize based on emotional intensity. Ideal for supply chain " "and logistics feedback monitoring." ), allow_flagging="never", ) # ----------------------------- # Run the app # ----------------------------- if __name__ == "__main__": app.launch()