File size: 29,590 Bytes
edf1149 3b1e6c7 edf1149 526a1d2 edf1149 526a1d2 edf1149 526a1d2 edf1149 3b1e6c7 edf1149 3b1e6c7 edf1149 3b1e6c7 edf1149 3b1e6c7 edf1149 |
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 |
# DEPENDENCIES
import time
from typing import Any
from typing import Dict
from typing import List
from loguru import logger
from typing import Optional
from dataclasses import dataclass
from config.settings import settings
from metrics.entropy import EntropyMetric
from config.threshold_config import Domain
from metrics.base_metric import MetricResult
from detector.ensemble import EnsembleResult
from metrics.perplexity import PerplexityMetric
from metrics.linguistic import LinguisticMetric
from metrics.structural import StructuralMetric
from detector.ensemble import EnsembleClassifier
from processors.text_processor import TextProcessor
from processors.text_processor import ProcessedText
from processors.domain_classifier import DomainClassifier
from processors.domain_classifier import DomainPrediction
from processors.language_detector import LanguageDetector
from metrics.semantic_analysis import SemanticAnalysisMetric
from processors.language_detector import LanguageDetectionResult
from metrics.multi_perturbation_stability import MultiPerturbationStabilityMetric
@dataclass
class DetectionResult:
"""
Complete detection result with all metadata
"""
# Final results
ensemble_result : EnsembleResult
# Input metadata
processed_text : ProcessedText
domain_prediction : DomainPrediction
language_result : Optional[LanguageDetectionResult]
# Metric details
metric_results : Dict[str, MetricResult]
# Performance metrics
processing_time : float
metrics_execution_time : Dict[str, float]
# Warnings and errors
warnings : List[str]
errors : List[str]
# File information
file_info : Optional[Dict[str, Any]] = None
def to_dict(self) -> Dict[str, Any]:
"""
Convert to dictionary for JSON serialization
"""
result = {"prediction" : {"verdict" : self.ensemble_result.final_verdict,
"ai_probability" : round(self.ensemble_result.ai_probability, 4),
"human_probability" : round(self.ensemble_result.human_probability, 4),
"mixed_probability" : round(self.ensemble_result.mixed_probability, 4),
"confidence" : round(self.ensemble_result.overall_confidence, 4),
},
"analysis" : {"domain" : self.domain_prediction.primary_domain.value,
"domain_confidence" : round(self.domain_prediction.confidence, 4),
"language" : self.language_result.primary_language.value if self.language_result else "unknown",
"language_confidence" : round(self.language_result.confidence, 4) if self.language_result else 0.0,
"text_length" : self.processed_text.word_count,
"sentence_count" : self.processed_text.sentence_count,
},
"metrics" : {name: result.to_dict() for name, result in self.metric_results.items()},
"ensemble" : self.ensemble_result.to_dict(),
"performance" : {"total_time" : round(self.processing_time, 3),
"metrics_time" : {name: round(t, 3) for name, t in self.metrics_execution_time.items()},
},
"warnings" : self.warnings,
"errors" : self.errors,
}
# Include file_info if available
if self.file_info:
result["file_info"] = self.file_info
return result
class DetectionOrchestrator:
"""
Coordinates the entire detection pipeline from text input to final results.
Pipeline:
1. Text preprocessing
2. Domain classification
3. Language detection (optional)
4. Metric execution (parallel/sequential)
5. Ensemble aggregation
6. Result generation
"""
def __init__(self, enable_language_detection: bool = False, parallel_execution: bool = False, skip_expensive_metrics: bool = False):
"""
Initialize detection orchestrator
Arguments:
----------
enable_language_detection { bool } : Enable language detection step
parallel_execution { bool } : Execute metrics in parallel (future feature)
skip_expensive_metrics { bool } : Skip computationally expensive metrics
"""
self.enable_language_detection = enable_language_detection
self.parallel_execution = parallel_execution
self.skip_expensive_metrics = skip_expensive_metrics
# Initialize processors
self.text_processor = TextProcessor(min_text_length = settings.MIN_TEXT_LENGTH,
max_text_length = settings.MAX_TEXT_LENGTH,
)
self.domain_classifier = DomainClassifier()
if self.enable_language_detection:
self.language_detector = LanguageDetector(use_model = True)
else:
self.language_detector = None
# Initialize metrics
self.metrics = self._initialize_metrics()
# Initialize ensemble
self.ensemble = EnsembleClassifier(primary_method = "confidence_calibrated",
fallback_method = "domain_weighted",
use_ml_ensemble = False,
min_metrics_required = 3,
)
logger.info(f"DetectionOrchestrator initialized (language_detection={enable_language_detection}, skip_expensive={skip_expensive_metrics})")
def _initialize_metrics(self) -> Dict[str, Any]:
"""
Initialize all enabled metrics
"""
metrics = dict()
# Structural metric (statistical analysis)
try:
metrics["structural"] = StructuralMetric()
logger.debug("Structural metric initialized")
except Exception as e:
logger.error(f"Failed to initialize structural metric: {repr(e)}")
# Entropy metric
try:
metrics["entropy"] = EntropyMetric()
logger.debug("Entropy metric initialized")
except Exception as e:
logger.error(f"Failed to initialize entropy metric: {repr(e)}")
# Perplexity metric
try:
metrics["perplexity"] = PerplexityMetric()
logger.debug("Perplexity metric initialized")
except Exception as e:
logger.error(f"Failed to initialize perplexity metric: {repr(e)}")
# Semantic analysis metric
try:
metrics["semantic_analysis"] = SemanticAnalysisMetric()
logger.debug("Semantic analysis metric initialized")
except Exception as e:
logger.error(f"Failed to initialize semantic analysis metric: {repr(e)}")
# Linguistic metric
try:
metrics["linguistic"] = LinguisticMetric()
logger.debug("Linguistic metric initialized")
except Exception as e:
logger.error(f"Failed to initialize linguistic metric: {repr(e)}")
# MultiPerturbationStability metric (expensive)
try:
metrics["multi_perturbation_stability"] = MultiPerturbationStabilityMetric()
logger.debug("MultiPerturbationStability metric initialized")
except Exception as e:
logger.error(f"Failed to initialize MultiPerturbationStability metric: {repr(e)}")
logger.info(f"Initialized {len(metrics)} metrics: {list(metrics.keys())}")
return metrics
def initialize(self) -> bool:
"""
Initialize all components (load models, etc.)
Returns:
--------
{ bool } : True if successful, False otherwise
"""
try:
logger.info("Initializing detection pipeline...")
# Initialize domain classifier
if not self.domain_classifier.initialize():
logger.warning("Domain classifier initialization failed")
# Initialize language detector
if self.language_detector:
if not self.language_detector.initialize():
logger.warning("Language detector initialization failed")
# Initialize metrics
successful_metrics = 0
for name, metric in self.metrics.items():
try:
if metric.initialize():
successful_metrics += 1
logger.debug(f"Metric {name} initialized successfully")
else:
logger.warning(f"Metric {name} initialization failed")
except Exception as e:
logger.error(f"Error initializing metric {name}: {repr(e)}")
# Need at least 3 metrics for reliable detection
logger.success(f"Detection pipeline initialized: {successful_metrics}/{len(self.metrics)} metrics ready")
return (successful_metrics >= 3)
except Exception as e:
logger.error(f"Failed to initialize detection pipeline: {repr(e)}")
return False
def analyze(self, text: str, domain: Optional[Domain] = None, **kwargs) -> DetectionResult:
"""
Analyze text and detect if AI-generated
Arguments:
----------
text { str } : Input text to analyze
domain { Domain } : Override automatic domain detection
**kwargs : Additional options
Returns:
--------
{ DetectionResult } : DetectionResult with complete analysis
"""
start_time = time.time()
warnings = list()
errors = list()
try:
# Preprocess text
logger.info("Step 1: Preprocessing text...")
processed_text = self.text_processor.process(text = text)
if not processed_text.is_valid:
logger.warning(f"Text validation failed: {processed_text.validation_errors}")
warnings.extend(processed_text.validation_errors)
# Continue anyway if text is present
# Detect language
language_result = None
if self.language_detector:
logger.info("Step 2: Detecting language...")
try:
language_result = self.language_detector.detect(processed_text.cleaned_text)
if (language_result.primary_language.value != "en"):
warnings.append(f"Non-English text detected ({language_result.primary_language.value}). Detection accuracy may be reduced.")
if (language_result.is_multilingual):
warnings.append("Multilingual content detected")
if (language_result.confidence < 0.7):
warnings.append(f"Low language detection confidence ({language_result.confidence:.2f})")
except Exception as e:
logger.warning(f"Language detection failed: {repr(e)}")
warnings.append("Language detection failed")
# Classify domain
logger.info("Step 3: Classifying domain...")
if domain is None:
try:
domain_prediction = self.domain_classifier.classify(processed_text.cleaned_text)
domain = domain_prediction.primary_domain
if (domain_prediction.confidence < 0.5):
warnings.append(f"Low domain classification confidence ({domain_prediction.confidence:.2f})")
except Exception as e:
logger.warning(f"Domain classification failed: {repr(e)}")
domain_prediction = DomainPrediction(primary_domain = Domain.GENERAL,
secondary_domain = None,
confidence = 0.5,
domain_scores = {},
)
domain = Domain.GENERAL
warnings.append("Domain classification failed, using GENERAL")
else:
# Use provided domain
domain_prediction = DomainPrediction(primary_domain = domain,
secondary_domain = None,
confidence = 1.0,
domain_scores = {domain.value: 1.0},
)
logger.info(f"Detected domain: {domain.value} (confidence: {domain_prediction.confidence:.2f})")
# Execute metrics calculations
logger.info("Step 4: Executing detection metrics calculations...")
metric_results = dict()
metrics_execution_time = dict()
for name, metric in self.metrics.items():
metric_start = time.time()
try:
# Check if we should skip expensive metrics
if (self.skip_expensive_metrics and (name == "multi_perturbation_stability")):
logger.info(f"Skipping expensive metric: {name}")
continue
logger.debug(f"Computing metric: {name}")
result = metric.compute(text = processed_text.cleaned_text,
domain = domain,
skip_expensive = self.skip_expensive_metrics,
)
metric_results[name] = result
if result.error:
warnings.append(f"{name} metric error: {result.error}")
except Exception as e:
logger.error(f"Error computing metric {name}: {repr(e)}")
errors.append(f"{name}: {repr(e)}")
# Create error result
metric_results[name] = MetricResult(metric_name = name,
ai_probability = 0.5,
human_probability = 0.5,
mixed_probability = 0.0,
confidence = 0.0,
error = repr(e),
)
finally:
metrics_execution_time[name] = time.time() - metric_start
logger.info(f"Executed {len(metric_results)} metrics successfully")
# Ensemble aggregation
logger.info("Step 5: Aggregating results with ensemble...")
try:
ensemble_result = self.ensemble.predict(metric_results = metric_results,
domain = domain,
)
except Exception as e:
logger.error(f"Ensemble prediction failed: {repr(e)}")
errors.append(f"Ensemble: {repr(e)}")
# Create fallback result
ensemble_result = EnsembleResult(final_verdict = "Error",
ai_probability = 0.5,
human_probability = 0.5,
mixed_probability = 0.0,
overall_confidence = 0.0,
domain = domain,
metric_results = metric_results,
metric_weights = {},
weighted_scores = {},
reasoning = ["Ensemble aggregation failed"],
uncertainty_score = 1.0,
consensus_level = 0.0,
)
# Calculate total processing time
processing_time = time.time() - start_time
logger.success(f"Analysis complete: {ensemble_result.final_verdict} "
f"(AI probability: {ensemble_result.ai_probability:.1%}, "
f"confidence: {ensemble_result.overall_confidence:.2f}) "
f"in {processing_time:.2f}s")
return DetectionResult(ensemble_result = ensemble_result,
processed_text = processed_text,
domain_prediction = domain_prediction,
language_result = language_result,
metric_results = metric_results,
processing_time = processing_time,
metrics_execution_time = metrics_execution_time,
warnings = warnings,
errors = errors,
)
except Exception as e:
logger.error(f"Fatal error in detection pipeline: {repr(e)}")
processing_time = time.time() - start_time
# Return error result
return DetectionResult(ensemble_result = EnsembleResult(final_verdict = "Error",
ai_probability = 0.5,
human_probability = 0.5,
mixed_probability = 0.0,
overall_confidence = 0.0,
domain = Domain.GENERAL,
metric_results = {},
metric_weights = {},
weighted_scores = {},
reasoning = [f"Fatal error: {str(e)}"],
uncertainty_score = 1.0,
consensus_level = 0.0,
),
processed_text = ProcessedText(original_text = text,
cleaned_text = "",
sentences = [],
words = [],
paragraphs = [],
char_count = 0,
word_count = 0,
sentence_count = 0,
paragraph_count = 0,
avg_sentence_length = 0.0,
avg_word_length = 0.0,
is_valid = False,
validation_errors = ["Processing failed"],
metadata = {},
),
domain_prediction = DomainPrediction(primary_domain = Domain.GENERAL,
secondary_domain = None,
confidence = 0.0,
domain_scores = {},
),
language_result = None,
metric_results = {},
processing_time = processing_time,
metrics_execution_time = {},
warnings = [],
errors = [f"Fatal error: {repr(e)}"],
)
def batch_analyze(self, texts: List[str], domain: Optional[Domain] = None) -> List[DetectionResult]:
"""
Analyze multiple texts
Arguments:
----------
texts { list } : List of texts to analyze
domain { Domain } : Override automatic domain detection
Returns:
--------
{ list } : List of DetectionResult objects
"""
logger.info(f"Batch analyzing {len(texts)} texts...")
results = list()
for i, text in enumerate(texts):
logger.info(f"Analyzing text {i+1}/{len(texts)}...")
try:
result = self.analyze(text = text,
domain = domain,
)
results.append(result)
except Exception as e:
logger.error(f"Error analyzing text {i+1}: {repr(e)}")
# Create error result for this text
error_result = DetectionResult(ensemble_result = EnsembleResult(final_verdict = "Error",
ai_probability = 0.5,
human_probability = 0.5,
mixed_probability = 0.0,
overall_confidence = 0.0,
domain = Domain.GENERAL,
metric_results = {},
metric_weights = {},
weighted_scores = {},
reasoning = [f"Analysis failed: {str(e)}"],
uncertainty_score = 1.0,
consensus_level = 0.0,
),
processed_text = ProcessedText(original_text = text,
cleaned_text = "",
sentences = [],
words = [],
paragraphs = [],
char_count = 0,
word_count = 0,
sentence_count = 0,
paragraph_count = 0,
avg_sentence_length = 0.0,
avg_word_length = 0.0,
is_valid = False,
validation_errors = ["Processing failed"],
metadata = {},
),
domain_prediction = DomainPrediction(primary_domain = Domain.GENERAL,
secondary_domain = None,
confidence = 0.0,
domain_scores = {},
),
language_result = None,
metric_results = {},
processing_time = 0.0,
metrics_execution_time = {},
warnings = [],
errors = [f"Analysis failed: {repr(e)}"],
)
results.append(error_result)
logger.info(f"Batch analysis complete: {len(results)}/{len(texts)} processed")
return results
def cleanup(self):
"""
Clean up resources
"""
logger.info("Cleaning up detection orchestrator...")
for name, metric in self.metrics.items():
try:
metric.cleanup()
logger.debug(f"Cleaned up metric: {name}")
except Exception as e:
logger.warning(f"Error cleaning up metric {name}: {repr(e)}")
if self.domain_classifier:
try:
self.domain_classifier.cleanup()
logger.debug("Cleaned up domain classifier")
except Exception as e:
logger.warning(f"Error cleaning up domain classifier: {repr(e)}")
if self.language_detector:
try:
self.language_detector.cleanup()
logger.debug("Cleaned up language detector")
except Exception as e:
logger.warning(f"Error cleaning up language detector: {repr(e)}")
logger.info("Cleanup complete")
# Export
__all__ = ["DetectionResult",
"DetectionOrchestrator",
] |