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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
SentilensAI - Enhanced Report Generator with Deep Learning Insights
|
| 4 |
+
|
| 5 |
+
Generates comprehensive reports including deep learning analysis,
|
| 6 |
+
learning recommendations, and actionable insights for agent training.
|
| 7 |
+
|
| 8 |
+
Author: Pravin Selvamuthu
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import json
|
| 12 |
+
import pandas as pd
|
| 13 |
+
import numpy as np
|
| 14 |
+
from datetime import datetime
|
| 15 |
+
from typing import Dict, List, Any
|
| 16 |
+
import matplotlib.pyplot as plt
|
| 17 |
+
import seaborn as sns
|
| 18 |
+
from collections import Counter, defaultdict
|
| 19 |
+
import logging
|
| 20 |
+
|
| 21 |
+
from enhanced_analysis import EnhancedSentimentAnalyzer
|
| 22 |
+
|
| 23 |
+
# Configure logging
|
| 24 |
+
logging.basicConfig(level=logging.INFO)
|
| 25 |
+
logger = logging.getLogger(__name__)
|
| 26 |
+
|
| 27 |
+
class EnhancedReportGenerator:
|
| 28 |
+
"""Generate comprehensive reports with deep learning insights"""
|
| 29 |
+
|
| 30 |
+
def __init__(self):
|
| 31 |
+
self.analyzer = EnhancedSentimentAnalyzer()
|
| 32 |
+
self.report_data = {}
|
| 33 |
+
|
| 34 |
+
def generate_comprehensive_report(self, conversations: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 35 |
+
"""Generate comprehensive report with deep learning analysis"""
|
| 36 |
+
|
| 37 |
+
print("🤖 SentilensAI - Generating Enhanced Comprehensive Report")
|
| 38 |
+
print("=" * 70)
|
| 39 |
+
|
| 40 |
+
# Analyze all conversations with enhanced methods
|
| 41 |
+
enhanced_analyses = []
|
| 42 |
+
for conv in conversations:
|
| 43 |
+
print(f"🔍 Analyzing {conv['conversation_id']} with deep learning...")
|
| 44 |
+
enhanced_analysis = self.analyzer.analyze_conversation_enhanced(conv)
|
| 45 |
+
enhanced_analyses.append(enhanced_analysis)
|
| 46 |
+
|
| 47 |
+
# Generate comprehensive report
|
| 48 |
+
report = {
|
| 49 |
+
'report_metadata': {
|
| 50 |
+
'generated_at': datetime.now().isoformat(),
|
| 51 |
+
'total_conversations': len(conversations),
|
| 52 |
+
'analysis_methods': ['Traditional ML', 'Deep Learning', 'Ensemble'],
|
| 53 |
+
'report_version': '2.0.0'
|
| 54 |
+
},
|
| 55 |
+
'executive_summary': self._generate_executive_summary(enhanced_analyses),
|
| 56 |
+
'deep_learning_insights': self._generate_deep_learning_insights(enhanced_analyses),
|
| 57 |
+
'learning_recommendations': self._generate_learning_recommendations(enhanced_analyses),
|
| 58 |
+
'quality_analysis': self._generate_quality_analysis(enhanced_analyses),
|
| 59 |
+
'improvement_roadmap': self._generate_improvement_roadmap(enhanced_analyses),
|
| 60 |
+
'technical_insights': self._generate_technical_insights(enhanced_analyses),
|
| 61 |
+
'conversation_details': enhanced_analyses
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
return report
|
| 65 |
+
|
| 66 |
+
def _generate_executive_summary(self, analyses: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 67 |
+
"""Generate executive summary with key insights"""
|
| 68 |
+
|
| 69 |
+
# Calculate overall metrics
|
| 70 |
+
total_conversations = len(analyses)
|
| 71 |
+
quality_scores = [analysis['quality_metrics']['overall_quality_score'] for analysis in analyses]
|
| 72 |
+
avg_quality = np.mean(quality_scores)
|
| 73 |
+
|
| 74 |
+
# Categorize conversations
|
| 75 |
+
high_quality = sum(1 for score in quality_scores if score > 0.7)
|
| 76 |
+
medium_quality = sum(1 for score in quality_scores if 0.4 <= score <= 0.7)
|
| 77 |
+
low_quality = sum(1 for score in quality_scores if score < 0.4)
|
| 78 |
+
|
| 79 |
+
# Deep learning metrics
|
| 80 |
+
ensemble_benefits = []
|
| 81 |
+
model_agreements = []
|
| 82 |
+
prediction_confidences = []
|
| 83 |
+
|
| 84 |
+
for analysis in analyses:
|
| 85 |
+
dl_insights = analysis['deep_learning_insights']
|
| 86 |
+
ensemble_benefits.append(dl_insights['deep_learning_insights']['ensemble_benefit'])
|
| 87 |
+
model_agreements.append(dl_insights['model_agreement_analysis']['average_agreement'])
|
| 88 |
+
prediction_confidences.append(dl_insights['deep_learning_insights']['prediction_confidence'])
|
| 89 |
+
|
| 90 |
+
avg_ensemble_benefit = np.mean(ensemble_benefits)
|
| 91 |
+
avg_model_agreement = np.mean(model_agreements)
|
| 92 |
+
avg_prediction_confidence = np.mean(prediction_confidences)
|
| 93 |
+
|
| 94 |
+
return {
|
| 95 |
+
'overall_performance': {
|
| 96 |
+
'average_quality_score': avg_quality,
|
| 97 |
+
'quality_distribution': {
|
| 98 |
+
'high_quality': high_quality,
|
| 99 |
+
'medium_quality': medium_quality,
|
| 100 |
+
'low_quality': low_quality
|
| 101 |
+
},
|
| 102 |
+
'performance_grade': self._calculate_performance_grade(avg_quality)
|
| 103 |
+
},
|
| 104 |
+
'deep_learning_metrics': {
|
| 105 |
+
'ensemble_benefit': avg_ensemble_benefit,
|
| 106 |
+
'model_agreement': avg_model_agreement,
|
| 107 |
+
'prediction_confidence': avg_prediction_confidence,
|
| 108 |
+
'ai_readiness_score': self._calculate_ai_readiness_score(
|
| 109 |
+
avg_ensemble_benefit, avg_model_agreement, avg_prediction_confidence
|
| 110 |
+
)
|
| 111 |
+
},
|
| 112 |
+
'key_insights': self._generate_key_insights(analyses),
|
| 113 |
+
'critical_issues': self._identify_critical_issues(analyses),
|
| 114 |
+
'success_factors': self._identify_success_factors(analyses)
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
def _calculate_performance_grade(self, avg_quality: float) -> str:
|
| 118 |
+
"""Calculate performance grade based on quality score"""
|
| 119 |
+
if avg_quality >= 0.8:
|
| 120 |
+
return 'A+ (Excellent)'
|
| 121 |
+
elif avg_quality >= 0.7:
|
| 122 |
+
return 'A (Very Good)'
|
| 123 |
+
elif avg_quality >= 0.6:
|
| 124 |
+
return 'B (Good)'
|
| 125 |
+
elif avg_quality >= 0.5:
|
| 126 |
+
return 'C (Average)'
|
| 127 |
+
else:
|
| 128 |
+
return 'D (Needs Improvement)'
|
| 129 |
+
|
| 130 |
+
def _calculate_ai_readiness_score(self, ensemble_benefit: float, model_agreement: float,
|
| 131 |
+
prediction_confidence: float) -> float:
|
| 132 |
+
"""Calculate AI readiness score"""
|
| 133 |
+
return (ensemble_benefit * 0.4 + model_agreement * 0.3 + prediction_confidence * 0.3)
|
| 134 |
+
|
| 135 |
+
def _generate_key_insights(self, analyses: List[Dict[str, Any]]) -> List[str]:
|
| 136 |
+
"""Generate key insights from analysis"""
|
| 137 |
+
insights = []
|
| 138 |
+
|
| 139 |
+
# Calculate metrics
|
| 140 |
+
quality_scores = [analysis['quality_metrics']['overall_quality_score'] for analysis in analyses]
|
| 141 |
+
avg_quality = np.mean(quality_scores)
|
| 142 |
+
|
| 143 |
+
ensemble_benefits = [analysis['deep_learning_insights']['deep_learning_insights']['ensemble_benefit']
|
| 144 |
+
for analysis in analyses]
|
| 145 |
+
avg_ensemble_benefit = np.mean(ensemble_benefits)
|
| 146 |
+
|
| 147 |
+
# Generate insights
|
| 148 |
+
if avg_quality > 0.7:
|
| 149 |
+
insights.append("High overall conversation quality indicates effective agent training")
|
| 150 |
+
elif avg_quality < 0.5:
|
| 151 |
+
insights.append("Low conversation quality suggests need for comprehensive training overhaul")
|
| 152 |
+
|
| 153 |
+
if avg_ensemble_benefit > 0.8:
|
| 154 |
+
insights.append("Strong ensemble benefit indicates effective use of multiple AI models")
|
| 155 |
+
elif avg_ensemble_benefit < 0.6:
|
| 156 |
+
insights.append("Limited ensemble benefit suggests need for model optimization")
|
| 157 |
+
|
| 158 |
+
# Count improvement opportunities
|
| 159 |
+
total_opportunities = sum(len(analysis['improvement_opportunities']) for analysis in analyses)
|
| 160 |
+
if total_opportunities > len(analyses) * 2:
|
| 161 |
+
insights.append("Multiple improvement opportunities identified across conversations")
|
| 162 |
+
|
| 163 |
+
return insights
|
| 164 |
+
|
| 165 |
+
def _identify_critical_issues(self, analyses: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
| 166 |
+
"""Identify critical issues requiring immediate attention"""
|
| 167 |
+
critical_issues = []
|
| 168 |
+
|
| 169 |
+
for analysis in analyses:
|
| 170 |
+
conv_id = analysis['conversation_id']
|
| 171 |
+
quality_metrics = analysis['quality_metrics']
|
| 172 |
+
|
| 173 |
+
# Check for critical quality issues
|
| 174 |
+
if quality_metrics['negative_response_rate'] > 0.2:
|
| 175 |
+
critical_issues.append({
|
| 176 |
+
'conversation_id': conv_id,
|
| 177 |
+
'issue': 'High negative response rate',
|
| 178 |
+
'severity': 'CRITICAL',
|
| 179 |
+
'description': f"Negative response rate: {quality_metrics['negative_response_rate']:.2f}",
|
| 180 |
+
'action_required': 'Immediate tone checking implementation'
|
| 181 |
+
})
|
| 182 |
+
|
| 183 |
+
if quality_metrics['average_confidence'] < 0.4:
|
| 184 |
+
critical_issues.append({
|
| 185 |
+
'conversation_id': conv_id,
|
| 186 |
+
'issue': 'Very low confidence scores',
|
| 187 |
+
'severity': 'HIGH',
|
| 188 |
+
'description': f"Average confidence: {quality_metrics['average_confidence']:.2f}",
|
| 189 |
+
'action_required': 'Confidence improvement training'
|
| 190 |
+
})
|
| 191 |
+
|
| 192 |
+
if quality_metrics['model_agreement_rate'] < 0.5:
|
| 193 |
+
critical_issues.append({
|
| 194 |
+
'conversation_id': conv_id,
|
| 195 |
+
'issue': 'Poor model agreement',
|
| 196 |
+
'severity': 'MEDIUM',
|
| 197 |
+
'description': f"Model agreement: {quality_metrics['model_agreement_rate']:.2f}",
|
| 198 |
+
'action_required': 'Ensemble optimization'
|
| 199 |
+
})
|
| 200 |
+
|
| 201 |
+
return critical_issues
|
| 202 |
+
|
| 203 |
+
def _identify_success_factors(self, analyses: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
| 204 |
+
"""Identify success factors from high-performing conversations"""
|
| 205 |
+
success_factors = []
|
| 206 |
+
|
| 207 |
+
# Find high-quality conversations
|
| 208 |
+
high_quality_convs = [analysis for analysis in analyses
|
| 209 |
+
if analysis['quality_metrics']['overall_quality_score'] > 0.7]
|
| 210 |
+
|
| 211 |
+
if high_quality_convs:
|
| 212 |
+
# Analyze common patterns
|
| 213 |
+
high_confidence_count = sum(1 for conv in high_quality_convs
|
| 214 |
+
if conv['quality_metrics']['average_confidence'] > 0.7)
|
| 215 |
+
|
| 216 |
+
if high_confidence_count > 0:
|
| 217 |
+
success_factors.append({
|
| 218 |
+
'factor': 'High Confidence Responses',
|
| 219 |
+
'description': f"{high_confidence_count} high-quality conversations had high confidence",
|
| 220 |
+
'recommendation': 'Replicate confidence-building techniques'
|
| 221 |
+
})
|
| 222 |
+
|
| 223 |
+
# Check for positive sentiment trends
|
| 224 |
+
positive_trend_count = 0
|
| 225 |
+
for conv in high_quality_convs:
|
| 226 |
+
user_sentiments = conv['deep_learning_insights']['sentiment_evolution']['user_trend']
|
| 227 |
+
if len(user_sentiments) >= 2 and user_sentiments[-1] == 'positive':
|
| 228 |
+
positive_trend_count += 1
|
| 229 |
+
|
| 230 |
+
if positive_trend_count > 0:
|
| 231 |
+
success_factors.append({
|
| 232 |
+
'factor': 'Positive Sentiment Management',
|
| 233 |
+
'description': f"{positive_trend_count} conversations successfully maintained positive sentiment",
|
| 234 |
+
'recommendation': 'Document and train on positive sentiment techniques'
|
| 235 |
+
})
|
| 236 |
+
|
| 237 |
+
return success_factors
|
| 238 |
+
|
| 239 |
+
def _generate_deep_learning_insights(self, analyses: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 240 |
+
"""Generate deep learning specific insights"""
|
| 241 |
+
|
| 242 |
+
# Aggregate deep learning metrics
|
| 243 |
+
all_ensemble_benefits = []
|
| 244 |
+
all_model_agreements = []
|
| 245 |
+
all_prediction_confidences = []
|
| 246 |
+
all_model_diversities = []
|
| 247 |
+
|
| 248 |
+
for analysis in analyses:
|
| 249 |
+
dl_insights = analysis['deep_learning_insights']['deep_learning_insights']
|
| 250 |
+
all_ensemble_benefits.append(dl_insights['ensemble_benefit'])
|
| 251 |
+
all_model_agreements.append(analysis['deep_learning_insights']['model_agreement_analysis']['average_agreement'])
|
| 252 |
+
all_prediction_confidences.append(dl_insights['prediction_confidence'])
|
| 253 |
+
all_model_diversities.append(dl_insights['model_diversity'])
|
| 254 |
+
|
| 255 |
+
return {
|
| 256 |
+
'ensemble_performance': {
|
| 257 |
+
'average_benefit': np.mean(all_ensemble_benefits),
|
| 258 |
+
'benefit_consistency': 1.0 - np.std(all_ensemble_benefits),
|
| 259 |
+
'recommendation': self._get_ensemble_recommendation(np.mean(all_ensemble_benefits))
|
| 260 |
+
},
|
| 261 |
+
'model_agreement_analysis': {
|
| 262 |
+
'average_agreement': np.mean(all_model_agreements),
|
| 263 |
+
'agreement_consistency': 1.0 - np.std(all_model_agreements),
|
| 264 |
+
'disagreement_rate': sum(1 for agree in all_model_agreements if agree < 0.7) / len(all_model_agreements)
|
| 265 |
+
},
|
| 266 |
+
'prediction_confidence': {
|
| 267 |
+
'average_confidence': np.mean(all_prediction_confidences),
|
| 268 |
+
'confidence_consistency': 1.0 - np.std(all_prediction_confidences),
|
| 269 |
+
'low_confidence_rate': sum(1 for conf in all_prediction_confidences if conf < 0.6) / len(all_prediction_confidences)
|
| 270 |
+
},
|
| 271 |
+
'model_diversity': {
|
| 272 |
+
'average_diversity': np.mean(all_model_diversities),
|
| 273 |
+
'diversity_consistency': 1.0 - np.std(all_model_diversities),
|
| 274 |
+
'recommendation': self._get_diversity_recommendation(np.mean(all_model_diversities))
|
| 275 |
+
},
|
| 276 |
+
'ai_optimization_opportunities': self._identify_ai_optimization_opportunities(analyses)
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
def _get_ensemble_recommendation(self, avg_benefit: float) -> str:
|
| 280 |
+
"""Get ensemble recommendation based on benefit score"""
|
| 281 |
+
if avg_benefit > 0.8:
|
| 282 |
+
return "Excellent ensemble performance - maintain current configuration"
|
| 283 |
+
elif avg_benefit > 0.6:
|
| 284 |
+
return "Good ensemble performance - consider fine-tuning weights"
|
| 285 |
+
else:
|
| 286 |
+
return "Poor ensemble performance - implement better model selection and weighting"
|
| 287 |
+
|
| 288 |
+
def _get_diversity_recommendation(self, avg_diversity: float) -> str:
|
| 289 |
+
"""Get diversity recommendation based on diversity score"""
|
| 290 |
+
if avg_diversity > 0.8:
|
| 291 |
+
return "High model diversity - good for robust predictions"
|
| 292 |
+
elif avg_diversity > 0.6:
|
| 293 |
+
return "Moderate diversity - consider adding more model types"
|
| 294 |
+
else:
|
| 295 |
+
return "Low diversity - add more diverse models to ensemble"
|
| 296 |
+
|
| 297 |
+
def _identify_ai_optimization_opportunities(self, analyses: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
| 298 |
+
"""Identify AI optimization opportunities"""
|
| 299 |
+
opportunities = []
|
| 300 |
+
|
| 301 |
+
# Analyze model performance patterns
|
| 302 |
+
low_confidence_convs = [analysis for analysis in analyses
|
| 303 |
+
if analysis['quality_metrics']['average_confidence'] < 0.6]
|
| 304 |
+
|
| 305 |
+
if len(low_confidence_convs) > len(analyses) * 0.3:
|
| 306 |
+
opportunities.append({
|
| 307 |
+
'area': 'Model Confidence',
|
| 308 |
+
'issue': 'Widespread low confidence predictions',
|
| 309 |
+
'solution': 'Implement confidence calibration and model fine-tuning',
|
| 310 |
+
'priority': 'High',
|
| 311 |
+
'expected_impact': 'Significant improvement in prediction reliability'
|
| 312 |
+
})
|
| 313 |
+
|
| 314 |
+
# Analyze ensemble effectiveness
|
| 315 |
+
low_agreement_convs = [analysis for analysis in analyses
|
| 316 |
+
if analysis['quality_metrics']['model_agreement_rate'] < 0.7]
|
| 317 |
+
|
| 318 |
+
if len(low_agreement_convs) > len(analyses) * 0.4:
|
| 319 |
+
opportunities.append({
|
| 320 |
+
'area': 'Model Agreement',
|
| 321 |
+
'issue': 'Poor model agreement across conversations',
|
| 322 |
+
'solution': 'Implement consensus mechanisms and model selection strategies',
|
| 323 |
+
'priority': 'Medium',
|
| 324 |
+
'expected_impact': 'Improved prediction consistency'
|
| 325 |
+
})
|
| 326 |
+
|
| 327 |
+
return opportunities
|
| 328 |
+
|
| 329 |
+
def _generate_learning_recommendations(self, analyses: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 330 |
+
"""Generate comprehensive learning recommendations"""
|
| 331 |
+
|
| 332 |
+
# Aggregate recommendations from all conversations
|
| 333 |
+
all_immediate_actions = []
|
| 334 |
+
all_short_term_improvements = []
|
| 335 |
+
all_long_term_strategies = []
|
| 336 |
+
all_technical_recommendations = []
|
| 337 |
+
|
| 338 |
+
for analysis in analyses:
|
| 339 |
+
recommendations = analysis['learning_recommendations']
|
| 340 |
+
all_immediate_actions.extend(recommendations['immediate_actions'])
|
| 341 |
+
all_short_term_improvements.extend(recommendations['short_term_improvements'])
|
| 342 |
+
all_long_term_strategies.extend(recommendations['long_term_strategy'])
|
| 343 |
+
all_technical_recommendations.extend(recommendations['technical_recommendations'])
|
| 344 |
+
|
| 345 |
+
# Consolidate and prioritize recommendations
|
| 346 |
+
consolidated_recommendations = {
|
| 347 |
+
'immediate_actions': self._consolidate_recommendations(all_immediate_actions),
|
| 348 |
+
'short_term_improvements': self._consolidate_recommendations(all_short_term_improvements),
|
| 349 |
+
'long_term_strategies': self._consolidate_recommendations(all_long_term_strategies),
|
| 350 |
+
'technical_recommendations': self._consolidate_recommendations(all_technical_recommendations),
|
| 351 |
+
'learning_priorities': self._generate_learning_priorities(analyses),
|
| 352 |
+
'training_roadmap': self._generate_training_roadmap(analyses)
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
return consolidated_recommendations
|
| 356 |
+
|
| 357 |
+
def _consolidate_recommendations(self, recommendations: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
| 358 |
+
"""Consolidate similar recommendations and remove duplicates"""
|
| 359 |
+
consolidated = []
|
| 360 |
+
seen_actions = set()
|
| 361 |
+
|
| 362 |
+
for rec in recommendations:
|
| 363 |
+
action_key = rec.get('action', rec.get('recommendation', '')).lower()
|
| 364 |
+
if action_key not in seen_actions:
|
| 365 |
+
consolidated.append(rec)
|
| 366 |
+
seen_actions.add(action_key)
|
| 367 |
+
|
| 368 |
+
return consolidated
|
| 369 |
+
|
| 370 |
+
def _generate_learning_priorities(self, analyses: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 371 |
+
"""Generate learning priorities based on analysis"""
|
| 372 |
+
|
| 373 |
+
# Count critical issues
|
| 374 |
+
critical_issues = self._identify_critical_issues(analyses)
|
| 375 |
+
issue_counts = Counter(issue['issue'] for issue in critical_issues)
|
| 376 |
+
|
| 377 |
+
priorities = {
|
| 378 |
+
'critical': list(issue_counts.keys())[:3], # Top 3 critical issues
|
| 379 |
+
'high_impact_areas': [
|
| 380 |
+
'Response Confidence',
|
| 381 |
+
'Sentiment Management',
|
| 382 |
+
'Model Agreement'
|
| 383 |
+
],
|
| 384 |
+
'training_focus': [
|
| 385 |
+
'Deep Learning Integration',
|
| 386 |
+
'Ensemble Optimization',
|
| 387 |
+
'Confidence Calibration'
|
| 388 |
+
]
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
return priorities
|
| 392 |
+
|
| 393 |
+
def _generate_training_roadmap(self, analyses: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 394 |
+
"""Generate training roadmap with timelines"""
|
| 395 |
+
|
| 396 |
+
return {
|
| 397 |
+
'phase_1_immediate': {
|
| 398 |
+
'timeline': '1-2 weeks',
|
| 399 |
+
'focus': 'Critical issue resolution',
|
| 400 |
+
'activities': [
|
| 401 |
+
'Implement tone checking system',
|
| 402 |
+
'Deploy confidence improvement training',
|
| 403 |
+
'Set up real-time monitoring'
|
| 404 |
+
]
|
| 405 |
+
},
|
| 406 |
+
'phase_2_short_term': {
|
| 407 |
+
'timeline': '1-2 months',
|
| 408 |
+
'focus': 'Deep learning optimization',
|
| 409 |
+
'activities': [
|
| 410 |
+
'Fine-tune transformer models',
|
| 411 |
+
'Implement ensemble consensus',
|
| 412 |
+
'Create training data pipeline'
|
| 413 |
+
]
|
| 414 |
+
},
|
| 415 |
+
'phase_3_long_term': {
|
| 416 |
+
'timeline': '3-6 months',
|
| 417 |
+
'focus': 'Advanced AI capabilities',
|
| 418 |
+
'activities': [
|
| 419 |
+
'Develop custom domain models',
|
| 420 |
+
'Implement active learning',
|
| 421 |
+
'Create continuous improvement pipeline'
|
| 422 |
+
]
|
| 423 |
+
}
|
| 424 |
+
}
|
| 425 |
+
|
| 426 |
+
def _generate_quality_analysis(self, analyses: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 427 |
+
"""Generate quality analysis with deep learning insights"""
|
| 428 |
+
|
| 429 |
+
quality_scores = [analysis['quality_metrics']['overall_quality_score'] for analysis in analyses]
|
| 430 |
+
confidence_scores = [analysis['quality_metrics']['average_confidence'] for analysis in analyses]
|
| 431 |
+
agreement_scores = [analysis['quality_metrics']['model_agreement_rate'] for analysis in analyses]
|
| 432 |
+
|
| 433 |
+
return {
|
| 434 |
+
'overall_quality_metrics': {
|
| 435 |
+
'average_quality_score': np.mean(quality_scores),
|
| 436 |
+
'quality_consistency': 1.0 - np.std(quality_scores),
|
| 437 |
+
'quality_distribution': {
|
| 438 |
+
'excellent': sum(1 for score in quality_scores if score > 0.8),
|
| 439 |
+
'good': sum(1 for score in quality_scores if 0.6 <= score <= 0.8),
|
| 440 |
+
'average': sum(1 for score in quality_scores if 0.4 <= score < 0.6),
|
| 441 |
+
'poor': sum(1 for score in quality_scores if score < 0.4)
|
| 442 |
+
}
|
| 443 |
+
},
|
| 444 |
+
'confidence_analysis': {
|
| 445 |
+
'average_confidence': np.mean(confidence_scores),
|
| 446 |
+
'confidence_consistency': 1.0 - np.std(confidence_scores),
|
| 447 |
+
'low_confidence_rate': sum(1 for conf in confidence_scores if conf < 0.6) / len(confidence_scores)
|
| 448 |
+
},
|
| 449 |
+
'model_agreement_analysis': {
|
| 450 |
+
'average_agreement': np.mean(agreement_scores),
|
| 451 |
+
'agreement_consistency': 1.0 - np.std(agreement_scores),
|
| 452 |
+
'disagreement_rate': sum(1 for agree in agreement_scores if agree < 0.7) / len(agreement_scores)
|
| 453 |
+
}
|
| 454 |
+
}
|
| 455 |
+
|
| 456 |
+
def _generate_improvement_roadmap(self, analyses: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 457 |
+
"""Generate improvement roadmap with specific actions"""
|
| 458 |
+
|
| 459 |
+
return {
|
| 460 |
+
'immediate_improvements': {
|
| 461 |
+
'timeline': '1-2 weeks',
|
| 462 |
+
'actions': [
|
| 463 |
+
'Deploy real-time tone checking',
|
| 464 |
+
'Implement confidence monitoring',
|
| 465 |
+
'Create quality alerts'
|
| 466 |
+
],
|
| 467 |
+
'expected_impact': 'Eliminate critical issues'
|
| 468 |
+
},
|
| 469 |
+
'short_term_improvements': {
|
| 470 |
+
'timeline': '1-2 months',
|
| 471 |
+
'actions': [
|
| 472 |
+
'Optimize ensemble methods',
|
| 473 |
+
'Improve model agreement',
|
| 474 |
+
'Enhance training data quality'
|
| 475 |
+
],
|
| 476 |
+
'expected_impact': 'Significant quality improvement'
|
| 477 |
+
},
|
| 478 |
+
'long_term_improvements': {
|
| 479 |
+
'timeline': '3-6 months',
|
| 480 |
+
'actions': [
|
| 481 |
+
'Develop custom AI models',
|
| 482 |
+
'Implement active learning',
|
| 483 |
+
'Create continuous improvement pipeline'
|
| 484 |
+
],
|
| 485 |
+
'expected_impact': 'Industry-leading performance'
|
| 486 |
+
}
|
| 487 |
+
}
|
| 488 |
+
|
| 489 |
+
def _generate_technical_insights(self, analyses: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 490 |
+
"""Generate technical insights and recommendations"""
|
| 491 |
+
|
| 492 |
+
return {
|
| 493 |
+
'model_performance': {
|
| 494 |
+
'ensemble_effectiveness': 'High' if np.mean([a['deep_learning_insights']['deep_learning_insights']['ensemble_benefit']
|
| 495 |
+
for a in analyses]) > 0.7 else 'Medium',
|
| 496 |
+
'model_diversity': 'Good' if np.mean([a['deep_learning_insights']['deep_learning_insights']['model_diversity']
|
| 497 |
+
for a in analyses]) > 0.7 else 'Needs Improvement',
|
| 498 |
+
'prediction_reliability': 'High' if np.mean([a['quality_metrics']['model_agreement_rate']
|
| 499 |
+
for a in analyses]) > 0.7 else 'Medium'
|
| 500 |
+
},
|
| 501 |
+
'infrastructure_recommendations': [
|
| 502 |
+
'Deploy scalable model serving infrastructure',
|
| 503 |
+
'Implement real-time monitoring and alerting',
|
| 504 |
+
'Create automated model retraining pipeline',
|
| 505 |
+
'Set up A/B testing for model improvements'
|
| 506 |
+
],
|
| 507 |
+
'data_quality_insights': {
|
| 508 |
+
'training_data_adequacy': 'Sufficient' if len(analyses) > 10 else 'Insufficient',
|
| 509 |
+
'data_diversity': 'Good' if len(set(a['conversation_id'] for a in analyses)) > 5 else 'Limited',
|
| 510 |
+
'quality_consistency': 'High' if np.std([a['quality_metrics']['overall_quality_score'] for a in analyses]) < 0.2 else 'Variable'
|
| 511 |
+
}
|
| 512 |
+
}
|
| 513 |
+
|
| 514 |
+
def save_report(self, report: Dict[str, Any], filename: str = None) -> str:
|
| 515 |
+
"""Save comprehensive report to file"""
|
| 516 |
+
|
| 517 |
+
if filename is None:
|
| 518 |
+
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 519 |
+
filename = f"enhanced_sentiment_report_{timestamp}.json"
|
| 520 |
+
|
| 521 |
+
with open(filename, 'w', encoding='utf-8') as f:
|
| 522 |
+
json.dump(report, f, indent=2, ensure_ascii=False)
|
| 523 |
+
|
| 524 |
+
logger.info(f"Enhanced report saved to: {filename}")
|
| 525 |
+
return filename
|
| 526 |
+
|
| 527 |
+
def main():
|
| 528 |
+
"""Demo function for enhanced report generation"""
|
| 529 |
+
print("🤖 SentilensAI - Enhanced Report Generator Demo")
|
| 530 |
+
print("=" * 60)
|
| 531 |
+
|
| 532 |
+
# Load sample conversations
|
| 533 |
+
import glob
|
| 534 |
+
result_files = glob.glob("sentiment_analysis_results_*.json")
|
| 535 |
+
if not result_files:
|
| 536 |
+
print("❌ No analysis results found!")
|
| 537 |
+
return
|
| 538 |
+
|
| 539 |
+
latest_file = max(result_files)
|
| 540 |
+
with open(latest_file, 'r', encoding='utf-8') as f:
|
| 541 |
+
results = json.load(f)
|
| 542 |
+
|
| 543 |
+
# Convert to conversation format
|
| 544 |
+
conversations = []
|
| 545 |
+
for conv in results['conversation_results']:
|
| 546 |
+
conversation = {
|
| 547 |
+
'conversation_id': conv['conversation_id'],
|
| 548 |
+
'timestamp': conv['timestamp'],
|
| 549 |
+
'messages': []
|
| 550 |
+
}
|
| 551 |
+
|
| 552 |
+
for msg in conv['message_analysis']:
|
| 553 |
+
conversation['messages'].append({
|
| 554 |
+
'user': msg['user_message'],
|
| 555 |
+
'bot': msg['bot_message'],
|
| 556 |
+
'timestamp': msg['timestamp']
|
| 557 |
+
})
|
| 558 |
+
|
| 559 |
+
conversations.append(conversation)
|
| 560 |
+
|
| 561 |
+
# Generate enhanced report
|
| 562 |
+
generator = EnhancedReportGenerator()
|
| 563 |
+
report = generator.generate_comprehensive_report(conversations)
|
| 564 |
+
|
| 565 |
+
# Save report
|
| 566 |
+
filename = generator.save_report(report)
|
| 567 |
+
|
| 568 |
+
# Display summary
|
| 569 |
+
print(f"\n📊 Enhanced Report Summary:")
|
| 570 |
+
print(f" Total Conversations: {report['report_metadata']['total_conversations']}")
|
| 571 |
+
print(f" Average Quality Score: {report['executive_summary']['overall_performance']['average_quality_score']:.2f}")
|
| 572 |
+
print(f" Performance Grade: {report['executive_summary']['overall_performance']['performance_grade']}")
|
| 573 |
+
print(f" AI Readiness Score: {report['executive_summary']['deep_learning_metrics']['ai_readiness_score']:.2f}")
|
| 574 |
+
|
| 575 |
+
print(f"\n🎓 Learning Recommendations:")
|
| 576 |
+
print(f" Immediate Actions: {len(report['learning_recommendations']['immediate_actions'])}")
|
| 577 |
+
print(f" Short-term Improvements: {len(report['learning_recommendations']['short_term_improvements'])}")
|
| 578 |
+
print(f" Long-term Strategies: {len(report['learning_recommendations']['long_term_strategies'])}")
|
| 579 |
+
|
| 580 |
+
print(f"\n💾 Report saved to: {filename}")
|
| 581 |
+
print(f"\n✅ Enhanced report generation completed!")
|
| 582 |
+
print(f"🚀 Deep learning insights and recommendations ready!")
|
| 583 |
+
|
| 584 |
+
if __name__ == "__main__":
|
| 585 |
+
main()
|