saptyfun commited on
Commit
1350f34
Β·
verified Β·
1 Parent(s): dd34aed

Upload streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +373 -79
src/streamlit_app.py CHANGED
@@ -1,7 +1,6 @@
1
  #!/usr/bin/env python3
2
  """
3
- Hugging Face Spaces compatible launcher for the Multi-Agent System Dashboard
4
- This file handles environment setup and permissions for deployment.
5
  """
6
 
7
  import os
@@ -10,105 +9,400 @@ import tempfile
10
  import sqlite3
11
  from pathlib import Path
12
  import warnings
 
 
13
 
14
- # Suppress warnings for cleaner output
15
  warnings.filterwarnings('ignore')
16
 
17
  # Set environment variables for Hugging Face Spaces
18
  os.environ['STREAMLIT_SERVER_HEADLESS'] = 'true'
19
  os.environ['STREAMLIT_SERVER_PORT'] = '7860'
20
  os.environ['STREAMLIT_BROWSER_GATHER_USAGE_STATS'] = 'false'
21
- os.environ['STREAMLIT_SERVER_ENABLE_CORS'] = 'false'
22
- os.environ['STREAMLIT_SERVER_ENABLE_XSRF_PROTECTION'] = 'false'
23
 
24
- # Create a writable directory for Streamlit config
25
- try:
26
- # Try to create .streamlit directory in temp
27
- streamlit_dir = Path(tempfile.gettempdir()) / '.streamlit'
28
- streamlit_dir.mkdir(exist_ok=True)
29
- os.environ['STREAMLIT_CONFIG_DIR'] = str(streamlit_dir)
30
-
31
- # Create config file
32
- config_content = """
33
- [server]
34
- headless = true
35
- port = 7860
36
- enableCORS = false
37
- enableXsrfProtection = false
38
 
39
- [browser]
40
- gatherUsageStats = false
 
 
 
 
41
 
42
- [theme]
43
- primaryColor = "#FF6B6B"
44
- backgroundColor = "#FFFFFF"
45
- secondaryBackgroundColor = "#F0F2F6"
46
- textColor = "#262730"
47
- """
48
-
49
- config_file = streamlit_dir / 'config.toml'
50
- with open(config_file, 'w') as f:
51
- f.write(config_content)
52
-
53
- except Exception as e:
54
- print(f"Warning: Could not create streamlit config: {e}")
55
 
56
- # Now import the main application
57
- try:
58
- # Import the main dashboard application
59
- from app import HuggingFaceDashboard
 
 
 
 
 
 
60
 
61
- # Initialize and run the dashboard
62
- dashboard = HuggingFaceDashboard()
63
- dashboard.run()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- except ImportError as e:
66
- # Fallback to a simple error page
67
- import streamlit as st
 
 
 
68
 
