Dhrubob commited on
Commit
67b7919
Β·
verified Β·
1 Parent(s): 2412007

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -18
app.py CHANGED
@@ -22,6 +22,8 @@ def init_db():
22
  CREATE TABLE IF NOT EXISTS emotion_logs (
23
  id INTEGER PRIMARY KEY AUTOINCREMENT,
24
  datetime TEXT,
 
 
25
  emotion TEXT,
26
  score REAL
27
  )
@@ -49,12 +51,12 @@ EMOTION_MAP = {
49
  # -------------------------------------------------
50
  # πŸ’Ύ LOGGING
51
  # -------------------------------------------------
52
- def log_to_db(datetime_str, emotion, score):
53
  conn = sqlite3.connect(DB_PATH)
54
  cursor = conn.cursor()
55
  cursor.execute(
56
- "INSERT INTO emotion_logs (datetime, emotion, score) VALUES (?, ?, ?)",
57
- (datetime_str, emotion, score),
58
  )
59
  conn.commit()
60
  conn.close()
@@ -62,27 +64,40 @@ def log_to_db(datetime_str, emotion, score):
62
  # -------------------------------------------------
63
  # 🧠 EMOTION ANALYSIS
64
  # -------------------------------------------------
65
- def analyze_emotion(audio):
66
  if audio is None:
67
  return "⚠️ Please record or upload audio first.", None
68
 
 
 
 
 
 
 
 
 
 
 
69
  if isinstance(audio, tuple):
70
  audio_file = audio[0]
71
  else:
72
  audio_file = audio
73
 
 
74
  results = emotion_classifier(audio_file)
75
  results = sorted(results, key=lambda x: x['score'], reverse=True)
76
  top = results[0]
77
  label, emoji, color = EMOTION_MAP.get(top['label'], (top['label'], "🎭", "#607d8b"))
78
  score = round(top['score'] * 100, 2)
79
 
80
- log_to_db(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), label, score)
 
81
 
82
- # Dashboard HTML
83
  dashboard_html = f"""
84
  <div style="text-align:center; padding:15px;">
85
- <h2>🎀 Emotion Detected</h2>
 
86
  <h3 style="color:{color}; font-size:26px;">{emoji} {label.upper()} β€” {score}%</h3>
87
  <p style="color:#666;">Detected Emotion Intensity (Confidence)</p>
88
  <div style="width:80%; margin:auto; background:#eee; border-radius:20px;">
@@ -101,11 +116,11 @@ def analyze_emotion(audio):
101
 
102
  # AI Insight
103
  insights = random.choice([
104
- "🧩 Calm and balanced tone detected β€” great composure!",
105
- "⚑ Slight emotional tension observed β€” consider a short break.",
106
- "πŸ’¬ Positive tone! Keep the good vibes going.",
107
- "🚨 Signs of stress β€” communication may need attention.",
108
- "πŸ“ˆ Emotional variation increasing β€” track for next interactions."
109
  ])
110
 
111
  insight_html = f"""
@@ -118,42 +133,67 @@ def analyze_emotion(audio):
118
 
119
  return dashboard_html, insight_html
120
 
 
121
  # -------------------------------------------------
122
  # 🎨 GRADIO APP DESIGN
123
  # -------------------------------------------------
 
 
 
124
  with gr.Blocks(theme=gr.themes.Soft()) as app:
 
125
  gr.HTML("""
126
  <div style="text-align:center; padding:20px;">
127
- <h1>🎧 Emotion Recognition Dashboard</h1>
128
  <p style="font-size:16px; color:#555;">
129
- Detect emotional tone directly from voice recordings.<br>
130
- Ideal for HR, leadership, or mental wellness tracking.
131
  </p>
132
  </div>
133
  """)
134
 
 
135
  with gr.Row():
136
  with gr.Column(scale=1):
 
 
 
 
 
 
137
  audio_input = gr.Audio(
138
  sources=["microphone", "upload"],
139
  type="filepath",
140
- label="πŸŽ™οΈ Record or Upload Meeting's Audio",
141
  )
142
  analyze_btn = gr.Button("πŸš€ Analyze Emotion", variant="primary")
 
143
  with gr.Column(scale=2):
144
  output_html = gr.HTML()
145
  insight_html = gr.HTML()
146
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  analyze_btn.click(
148
  fn=analyze_emotion,
149
- inputs=[audio_input],
150
  outputs=[output_html, insight_html],
151
  )
152
 
153
  gr.HTML("""
154
  <hr>
155
  <p style="text-align:center; color:#999;">
156
- πŸ’Ύ All results stored in <code>scm_emotion.db</code> for Power BI visualization.
157
  </p>
158
  """)
159
 
 
22
  CREATE TABLE IF NOT EXISTS emotion_logs (
23
  id INTEGER PRIMARY KEY AUTOINCREMENT,
24
  datetime TEXT,
25
+ team TEXT,
26
+ purpose TEXT,
27
  emotion TEXT,
28
  score REAL
29
  )
 
51
  # -------------------------------------------------
52
  # πŸ’Ύ LOGGING
53
  # -------------------------------------------------
54
+ def log_to_db(datetime_str, team, purpose, emotion, score):
55
  conn = sqlite3.connect(DB_PATH)
56
  cursor = conn.cursor()
57
  cursor.execute(
58
+ "INSERT INTO emotion_logs (datetime, team, purpose, emotion, score) VALUES (?, ?, ?, ?, ?)",
59
+ (datetime_str, team, purpose, emotion, score),
60
  )
61
  conn.commit()
62
  conn.close()
 
