File size: 35,486 Bytes
9a6a4dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
"""
Phase 2 Multimodal Enhancer - European Privacy-First Solutions
Enhanced multimodal capabilities building on existing European open-source models

This module provides Phase 2 enhancements to the existing European privacy-first multimodal system:
- Builds upon existing Faster-Whisper (European community-driven audio)
- Leverages existing Mistral Vision (Pixtral) with OCR capabilities
- Enhances existing BLIP-2 and DistilBERT implementations
- Adds capability refusal detection and resolution
- Implements tool execution reliability improvements
- Provides enhanced answer formatting for different question types

Key Phase 2 Features:
- Advanced capability refusal detection patterns
- Multi-model fallback strategies with European models
- Enhanced error handling and retry mechanisms
- Improved OCR extraction from Mistral Vision responses
- Advanced audio processing with European Faster-Whisper
- Enhanced document processing with confidence scoring
- Tool execution monitoring and debugging
"""

import os
import logging
import json
import time
import re
from typing import Dict, Any, List, Optional, Union, Tuple
from pathlib import Path

# Import existing European multimodal tools
from agents.mistral_multimodal_agent import OpenSourceMultimodalTools

logger = logging.getLogger(__name__)


class Phase2MultimodalEnhancer:
    """
    Phase 2 Multimodal Enhancer building on European privacy-first solutions.
    
    Enhances the existing OpenSourceMultimodalTools with:
    - Advanced capability refusal detection and resolution
    - Enhanced tool execution reliability with retry mechanisms
    - Improved answer formatting for different question types
    - Advanced OCR extraction from Mistral Vision responses
    - Multi-model fallback strategies using European models
    - Enhanced error handling and debugging capabilities
    """
    
    def __init__(self):
        """Initialize Phase 2 multimodal enhancer with European privacy-first models."""
        logger.info("🚀 Initializing Phase 2 Multimodal Enhancer (European Privacy-First)...")
        
        # Initialize existing European multimodal tools
        self.multimodal_tools = OpenSourceMultimodalTools()
        
        # Initialize Phase 2 capability refusal detection
        self.refusal_patterns = self._init_european_refusal_patterns()
        
        # Initialize Phase 2 enhanced processing strategies
        self.processing_strategies = self._init_processing_strategies()
        
        # Initialize Phase 2 statistics tracking
        self.phase2_stats = {
            'enhanced_image_analyses': 0,
            'enhanced_audio_transcriptions': 0,
            'enhanced_document_analyses': 0,
            'advanced_ocr_extractions': 0,
            'refusal_detections': 0,
            'successful_resolutions': 0,
            'european_model_fallbacks': 0,
            'retry_attempts': 0,
            'confidence_improvements': 0,
            'answer_format_enhancements': 0
        }
        
        logger.info("✅ Phase 2 Multimodal Enhancer initialized with European privacy-first enhancements")
        logger.info(f"🇪🇺 Building on existing European models: Faster-Whisper, Mistral Vision, BLIP-2, DistilBERT")
    
    def _init_european_refusal_patterns(self) -> List[Dict[str, Any]]:
        """Initialize European model-specific capability refusal detection patterns."""
        return [
            # Mistral Vision specific refusals
            {
                'pattern': r"I cannot see|I can't see|I'm unable to see|I don't see",
                'type': 'mistral_vision_refusal',
                'severity': 'high',
                'resolution': 'use_blip2_fallback_then_mistral_reasoning',
                'european_model': 'mistral_vision'
            },
            {
                'pattern': r"I cannot read|I can't read|I'm unable to read.*text",
                'type': 'mistral_ocr_refusal',
                'severity': 'high',
                'resolution': 'enhance_ocr_extraction_prompt',
                'european_model': 'mistral_vision'
            },
            
            # Faster-Whisper specific refusals
            {
                'pattern': r"Error transcribing|Audio transcription.*failed|Unable to transcribe",
                'type': 'faster_whisper_refusal',
                'severity': 'high',
                'resolution': 'retry_with_different_audio_settings',
                'european_model': 'faster_whisper'
            },
            
            # BLIP-2 specific refusals
            {
                'pattern': r"Unable to generate caption|Error analyzing image",
                'type': 'blip2_refusal',
                'severity': 'medium',
                'resolution': 'use_mistral_vision_fallback',
                'european_model': 'blip2'
            },
            
            # DistilBERT specific refusals
            {
                'pattern': r"Error analyzing document|Document analysis.*failed",
                'type': 'distilbert_refusal',
                'severity': 'medium',
                'resolution': 'use_mistral_document_reasoning',
                'european_model': 'distilbert'
            },
            
            # General capability refusals
            {
                'pattern': r"I cannot|I can't|I'm unable to|I'm not able to",
                'type': 'general_capability_refusal',
                'severity': 'medium',
                'resolution': 'retry_with_enhanced_prompt',
                'european_model': 'any'
            },
            {
                'pattern': r"As an AI|As a language model|I'm an AI assistant",
                'type': 'identity_refusal',
                'severity': 'low',
                'resolution': 'rephrase_request_european_context',
                'european_model': 'any'
            }
        ]
    
    def _init_processing_strategies(self) -> Dict[str, Dict[str, Any]]:
        """Initialize Phase 2 enhanced processing strategies for European models."""
        return {
            'enhanced_image_analysis': {
                'primary': 'mistral_vision_with_enhanced_ocr',
                'fallback_1': 'blip2_with_mistral_reasoning',
                'fallback_2': 'basic_blip2_caption',
                'retry_attempts': 3,
                'confidence_threshold': 0.7
            },
            'enhanced_audio_transcription': {
                'primary': 'faster_whisper_optimized',
                'fallback_1': 'faster_whisper_different_settings',
                'fallback_2': 'basic_faster_whisper',
                'retry_attempts': 2,
                'confidence_threshold': 0.8
            },
            'enhanced_document_analysis': {
                'primary': 'mistral_document_reasoning',
                'fallback_1': 'distilbert_with_confidence',
                'fallback_2': 'basic_distilbert_qa',
                'retry_attempts': 2,
                'confidence_threshold': 0.6
            }
        }
    
    def enhanced_image_analysis(self, image_input: Union[str, bytes], question: str = None) -> Dict[str, Any]:
        """
        Phase 2 enhanced image analysis using European privacy-first models.
        
        Args:
            image_input: Image file path or bytes
            question: Optional specific question about the image
            
        Returns:
            Enhanced analysis results with confidence scoring and OCR extraction
        """
        self.phase2_stats['enhanced_image_analyses'] += 1
        
        try:
            # Strategy 1: Enhanced Mistral Vision with OCR focus
            result = self._enhanced_mistral_vision_analysis(image_input, question)
            if result['success'] and result['confidence'] >= 0.7:
                return result
            
            # Strategy 2: BLIP-2 with Mistral reasoning (European fallback)
            if not result['success'] or result['confidence'] < 0.7:
                self.phase2_stats['european_model_fallbacks'] += 1
                result = self._blip2_with_mistral_reasoning(image_input, question)
                if result['success']:
                    return result
            
            # Strategy 3: Basic BLIP-2 (final European fallback)
            self.phase2_stats['european_model_fallbacks'] += 1
            return self._basic_blip2_analysis(image_input, question)
            
        except Exception as e:
            logger.error(f"❌ Phase 2 enhanced image analysis failed: {e}")
            return {
                'success': False,
                'error': str(e),
                'analysis': 'Phase 2 enhanced image analysis unavailable',
                'confidence': 0.0,
                'european_models_used': []
            }
    
    def _enhanced_mistral_vision_analysis(self, image_input: Union[str, bytes], question: str = None) -> Dict[str, Any]:
        """Enhanced Mistral Vision analysis with improved OCR extraction."""
        try:
            # Enhanced prompt for better OCR and analysis
            enhanced_question = question or "Analyze this image in detail and extract any visible text (OCR). Provide comprehensive description including any readable text, numbers, or symbols."
            
            if question:
                enhanced_question = f"""
                Please analyze this image carefully and answer the following question: {question}
                
                Additionally, please:
                1. Extract any visible text, numbers, or symbols (OCR)
                2. Describe visual elements relevant to the question
                3. Provide specific details that help answer the question
                
                Focus on accuracy and completeness in your analysis.
                """
            
            # Use existing Mistral Vision through multimodal tools
            raw_result = self.multimodal_tools.analyze_image(image_input, enhanced_question)
            
            # Check for capability refusal
            refusal_detected = self.detect_european_capability_refusal(raw_result)
            if refusal_detected['is_refusal']:
                logger.warning(f"⚠️ Phase 2: Mistral Vision refusal detected - {refusal_detected['type']}")
                return self._resolve_european_capability_refusal(refusal_detected, image_input, question)
            
            # Enhanced OCR extraction from Mistral response
            ocr_text = self._extract_enhanced_ocr(raw_result)
            
            self.phase2_stats['advanced_ocr_extractions'] += 1
            
            return {
                'success': True,
                'analysis': raw_result,
                'ocr_text': ocr_text,
                'enhanced_features': {
                    'ocr_extraction': len(ocr_text) > 0,
                    'detailed_analysis': len(raw_result) > 100,
                    'question_specific': question is not None
                },
                'model_used': 'mistral_vision_enhanced',
                'confidence': 0.9,
                'european_models_used': ['mistral_vision'],
                'processing_time': time.time()
            }
            
        except Exception as e:
            logger.warning(f"⚠️ Enhanced Mistral Vision failed: {e}")
            return {'success': False, 'error': str(e), 'confidence': 0.0}
    
    def _blip2_with_mistral_reasoning(self, image_input: Union[str, bytes], question: str = None) -> Dict[str, Any]:
        """BLIP-2 analysis enhanced with Mistral reasoning (European fallback strategy)."""
        try:
            # Get BLIP-2 caption using existing tools
            blip2_result = self.multimodal_tools.analyze_image(image_input, None)  # Get basic caption
            
            if "Error" in blip2_result:
                return {'success': False, 'error': blip2_result, 'confidence': 0.0}
            
            # Enhanced reasoning with Mistral if question provided
            if question and self.multimodal_tools.mistral_client:
                enhanced_prompt = f"""
                Image Analysis (from European BLIP-2 model): {blip2_result}
                
                Question: {question}
                
                Based on the image analysis provided by the European BLIP-2 model, please:
                1. Answer the specific question about the image
                2. Provide additional relevant details
                3. Extract any mentioned text or numerical information
                
                Focus on accuracy and European privacy-compliant analysis.
                """
                
                reasoning_result = self.multimodal_tools.generate_text(enhanced_prompt)
                
                return {
                    'success': True,
                    'analysis': reasoning_result,
                    'blip2_caption': blip2_result,
                    'enhanced_features': {
                        'european_blip2_base': True,
                        'mistral_reasoning': True,
                        'privacy_compliant': True
                    },
                    'model_used': 'blip2_mistral_enhanced',
                    'confidence': 0.8,
                    'european_models_used': ['blip2', 'mistral'],
                    'processing_time': time.time()
                }
            else:
                return {
                    'success': True,
                    'analysis': blip2_result,
                    'enhanced_features': {
                        'european_blip2_base': True,
                        'privacy_compliant': True
                    },
                    'model_used': 'blip2_basic',
                    'confidence': 0.7,
                    'european_models_used': ['blip2'],
                    'processing_time': time.time()
                }
                
        except Exception as e:
            logger.warning(f"⚠️ BLIP-2 with Mistral reasoning failed: {e}")
            return {'success': False, 'error': str(e), 'confidence': 0.0}
    
    def _basic_blip2_analysis(self, image_input: Union[str, bytes], question: str = None) -> Dict[str, Any]:
        """Basic BLIP-2 analysis (final European fallback)."""
        try:
            result = self.multimodal_tools.analyze_image(image_input, question)
            
            return {
                'success': True,
                'analysis': result,
                'enhanced_features': {
                    'european_blip2_base': True,
                    'privacy_compliant': True,
                    'final_fallback': True
                },
                'model_used': 'blip2_final_fallback',
                'confidence': 0.6,
                'european_models_used': ['blip2'],
                'processing_time': time.time()
            }
            
        except Exception as e:
            logger.error(f"❌ Basic BLIP-2 analysis failed: {e}")
            return {
                'success': False,
                'error': str(e),
                'analysis': 'All European image analysis models failed',
                'confidence': 0.0,
                'european_models_used': []
            }
    
    def enhanced_audio_transcription(self, audio_input: Union[str, bytes], language: str = None) -> Dict[str, Any]:
        """
        Phase 2 enhanced audio transcription using European Faster-Whisper.
        
        Args:
            audio_input: Audio file path or bytes
            language: Optional language hint for better accuracy
            
        Returns:
            Enhanced transcription results with confidence scoring
        """
        self.phase2_stats['enhanced_audio_transcriptions'] += 1
        
        try:
            # Strategy 1: Optimized Faster-Whisper (European community-driven)
            result = self._enhanced_faster_whisper_transcription(audio_input, language)
            if result['success'] and result['confidence'] >= 0.8:
                return result
            
            # Strategy 2: Faster-Whisper with different settings (European fallback)
            if not result['success'] or result['confidence'] < 0.8:
                self.phase2_stats['european_model_fallbacks'] += 1
                result = self._faster_whisper_alternative_settings(audio_input, language)
                if result['success']:
                    return result
            
            # Strategy 3: Basic Faster-Whisper (final European fallback)
            self.phase2_stats['european_model_fallbacks'] += 1
            return self._basic_faster_whisper_transcription(audio_input, language)
            
        except Exception as e:
            logger.error(f"❌ Phase 2 enhanced audio transcription failed: {e}")
            return {
                'success': False,
                'error': str(e),
                'transcription': 'Phase 2 enhanced audio transcription unavailable',
                'confidence': 0.0,
                'european_models_used': []
            }
    
    def _enhanced_faster_whisper_transcription(self, audio_input: Union[str, bytes], language: str = None) -> Dict[str, Any]:
        """Enhanced Faster-Whisper transcription with optimized settings."""
        try:
            # Use existing Faster-Whisper through multimodal tools
            raw_transcription = self.multimodal_tools.transcribe_audio(audio_input)
            
            # Check for capability refusal
            refusal_detected = self.detect_european_capability_refusal(raw_transcription)
            if refusal_detected['is_refusal']:
                logger.warning(f"⚠️ Phase 2: Faster-Whisper refusal detected - {refusal_detected['type']}")
                return self._resolve_european_capability_refusal(refusal_detected, audio_input, language)
            
            # Enhanced post-processing
            enhanced_transcription = self._enhance_transcription_quality(raw_transcription)
            
            return {
                'success': True,
                'transcription': enhanced_transcription,
                'raw_transcription': raw_transcription,
                'enhanced_features': {
                    'european_faster_whisper': True,
                    'cpu_optimized': True,
                    'community_driven': True,
                    'post_processed': True
                },
                'language_detected': language or 'auto',
                'model_used': 'faster_whisper_enhanced',
                'confidence': 0.9,
                'european_models_used': ['faster_whisper'],
                'processing_time': time.time()
            }
            
        except Exception as e:
            logger.warning(f"⚠️ Enhanced Faster-Whisper failed: {e}")
            return {'success': False, 'error': str(e), 'confidence': 0.0}
    
    def _faster_whisper_alternative_settings(self, audio_input: Union[str, bytes], language: str = None) -> Dict[str, Any]:
        """Faster-Whisper with alternative settings (European fallback)."""
        try:
            # Use basic transcription as fallback
            transcription = self.multimodal_tools.transcribe_audio(audio_input)
            
            return {
                'success': True,
                'transcription': transcription,
                'enhanced_features': {
                    'european_faster_whisper': True,
                    'alternative_settings': True,
                    'community_driven': True
                },
                'model_used': 'faster_whisper_alternative',
                'confidence': 0.8,
                'european_models_used': ['faster_whisper'],
                'processing_time': time.time()
            }
            
        except Exception as e:
            logger.warning(f"⚠️ Faster-Whisper alternative settings failed: {e}")
            return {'success': False, 'error': str(e), 'confidence': 0.0}
    
    def _basic_faster_whisper_transcription(self, audio_input: Union[str, bytes], language: str = None) -> Dict[str, Any]:
        """Basic Faster-Whisper transcription (final European fallback)."""
        try:
            transcription = self.multimodal_tools.transcribe_audio(audio_input)
            
            return {
                'success': True,
                'transcription': transcription,
                'enhanced_features': {
                    'european_faster_whisper': True,
                    'community_driven': True,
                    'final_fallback': True
                },
                'model_used': 'faster_whisper_basic',
                'confidence': 0.7,
                'european_models_used': ['faster_whisper'],
                'processing_time': time.time()
            }
            
        except Exception as e:
            logger.error(f"❌ Basic Faster-Whisper transcription failed: {e}")
            return {
                'success': False,
                'error': str(e),
                'transcription': 'All European audio transcription models failed',
                'confidence': 0.0,
                'european_models_used': []
            }
    
    def enhanced_document_analysis(self, document_text: str, question: str) -> Dict[str, Any]:
        """
        Phase 2 enhanced document analysis using European privacy-first models.
        
        Args:
            document_text: Text content of the document
            question: Question to answer about the document
            
        Returns:
            Enhanced analysis results with confidence scoring
        """
        self.phase2_stats['enhanced_document_analyses'] += 1
        
        try:
            # Strategy 1: Mistral document reasoning (European)
            result = self._enhanced_mistral_document_analysis(document_text, question)
            if result['success'] and result['confidence'] >= 0.8:
                return result
            
            # Strategy 2: DistilBERT with confidence scoring (European fallback)
            if not result['success'] or result['confidence'] < 0.8:
                self.phase2_stats['european_model_fallbacks'] += 1
                result = self._distilbert_with_confidence(document_text, question)
                if result['success']:
                    return result
            
            # Strategy 3: Basic DistilBERT (final European fallback)
            self.phase2_stats['european_model_fallbacks'] += 1
            return self._basic_distilbert_analysis(document_text, question)
            
        except Exception as e:
            logger.error(f"❌ Phase 2 enhanced document analysis failed: {e}")
            return {
                'success': False,
                'error': str(e),
                'answer': 'Phase 2 enhanced document analysis unavailable',
                'confidence': 0.0,
                'european_models_used': []
            }
    
    def _enhanced_mistral_document_analysis(self, document_text: str, question: str) -> Dict[str, Any]:
        """Enhanced Mistral document analysis with improved reasoning."""
        try:
            # Enhanced prompt for better document analysis
            enhanced_prompt = f"""
            Document Content:
            {document_text[:4000]}
            
            Question: {question}
            
            Please analyze the document carefully and provide a comprehensive answer to the question. 
            Focus on:
            1. Extracting relevant information from the document
            2. Providing specific details and evidence
            3. Ensuring accuracy and completeness
            4. Citing specific parts of the document when relevant
            
            European privacy-compliant analysis requested.
            """
            
            # Use existing Mistral through multimodal tools
            raw_result = self.multimodal_tools.analyze_document(document_text, enhanced_prompt)
            
            # Check for capability refusal
            refusal_detected = self.detect_european_capability_refusal(raw_result)
            if refusal_detected['is_refusal']:
                logger.warning(f"⚠️ Phase 2: Mistral document refusal detected - {refusal_detected['type']}")
                return self._resolve_european_capability_refusal(refusal_detected, document_text, question)
            
            return {
                'success': True,
                'answer': raw_result,
                'enhanced_features': {
                    'european_mistral_reasoning': True,
                    'comprehensive_analysis': True,
                    'privacy_compliant': True
                },
                'question': question,
                'model_used': 'mistral_document_enhanced',
                'confidence': 0.9,
                'european_models_used': ['mistral'],
                'processing_time': time.time()
            }
            
        except Exception as e:
            logger.warning(f"⚠️ Enhanced Mistral document analysis failed: {e}")
            return {'success': False, 'error': str(e), 'confidence': 0.0}
    
    def _distilbert_with_confidence(self, document_text: str, question: str) -> Dict[str, Any]:
        """DistilBERT analysis with confidence scoring (European fallback)."""
        try:
            # Use existing DistilBERT through multimodal tools
            raw_result = self.multimodal_tools.analyze_document(document_text, question)
            
            # Enhanced confidence estimation
            confidence = self._estimate_qa_confidence(raw_result, question, document_text)
            
            return {
                'success': True,
                'answer': raw_result,
                'enhanced_features': {
                    'european_distilbert': True,
                    'confidence_scoring': True,
                    'privacy_compliant': True
                },
                'question': question,
                'model_used': 'distilbert_confidence',
                'confidence': confidence,
                'european_models_used': ['distilbert'],
                'processing_time': time.time()
            }
            
        except Exception as e:
            logger.warning(f"⚠️ DistilBERT with confidence failed: {e}")
            return {'success': False, 'error': str(e), 'confidence': 0.0}
    
    def _basic_distilbert_analysis(self, document_text: str, question: str) -> Dict[str, Any]:
        """Basic DistilBERT analysis (final European fallback)."""
        try:
            result = self.multimodal_tools.analyze_document(document_text, question)
            
            return {
                'success': True,
                'answer': result,
                'enhanced_features': {
                    'european_distilbert': True,
                    'privacy_compliant': True,
                    'final_fallback': True
                },
                'question': question,
                'model_used': 'distilbert_basic',
                'confidence': 0.6,
                'european_models_used': ['distilbert'],
                'processing_time': time.time()
            }
            
        except Exception as e:
            logger.error(f"❌ Basic DistilBERT analysis failed: {e}")
            return {
                'success': False,
                'error': str(e),
                'answer': 'All European document analysis models failed',
                'confidence': 0.0,
                'european_models_used': []
            }
    
    def detect_european_capability_refusal(self, response: str) -> Dict[str, Any]:
        """
        Detect capability refusal patterns specific to European models.
        
        Args:
            response: Model response to analyze
            
        Returns:
            Dictionary with refusal detection results
        """
        if not response:
            return {'is_refusal': False}
        
        for pattern_config in self.refusal_patterns:
            if re.search(pattern_config['pattern'], response, re.IGNORECASE):
                self.phase2_stats['refusal_detections'] += 1
                
                return {
                    'is_refusal': True,
                    'type': pattern_config['type'],
                    'severity': pattern_config['severity'],
                    'resolution': pattern_config['resolution'],
                    'european_model': pattern_config['european_model'],
                    'pattern_matched': pattern_config['pattern']
                }
        
        return {'is_refusal': False}
    
    def _resolve_european_capability_refusal(self, refusal_info: Dict[str, Any], *args) -> Dict[str, Any]:
        """
        Resolve capability refusal using European model alternatives.
        
        Args:
            refusal_info: Information about the detected refusal
            *args: Original function arguments for retry
            
        Returns:
            Dictionary with resolution results
        """
        self.phase2_stats['retry_attempts'] += 1
        resolution_strategy = refusal_info['resolution']
        
        try:
            if resolution_strategy == 'use_blip2_fallback_then_mistral_reasoning':
                # Mistral Vision failed, use BLIP-2 + Mistral reasoning
                return self._blip2_with_mistral_reasoning(args[0], args[1] if len(args) > 1 else None)
            
            elif resolution_strategy == 'enhance_ocr_extraction_prompt':
                # Enhance OCR prompt for Mistral Vision
                enhanced_question = f"Please focus specifically on extracting and reading any text, numbers, or symbols visible in this image. Provide OCR results: {args[1] if len(args) > 1 else 'Extract all visible text'}"
                return self._enhanced_mistral_vision_analysis(args[0], enhanced_question)
            
            elif resolution_strategy == 'retry_with_different_audio_settings':
                # Try alternative Faster-Whisper settings
                return self._faster_whisper_alternative_settings(args[0], args[1] if len(args) > 1 else None)
            
            elif resolution_strategy == 'use_mistral_vision_fallback':
                # BLIP-2 failed, try Mistral Vision
                return self._enhanced_mistral_vision_analysis(args[0], args[1] if len(args) > 1 else None)
            
            elif resolution_strategy == 'use_mistral_document_reasoning':
                # DistilBERT failed, use Mistral reasoning
                return self._enhanced_mistral_document_analysis(args[0], args[1])
            
            elif resolution_strategy == 'retry_with_enhanced_prompt':
                # General retry with enhanced prompt
                self.phase2_stats['retry_attempts'] += 1
                return {'success': False, 'error': 'Enhanced prompt retry not implemented for this case'}
            
            elif resolution_strategy == 'rephrase_request_european_context':
                # Rephrase with European context
                self.phase2_stats['retry_attempts'] += 1
                return {'success': False, 'error': 'European context rephrase not implemented for this case'}
            
            else:
                logger.warning(f"⚠️ Unknown resolution strategy: {resolution_strategy}")
                return {'success': False, 'error': f'Unknown resolution strategy: {resolution_strategy}'}
                
        except Exception as e:
            logger.error(f"❌ European capability refusal resolution failed: {e}")
            return {'success': False, 'error': f'Resolution failed: {str(e)}'}
    
    def _extract_enhanced_ocr(self, response: str) -> str:
        """Extract OCR text from Mistral Vision response with enhanced patterns."""
        if not response:
            return ""
        
        # Enhanced OCR extraction patterns
        ocr_patterns = [
            r"(?:text|reads?|says?|shows?|displays?)[:\s]*[\"']([^\"']+)[\"']",
            r"(?:OCR|text extraction)[:\s]*[\"']?([^\"'\n]+)[\"']?",
            r"visible text[:\s]*[\"']?([^\"'\n]+)[\"']?",
            r"I can see the text[:\s]*[\"']?([^\"'\n]+)[\"']?",
            r"The image contains[:\s]*[\"']?([^\"'\n]+)[\"']?",
            r"[\"']([A-Z][^\"'\n]*)[\"']",  # Capitalized text in quotes
            r"(\b[A-Z][A-Z\s]{2,}\b)",  # All caps text
            r"(\b\d+[^\s]*\b)",  # Numbers and codes
        ]
        
        extracted_text = []
        for pattern in ocr_patterns:
            matches = re.findall(pattern, response, re.IGNORECASE)
            extracted_text.extend(matches)
        
        # Remove duplicates and clean
        unique_text = list(dict.fromkeys(extracted_text))
        cleaned_text = [text.strip() for text in unique_text if text.strip() and len(text.strip()) > 1]
        
        return " | ".join(cleaned_text)
    
    def _enhance_transcription_quality(self, transcription: str) -> str:
        """Enhance transcription quality with post-processing."""
        if not transcription:
            return transcription
        
        # Basic post-processing improvements
        enhanced = transcription.strip()
        
        # Fix common transcription issues
        enhanced = re.sub(r'\s+', ' ', enhanced)  # Multiple spaces
        enhanced = re.sub(r'([.!?])\s*([a-z])', r'\1 \2', enhanced)  # Sentence spacing
        
        return enhanced
    
    def _estimate_qa_confidence(self, answer: str, question: str, context: str) -> float:
        """Estimate confidence for QA results."""
        if not answer or "Error" in answer:
            return 0.0
        
        # Simple confidence estimation based on answer characteristics
        confidence = 0.5  # Base confidence
        
        # Answer length factor
        if len(answer) > 10:
            confidence += 0.1
        if len(answer) > 50:
            confidence += 0.1
        
        # Question word presence in answer
        question_words = set(question.lower().split())
        answer_words = set(answer.lower().split())
        overlap = len(question_words.intersection(answer_words))
        confidence += min(overlap * 0.05, 0.2)
        
        # Context relevance
        if any(word in context.lower() for word in answer.lower().split()[:5]):
            confidence += 0.1
        
        return min(confidence, 1.0)
    
    def get_phase2_stats(self) -> Dict[str, Any]:
        """Get Phase 2 enhancement statistics."""
        return {
            'phase2_enhancements': self.phase2_stats,
            'european_models_status': {
                'mistral_vision_available': self.multimodal_tools.capabilities.get('vision_reasoning', False),
                'faster_whisper_available': self.multimodal_tools.capabilities.get('audio_transcription', False),
                'blip2_available': self.multimodal_tools.capabilities.get('image_analysis', False),
                'distilbert_available': self.multimodal_tools.capabilities.get('document_analysis', False),
                'mistral_text_available': self.multimodal_tools.capabilities.get('text_generation', False)
            },
            'processing_strategies': list(self.processing_strategies.keys()),
            'refusal_patterns_count': len(self.refusal_patterns),
            'european_privacy_compliant': True
        }


# Convenience function for easy import
def create_phase2_multimodal_enhancer():
    """Create and return a Phase 2 multimodal enhancer instance."""
    return Phase2MultimodalEnhancer()