69
- st.set_page_config(
70
- page_title="πŸ€– Multi-Agent System Dashboard",
71
- page_icon="πŸ€–",
72
- layout="wide"
73
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
- st.error(f"Import Error: {str(e)}")
76
- st.info("Please ensure all dependencies are installed correctly.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
- # Show basic info
79
- st.header("πŸ€– Multi-Agent System Dashboard")
80
- st.markdown("""
81
- This dashboard provides comprehensive monitoring and evaluation capabilities for multi-agent systems.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
- **Features:**
84
- - 🎯 Real-time performance monitoring
85
- - πŸ“Š Comprehensive evaluation metrics
86
- - πŸ” Detailed analytics and insights
87
- - πŸ“ˆ Interactive visualizations
88
- - πŸ›‘οΈ Safety and guardrails monitoring
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
- **Error:** The application encountered an import error. Please contact support.
91
- """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  except Exception as e:
94
- # Handle any other errors gracefully
95
- import streamlit as st
96
-
97
- st.set_page_config(
98
- page_title="πŸ€– Multi-Agent System Dashboard - Error",
99
- page_icon="⚠️",
100
- layout="wide"
101
- )
102
-
103
  st.error(f"Application Error: {str(e)}")
104
- st.info("The application encountered an unexpected error. Please refresh the page.")
105
 
106
- # Show debug info in expander
107
  with st.expander("Debug Information"):
108
  st.code(f"""
109
- Error Type: {type(e).__name__}
110
- Error Message: {str(e)}
111
- Python Version: {sys.version}
112
- Working Directory: {os.getcwd()}
113
- Temp Directory: {tempfile.gettempdir()}
114
  """)
 
1
  #!/usr/bin/env python3
2
  """
3
+ Simplified Hugging Face Spaces compatible Multi-Agent System Dashboard
 
4
  """
5
 
6
  import os
 
9
  import sqlite3
10
  from pathlib import Path
11
  import warnings
12
+ from datetime import datetime, timedelta
13
+ import random
14
 
15
+ # Suppress warnings
16
  warnings.filterwarnings('ignore')
17
 
18
  # Set environment variables for Hugging Face Spaces
19
  os.environ['STREAMLIT_SERVER_HEADLESS'] = 'true'
20
  os.environ['STREAMLIT_SERVER_PORT'] = '7860'
21
  os.environ['STREAMLIT_BROWSER_GATHER_USAGE_STATS'] = 'false'
 
 
22
 
23
+ # Import streamlit first and set page config
24
+ import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ st.set_page_config(
27
+ page_title="πŸ€– Multi-Agent System Dashboard",
28
+ page_icon="πŸ€–",
29
+ layout="wide",
30
+ initial_sidebar_state="expanded"
31
+ )
32
 
33
+ # Import other required modules
34
+ import pandas as pd
35
+ import plotly.express as px
36
+ import plotly.graph_objects as go
37
+ import json
38
+ import numpy as np
39
+ from typing import Dict, List, Any
 
 
 
 
 
 
40
 
41
+ class SimpleDashboard:
42
+ def __init__(self):
43
+ # Use temp directory for database
44
+ temp_dir = tempfile.gettempdir()
45
+ self.db_path = os.path.join(temp_dir, "evaluation_logs.db")
46
+ try:
47
+ self.setup_demo_data()
48
+ except Exception as e:
49
+ st.error(f"Setup error: {str(e)}")
50
+ self.create_fallback_data()
51
 
52
+ def create_fallback_data(self):
53
+ """Create fallback data if database fails"""
54
+ st.warning("Using fallback demo data")
55
+
56
+ # Create sample data directly
57
+ agents = ["Diet Agent", "Support Agent", "Queries Agent"]
58
+ data = []
59
+
60
+ for i in range(50):
61
+ data.append({
62
+ 'id': i,
63
+ 'session_id': f"session_{random.randint(1000, 9999)}",
64
+ 'agent_name': random.choice(agents),
65
+ 'query': f"Sample query {i}",
66
+ 'response': f"Sample response {i} with detailed information...",
67
+ 'overall_score': random.uniform(7.0, 9.5),
68
+ 'relevance_score': random.uniform(7.0, 9.5),
69
+ 'accuracy_score': random.uniform(7.0, 9.5),
70
+ 'completeness_score': random.uniform(7.0, 9.5),
71
+ 'coherence_score': random.uniform(7.0, 9.5),
72
+ 'guardrails_passed': True,
73
+ 'safety_score': random.uniform(8.0, 10.0),
74
+ 'execution_time_ms': random.uniform(500, 2000),
75
+ 'timestamp': datetime.now() - timedelta(days=random.randint(0, 30))
76
+ })
77
+
78
+ self.fallback_df = pd.DataFrame(data)
79
+ self.use_fallback = True
80
 
81
+ def setup_demo_data(self):
82
+ """Setup demo database"""
83
+ self.use_fallback = False
84
+
85
+ if not os.path.exists(self.db_path):
86
+ self.create_demo_database()
87
 
88
+ def create_demo_database(self):
89
+ """Create demo database"""
90
+ conn = sqlite3.connect(self.db_path)
91
+ cursor = conn.cursor()
92
+
93
+ # Create table
94
+ cursor.execute('''
95
+ CREATE TABLE IF NOT EXISTS evaluation_logs (
96
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
97
+ session_id TEXT NOT NULL,
98
+ agent_name TEXT NOT NULL,
99
+ query TEXT NOT NULL,
100
+ response TEXT,
101
+ overall_score REAL,
102
+ relevance_score REAL,
103
+ accuracy_score REAL,
104
+ completeness_score REAL,
105
+ coherence_score REAL,
106
+ guardrails_passed BOOLEAN,
107
+ safety_score REAL,
108
+ execution_time_ms REAL,
109
+ timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
110
+ )
111
+ ''')
112
+
113
+ # Insert demo data
114
+ agents = ["Diet Agent", "Support Agent", "Queries Agent"]
115
+
116
+ sample_queries = {
117
+ "Diet Agent": [
118
+ "What's a healthy meal plan for weight loss?",
119
+ "Can you suggest low-carb breakfast options?",
120
+ "What are the benefits of intermittent fasting?",
121
+ "How much protein should I eat daily?",
122
+ "What foods are good for heart health?"
123
+ ],
124
+ "Support Agent": [
125
+ "I'm having trouble sleeping, can you help?",
126
+ "How do I manage work stress?",
127
+ "I feel overwhelmed with my tasks",
128
+ "Can you help me organize my schedule?",
129
+ "How to improve my productivity?"
130
+ ],
131
+ "Queries Agent": [
132
+ "What are the latest developments in AI?",
133
+ "How does blockchain technology work?",
134
+ "What is quantum computing?",
135
+ "Explain machine learning algorithms",
136
+ "What are the benefits of cloud computing?"
137
+ ]
138
+ }
139
+
140
+ for i in range(100):
141
+ session_id = f"session_{random.randint(1000, 9999)}"
142
+ agent = random.choice(agents)
143
+ query = random.choice(sample_queries[agent])
144
+
145
+ # Generate detailed response
146
+ response = f"Based on your query about {query[:30]}..., here's a comprehensive response with detailed information and actionable recommendations."
147
+
148
+ # Generate realistic scores
149
+ base_score = random.uniform(7.0, 9.5)
150
+
151
+ timestamp = datetime.now() - timedelta(days=random.randint(0, 30))
152
+
153
+ cursor.execute('''
154
+ INSERT INTO evaluation_logs (
155
+ session_id, agent_name, query, response, overall_score,
156
+ relevance_score, accuracy_score, completeness_score, coherence_score,
157
+ guardrails_passed, safety_score, execution_time_ms, timestamp
158
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
159
+ ''', (
160
+ session_id, agent, query, response, base_score,
161
+ base_score + random.uniform(-0.3, 0.3),
162
+ base_score + random.uniform(-0.4, 0.2),
163
+ base_score + random.uniform(-0.5, 0.3),
164
+ base_score + random.uniform(-0.2, 0.4),
165
+ random.choice([True, True, True, False]), # 75% pass rate
166
+ random.uniform(8.0, 10.0),
167
+ random.uniform(500, 2000),
168
+ timestamp.isoformat()
169
+ ))
170
+
171
+ conn.commit()
172
+ conn.close()
173
 
174
+ def load_data(self):
175
+ """Load data"""
176
+ if self.use_fallback:
177
+ return self.fallback_df
178
+
179
+ try:
180
+ conn = sqlite3.connect(self.db_path)
181
+ df = pd.read_sql_query("SELECT * FROM evaluation_logs ORDER BY timestamp DESC", conn)
182
+ conn.close()
183
+
184
+ if not df.empty:
185
+ df['timestamp'] = pd.to_datetime(df['timestamp'])
186
+
187
+ return df
188
+ except Exception as e:
189
+ st.error(f"Data loading error: {str(e)}")
190
+ return pd.DataFrame()
191
 
192
+ def show_overview(self, df):
193
+ """Show overview tab"""
194
+ st.header("πŸ“ˆ Executive Summary")
195
+
196
+ if df.empty:
197
+ st.warning("No data available")
198
+ return
199
+
200
+ # Key metrics
201
+ col1, col2, col3, col4 = st.columns(4)
202
+
203
+ with col1:
204
+ st.metric("Total Evaluations", len(df))
205
+
206
+ with col2:
207
+ avg_score = df['overall_score'].mean()
208
+ st.metric("Average Score", f"{avg_score:.2f}/10")
209
+
210
+ with col3:
211
+ safety_rate = (df['guardrails_passed'].sum() / len(df)) * 100
212
+ st.metric("Safety Rate", f"{safety_rate:.1f}%")
213
+
214
+ with col4:
215
+ avg_time = df['execution_time_ms'].mean() / 1000
216
+ st.metric("Avg Response Time", f"{avg_time:.2f}s")
217
+
218
+ # Charts
219
+ col1, col2 = st.columns(2)
220
+
221
+ with col1:
222
+ st.subheader("πŸ“Š Performance by Agent")
223
+ agent_scores = df.groupby('agent_name')['overall_score'].mean().reset_index()
224
+ fig = px.bar(
225
+ agent_scores,
226
+ x='agent_name',
227
+ y='overall_score',
228
+ title="Average Score by Agent",
229
+ color='overall_score',
230
+ color_continuous_scale='viridis'
231
+ )
232
+ st.plotly_chart(fig, use_container_width=True)
233
+
234
+ with col2:
235
+ st.subheader("πŸ“ˆ Score Distribution")
236
+ fig = px.histogram(
237
+ df,
238
+ x='overall_score',
239
+ nbins=20,
240
+ title="Score Distribution",
241
+ color_discrete_sequence=['#1f77b4']
242
+ )
243
+ st.plotly_chart(fig, use_container_width=True)
244
 
245
+ def show_agent_performance(self, df):
246
+ """Show agent performance tab"""
247
+ st.header("πŸ€– Agent Performance Analysis")
248
+
249
+ if df.empty:
250
+ st.warning("No data available")
251
+ return
252
+
253
+ # Agent selector
254
+ agents = df['agent_name'].unique()
255
+ selected_agent = st.selectbox("Select Agent", ["All Agents"] + list(agents))
256
+
257
+ # Filter data
258
+ if selected_agent != "All Agents":
259
+ filtered_df = df[df['agent_name'] == selected_agent]
260
+ else:
261
+ filtered_df = df
262
+
263
+ # Performance metrics
264
+ col1, col2 = st.columns(2)
265
+
266
+ with col1:
267
+ st.subheader("🎯 Score Breakdown")
268
+ score_cols = ['relevance_score', 'accuracy_score', 'completeness_score', 'coherence_score']
269
+ available_scores = [col for col in score_cols if col in filtered_df.columns]
270
+
271
+ if available_scores:
272
+ avg_scores = filtered_df[available_scores].mean()
273
+ fig = px.bar(
274
+ x=avg_scores.index,
275
+ y=avg_scores.values,
276
+ title=f"Average Scores - {selected_agent}",
277
+ labels={'x': 'Metric', 'y': 'Score'}
278
+ )
279
+ st.plotly_chart(fig, use_container_width=True)
280
+
281
+ with col2:
282
+ st.subheader("⏱️ Response Time Analysis")
283
+ fig = px.box(
284
+ filtered_df,
285
+ x='agent_name',
286
+ y='execution_time_ms',
287
+ title="Response Time Distribution"
288
+ )
289
+ st.plotly_chart(fig, use_container_width=True)
290
+
291
+ # Recent evaluations table
292
+ st.subheader("πŸ“‹ Recent Evaluations")
293
+ display_cols = ['agent_name', 'query', 'overall_score', 'execution_time_ms', 'timestamp']
294
+ available_cols = [col for col in display_cols if col in filtered_df.columns]
295
+
296
+ if available_cols:
297
+ recent_data = filtered_df[available_cols].head(20)
298
+ st.dataframe(recent_data, use_container_width=True)
299
 
300
+ def show_response_analysis(self, df):
301
+ """Show response analysis tab"""
302
+ st.header("πŸ“ Response Analysis & Tracing")
303
+
304
+ if df.empty:
305
+ st.warning("No data available")
306
+ return
307
+
308
+ # Response metrics
309
+ col1, col2, col3 = st.columns(3)
310
+
311
+ with col1:
312
+ if 'response' in df.columns:
313
+ avg_length = df['response'].str.len().mean()
314
+ st.metric("Avg Response Length", f"{avg_length:.0f} chars")
315
+ else:
316
+ st.metric("Avg Response Length", "N/A")
317
+
318
+ with col2:
319
+ if 'response' in df.columns:
320
+ avg_words = df['response'].str.split().str.len().mean()
321
+ st.metric("Avg Word Count", f"{avg_words:.0f} words")
322
+ else:
323
+ st.metric("Avg Word Count", "N/A")
324
+
325
+ with col3:
326
+ response_rate = (df['response'].notna().sum() / len(df)) * 100
327
+ st.metric("Response Rate", f"{response_rate:.1f}%")
328
+
329
+ # Search functionality
330
+ st.subheader("πŸ” Search Responses")
331
+ search_term = st.text_input("Search in responses:", placeholder="Enter keywords...")
332
+
333
+ if search_term and 'response' in df.columns:
334
+ mask = df['response'].str.contains(search_term, case=False, na=False)
335
+ search_results = df[mask]
336
+ else:
337
+ search_results = df.head(10)
338
+
339
+ # Display results
340
+ if not search_results.empty:
341
+ st.write(f"**Showing {len(search_results)} results**")
342
+
343
+ for idx, row in search_results.iterrows():
344
+ with st.expander(f"πŸ€– {row['agent_name']} - Score: {row['overall_score']:.1f}"):
345
+ col1, col2 = st.columns([2, 1])
346
+
347
+ with col1:
348
+ st.write("**Query:**")
349
+ st.write(row['query'])
350
+
351
+ if 'response' in row and pd.notna(row['response']):
352
+ st.write("**Response:**")
353
+ st.write(row['response'])
354
+
355
+ with col2:
356
+ st.write("**Metrics:**")
357
+ st.write(f"Overall Score: {row['overall_score']:.1f}/10")
358
+ if 'execution_time_ms' in row:
359
+ st.write(f"Response Time: {row['execution_time_ms']:.0f}ms")
360
+ if 'timestamp' in row:
361
+ st.write(f"Timestamp: {row['timestamp']}")
362
 
363
+ def run(self):
364
+ """Run the dashboard"""
365
+ st.title("πŸ€– Multi-Agent System Dashboard")
366
+ st.markdown("---")
367
+
368
+ st.info("πŸŽ‰ **Welcome!** This dashboard showcases evaluation metrics for Diet, Support, and Queries agents.")
369
+
370
+ # Load data
371
+ df = self.load_data()
372
+
373
+ # Create tabs
374
+ tab1, tab2, tab3 = st.tabs([
375
+ "πŸ“ˆ Overview",
376
+ "πŸ€– Agent Performance",
377
+ "πŸ“ Response Analysis"
378
+ ])
379
+
380
+ with tab1:
381
+ self.show_overview(df)
382
+
383
+ with tab2:
384
+ self.show_agent_performance(df)
385
+
386
+ with tab3:
387
+ self.show_response_analysis(df)
388
+
389
+ # Footer
390
+ st.markdown("---")
391
+ st.markdown("πŸš€ **Multi-Agent System Dashboard** | Built with Streamlit & Plotly")
392
+
393
+ # Run the dashboard
394
+ try:
395
+ dashboard = SimpleDashboard()
396
+ dashboard.run()
397
  except Exception as e:
 
 
 
 
 
 
 
 
 
398
  st.error(f"Application Error: {str(e)}")
399
+ st.info("Please refresh the page.")
400
 
 
401
  with st.expander("Debug Information"):
402
  st.code(f"""
403
+ Error: {str(e)}
404
+ Type: {type(e).__name__}
405
+ Python: {sys.version}
406
+ Working Dir: {os.getcwd()}
407
+ Temp Dir: {tempfile.gettempdir()}
408
  """)