64
  # -------------------------------------------------
65
  # 🧠 EMOTION ANALYSIS
66
  # -------------------------------------------------
67
+ def analyze_emotion(audio, team, custom_team, purpose, custom_purpose):
68
  if audio is None:
69
  return "⚠️ Please record or upload audio first.", None
70
 
71
+ # Determine final values
72
+ team_name = custom_team if team == "Other" else team
73
+ purpose_name = custom_purpose if purpose == "Other" else purpose
74
+
75
+ if not team_name:
76
+ team_name = "Unspecified Team"
77
+ if not purpose_name:
78
+ purpose_name = "Unspecified Purpose"
79
+
80
+ # Handle file input
81
  if isinstance(audio, tuple):
82
  audio_file = audio[0]
83
  else:
84
  audio_file = audio
85
 
86
+ # Predict emotion
87
  results = emotion_classifier(audio_file)
88
  results = sorted(results, key=lambda x: x['score'], reverse=True)
89
  top = results[0]
90
  label, emoji, color = EMOTION_MAP.get(top['label'], (top['label'], "🎭", "#607d8b"))
91
  score = round(top['score'] * 100, 2)
92
 
93
+ # Log data
94
+ log_to_db(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), team_name, purpose_name, label, score)
95
 
96
+ # Dashboard UI
97
  dashboard_html = f"""
98
  <div style="text-align:center; padding:15px;">
99
+ <h2>🏒 <b>{team_name}</b></h2>
100
+ <h4>🎯 Purpose: <b>{purpose_name}</b></h4>
101
  <h3 style="color:{color}; font-size:26px;">{emoji} {label.upper()} β€” {score}%</h3>
102
  <p style="color:#666;">Detected Emotion Intensity (Confidence)</p>
103
  <div style="width:80%; margin:auto; background:#eee; border-radius:20px;">
 
116
 
117
  # AI Insight
118
  insights = random.choice([
119
+ "🧩 The conversation tone was balanced β€” good team stability.",
120
+ "⚑ Slight emotional tension detected β€” consider a quick check-in.",
121
+ "πŸ’¬ Positive tone overall β€” keep up the good engagement.",
122
+ "🚨 Possible stress indicators β€” leadership follow-up advised.",
123
+ "πŸ“ˆ Emotional fluctuation noted β€” monitor workload or dynamics."
124
  ])
125
 
126
  insight_html = f"""
 
133
 
134
  return dashboard_html, insight_html
135
 
136
+
137
  # -------------------------------------------------
138
  # 🎨 GRADIO APP DESIGN
139
  # -------------------------------------------------
140
+ TEAM_OPTIONS = ["Procurement", "Logistics", "Planning", "HR", "Operations", "IT", "Other"]
141
+ PURPOSE_OPTIONS = ["HR Meeting", "Team Stand-up", "One-on-One", "Customer Call", "Interview", "Project Review", "Other"]
142
+
143
  with gr.Blocks(theme=gr.themes.Soft()) as app:
144
+ # Header
145
  gr.HTML("""
146
  <div style="text-align:center; padding:20px;">
147
+ <h1>🎧 Emotion Intelligence for Teams</h1>
148
  <p style="font-size:16px; color:#555;">
149
+ Detect and visualize emotional tone from team meetings or recorded conversations.<br>
150
+ Designed for managers and HR to track emotional health and engagement trends.
151
  </p>
152
  </div>
153
  """)
154
 
155
+ # Inputs
156
  with gr.Row():
157
  with gr.Column(scale=1):
158
+ team_dropdown = gr.Dropdown(TEAM_OPTIONS, label="🏒 Select Team / Department")
159
+ custom_team = gr.Textbox(label="✏️ Enter Team (if 'Other')", placeholder="Enter custom team name here")
160
+
161
+ purpose_dropdown = gr.Dropdown(PURPOSE_OPTIONS, label="🎯 Purpose of the Recording")
162
+ custom_purpose = gr.Textbox(label="✏️ Enter Purpose (if 'Other')", placeholder="Enter custom meeting purpose here")
163
+
164
  audio_input = gr.Audio(
165
  sources=["microphone", "upload"],
166
  type="filepath",
167
+ label="πŸŽ™οΈ Record or Upload Team Meeting Audio",
168
  )
169
  analyze_btn = gr.Button("πŸš€ Analyze Emotion", variant="primary")
170
+
171
  with gr.Column(scale=2):
172
  output_html = gr.HTML()
173
  insight_html = gr.HTML()
174
 
175
+ # Disclaimer
176
+ gr.HTML("""
177
+ <div style="background:#fff3cd; padding:10px; border-radius:10px; margin-top:15px; border-left:6px solid #ff9800;">
178
+ <h4 style="color:#444;">⚠️ Disclaimer</h4>
179
+ <p style="color:#333; font-size:14px;">
180
+ This is a demo version. Your voice data is <b>not being saved or shared</b> anywhere.<br>
181
+ The database logging is simulated for demonstration β€” feel free to explore the system freely.
182
+ </p>
183
+ </div>
184
+ """)
185
+
186
+ # Button Action
187
  analyze_btn.click(
188
  fn=analyze_emotion,
189
+ inputs=[audio_input, team_dropdown, custom_team, purpose_dropdown, custom_purpose],
190
  outputs=[output_html, insight_html],
191
  )
192
 
193
  gr.HTML("""
194
  <hr>
195
  <p style="text-align:center; color:#999;">
196
+ πŸ’Ύ Results are locally logged in <code>scm_emotion.db</code> (for Power BI integration demo).
197
  </p>
198
  """)
199