Spaces:
Sleeping
Sleeping
File size: 27,133 Bytes
38e0063 1350f34 38e0063 1350f34 38e0063 1350f34 38e0063 1350f34 38e0063 1350f34 38e0063 1350f34 38e0063 1350f34 38e0063 1350f34 d053b0b 1350f34 d053b0b 1350f34 d053b0b 1350f34 d053b0b 1350f34 d053b0b 1350f34 38e0063 1350f34 38e0063 1350f34 d053b0b 1350f34 d053b0b 1350f34 d053b0b 1350f34 d053b0b 1350f34 d053b0b 1350f34 d053b0b 1350f34 38e0063 1350f34 38e0063 1350f34 38e0063 1350f34 38e0063 1350f34 38e0063 d053b0b 1350f34 d053b0b 1350f34 d053b0b 1350f34 d053b0b 1350f34 38e0063 1350f34 38e0063 1350f34 38e0063 |
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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 |
#!/usr/bin/env python3
"""
Simplified Hugging Face Spaces compatible Multi-Agent System Dashboard
"""
import os
import sys
import tempfile
import sqlite3
from pathlib import Path
import warnings
from datetime import datetime, timedelta
import random
# Suppress warnings
warnings.filterwarnings('ignore')
# Set environment variables for Hugging Face Spaces
os.environ['STREAMLIT_SERVER_HEADLESS'] = 'true'
os.environ['STREAMLIT_SERVER_PORT'] = '7860'
os.environ['STREAMLIT_BROWSER_GATHER_USAGE_STATS'] = 'false'
# Import streamlit first and set page config
import streamlit as st
st.set_page_config(
page_title="π€ Multi-Agent System Dashboard",
page_icon="π€",
layout="wide",
initial_sidebar_state="expanded"
)
# Import other required modules
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import json
import numpy as np
from typing import Dict, List, Any
class SimpleDashboard:
def __init__(self):
# Use temp directory for database
temp_dir = tempfile.gettempdir()
self.db_path = os.path.join(temp_dir, "evaluation_logs.db")
try:
self.setup_demo_data()
except Exception as e:
st.error(f"Setup error: {str(e)}")
self.create_fallback_data()
def create_fallback_data(self):
"""Create fallback data if database fails"""
st.warning("Using fallback demo data")
# Create sample data directly
agents = ["Diet Agent", "Support Agent", "Queries Agent"]
data = []
for i in range(50):
base_score = random.uniform(7.0, 9.5)
accuracy = random.uniform(7.0, 9.5)
data.append({
'id': i,
'session_id': f"session_{random.randint(1000, 9999)}",
'agent_name': random.choice(agents),
'query': f"Sample query {i}",
'response': f"Sample response {i} with detailed information and comprehensive guidance...",
'overall_score': base_score,
'relevance_score': random.uniform(7.0, 9.5),
'accuracy_score': accuracy,
'completeness_score': random.uniform(7.0, 9.5),
'coherence_score': random.uniform(7.0, 9.5),
'hallucination_score': max(0, min(10, 10 - accuracy + random.uniform(-1.0, 1.0))),
'guardrails_passed': True,
'safety_score': random.uniform(8.0, 10.0),
'execution_time_ms': random.uniform(500, 2000),
'input_tokens': random.randint(20, 100),
'output_tokens': random.randint(100, 500),
'total_tokens': random.randint(120, 600),
'cost_usd': random.uniform(0.001, 0.02),
'llm_provider': random.choice(["azure", "openai", "anthropic"]),
'model_name': 'gpt-4o',
'timestamp': datetime.now() - timedelta(days=random.randint(0, 30))
})
self.fallback_df = pd.DataFrame(data)
self.use_fallback = True
def setup_demo_data(self):
"""Setup demo database"""
self.use_fallback = False
if not os.path.exists(self.db_path):
self.create_demo_database()
def create_demo_database(self):
"""Create demo database"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Create table
cursor.execute('''
CREATE TABLE IF NOT EXISTS evaluation_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL,
agent_name TEXT NOT NULL,
query TEXT NOT NULL,
response TEXT,
overall_score REAL,
relevance_score REAL,
accuracy_score REAL,
completeness_score REAL,
coherence_score REAL,
hallucination_score REAL,
guardrails_passed BOOLEAN,
safety_score REAL,
execution_time_ms REAL,
input_tokens INTEGER,
output_tokens INTEGER,
total_tokens INTEGER,
cost_usd REAL,
llm_provider TEXT,
model_name TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
''')
# Insert demo data
agents = ["Diet Agent", "Support Agent", "Queries Agent"]
sample_queries = {
"Diet Agent": [
"What's a healthy meal plan for weight loss?",
"Can you suggest low-carb breakfast options?",
"What are the benefits of intermittent fasting?",
"How much protein should I eat daily?",
"What foods are good for heart health?"
],
"Support Agent": [
"I'm having trouble sleeping, can you help?",
"How do I manage work stress?",
"I feel overwhelmed with my tasks",
"Can you help me organize my schedule?",
"How to improve my productivity?"
],
"Queries Agent": [
"What are the latest developments in AI?",
"How does blockchain technology work?",
"What is quantum computing?",
"Explain machine learning algorithms",
"What are the benefits of cloud computing?"
]
}
for i in range(100):
session_id = f"session_{random.randint(1000, 9999)}"
agent = random.choice(agents)
query = random.choice(sample_queries[agent])
# Generate comprehensive response
response_templates = {
"Diet Agent": [
"Thank you for your question about nutrition and dietary guidance. I'd be happy to help you develop a healthier relationship with food and create sustainable eating habits.",
"I understand you're looking for dietary advice, and I'm here to provide evidence-based nutritional guidance tailored to your specific needs and goals."
],
"Support Agent": [
"I appreciate you reaching out for support. It takes courage to ask for help, and I'm here to provide you with practical strategies and emotional guidance.",
"Thank you for sharing your concerns with me. I understand this can be challenging, and I want to help you work through this step by step with compassion and understanding."
],
"Queries Agent": [
"Excellent question! This is a fascinating topic that involves cutting-edge technology and has significant implications for our future. Let me provide you with a comprehensive overview.",
"Thank you for this thought-provoking question. This subject encompasses multiple disciplines and recent innovations. I'll break this down into key concepts and practical applications."
]
}
base_response = random.choice(response_templates[agent])
# Add detailed information
if agent == "Diet Agent":
details = "**Key Nutritional Recommendations:**\n\n1. **Whole Foods Focus**: Prioritize unprocessed foods like fresh fruits, vegetables, whole grains, lean proteins, and healthy fats.\n\n2. **Portion Control**: Use the plate method - fill half your plate with non-starchy vegetables, one quarter with lean protein, and one quarter with complex carbohydrates.\n\n3. **Hydration**: Aim for 8-10 glasses of water daily to support metabolism and overall health."
elif agent == "Support Agent":
details = "**Comprehensive Support Strategy:**\n\n**Immediate Coping Techniques:**\n1. **Deep Breathing**: Practice the 4-7-8 technique - inhale for 4 counts, hold for 7, exhale for 8.\n\n2. **Grounding Exercises**: Use the 5-4-3-2-1 method - identify 5 things you can see, 4 you can touch, 3 you can hear, 2 you can smell, and 1 you can taste.\n\n**Long-term Strategies:**\n- Establish a consistent daily routine\n- Practice mindfulness meditation for 10-15 minutes daily"
else: # Queries Agent
details = "**Technical Deep Dive:**\n\n**Fundamental Concepts:**\nThis technology represents a convergence of multiple disciplines including computer science, mathematics, engineering, and domain-specific expertise.\n\n**Current Implementation:**\n1. **Healthcare**: AI-powered diagnostic tools and personalized treatment plans\n2. **Finance**: Algorithmic trading and fraud detection\n3. **Transportation**: Autonomous vehicles and traffic optimization"
response = f"{base_response}\n\n{details}"
# Generate realistic scores
base_score = random.uniform(7.0, 9.5)
relevance_score = max(0, min(10, base_score + random.uniform(-0.3, 0.3)))
accuracy_score = max(0, min(10, base_score + random.uniform(-0.4, 0.2)))
completeness_score = max(0, min(10, base_score + random.uniform(-0.5, 0.3)))
coherence_score = max(0, min(10, base_score + random.uniform(-0.2, 0.4)))
hallucination_score = max(0, min(10, 10 - accuracy_score + random.uniform(-1.0, 1.0)))
# Generate token consumption
response_length = len(response)
input_tokens = int(len(query.split()) * 1.3)
output_tokens = int(response_length / 4)
total_tokens = input_tokens + output_tokens
# Calculate cost
llm_provider = random.choice(["azure", "openai", "anthropic"])
cost_per_1k = {"azure": 0.03, "openai": 0.03, "anthropic": 0.025}
cost_usd = (total_tokens / 1000) * cost_per_1k[llm_provider]
timestamp = datetime.now() - timedelta(days=random.randint(0, 30))
cursor.execute('''
INSERT INTO evaluation_logs (
session_id, agent_name, query, response, overall_score,
relevance_score, accuracy_score, completeness_score, coherence_score,
hallucination_score, guardrails_passed, safety_score, execution_time_ms,
input_tokens, output_tokens, total_tokens, cost_usd, llm_provider, model_name, timestamp
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
session_id, agent, query, response, base_score,
relevance_score, accuracy_score, completeness_score, coherence_score,
hallucination_score, random.choice([True, True, True, False]), # 75% pass rate
random.uniform(8.0, 10.0), random.uniform(500, 2000),
input_tokens, output_tokens, total_tokens, round(cost_usd, 4),
llm_provider, "gpt-4o", timestamp.isoformat()
))
conn.commit()
conn.close()
def load_data(self):
"""Load data"""
if self.use_fallback:
return self.fallback_df
try:
conn = sqlite3.connect(self.db_path)
df = pd.read_sql_query("SELECT * FROM evaluation_logs ORDER BY timestamp DESC", conn)
conn.close()
if not df.empty:
df['timestamp'] = pd.to_datetime(df['timestamp'])
return df
except Exception as e:
st.error(f"Data loading error: {str(e)}")
return pd.DataFrame()
def show_overview(self, df):
"""Show overview tab"""
st.header("π Executive Summary")
if df.empty:
st.warning("No data available")
return
# Key metrics
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Total Evaluations", len(df))
with col2:
avg_score = df['overall_score'].mean()
st.metric("Average Score", f"{avg_score:.2f}/10")
with col3:
safety_rate = (df['guardrails_passed'].sum() / len(df)) * 100
st.metric("Safety Rate", f"{safety_rate:.1f}%")
with col4:
avg_time = df['execution_time_ms'].mean() / 1000
st.metric("Avg Response Time", f"{avg_time:.2f}s")
# Charts
col1, col2 = st.columns(2)
with col1:
st.subheader("π Performance by Agent")
agent_scores = df.groupby('agent_name')['overall_score'].mean().reset_index()
fig = px.bar(
agent_scores,
x='agent_name',
y='overall_score',
title="Average Score by Agent",
color='overall_score',
color_continuous_scale='viridis'
)
st.plotly_chart(fig, use_container_width=True)
with col2:
st.subheader("π Score Distribution")
fig = px.histogram(
df,
x='overall_score',
nbins=20,
title="Score Distribution",
color_discrete_sequence=['#1f77b4']
)
st.plotly_chart(fig, use_container_width=True)
def show_agent_performance(self, df):
"""Show agent performance tab"""
st.header("π€ Agent Performance Analysis")
if df.empty:
st.warning("No data available")
return
# Agent selector
agents = df['agent_name'].unique()
selected_agent = st.selectbox("Select Agent", ["All Agents"] + list(agents))
# Filter data
if selected_agent != "All Agents":
filtered_df = df[df['agent_name'] == selected_agent]
else:
filtered_df = df
# Performance metrics
col1, col2 = st.columns(2)
with col1:
st.subheader("π― Score Breakdown")
score_cols = ['relevance_score', 'accuracy_score', 'completeness_score', 'coherence_score']
available_scores = [col for col in score_cols if col in filtered_df.columns]
if available_scores:
avg_scores = filtered_df[available_scores].mean()
fig = px.bar(
x=avg_scores.index,
y=avg_scores.values,
title=f"Average Scores - {selected_agent}",
labels={'x': 'Metric', 'y': 'Score'}
)
st.plotly_chart(fig, use_container_width=True)
with col2:
st.subheader("β±οΈ Response Time Analysis")
fig = px.box(
filtered_df,
x='agent_name',
y='execution_time_ms',
title="Response Time Distribution"
)
st.plotly_chart(fig, use_container_width=True)
# Recent evaluations table
st.subheader("π Recent Evaluations")
display_cols = ['agent_name', 'query', 'overall_score', 'execution_time_ms', 'timestamp']
available_cols = [col for col in display_cols if col in filtered_df.columns]
if available_cols:
recent_data = filtered_df[available_cols].head(20)
st.dataframe(recent_data, use_container_width=True)
def show_response_analysis(self, df):
"""Show response analysis tab"""
st.header("π Response Analysis & Tracing")
if df.empty:
st.warning("No data available")
return
# Response metrics
col1, col2, col3 = st.columns(3)
with col1:
if 'response' in df.columns:
avg_length = df['response'].str.len().mean()
st.metric("Avg Response Length", f"{avg_length:.0f} chars")
else:
st.metric("Avg Response Length", "N/A")
with col2:
if 'response' in df.columns:
avg_words = df['response'].str.split().str.len().mean()
st.metric("Avg Word Count", f"{avg_words:.0f} words")
else:
st.metric("Avg Word Count", "N/A")
with col3:
response_rate = (df['response'].notna().sum() / len(df)) * 100
st.metric("Response Rate", f"{response_rate:.1f}%")
# Search functionality
st.subheader("π Search Responses")
search_term = st.text_input("Search in responses:", placeholder="Enter keywords...")
if search_term and 'response' in df.columns:
mask = df['response'].str.contains(search_term, case=False, na=False)
search_results = df[mask]
else:
search_results = df.head(10)
# Display results
if not search_results.empty:
st.write(f"**Showing {len(search_results)} results**")
for idx, row in search_results.iterrows():
with st.expander(f"π€ {row['agent_name']} - Score: {row['overall_score']:.1f}"):
col1, col2 = st.columns([2, 1])
with col1:
st.write("**Query:**")
st.write(row['query'])
if 'response' in row and pd.notna(row['response']):
st.write("**Response:**")
st.write(row['response'])
with col2:
st.write("**Metrics:**")
st.write(f"Overall Score: {row['overall_score']:.1f}/10")
if 'execution_time_ms' in row:
st.write(f"Response Time: {row['execution_time_ms']:.0f}ms")
if 'timestamp' in row:
st.write(f"Timestamp: {row['timestamp']}")
def show_workflow_visualization(self, df):
"""Show workflow visualization tab"""
st.header("π Workflow Visualization")
if df.empty:
st.warning("No data available for workflow visualization.")
return
# Session selection
sessions = df['session_id'].unique()
selected_session = st.selectbox("Select Session", sessions, key="workflow_session")
# Filter data for selected session
session_data = df[df['session_id'] == selected_session]
if session_data.empty:
st.warning("No data found for selected session.")
return
# Session metrics overview
st.subheader("π Session Metrics Overview")
col1, col2, col3, col4 = st.columns(4)
with col1:
avg_score = session_data['overall_score'].mean()
st.metric("Avg Overall Score", f"{avg_score:.2f}/10")
with col2:
avg_latency = session_data['execution_time_ms'].mean()
st.metric("Avg Response Time", f"{avg_latency:.0f}ms")
with col3:
if 'hallucination_score' in session_data.columns:
avg_hallucination = session_data['hallucination_score'].mean()
st.metric("Avg Hallucination", f"{avg_hallucination:.2f}/10")
else:
st.metric("Avg Hallucination", "N/A")
with col4:
if 'total_tokens' in session_data.columns:
total_tokens = session_data['total_tokens'].sum()
total_cost = session_data['cost_usd'].sum() if 'cost_usd' in session_data.columns else 0
st.metric("Total Cost", f"${total_cost:.4f}", f"{total_tokens:,} tokens")
else:
st.metric("Total Cost", "N/A")
# Workflow steps
st.subheader("π Workflow Steps")
for idx, (_, row) in enumerate(session_data.iterrows()):
with st.expander(f"Step {idx + 1}: {row['agent_name']} - Score: {row['overall_score']:.2f}/10"):
col1, col2 = st.columns([1, 1])
with col1:
st.markdown("**Query:**")
st.write(row['query'])
# Performance metrics chart
st.markdown("**Performance Metrics:**")
metrics_data = {
'Overall': row['overall_score'],
'Relevance': row.get('relevance_score', 0),
'Accuracy': row.get('accuracy_score', 0),
'Completeness': row.get('completeness_score', 0),
'Coherence': row.get('coherence_score', 0)
}
if 'hallucination_score' in row:
metrics_data['Hallucination'] = row['hallucination_score']
fig = px.bar(
x=list(metrics_data.keys()),
y=list(metrics_data.values()),
title="Score Breakdown",
labels={'x': 'Metric', 'y': 'Score (0-10)'}
)
fig.update_layout(height=300, showlegend=False)
st.plotly_chart(fig, use_container_width=True)
with col2:
st.markdown("**Response:**")
if pd.notna(row['response']):
st.write(row['response'])
else:
st.write("No response available")
# Resource consumption
st.markdown("**Resource Consumption:**")
if 'input_tokens' in row and pd.notna(row['input_tokens']):
token_col1, token_col2 = st.columns(2)
with token_col1:
st.metric("Input Tokens", f"{int(row['input_tokens']):,}")
st.metric("Output Tokens", f"{int(row.get('output_tokens', 0)):,}")
with token_col2:
st.metric("Total Tokens", f"{int(row.get('total_tokens', 0)):,}")
st.metric("Cost", f"${row.get('cost_usd', 0):.4f}")
# Execution details
st.markdown("**Execution Details:**")
st.write(f"β±οΈ **Execution Time:** {row['execution_time_ms']:.0f}ms")
if 'llm_provider' in row:
st.write(f"π€ **LLM Provider:** {row['llm_provider']}")
if 'model_name' in row:
st.write(f"π§ **Model:** {row['model_name']}")
st.write(f"π‘οΈ **Safety Passed:** {'β
' if row['guardrails_passed'] else 'β'}")
# Session summary
st.subheader("π Session Summary")
summary_col1, summary_col2, summary_col3 = st.columns(3)
with summary_col1:
st.markdown("**Quality Metrics:**")
st.write(f"β’ Average Overall Score: {session_data['overall_score'].mean():.2f}/10")
best_step = session_data.loc[session_data['overall_score'].idxmax()]
st.write(f"β’ Best Performing Step: {best_step['agent_name']}")
st.write(f"β’ Consistency (Std Dev): {session_data['overall_score'].std():.2f}")
with summary_col2:
st.markdown("**Performance Metrics:**")
st.write(f"β’ Total Execution Time: {session_data['execution_time_ms'].sum():.0f}ms")
st.write(f"β’ Average Response Time: {session_data['execution_time_ms'].mean():.0f}ms")
st.write(f"β’ Fastest Step: {session_data['execution_time_ms'].min():.0f}ms")
with summary_col3:
st.markdown("**Resource Usage:**")
if 'total_tokens' in session_data.columns:
st.write(f"β’ Total Tokens Used: {session_data['total_tokens'].sum():,}")
if 'cost_usd' in session_data.columns:
st.write(f"β’ Total Cost: ${session_data['cost_usd'].sum():.4f}")
st.write(f"β’ Avg Cost per Query: ${session_data['cost_usd'].mean():.4f}")
else:
st.write("β’ Token data not available")
# Export functionality
st.subheader("π€ Export Workflow Data")
if st.button("Export Session Data to CSV", key="export_workflow"):
csv_data = session_data.to_csv(index=False)
st.download_button(
label="Download CSV",
data=csv_data,
file_name=f"workflow_session_{selected_session}.csv",
mime="text/csv"
)
def run(self):
"""Run the dashboard"""
st.title("π€ Multi-Agent System Dashboard")
st.markdown("---")
st.info("π **Welcome!** This dashboard showcases evaluation metrics for Diet, Support, and Queries agents.")
# Load data
df = self.load_data()
# Create tabs
tab1, tab2, tab3, tab4 = st.tabs([
"π Overview",
"π€ Agent Performance",
"π Response Analysis",
"π Workflow Visualization"
])
with tab1:
self.show_overview(df)
with tab2:
self.show_agent_performance(df)
with tab3:
self.show_response_analysis(df)
with tab4:
self.show_workflow_visualization(df)
# Footer
st.markdown("---")
st.markdown("π **Multi-Agent System Dashboard** | Built with Streamlit & Plotly")
# Run the dashboard
try:
dashboard = SimpleDashboard()
dashboard.run()
except Exception as e:
st.error(f"Application Error: {str(e)}")
st.info("Please refresh the page.")
with st.expander("Debug Information"):
st.code(f"""
Error: {str(e)}
Type: {type(e).__name__}
Python: {sys.version}
Working Dir: {os.getcwd()}
Temp Dir: {tempfile.gettempdir()}
""") |