Spaces:
Sleeping
Sleeping
Upload app.py
Browse files80 unmodified lines818283848586878889169 unmodified lines2592602612622632642652662672682692702712722732742752762773 unmodified lines2812822832842852862872882892902912922932942952962972982993003013023033043053062 unmodified lines30931031131231331423 unmodified lines3383393403413423433443453463473484 unmodified lines353354355356357358359360361362363364365366367368188 unmodified lines55755855956056156230 unmodified lines593594595596597598599600601602366 unmodified lines96997097197297397428 unmodified lines100310041005100610071008100910101011101210131014101510168 unmodified lines10251026102710281029103010311032103380 unmodified linesaccuracy_score REAL,completeness_score REAL,coherence_score REAL,guardrails_passed BOOLEAN,safety_score REAL,execution_time_ms REAL,error_occurred BOOLEAN DEFAULT FALSE,llm_provider TEXT,model_name TEXT,169 unmodified lines# Generate realistic responseresponse_templates = {"Diet Agent": [f"Based on your query about {query[:30]}..., I recommend focusing on balanced nutrition with emphasis on whole foods, proper portion sizes, and regular meal timing.",f"For your question regarding {query[:30]}..., here's a comprehensive approach that considers your nutritional needs and health goals.",f"Addressing your concern about {query[:30]}..., let me provide evidence-based dietary guidance tailored to your situation."],"Support Agent": [f"I understand you're dealing with {query[:30]}... This is a common challenge, and I'm here to help you work through it step by step.",f"Thank you for sharing your concern about {query[:30]}... Let's explore some practical strategies that can make a real difference.",f"Your question about {query[:30]}... resonates with many people. Here are some effective approaches you can try."],"Queries Agent": [f"Great question about {query[:30]}... This is a complex topic that involves several key concepts and recent developments.",f"To answer your query about {query[:30]}..., let me break this down into the fundamental principles and current applications.",f"Your question regarding {query[:30]}... touches on important technological and societal implications. Here's a comprehensive overview."]}3 unmodified lines# Add specific details based on agent typeif agent == "Diet Agent":details = ["Key recommendations: 1) Focus on whole foods, 2) Control portions, 3) Stay hydrated","Nutritional guidelines: Aim for 50% vegetables, 25% lean protein, 25% complex carbs","Meal timing: Consider eating every 3-4 hours to maintain stable blood sugar","Sample foods: Quinoa, salmon, leafy greens, berries, nuts, and legumes"]elif agent == "Support Agent":details = ["Action steps: 1) Identify triggers, 2) Develop coping strategies, 3) Practice regularly","Techniques to try: Deep breathing, progressive muscle relaxation, mindfulness meditation","Timeline: Start with 5-10 minutes daily, gradually increase as comfort grows","Resources: Consider apps like Headspace, Calm, or consulting a professional"]else: # Queries Agentdetails = ["Technical overview: This involves complex algorithms and data processing methods","Current applications: Used in healthcare, finance, transportation, and entertainment","Future implications: Expected to revolutionize how we work and interact with technology","Key considerations: Privacy, security, ethical implications, and regulatory frameworks"]response = f"{base_response}\n\n{random.choice(details)}"# Generate correlated scores (realistic relationships)2 unmodified linescompleteness_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)))# Realistic safety scenariossafety_pass_rate = 0.95 # 95% pass rateif random.random() < 0.02: # 2% chance of safety issues23 unmodified linesaccuracy_score, # accuracy_scorecompleteness_score, # completeness_scorecoherence_score, # coherence_scoreguardrails_passed, # guardrails_passedsafety_score, # safety_scoreexecution_time, # execution_time_msFalse, # error_occurred"azure", # llm_provider"gpt-4o", # model_namef"Comprehensive evaluation for {agent}: The response demonstrates good understanding of the query with appropriate depth and accuracy. Score breakdown reflects the quality across multiple dimensions.", # judge_reasoningguardrails_failures, # guardrails_failures4 unmodified linesINSERT INTO evaluation_logs (session_id, agent_name, query, response, overall_score,relevance_score, accuracy_score, completeness_score, coherence_score,guardrails_passed, safety_score, execution_time_ms, error_occurred,llm_provider, model_name, judge_reasoning, guardrails_failures, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''', eval_data)# Get the evaluation ID for response analysisevaluation_id = cursor.lastrowid# Insert detailed response analysisself.insert_response_analysis(cursor, evaluation_id, session_id, agent, response, timestamp)def insert_response_analysis(self, cursor, evaluation_id, session_id, agent_name, response_text, timestamp):"""Insert detailed response analysis data"""188 unmodified linesvalue=False)return filtersdef show_executive_summary(self, data: Dict[str, pd.DataFrame]):30 unmodified linesst.metric("Unique Sessions", f"{unique_sessions:,}")# Performance trendsst.subheader("π Performance Trends")# Daily performance trenddf_daily = df.groupby(df['timestamp'].dt.date).agg({'overall_score': 'mean','execution_time_ms': 'mean','guardrails_passed': lambda x: (x.sum() / len(x)) * 100366 unmodified linesmime="text/csv")def run(self):"""Run the dashboard"""st.title("π€ Multi-Agent System Dashboard - Demo")28 unmodified linesif filters.get('safety_only', False):df = df[df['guardrails_passed'] == True]filtered_data['evaluations'] = df# Create tabstab1, tab2, tab3, tab4 = st.tabs(["π Executive Summary","π€ Agent Performance","π‘οΈ Safety Analysis","π Response Analysis"])with tab1:8 unmodified lineswith tab4:self.show_response_analysis(filtered_data)# Footerst.markdown("---")st.markdown("π **Multi-Agent System Dashboard** | Built with Streamlit & Plotly | Demo hosted on Hugging Face Spaces")if __name__ == "__main__":dashboard = HuggingFaceDashboard()80 unmodified lines8182838485868788899091929394169 unmodified lines2642652662672682692702712722732742752762772782792802812823 unmodified lines2862872882892902912922932942952962972982993003013023033043053063073083093103113123133143152 unmodified lines31831932032132232332432532632732832933033133233333433533633733833934023 unmodified lines3643653663673683693703713723733743753763773783794 unmodified lines384385386387388389390391392393394395396397398399400188 unmodified lines58959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463530 unmodified lines666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696366 unmodified lines106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161528 unmodified lines1644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516768 unmodified lines1685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174380 unmodified linesaccuracy_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,error_occurred BOOLEAN DEFAULT FALSE,llm_provider TEXT,model_name TEXT,169 unmodified lines# Generate realistic responseresponse_templates = {"Diet Agent": [f"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.",f"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.",f"Great question about nutrition! Let me share some comprehensive dietary recommendations that can help you achieve better health outcomes."],"Support Agent": [f"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.",f"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.",f"I'm glad you've come to me for support. Your feelings are valid, and together we can explore effective coping strategies and build resilience."],"Queries Agent": [f"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.",f"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.",f"Great inquiry! This is an evolving field with exciting developments. Let me explain the fundamental principles and explore the current state of research and implementation."]}3 unmodified lines# Add specific details based on agent typeif 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. These provide essential nutrients and fiber while avoiding added sugars and preservatives.\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. Proper hydration supports metabolism, digestion, and overall health.\n\n4. **Meal Timing**: Eat regular meals every 3-4 hours to maintain stable blood sugar levels and prevent overeating.\n\n**Sample Daily Meal Plan:**\n- Breakfast: Greek yogurt with berries and nuts\n- Lunch: Quinoa salad with grilled chicken and vegetables\n- Dinner: Baked salmon with roasted sweet potatoes and broccoli\n- Snacks: Apple with almond butter, or handful of mixed nuts","**Evidence-Based Dietary Guidelines:**\n\n1. **Macronutrient Balance**: Aim for 45-65% carbohydrates (focus on complex carbs), 20-35% healthy fats, and 10-35% protein based on your activity level.\n\n2. **Micronutrient Density**: Choose foods rich in vitamins, minerals, and antioxidants. Include colorful fruits and vegetables to ensure variety.\n\n3. **Fiber Intake**: Target 25-35 grams daily through whole grains, legumes, fruits, and vegetables to support digestive health.\n\n4. **Healthy Fats**: Include omega-3 fatty acids from fish, walnuts, and flaxseeds, while limiting saturated and trans fats.\n\n**Practical Implementation Tips:**\n- Meal prep on weekends to ensure healthy options are available\n- Read nutrition labels to make informed choices\n- Practice mindful eating by eating slowly and paying attention to hunger cues\n- Keep a food diary to track patterns and identify areas for improvement","**Personalized Nutrition Approach:**\n\nEvery individual has unique nutritional needs based on age, gender, activity level, health conditions, and personal preferences. Here's how to customize your approach:\n\n1. **Assessment**: Consider your current health status, goals (weight management, energy levels, disease prevention), and any dietary restrictions.\n\n2. **Gradual Changes**: Implement changes slowly to ensure sustainability. Start with one or two modifications per week.\n\n3. **Professional Guidance**: Consider consulting with a registered dietitian for personalized meal planning, especially if you have specific health conditions.\n\n4. **Regular Monitoring**: Track your progress through energy levels, sleep quality, and how you feel overall, not just weight.\n\n**Common Nutritional Myths Debunked:**\n- Carbs aren't inherently bad - choose complex carbohydrates over simple sugars\n- Fat doesn't make you fat - healthy fats are essential for hormone production and nutrient absorption\n- Skipping meals doesn't help with weight loss and can lead to overeating later"]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. This activates your parasympathetic nervous system.\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\n3. **Progressive Muscle Relaxation**: Tense and release each muscle group from toes to head, holding tension for 5 seconds before releasing.\n\n**Long-term Strategies:**\n- Establish a consistent daily routine to provide structure and predictability\n- Practice mindfulness meditation for 10-15 minutes daily\n- Maintain a journal to process emotions and identify patterns\n- Build a support network of trusted friends, family, or support groups\n\n**Professional Resources:**\nConsider reaching out to mental health professionals if you're experiencing persistent difficulties. Many offer telehealth options for convenience.","**Building Emotional Resilience:**\n\n**Understanding Your Emotions:**\nEmotions are natural responses to life events. Learning to recognize, understand, and manage them is a skill that can be developed with practice.\n\n**Practical Steps:**\n1. **Emotion Identification**: Use an emotion wheel or journal to name specific feelings rather than general terms like 'bad' or 'stressed.'\n\n2. **Trigger Awareness**: Notice what situations, people, or thoughts tend to trigger difficult emotions.\n\n3. **Response vs. Reaction**: Create a pause between feeling and action. Ask yourself: 'What would be most helpful right now?'\n\n4. **Self-Compassion**: Treat yourself with the same kindness you'd offer a good friend facing similar challenges.\n\n**Daily Practices:**\n- Morning intention setting (5 minutes)\n- Midday check-in with your emotional state\n- Evening reflection on what went well and what you learned\n- Regular physical activity to support mental health\n\n**Crisis Resources:**\nIf you're experiencing thoughts of self-harm, please reach out immediately to a crisis hotline, emergency services, or trusted healthcare provider.","**Stress Management and Well-being:**\n\n**Understanding Stress:**\nStress is a normal part of life, but chronic stress can impact your physical and mental health. Learning effective management techniques is crucial for long-term well-being.\n\n**Evidence-Based Techniques:**\n1. **Cognitive Restructuring**: Challenge negative thought patterns by asking: 'Is this thought realistic? What evidence supports or contradicts it? What would I tell a friend in this situation?'\n\n2. **Time Management**: Use techniques like the Pomodoro method, prioritization matrices, and saying no to non-essential commitments.\n\n3. **Physical Self-Care**: Regular exercise, adequate sleep (7-9 hours), and proper nutrition form the foundation of stress resilience.\n\n4. **Social Connection**: Maintain relationships with supportive people. Even brief positive interactions can improve mood and reduce stress.\n\n**Creating Your Personal Toolkit:**\n- Identify 3-5 coping strategies that work best for you\n- Practice them regularly, not just during stressful times\n- Adjust and refine your approach based on what's most effective\n- Remember that seeking help is a sign of strength, not weakness"]else: # Queries Agentdetails = ["**Technical Deep Dive:**\n\n**Fundamental Concepts:**\nThis technology represents a convergence of multiple disciplines including computer science, mathematics, engineering, and domain-specific expertise. The underlying principles involve complex algorithms, data structures, and computational methods.\n\n**Current Implementation:**\n1. **Healthcare**: AI-powered diagnostic tools, personalized treatment plans, drug discovery acceleration, and robotic surgery assistance.\n\n2. **Finance**: Algorithmic trading, fraud detection, risk assessment, and automated customer service through chatbots.\n\n3. **Transportation**: Autonomous vehicles, traffic optimization, predictive maintenance, and route planning algorithms.\n\n4. **Entertainment**: Recommendation systems, content generation, virtual reality experiences, and interactive gaming.\n\n**Technical Architecture:**\n- Data processing pipelines that handle massive datasets in real-time\n- Machine learning models trained on diverse, high-quality datasets\n- Cloud infrastructure enabling scalable deployment and accessibility\n- APIs and interfaces that allow integration with existing systems\n\n**Performance Metrics:**\nSuccess is measured through accuracy rates, processing speed, user engagement, cost efficiency, and real-world impact on problem-solving.","**Industry Applications and Impact:**\n\n**Current Market Landscape:**\nThe technology sector is experiencing rapid transformation with significant investments in research and development. Major companies are competing to develop more efficient, ethical, and accessible solutions.\n\n**Real-World Applications:**\n1. **Smart Cities**: IoT sensors, traffic management, energy optimization, and public safety systems working together to improve urban living.\n\n2. **Environmental Monitoring**: Satellite imagery analysis, climate modeling, pollution tracking, and renewable energy optimization.\n\n3. **Education**: Personalized learning platforms, automated grading systems, virtual tutors, and accessibility tools for diverse learners.\n\n4. **Manufacturing**: Predictive maintenance, quality control, supply chain optimization, and human-robot collaboration.\n\n**Economic Impact:**\n- Job creation in new fields while transforming traditional roles\n- Increased productivity and efficiency across industries\n- New business models and revenue streams\n- Global competitiveness and innovation drivers\n\n**Challenges and Solutions:**\n- Addressing ethical concerns through responsible development practices\n- Ensuring data privacy and security through robust frameworks\n- Managing the digital divide through inclusive design and accessibility","**Future Implications and Trends:**\n\n**Emerging Developments:**\nThe field is evolving rapidly with breakthrough research in quantum computing, neuromorphic chips, and advanced algorithms that promise to solve previously intractable problems.\n\n**Next 5-10 Years:**\n1. **Integration**: Seamless integration across platforms and devices, creating more intuitive user experiences.\n\n2. **Personalization**: Highly customized solutions that adapt to individual preferences and needs in real-time.\n\n3. **Sustainability**: Green technology initiatives focusing on energy efficiency and environmental responsibility.\n\n4. **Accessibility**: Universal design principles ensuring technology benefits all users regardless of abilities or circumstances.\n\n**Societal Considerations:**\n- Regulatory frameworks evolving to balance innovation with consumer protection\n- Educational systems adapting to prepare workforce for technological changes\n- International cooperation on standards and ethical guidelines\n- Public discourse on the role of technology in society\n\n**Preparing for the Future:**\n- Continuous learning and skill development\n- Critical thinking about technology's role in daily life\n- Participation in discussions about technology policy and ethics\n- Understanding both opportunities and risks associated with technological advancement"]# Create a more comprehensive responseresponse = f"{base_response}\n\n{random.choice(details)}"# Generate correlated scores (realistic relationships)2 unmodified linescompleteness_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)))# Generate hallucination score (inverse relationship with accuracy)hallucination_score = max(0, min(10, 10 - accuracy_score + random.uniform(-1.0, 1.0)))# Generate token consumption based on response length and agent typeresponse_length = len(response)input_tokens = len(query.split()) * 1.3 # Rough estimateoutput_tokens = response_length / 4 # Rough estimate (4 chars per token)total_tokens = int(input_tokens + output_tokens)# Calculate cost (rough estimates per 1K tokens)cost_per_1k_tokens = {"azure": 0.03, # GPT-4"openai": 0.03,"anthropic": 0.025}cost_usd = (total_tokens / 1000) * cost_per_1k_tokens.get(llm_provider, 0.03)# Realistic safety scenariossafety_pass_rate = 0.95 # 95% pass rateif random.random() < 0.02: # 2% chance of safety issues23 unmodified linesaccuracy_score, # accuracy_scorecompleteness_score, # completeness_scorecoherence_score, # coherence_scorehallucination_score, # hallucination_scoreguardrails_passed, # guardrails_passedsafety_score, # safety_scoreexecution_time, # execution_time_msint(input_tokens), # input_tokensint(output_tokens), # output_tokenstotal_tokens, # total_tokensround(cost_usd, 4), # cost_usdFalse, # error_occurredllm_provider, # llm_provider"gpt-4o", # model_namef"Comprehensive evaluation for {agent}: The response demonstrates good understanding of the query with appropriate depth and accuracy. Score breakdown reflects the quality across multiple dimensions.", # judge_reasoningguardrails_failures, # guardrails_failures4 unmodified linesINSERT 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, error_occurred,llm_provider, model_name, judge_reasoning, guardrails_failures, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''', eval_data)# Get the evaluation ID for response analysisevaluation_id = cursor.lastrowid# Insert detailed response analysisself.insert_response_analysis(cursor, evaluation_id, eval_data[0], agent, response, timestamp)def insert_response_analysis(self, cursor, evaluation_id, session_id, agent_name, response_text, timestamp):"""Insert detailed response analysis data"""188 unmodified linesvalue=False)# Advanced filtersst.sidebar.markdown("### π¬ Advanced Filters")# Performance tier filterfilters['performance_tier'] = st.sidebar.selectbox("π Performance Tier",options=["All", "Excellent (8.5+)", "Good (7.0-8.5)", "Needs Improvement (<7.0)"],index=0)# Response time filterif not data['evaluations'].empty:max_time = data['evaluations']['execution_time_ms'].max()filters['max_response_time'] = st.sidebar.slider("β±οΈ Max Response Time (ms)",min_value=0,max_value=int(max_time),value=int(max_time),step=100)# Model/Provider filterif not data['evaluations'].empty and 'llm_provider' in data['evaluations'].columns:providers = data['evaluations']['llm_provider'].unique().tolist()filters['providers'] = st.sidebar.multiselect("π€ LLM Providers",options=providers,default=providers)# Auto-refresh optionfilters['auto_refresh'] = st.sidebar.checkbox("π Auto-refresh (30s)",value=False,help="Automatically refresh data every 30 seconds")if filters.get('auto_refresh', False):st.sidebar.success("π Auto-refresh enabled")# Add auto-refresh logic here if neededreturn filtersdef show_executive_summary(self, data: Dict[str, pd.DataFrame]):30 unmodified linesst.metric("Unique Sessions", f"{unique_sessions:,}")# Performance trendscol1, col2 = st.columns([3, 1])with col1:st.subheader("π Performance Trends")with col2:trend_period = st.selectbox("π Period",options=["7 days", "30 days", "All time"],index=1,key="trend_period")# Filter data based on selected periodif trend_period == "7 days":cutoff_date = datetime.now() - timedelta(days=7)trend_df = df[df['timestamp'] >= cutoff_date]elif trend_period == "30 days":cutoff_date = datetime.now() - timedelta(days=30)trend_df = df[df['timestamp'] >= cutoff_date]else:trend_df = df# Daily performance trenddf_daily = trend_df.groupby(trend_df['timestamp'].dt.date).agg({'overall_score': 'mean','execution_time_ms': 'mean','guardrails_passed': lambda x: (x.sum() / len(x)) * 100366 unmodified linesmime="text/csv")def show_advanced_analytics(self, data: Dict[str, pd.DataFrame]):"""Show advanced analytics and insights"""st.header("π¬ Advanced Analytics & AI Insights")if data['evaluations'].empty:st.warning("No evaluation data available")returndf_eval = data['evaluations']df_analysis = data.get('response_analysis', pd.DataFrame())# Performance trends and predictionsst.subheader("π Performance Trends & Predictions")col1, col2 = st.columns(2)with col1:st.write("**π Score Trends Over Time**")# Daily performance trend with moving averagedf_daily = df_eval.groupby(df_eval['timestamp'].dt.date).agg({'overall_score': ['mean', 'count'],'execution_time_ms': 'mean'}).reset_index()df_daily.columns = ['date', 'avg_score', 'count', 'avg_time']# Calculate moving averagedf_daily['score_ma'] = df_daily['avg_score'].rolling(window=7, min_periods=1).mean()fig = go.Figure()fig.add_trace(go.Scatter(x=df_daily['date'],y=df_daily['avg_score'],mode='lines+markers',name='Daily Score',line=dict(color='lightblue', width=1),opacity=0.7))fig.add_trace(go.Scatter(x=df_daily['date'],y=df_daily['score_ma'],mode='lines',name='7-Day Moving Average',line=dict(color='red', width=3)))fig.update_layout(title="Score Trends with Moving Average",xaxis_title="Date",yaxis_title="Score",height=400)st.plotly_chart(fig, use_container_width=True)with col2:st.write("**β‘ Performance Correlation Matrix**")# Correlation analysisscore_cols = ['overall_score', 'relevance_score', 'accuracy_score','completeness_score', 'coherence_score', 'execution_time_ms']available_cols = [col for col in score_cols if col in df_eval.columns]if len(available_cols) > 2:corr_matrix = df_eval[available_cols].corr()fig = px.imshow(corr_matrix,title="Performance Metrics Correlation",color_continuous_scale='RdBu',aspect="auto")fig.update_layout(height=400)st.plotly_chart(fig, use_container_width=True)else:st.info("Need more metrics for correlation analysis")# Agent comparison and benchmarkingst.subheader("π Agent Benchmarking & Competition")col1, col2, col3 = st.columns(3)with col1:st.write("**π₯ Agent Leaderboard**")leaderboard = df_eval.groupby('agent_name').agg({'overall_score': ['mean', 'std', 'count'],'execution_time_ms': 'mean'}).round(2)leaderboard.columns = ['Avg Score', 'Score StdDev', 'Total Evals', 'Avg Time (ms)']leaderboard['Efficiency'] = (leaderboard['Avg Score'] / (leaderboard['Avg Time (ms)'] / 1000)).round(2)leaderboard = leaderboard.sort_values('Avg Score', ascending=False)# Add rank and medalsleaderboard['Rank'] = range(1, len(leaderboard) + 1)medals = ['π₯', 'π₯', 'π₯'] + ['π '] * (len(leaderboard) - 3)leaderboard['Medal'] = medals[:len(leaderboard)]st.dataframe(leaderboard[['Medal', 'Rank', 'Avg Score', 'Efficiency', 'Total Evals']], use_container_width=True)with col2:st.write("**π Performance Distribution**")fig = px.box(df_eval,x='agent_name',y='overall_score',title="Score Distribution by Agent",color='agent_name')fig.update_layout(height=300, showlegend=False)st.plotly_chart(fig, use_container_width=True)with col3:st.write("**β‘ Speed vs Quality**")agent_perf = df_eval.groupby('agent_name').agg({'overall_score': 'mean','execution_time_ms': 'mean'}).reset_index()fig = px.scatter(agent_perf,x='execution_time_ms',y='overall_score',size='overall_score',color='agent_name',title="Speed vs Quality Trade-off",labels={'execution_time_ms': 'Response Time (ms)', 'overall_score': 'Quality Score'})fig.update_layout(height=300)st.plotly_chart(fig, use_container_width=True)# AI-powered insights and recommendationsst.subheader("π€ AI-Powered Insights & Recommendations")# Generate insightsinsights = self.generate_ai_insights(df_eval, df_analysis)col1, col2 = st.columns(2)with col1:st.write("**π‘ Key Insights**")for insight in insights['insights']:st.info(f"π {insight}")with col2:st.write("**π Recommendations**")for rec in insights['recommendations']:st.success(f"π‘ {rec}")# Performance anomaly detectionst.subheader("π Anomaly Detection")anomalies = self.detect_anomalies(df_eval)if anomalies:st.warning(f"β οΈ Detected {len(anomalies)} potential anomalies:")for anomaly in anomalies:st.write(f"β’ {anomaly}")else:st.success("β No performance anomalies detected")# Real-time monitoring simulationst.subheader("π‘ Real-time Monitoring Simulation")if st.button("π Simulate Real-time Update"):# Simulate new datalatest_data = self.simulate_realtime_data()col1, col2, col3 = st.columns(3)with col1:st.metric("Latest Score", f"{latest_data['score']:.2f}", f"{latest_data['score_delta']:+.2f}")with col2:st.metric("Response Time", f"{latest_data['time']:.0f}ms", f"{latest_data['time_delta']:+.0f}ms")with col3:st.metric("Safety Status", "β Passed" if latest_data['safe'] else "β Failed")st.success("π Dashboard updated with latest data!")def generate_ai_insights(self, df_eval, df_analysis):"""Generate AI-powered insights from the data"""insights = []recommendations = []# Performance insightsbest_agent = df_eval.groupby('agent_name')['overall_score'].mean().idxmax()worst_agent = df_eval.groupby('agent_name')['overall_score'].mean().idxmin()avg_score = df_eval['overall_score'].mean()score_trend = df_eval.groupby(df_eval['timestamp'].dt.date)['overall_score'].mean()if len(score_trend) > 1:recent_trend = score_trend.iloc[-3:].mean() - score_trend.iloc[:3].mean()if recent_trend > 0.5:insights.append(f"Performance is improving! Recent scores are {recent_trend:.1f} points higher than earlier.")elif recent_trend < -0.5:insights.append(f"Performance decline detected. Recent scores are {abs(recent_trend):.1f} points lower.")# Agent insightsinsights.append(f"{best_agent} is the top performer with highest average scores.")insights.append(f"Overall system performance: {avg_score:.1f}/10 - {'Excellent' if avg_score > 8.5 else 'Good' if avg_score > 7.5 else 'Needs Improvement'}")# Response time insightsavg_time = df_eval['execution_time_ms'].mean()if avg_time > 2000:insights.append(f"Response times are high (avg: {avg_time:.0f}ms). Consider optimization.")# Safety insightssafety_rate = (df_eval['guardrails_passed'].sum() / len(df_eval)) * 100if safety_rate < 95:insights.append(f"Safety pass rate is {safety_rate:.1f}% - below recommended 95% threshold.")# Recommendationsif worst_agent != best_agent:recommendations.append(f"Consider retraining {worst_agent} using patterns from {best_agent}")if avg_time > 1500:recommendations.append("Implement caching or optimize model inference to reduce response times")recommendations.append("Schedule regular performance reviews every 2 weeks")recommendations.append("Set up automated alerts for scores below 7.0 or response times above 3 seconds")if not df_analysis.empty:avg_readability = df_analysis['readability_score'].mean()if avg_readability < 6:recommendations.append("Improve response readability - consider simpler language and shorter sentences")return {'insights': insights, 'recommendations': recommendations}def detect_anomalies(self, df_eval):"""Detect performance anomalies"""anomalies = []# Score anomalies (using IQR method)Q1 = df_eval['overall_score'].quantile(0.25)Q3 = df_eval['overall_score'].quantile(0.75)IQR = Q3 - Q1lower_bound = Q1 - 1.5 * IQRupper_bound = Q3 + 1.5 * IQRscore_anomalies = df_eval[(df_eval['overall_score'] < lower_bound) | (df_eval['overall_score'] > upper_bound)]if len(score_anomalies) > 0:anomalies.append(f"{len(score_anomalies)} evaluations with unusual scores detected")# Response time anomaliestime_Q1 = df_eval['execution_time_ms'].quantile(0.25)time_Q3 = df_eval['execution_time_ms'].quantile(0.75)time_IQR = time_Q3 - time_Q1time_upper = time_Q3 + 1.5 * time_IQRtime_anomalies = df_eval[df_eval['execution_time_ms'] > time_upper]if len(time_anomalies) > 0:anomalies.append(f"{len(time_anomalies)} evaluations with unusually long response times")# Safety anomaliessafety_failures = df_eval[df_eval['guardrails_passed'] == False]if len(safety_failures) > len(df_eval) * 0.1: # More than 10% failuresanomalies.append(f"High safety failure rate: {len(safety_failures)} failures out of {len(df_eval)} evaluations")return anomaliesdef simulate_realtime_data(self):"""Simulate real-time data update"""import randomreturn {'score': random.uniform(7.0, 9.5),'score_delta': random.uniform(-0.5, 0.5),'time': random.uniform(500, 2000),'time_delta': random.uniform(-200, 200),'safe': random.choice([True, True, True, False]) # 75% safe}def show_workflow_visualization(self, data: Dict[str, pd.DataFrame]):"""Show workflow visualization with queries, scores, latency, hallucination, and token consumption"""st.header("π Workflow Visualization")df_eval = data['evaluations']if df_eval.empty:st.warning("No evaluation data available for workflow visualization.")return# Create workflow selectioncol1, col2 = st.columns([1, 1])with col1:sessions = df_eval['session_id'].unique()selected_session = st.selectbox("Select Session", sessions, key="workflow_session")with col2:agents = df_eval['agent_name'].unique()selected_agent = st.selectbox("Select Agent (Optional)", ['All'] + list(agents), key="workflow_agent")# Filter datasession_data = df_eval[df_eval['session_id'] == selected_session]if selected_agent != 'All':session_data = session_data[session_data['agent_name'] == selected_agent]if session_data.empty:st.warning("No data found for selected filters.")return# Create workflow diagramst.subheader("π Workflow Flow Diagram")# Generate Mermaid diagrammermaid_diagram = self.create_workflow_diagram(session_data)# Display the diagram using markdown (since create_diagram might not be available)st.markdown("```mermaid\n" + mermaid_diagram + "\n```")# Workflow metrics overviewst.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",delta=f"{avg_score - 7.5:.2f}" if avg_score > 7.5 else f"{avg_score - 7.5:.2f}")with col2:avg_latency = session_data['execution_time_ms'].mean()st.metric("Avg Response Time", f"{avg_latency:.0f}ms",delta=f"{avg_latency - 3000:.0f}ms" if avg_latency < 3000 else f"+{avg_latency - 3000:.0f}ms")with col3:avg_hallucination = session_data['hallucination_score'].mean() if 'hallucination_score' in session_data.columns else 0st.metric("Avg Hallucination", f"{avg_hallucination:.2f}/10",delta=f"{5.0 - avg_hallucination:.2f}" if avg_hallucination < 5.0 else f"-{avg_hallucination - 5.0:.2f}")with col4:total_tokens = session_data['total_tokens'].sum() if 'total_tokens' in session_data.columns else 0total_cost = session_data['cost_usd'].sum() if 'cost_usd' in session_data.columns else 0st.metric("Total Cost", f"${total_cost:.4f}", f"{total_tokens:,} tokens")# Detailed workflow stepsst.subheader("π Detailed Workflow Steps")for idx, row in session_data.iterrows():with st.expander(f"Step {idx + 1}: {row['agent_name']} - Score: {row['overall_score']:.2f}/10"):# Query and Responsecol1, col2 = st.columns([1, 1])with col1:st.markdown("**Query:**")st.write(row['query'])# Performance metricsst.markdown("**Performance Metrics:**")metrics_data = {'Overall Score': row['overall_score'],'Relevance': row['relevance_score'],'Accuracy': row['accuracy_score'],'Completeness': row['completeness_score'],'Coherence': row['coherence_score'],'Hallucination': row.get('hallucination_score', 0),'Safety': row['safety_score']}# Create a bar chart for scoresimport plotly.graph_objects as gofig = go.Figure(data=[go.Bar(x=list(metrics_data.keys()), y=list(metrics_data.values()),marker_color=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2'])])fig.update_layout(title="Score Breakdown",yaxis_title="Score (0-10)",height=300,showlegend=False)st.plotly_chart(fig, use_container_width=True)with col2:st.markdown("**Response:**")st.write(row['response'])# Token and cost informationst.markdown("**Resource Consumption:**")token_col1, token_col2 = st.columns(2)with token_col1:input_tokens = row.get('input_tokens', 0)output_tokens = row.get('output_tokens', 0)st.metric("Input Tokens", f"{input_tokens:,}")st.metric("Output Tokens", f"{output_tokens:,}")with token_col2:total_tokens = row.get('total_tokens', 0)cost = row.get('cost_usd', 0)st.metric("Total Tokens", f"{total_tokens:,}")st.metric("Cost", f"${cost:.4f}")# Execution detailsst.markdown("**Execution Details:**")exec_time = row['execution_time_ms']llm_provider = row.get('llm_provider', 'Unknown')model_name = row.get('model_name', 'Unknown')st.write(f"β±οΈ **Execution Time:** {exec_time:.0f}ms")st.write(f"π€ **LLM Provider:** {llm_provider}")st.write(f"π§ **Model:** {model_name}")st.write(f"π‘οΈ **Safety Passed:** {'β ' if row['guardrails_passed'] else 'β'}")# Comparative analysisst.subheader("π Comparative Analysis")# Create comparison chartscol1, col2 = st.columns(2)with col1:# Score comparisonfig = go.Figure()score_columns = ['overall_score', 'relevance_score', 'accuracy_score', 'completeness_score', 'coherence_score']if 'hallucination_score' in session_data.columns:score_columns.append('hallucination_score')for i, (idx, row) in enumerate(session_data.iterrows()):fig.add_trace(go.Scatterpolar(r=[row[col] for col in score_columns],theta=[col.replace('_score', '').title() for col in score_columns],fill='toself',name=f"{row['agent_name']} - Step {i+1}"))fig.update_layout(polar=dict(radialaxis=dict(visible=True,range=[0, 10])),showlegend=True,title="Score Comparison Radar Chart")st.plotly_chart(fig, use_container_width=True)with col2:# Token consumption over stepsif 'total_tokens' in session_data.columns:fig = go.Figure()steps = [f"Step {i+1}" for i in range(len(session_data))]fig.add_trace(go.Bar(x=steps,y=session_data['total_tokens'],name='Total Tokens',marker_color='lightblue'))fig.add_trace(go.Scatter(x=steps,y=session_data['execution_time_ms'],yaxis='y2',name='Response Time (ms)',line=dict(color='red', width=2),mode='lines+markers'))fig.update_layout(title="Token Consumption vs Response Time",xaxis_title="Workflow Steps",yaxis_title="Total Tokens",yaxis2=dict(title="Response Time (ms)",overlaying='y',side='right'),height=400)st.plotly_chart(fig, use_container_width=True)# Session summaryst.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")st.write(f"β’ Best Performing Step: {session_data.loc[session_data['overall_score'].idxmax(), '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():,}")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 functionalityst.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 create_workflow_diagram(self, session_data):"""Create a Mermaid workflow diagram"""diagram = "graph TD\n"diagram += " Start([Session Start])\n"for i, (idx, row) in enumerate(session_data.iterrows()):step_id = f"Step{i+1}"agent_name = row['agent_name'].replace(' ', '_')score = row['overall_score']exec_time = row['execution_time_ms']# Color based on scoreif score >= 8.5:color = "fill:#90EE90" # Light greenelif score >= 7.0:color = "fill:#FFE4B5" # Light orangeelse:color = "fill:#FFB6C1" # Light pinkdiagram += f" {step_id}[\"{agent_name}<br/>Score: {score:.1f}/10<br/>Time: {exec_time:.0f}ms\"]\n"diagram += f" {step_id} --> {step_id}_result{{Result}}\n"if i == 0:diagram += f" Start --> {step_id}\n"else:prev_step = f"Step{i}"diagram += f" {prev_step}_result --> {step_id}\n"# Add stylingdiagram += f" class {step_id} stepClass;\n"# Add end nodelast_step = f"Step{len(session_data)}"diagram += f" {last_step}_result --> End([Session End])\n"# Add class definitionsdiagram += " classDef stepClass fill:#e1f5fe,stroke:#01579b,stroke-width:2px;\n"return diagramdef run(self):"""Run the dashboard"""st.title("π€ Multi-Agent System Dashboard - Demo")28 unmodified linesif filters.get('safety_only', False):df = df[df['guardrails_passed'] == True]# Performance tier filterif filters.get('performance_tier') != "All":if filters['performance_tier'] == "Excellent (8.5+)":df = df[df['overall_score'] >= 8.5]elif filters['performance_tier'] == "Good (7.0-8.5)":df = df[(df['overall_score'] >= 7.0) & (df['overall_score'] < 8.5)]elif filters['performance_tier'] == "Needs Improvement (<7.0)":df = df[df['overall_score'] < 7.0]# Response time filterif 'max_response_time' in filters:df = df[df['execution_time_ms'] <= filters['max_response_time']]# Provider filterif 'providers' in filters and filters['providers']:df = df[df['llm_provider'].isin(filters['providers'])]filtered_data['evaluations'] = df# Create tabstab1, tab2, tab3, tab4, tab5, tab6 = st.tabs(["π Executive Summary","π€ Agent Performance","π‘οΈ Safety Analysis","π Response Analysis","π¬ Advanced Analytics","π Workflow Visualization"])with tab1:8 unmodified lineswith tab4:self.show_response_analysis(filtered_data)with tab5:self.show_advanced_analytics(filtered_data)with tab6:self.show_workflow_visualization(filtered_data)# Quick actions sidebarst.sidebar.markdown("---")st.sidebar.markdown("### β‘ Quick Actions")if st.sidebar.button("π Generate Report"):st.sidebar.success("π Report generated!")# Could generate PDF report hereif st.sidebar.button("π Refresh Data"):st.sidebar.success("π Data refreshed!")st.experimental_rerun()if st.sidebar.button("π§ Send Alert"):st.sidebar.success("π§ Alert sent to team!")# Data summary in sidebarif not filtered_data['evaluations'].empty:st.sidebar.markdown("### π Current Session")st.sidebar.metric("Filtered Records", len(filtered_data['evaluations']))st.sidebar.metric("Avg Score", f"{filtered_data['evaluations']['overall_score'].mean():.2f}")st.sidebar.metric("Success Rate", f"{(filtered_data['evaluations']['guardrails_passed'].sum() / len(filtered_data['evaluations']) * 100):.1f}%")# Footerst.markdown("---")col1, col2, col3 = st.columns(3)with col1:st.markdown("π **Multi-Agent System Dashboard**")with col2:st.markdown("Built with Streamlit & Plotly")with col3:if st.button("βΉοΈ About"):st.info("""**Multi-Agent System Dashboard v2.0**Features:- π Real-time monitoring- π€ AI-powered insights- π Advanced analytics- π Response tracing- π‘οΈ Safety monitoring- π Performance benchmarkingBuilt for production-grade multi-agent systems.""")if __name__ == "__main__":dashboard = HuggingFaceDashboard()
src/app.py+740-30