Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
|
| 81 |
|
| 82 |
-
# Dashboard
|
| 83 |
dashboard_html = f"""
|
| 84 |
<div style="text-align:center; padding:15px;">
|
| 85 |
-
<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 |
-
"π§©
|
| 105 |
-
"β‘ Slight emotional tension
|
| 106 |
-
"π¬ Positive tone
|
| 107 |
-
"π¨
|
| 108 |
-
"π Emotional
|
| 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
|
| 128 |
<p style="font-size:16px; color:#555;">
|
| 129 |
-
Detect emotional tone
|
| 130 |
-
|
| 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
|
| 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 |
-
πΎ
|
| 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 |
|