-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
333 lines (284 loc) Β· 12.3 KB
/
streamlit_app.py
File metadata and controls
333 lines (284 loc) Β· 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import streamlit as st
import pandas as pd
import numpy as np
import time
from datetime import datetime
import threading
import queue
import io
import base64
import tempfile
import os
import sys
from pathlib import Path
import plotly.graph_objects as go
import plotly.express as px
from collections import deque, Counter
# Add the necessary paths for imports
current_dir = Path(__file__).parent
components_path = current_dir / "components"
sys.path.append(str(components_path))
# Import custom components
try:
from audio_monitor import render_audio_component, get_audio_processor
from screen_monitor import render_screen_component, get_screen_processor, render_context_insights, get_productivity_score
COMPONENTS_AVAILABLE = True
except ImportError as e:
st.warning(f"Some components not available: {e}")
COMPONENTS_AVAILABLE = False
# Check if running in cloud environment
try:
# Multiple ways to detect cloud environment
import os
CLOUD_MODE = (
# Check environment variables that indicate cloud deployment
os.getenv('STREAMLIT_SHARING') is not None or
os.getenv('STREAMLIT_CLOUD') is not None or
# Check if we're running on common cloud platforms
'streamlit' in os.getenv('USER', '').lower() or
'app' in os.getenv('USER', '').lower() or
# Check for typical cloud paths
'/app' in os.getcwd() or
# Check if audio libraries are available (they won't be in cloud)
not COMPONENTS_AVAILABLE
)
except:
CLOUD_MODE = False
# Set page config
st.set_page_config(
page_title="Real-time Engagement Monitor",
page_icon="π―",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for better styling
st.markdown("""
<style>
.main-header {
font-size: 3rem;
color: #1f77b4;
text-align: center;
margin-bottom: 2rem;
font-weight: bold;
}
.metric-container {
background-color: #f0f2f6;
padding: 1rem;
border-radius: 10px;
margin: 0.5rem 0;
}
.engagement-engaged {
color: #28a745;
font-weight: bold;
font-size: 1.2rem;
}
.engagement-distracted {
color: #dc3545;
font-weight: bold;
font-size: 1.2rem;
}
.status-running {
color: #28a745;
}
.status-stopped {
color: #dc3545;
}
</style>
""", unsafe_allow_html=True)
def main():
"""Main Streamlit application"""
# Title and description
st.markdown('<h1 class="main-header">π― Real-time Engagement Monitor</h1>', unsafe_allow_html=True)
if CLOUD_MODE:
st.info("π **Cloud Demo Mode** - Running with simulated data for demonstration")
st.markdown("""
### A comprehensive engagement monitoring system that combines:
- π€ **Audio Emotion Detection** - Real-time analysis of emotional state through voice
- π₯οΈ **Screen Activity Analysis** - Context detection, sentiment analysis, and productivity monitoring
- π **Live Dashboard** - Real-time visualization of engagement metrics
""")
if CLOUD_MODE:
st.markdown("""
**Note:** This cloud version uses simulated data for demonstration.
For full functionality with real audio and screen capture, please run locally.
""")
# Check if components are available
if not COMPONENTS_AVAILABLE:
st.error("Required components are not available. Please check the installation.")
return
# Sidebar for controls
with st.sidebar:
st.header("π§ Controls")
# Monitoring toggles
st.subheader("π Monitoring")
audio_enabled = st.checkbox("π€ Audio Emotion Detection", value=True)
screen_enabled = st.checkbox("π₯οΈ Screen Analysis", value=True)
# Settings
st.subheader("βοΈ Settings")
update_interval = st.slider("Update Interval (seconds)", 1, 10, 3)
audio_window = st.slider("Audio Analysis Window (seconds)", 2, 5, 3)
# Model settings
st.subheader("π€ Model Settings")
confidence_threshold = st.slider("Confidence Threshold", 0.1, 1.0, 0.6)
# Data retention
max_data_points = st.slider("Max Data Points", 50, 500, 100)
# Export data
st.subheader("πΎ Data Export")
if st.button("π₯ Export Session Data", use_container_width=True):
if 'session_data' in st.session_state and st.session_state.session_data:
df = pd.DataFrame(st.session_state.session_data)
csv = df.to_csv(index=False)
st.download_button(
label="Download CSV",
data=csv,
file_name=f"engagement_data_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
mime="text/csv"
)
else:
st.warning("No data to export")
# Initialize session state
if 'monitoring_active' not in st.session_state:
st.session_state.monitoring_active = False
if 'session_data' not in st.session_state:
st.session_state.session_data = deque(maxlen=max_data_points)
if 'current_emotion' not in st.session_state:
st.session_state.current_emotion = "Unknown"
if 'current_engagement' not in st.session_state:
st.session_state.current_engagement = "Unknown"
if 'current_context' not in st.session_state:
st.session_state.current_context = "Unknown"
if 'current_sentiment' not in st.session_state:
st.session_state.current_sentiment = "Neutral"
if 'productivity_score' not in st.session_state:
st.session_state.productivity_score = 0
# Status indicator
status_indicator = st.empty()
# Create main layout
tab1, tab2, tab3, tab4 = st.tabs(["π Dashboard", "π€ Audio Monitor", "π₯οΈ Screen Monitor", "π Analytics"])
with tab1:
# Real-time metrics dashboard
st.subheader("οΏ½ Real-time Metrics")
# Main metrics row
col1, col2, col3, col4, col5 = st.columns(5)
with col1:
engagement_color = "#28a745" if st.session_state.current_engagement == "Engaged" else "#dc3545"
st.metric(
"π― Engagement",
st.session_state.current_engagement,
delta=None
)
with col2:
st.metric("π Emotion", st.session_state.current_emotion)
with col3:
st.metric("π± Context", st.session_state.current_context)
with col4:
st.metric("π Sentiment", st.session_state.current_sentiment)
with col5:
st.metric("οΏ½ Productivity", f"{st.session_state.productivity_score}%")
# Live charts
if st.session_state.session_data:
col1, col2 = st.columns(2)
with col1:
# Engagement over time
df = pd.DataFrame(list(st.session_state.session_data))
if 'timestamp' in df.columns and 'engagement' in df.columns:
df['time'] = pd.to_datetime(df['timestamp'], unit='s')
df['engaged_binary'] = df['engagement'].map({'Engaged': 1, 'Distracted': 0}).fillna(0)
fig = px.line(df, x='time', y='engaged_binary',
title='Engagement Over Time',
labels={'engaged_binary': 'Engaged (1) / Distracted (0)'})
fig.update_layout(height=300)
st.plotly_chart(fig, use_container_width=True)
with col2:
# Context distribution
if 'context' in df.columns:
context_counts = df['context'].value_counts()
fig = px.pie(values=context_counts.values, names=context_counts.index,
title='Context Distribution')
fig.update_layout(height=300)
st.plotly_chart(fig, use_container_width=True)
# Session summary
st.subheader("οΏ½ Session Summary")
if st.session_state.session_data:
total_time = len(st.session_state.session_data) * update_interval
engaged_count = sum(1 for item in st.session_state.session_data
if item.get('engagement') == 'Engaged')
engagement_rate = (engaged_count / len(st.session_state.session_data)) * 100
summary_col1, summary_col2, summary_col3 = st.columns(3)
with summary_col1:
st.metric("Total Session Time", f"{total_time//60:.0f}m {total_time%60:.0f}s")
with summary_col2:
st.metric("Engagement Rate", f"{engagement_rate:.1f}%")
with summary_col3:
st.metric("Data Points", len(st.session_state.session_data))
else:
st.info("Start monitoring to see session summary")
with tab2:
# Audio monitoring tab
if audio_enabled:
audio_processor = render_audio_component()
else:
st.info("Audio monitoring disabled in settings")
with tab3:
# Screen monitoring tab
if screen_enabled:
screen_processor = render_screen_component()
render_context_insights()
else:
st.info("Screen monitoring disabled in settings")
with tab4:
# Analytics tab
st.subheader("π Advanced Analytics")
if st.session_state.session_data:
df = pd.DataFrame(list(st.session_state.session_data))
# Time-based analysis
st.subheader("β° Time-based Analysis")
if 'timestamp' in df.columns:
df['time'] = pd.to_datetime(df['timestamp'], unit='s')
df['hour'] = df['time'].dt.hour
# Engagement by hour
if 'engagement' in df.columns:
hourly_engagement = df.groupby('hour')['engagement'].apply(
lambda x: (x == 'Engaged').mean() * 100
).reset_index()
fig = px.bar(hourly_engagement, x='hour', y='engagement',
title='Engagement Rate by Hour',
labels={'engagement': 'Engagement Rate (%)'})
st.plotly_chart(fig, use_container_width=True)
# Correlation analysis
st.subheader("π Pattern Analysis")
if len(df) > 10:
# Create correlation matrix for categorical variables
patterns = {}
if 'context' in df.columns and 'engagement' in df.columns:
context_engagement = pd.crosstab(df['context'], df['engagement'], normalize='index') * 100
st.write("**Context vs Engagement (%)**")
st.dataframe(context_engagement)
if 'emotion' in df.columns and 'engagement' in df.columns:
emotion_engagement = pd.crosstab(df['emotion'], df['engagement'], normalize='index') * 100
st.write("**Emotion vs Engagement (%)**")
st.dataframe(emotion_engagement)
else:
st.info("No data available for analytics. Start monitoring to collect data.")
# Update session data if monitoring is active
if st.session_state.get('audio_active') or st.session_state.get('screen_active'):
current_data = {
'timestamp': time.time(),
'emotion': st.session_state.current_emotion,
'engagement': st.session_state.current_engagement,
'context': st.session_state.current_context,
'sentiment': st.session_state.current_sentiment
}
# Add to session data
st.session_state.session_data.append(current_data)
# Update productivity score
st.session_state.productivity_score = get_productivity_score()
# Show status
status_indicator.success("π’ Monitoring Active - Data being collected")
# Auto-refresh
time.sleep(update_interval)
st.rerun()
else:
status_indicator.info("π΄ Monitoring Inactive - Start audio or screen monitoring")
if __name__ == "__main__":
main()