File size: 35,938 Bytes
1c174b0
0dbeec3
8f9d030
18e2e5a
 
 
6101878
18e2e5a
 
0dbeec3
18e2e5a
6101878
 
 
 
 
 
0dbeec3
 
6101878
 
0dbeec3
 
18e2e5a
0dbeec3
18e2e5a
 
 
 
 
 
 
 
0dbeec3
 
6101878
0dbeec3
 
6101878
0dbeec3
18e2e5a
0dbeec3
6101878
 
0dbeec3
 
18e2e5a
 
 
 
0dbeec3
 
18e2e5a
0dbeec3
18e2e5a
 
 
 
 
0dbeec3
 
18e2e5a
0dbeec3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6101878
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0dbeec3
6101878
18e2e5a
6101878
 
 
 
18e2e5a
6101878
 
 
 
18e2e5a
 
 
 
6101878
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18e2e5a
6101878
 
 
18e2e5a
 
 
 
 
6101878
18e2e5a
6101878
 
 
 
 
 
 
 
 
 
 
 
 
18e2e5a
6101878
 
18e2e5a
6101878
 
18e2e5a
6101878
18e2e5a
 
6101878
18e2e5a
 
6101878
18e2e5a
 
 
 
 
 
 
 
 
6101878
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31a862d
6101878
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31a862d
6101878
 
 
 
 
 
 
 
 
 
 
18e2e5a
 
 
 
6101878
 
18e2e5a
 
6101878
 
18e2e5a
6101878
 
 
 
 
 
18e2e5a
 
6101878
18e2e5a
6101878
 
 
18e2e5a
6101878
 
 
 
18e2e5a
 
6101878
 
 
 
 
 
 
18e2e5a
6101878
 
 
18e2e5a
6101878
18e2e5a
 
 
 
 
6101878
 
18e2e5a
6101878
 
 
 
 
 
 
18e2e5a
 
 
6101878
18e2e5a
 
6101878
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18e2e5a
6101878
 
 
 
 
 
 
 
18e2e5a
6101878
18e2e5a
6101878
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18e2e5a
6101878
 
 
 
 
 
18e2e5a
6101878
 
 
 
 
 
 
 
 
18e2e5a
6101878
 
 
 
 
18e2e5a
6101878
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18e2e5a
6101878
0dbeec3
 
18e2e5a
0dbeec3
18e2e5a
 
 
 
 
0dbeec3
18e2e5a
 
 
 
0dbeec3
18e2e5a
 
 
 
6101878
 
 
18e2e5a
 
6101878
18e2e5a
 
 
 
 
 
 
 
6101878
 
0dbeec3
18e2e5a
 
 
 
 
 
6101878
0dbeec3
18e2e5a
 
dde6bd9
6101878
18e2e5a
 
 
 
993d932
18e2e5a
 
 
 
 
 
 
6101878
18e2e5a
 
 
 
6101878
 
 
 
 
 
 
 
18e2e5a
 
6101878
18e2e5a
 
0dbeec3
18e2e5a
 
 
6101878
 
 
 
 
18e2e5a
6101878
18e2e5a
6101878
18e2e5a
6101878
 
 
 
 
 
 
 
18e2e5a
 
 
 
 
 
 
0dbeec3
18e2e5a
 
 
 
 
 
 
 
 
 
0dbeec3
18e2e5a
6101878
 
 
18e2e5a
 
 
 
 
6101878
 
18e2e5a
6101878
18e2e5a
 
6101878
18e2e5a
 
6101878
18e2e5a
 
 
 
 
 
6101878
18e2e5a
6101878
 
18e2e5a
 
6101878
18e2e5a
 
 
6101878
 
18e2e5a
6101878
 
 
 
18e2e5a
 
6101878
18e2e5a
6101878
18e2e5a
6101878
18e2e5a
 
6101878
18e2e5a
 
 
 
6101878
 
 
 
 
 
 
18e2e5a
 
 
6101878
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18e2e5a
6101878
 
 
 
 
 
 
18e2e5a
6101878
 
 
18e2e5a
6101878
18e2e5a
 
6101878
 
18e2e5a
 
 
 
6101878
18e2e5a
 
 
 
 
 
 
 
 
6101878
18e2e5a
 
 
 
 
 
 
6101878
18e2e5a
 
6101878
 
18e2e5a
 
6101878
 
 
 
 
 
 
0dbeec3
18e2e5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6101878
18e2e5a
 
 
 
31a862d
6101878
31a862d
 
6101878
31a862d
6101878
31a862d
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
import os
import re
import torch
import logging
import gc
import sys
import numpy as np
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Dict, List, Optional
from transformers import (
    AutoTokenizer, 
    AutoModelForSequenceClassification,
    AutoModelForCausalLM,
    pipeline
)
from tokenizers.normalizers import Sequence, Replace, Strip
from tokenizers import Regex
import math
from collections import Counter

# =====================================================
# 🔧 تكوين البيئة والإعدادات
# =====================================================
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# إعدادات الذاكرة والكاش
CACHE_DIR = "/tmp/huggingface_cache"
os.makedirs(CACHE_DIR, exist_ok=True)

# تكوين متغيرات البيئة لـ Hugging Face
os.environ.update({
    "HF_HOME": CACHE_DIR,
    "TRANSFORMERS_CACHE": CACHE_DIR,
    "HF_DATASETS_CACHE": CACHE_DIR,
    "HUGGINGFACE_HUB_CACHE": CACHE_DIR,
    "TORCH_HOME": CACHE_DIR,
    "TOKENIZERS_PARALLELISM": "false",
    "TRANSFORMERS_OFFLINE": "0",
})

# إعدادات PyTorch للذاكرة
if torch.cuda.is_available():
    os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:128'
    torch.backends.cudnn.benchmark = True

# =====================================================
# 🚀 تحديد الجهاز (GPU أو CPU)
# =====================================================
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
logger.info(f"🖥️ Using device: {device}")
if torch.cuda.is_available():
    logger.info(f"🎮 CUDA Device: {torch.cuda.get_device_name(0)}")
    logger.info(f"💾 CUDA Memory: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.2f} GB")

# =====================================================
# 📊 خريطة الموديلات
# =====================================================
label_mapping = {
    0: '13B', 1: '30B', 2: '65B', 3: '7B', 4: 'GLM130B', 5: 'bloom_7b',
    6: 'bloomz', 7: 'cohere', 8: 'davinci', 9: 'dolly', 10: 'dolly-v2-12b',
    11: 'flan_t5_base', 12: 'flan_t5_large', 13: 'flan_t5_small',
    14: 'flan_t5_xl', 15: 'flan_t5_xxl', 16: 'gemma-7b-it', 17: 'gemma2-9b-it',
    18: 'gpt-3.5-turbo', 19: 'gpt-35', 20: 'gpt4', 21: 'gpt4o',
    22: 'gpt_j', 23: 'gpt_neox', 24: 'human', 25: 'llama3-70b', 26: 'llama3-8b',
    27: 'mixtral-8x7b', 28: 'opt_1.3b', 29: 'opt_125m', 30: 'opt_13b',
    31: 'opt_2.7b', 32: 'opt_30b', 33: 'opt_350m', 34: 'opt_6.7b',
    35: 'opt_iml_30b', 36: 'opt_iml_max_1.3b', 37: 't0_11b', 38: 't0_3b',
    39: 'text-davinci-002', 40: 'text-davinci-003'
}

# =====================================================
# 📈 حسابات Perplexity و Burstiness
# =====================================================
class TextMetrics:
    """حساب المقاييس الإحصائية للنص"""
    
    @staticmethod
    def calculate_perplexity(text: str, model=None, tokenizer=None):
        """
        حساب Perplexity - قياس مدى "تفاجؤ" الموديل بالنص
        نصوص AI عادة لها perplexity أقل (أكثر قابلية للتنبؤ)
        """
        try:
            if model is None or tokenizer is None:
                # حساب تقريبي بناءً على تكرار الكلمات
                words = text.lower().split()
                word_freq = Counter(words)
                total_words = len(words)
                
                # حساب entropy
                entropy = 0
                for count in word_freq.values():
                    probability = count / total_words
                    if probability > 0:
                        entropy -= probability * math.log2(probability)
                
                # تقريب perplexity
                perplexity = 2 ** entropy
                return min(perplexity, 1000)  # Cap at 1000
            else:
                # حساب حقيقي باستخدام موديل
                inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
                with torch.no_grad():
                    outputs = model(**inputs, labels=inputs["input_ids"])
                    loss = outputs.loss
                    perplexity = torch.exp(loss).item()
                return min(perplexity, 1000)
        except Exception as e:
            logger.warning(f"Error calculating perplexity: {e}")
            return 50.0  # Default value
    
    @staticmethod
    def calculate_burstiness(text: str):
        """
        حساب Burstiness - قياس التنوع في طول الجمل
        البشر عندهم burstiness أعلى (جمل متنوعة الطول)
        AI عادة أكثر اتساقاً
        """
        try:
            # تقسيم النص لجمل
            sentences = re.split(r'[.!?]+', text)
            sentences = [s.strip() for s in sentences if s.strip()]
            
            if len(sentences) < 2:
                return 0.0
            
            # حساب طول كل جملة
            sentence_lengths = [len(s.split()) for s in sentences]
            
            # حساب الانحراف المعياري والمتوسط
            mean_length = np.mean(sentence_lengths)
            std_length = np.std(sentence_lengths)
            
            # Burstiness = الانحراف المعياري / المتوسط
            if mean_length > 0:
                burstiness = std_length / mean_length
            else:
                burstiness = 0.0
            
            return round(burstiness, 4)
        except Exception as e:
            logger.warning(f"Error calculating burstiness: {e}")
            return 0.5
    
    @staticmethod
    def calculate_vocabulary_diversity(text: str):
        """
        حساب تنوع المفردات
        البشر يستخدمون كلمات أكثر تنوعاً
        """
        words = text.lower().split()
        unique_words = set(words)
        if len(words) > 0:
            diversity = len(unique_words) / len(words)
        else:
            diversity = 0
        return round(diversity, 4)
    
    @staticmethod
    def detect_ai_patterns(text: str):
        """
        كشف الأنماط الشائعة في نصوص AI
        """
        ai_patterns = [
            r"it['\s]+s important to note",
            r"in conclusion",
            r"furthermore",
            r"comprehensive understanding",
            r"it is worth noting",
            r"however, it should be noted",
            r"on the other hand",
            r"in summary",
            r"to begin with",
            r"first and foremost"
        ]
        
        pattern_count = 0
        for pattern in ai_patterns:
            if re.search(pattern, text.lower()):
                pattern_count += 1
        
        return pattern_count
    
    @staticmethod
    def detect_human_patterns(text: str):
        """
        كشف الأنماط الشائعة في الكتابة البشرية
        """
        human_patterns = [
            r"kinda|sorta|gonna|wanna|gotta",
            r"tbh|idk|lol|omg|btw",
            r"!{2,}|\?{2,}|\.{3,}",
            r"i think|i feel|i believe",
            r"like,|you know,|i mean,",
            r"anyway|anyhow|whatever"
        ]
        
        pattern_count = 0
        for pattern in human_patterns:
            if re.search(pattern, text.lower()):
                pattern_count += 1
        
        return pattern_count

# =====================================================
# 🤖 Model Manager - إدارة الموديلات المحسنة
# =====================================================
class EnhancedModelManager:
    def __init__(self):
        self.modernbert_tokenizer = None
        self.modernbert_models = []
        self.additional_models = {}
        self.additional_tokenizers = {}
        self.models_loaded = False
        self.metrics = TextMetrics()
        
        # ModernBERT URLs
        self.modernbert_urls = [
            "https://huggingface.co/mihalykiss/modernbert_2/resolve/main/Model_groups_3class_seed12",
            "https://huggingface.co/mihalykiss/modernbert_2/resolve/main/Model_groups_3class_seed22"
        ]
        
        # Additional models to try
        self.additional_model_configs = [
            {
                "name": "chatgpt-detector-roberta",
                "model_id": "Hello-SimpleAI/chatgpt-detector-roberta",
                "type": "classification"
            },
            {
                "name": "openai-detector",
                "model_id": "roberta-base-openai-detector",
                "type": "classification"
            },
            {
                "name": "ai-content-detector",
                "model_id": "PirateXX/AI-Content-Detector",
                "type": "classification"
            }
        ]
    
    def load_modernbert_tokenizer(self):
        """تحميل ModernBERT tokenizer"""
        try:
            logger.info("📝 Loading ModernBERT tokenizer...")
            self.modernbert_tokenizer = AutoTokenizer.from_pretrained(
                "answerdotai/ModernBERT-base",
                cache_dir=CACHE_DIR,
                use_fast=True,
                trust_remote_code=False
            )
            
            # إعداد معالج النصوص
            try:
                newline_to_space = Replace(Regex(r'\s*\n\s*'), " ")
                join_hyphen_break = Replace(Regex(r'(\w+)[--]\s*\n\s*(\w+)'), r"\1\2")
                self.modernbert_tokenizer.backend_tokenizer.normalizer = Sequence([
                    self.modernbert_tokenizer.backend_tokenizer.normalizer,
                    join_hyphen_break,
                    newline_to_space,
                    Strip()
                ])
            except Exception as e:
                logger.warning(f"⚠️ Could not set custom normalizer: {e}")
            
            logger.info("✅ ModernBERT tokenizer loaded")
            return True
        except Exception as e:
            logger.error(f"❌ Failed to load tokenizer: {e}")
            return False
    
    def load_modernbert_model(self, model_url=None, model_path=None, model_name="ModernBERT"):
        """تحميل موديل ModernBERT واحد"""
        try:
            logger.info(f"🤖 Loading {model_name}...")
            
            base_model = AutoModelForSequenceClassification.from_pretrained(
                "answerdotai/ModernBERT-base",
                num_labels=41,
                cache_dir=CACHE_DIR,
                torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
                low_cpu_mem_usage=True,
                trust_remote_code=False
            )
            
            if model_path and os.path.exists(model_path):
                logger.info(f"📁 Loading from local file: {model_path}")
                state_dict = torch.load(model_path, map_location=device, weights_only=True)
                base_model.load_state_dict(state_dict, strict=False)
            elif model_url:
                logger.info(f"🌐 Downloading weights from URL...")
                try:
                    state_dict = torch.hub.load_state_dict_from_url(
                        model_url,
                        map_location=device,
                        progress=True,
                        check_hash=False,
                        file_name=f"{model_name}.pt"
                    )
                    base_model.load_state_dict(state_dict, strict=False)
                except Exception as e:
                    logger.warning(f"⚠️ Could not load weights: {e}")
                    logger.info("📊 Using model with random initialization")
            
            model = base_model.to(device)
            model.eval()
            
            if 'state_dict' in locals():
                del state_dict
            gc.collect()
            if torch.cuda.is_available():
                torch.cuda.empty_cache()
            
            logger.info(f"✅ {model_name} loaded")
            return model
            
        except Exception as e:
            logger.error(f"❌ Failed to load {model_name}: {e}")
            return None
    
    def load_additional_model(self, model_config):
        """تحميل موديلات إضافية للكشف عن AI"""
        try:
            model_name = model_config["name"]
            model_id = model_config["model_id"]
            
            logger.info(f"🔧 Loading {model_name}...")
            
            # Try loading as a pipeline first (easier)
            try:
                classifier = pipeline(
                    "text-classification",
                    model=model_id,
                    device=0 if torch.cuda.is_available() else -1,
                    model_kwargs={"cache_dir": CACHE_DIR}
                )
                self.additional_models[model_name] = classifier
                logger.info(f"✅ {model_name} loaded as pipeline")
                return True
            except:
                # Try loading manually
                tokenizer = AutoTokenizer.from_pretrained(
                    model_id,
                    cache_dir=CACHE_DIR
                )
                model = AutoModelForSequenceClassification.from_pretrained(
                    model_id,
                    cache_dir=CACHE_DIR,
                    torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
                ).to(device)
                model.eval()
                
                self.additional_tokenizers[model_name] = tokenizer
                self.additional_models[model_name] = model
                logger.info(f"✅ {model_name} loaded manually")
                return True
                
        except Exception as e:
            logger.warning(f"⚠️ Could not load {model_config['name']}: {e}")
            return False
    
    def load_all_models(self, max_modernbert=2, load_additional=True):
        """تحميل جميع الموديلات"""
        if self.models_loaded:
            logger.info("✨ Models already loaded")
            return True
        
        # Load ModernBERT tokenizer
        if not self.load_modernbert_tokenizer():
            return False
        
        # Load ModernBERT models
        logger.info(f"🚀 Loading up to {max_modernbert} ModernBERT models...")
        
        # Try local file first
        local_path = "modernbert.bin"
        if os.path.exists(local_path):
            model = self.load_modernbert_model(
                model_path=local_path,
                model_name="ModernBERT-Local"
            )
            if model is not None:
                self.modernbert_models.append(model)
        
        # Load from URLs
        for i, url in enumerate(self.modernbert_urls[:max_modernbert - len(self.modernbert_models)]):
            if len(self.modernbert_models) >= max_modernbert:
                break
            
            model = self.load_modernbert_model(
                model_url=url,
                model_name=f"ModernBERT-{i+1}"
            )
            if model is not None:
                self.modernbert_models.append(model)
        
        # Load additional models
        if load_additional:
            logger.info("🎯 Loading additional AI detection models...")
            for config in self.additional_model_configs:
                self.load_additional_model(config)
        
        # Check success
        total_models = len(self.modernbert_models) + len(self.additional_models)
        if total_models > 0:
            self.models_loaded = True
            logger.info(f"✅ Loaded {len(self.modernbert_models)} ModernBERT + {len(self.additional_models)} additional models")
            return True
        else:
            logger.error("❌ No models could be loaded")
            return False
    
    def classify_with_modernbert(self, text: str, model_index: int):
        """تصنيف النص باستخدام موديل ModernBERT واحد"""
        try:
            if model_index >= len(self.modernbert_models):
                return None
            
            model = self.modernbert_models[model_index]
            cleaned_text = clean_text(text)
            
            inputs = self.modernbert_tokenizer(
                cleaned_text,
                return_tensors="pt",
                truncation=True,
                max_length=512,
                padding=True
            ).to(device)
            
            with torch.no_grad():
                logits = model(**inputs).logits
                probs = torch.softmax(logits[0], dim=0)
                
                human_prob = probs[24].item()
                ai_probs = probs.clone()
                ai_probs[24] = 0
                ai_total = ai_probs.sum().item()
                
                total = human_prob + ai_total
                if total > 0:
                    human_pct = (human_prob / total) * 100
                    ai_pct = (ai_total / total) * 100
                else:
                    human_pct = ai_pct = 50
                
                ai_model_idx = torch.argmax(ai_probs).item()
                
                return {
                    "model_name": f"ModernBERT-{model_index+1}",
                    "human_score": round(human_pct, 2),
                    "ai_score": round(ai_pct, 2),
                    "predicted_model": label_mapping.get(ai_model_idx, "Unknown"),
                    "confidence": round(max(human_pct, ai_pct), 2)
                }
        except Exception as e:
            logger.error(f"Error in ModernBERT {model_index}: {e}")
            return None
    
    def classify_with_additional(self, text: str, model_name: str):
        """تصنيف النص باستخدام موديل إضافي"""
        try:
            if model_name not in self.additional_models:
                return None
            
            model = self.additional_models[model_name]
            
            # Check if it's a pipeline or model
            if hasattr(model, '__call__'):
                # It's a pipeline
                result = model(text, truncation=True, max_length=512)
                
                # Parse results based on model output format
                ai_score = 0
                human_score = 0
                
                for item in result:
                    label = item['label'].lower()
                    score = item['score'] * 100
                    
                    if 'fake' in label or 'ai' in label or 'gpt' in label:
                        ai_score = max(ai_score, score)
                    elif 'real' in label or 'human' in label:
                        human_score = max(human_score, score)
                
                # Normalize if needed
                if ai_score == 0 and human_score == 0:
                    ai_score = human_score = 50
                
                return {
                    "model_name": model_name,
                    "human_score": round(human_score, 2),
                    "ai_score": round(ai_score, 2),
                    "predicted_model": "AI" if ai_score > human_score else "Human",
                    "confidence": round(max(ai_score, human_score), 2)
                }
            else:
                # It's a model, use tokenizer
                tokenizer = self.additional_tokenizers.get(model_name)
                if tokenizer is None:
                    return None
                
                inputs = tokenizer(
                    text,
                    return_tensors="pt",
                    truncation=True,
                    max_length=512,
                    padding=True
                ).to(device)
                
                with torch.no_grad():
                    outputs = model(**inputs)
                    probs = torch.softmax(outputs.logits[0], dim=0)
                    
                    # Assuming binary classification (AI vs Human)
                    if len(probs) == 2:
                        human_score = probs[0].item() * 100
                        ai_score = probs[1].item() * 100
                    else:
                        # Handle multi-class
                        ai_score = human_score = 50
                    
                    return {
                        "model_name": model_name,
                        "human_score": round(human_score, 2),
                        "ai_score": round(ai_score, 2),
                        "predicted_model": "AI" if ai_score > human_score else "Human",
                        "confidence": round(max(ai_score, human_score), 2)
                    }
                    
        except Exception as e:
            logger.warning(f"Error in {model_name}: {e}")
            return None
    
    def comprehensive_analysis(self, text: str):
        """تحليل شامل باستخدام جميع الموديلات والمقاييس"""
        if not self.models_loaded:
            raise ValueError("No models loaded")
        
        results = {
            "individual_models": [],
            "ensemble_result": {},
            "metrics": {},
            "pattern_analysis": {}
        }
        
        # 1. Calculate text metrics
        logger.info("📊 Calculating text metrics...")
        results["metrics"] = {
            "perplexity": self.metrics.calculate_perplexity(text),
            "burstiness": self.metrics.calculate_burstiness(text),
            "vocabulary_diversity": self.metrics.calculate_vocabulary_diversity(text),
            "text_length": len(text.split()),
            "sentence_count": len(re.split(r'[.!?]+', text))
        }
        
        # 2. Pattern detection
        results["pattern_analysis"] = {
            "ai_patterns_found": self.metrics.detect_ai_patterns(text),
            "human_patterns_found": self.metrics.detect_human_patterns(text)
        }
        
        # 3. Run ModernBERT models
        modernbert_results = []
        for i in range(len(self.modernbert_models)):
            result = self.classify_with_modernbert(text, i)
            if result:
                results["individual_models"].append(result)
                modernbert_results.append(result)
        
        # 4. Run additional models
        for model_name in self.additional_models.keys():
            result = self.classify_with_additional(text, model_name)
            if result:
                results["individual_models"].append(result)
        
        # 5. Calculate ensemble result (weighted average)
        if results["individual_models"]:
            total_ai = 0
            total_human = 0
            weights_sum = 0
            
            for i, result in enumerate(results["individual_models"]):
                # Give ModernBERT models higher weight
                weight = 1.5 if i < len(modernbert_results) else 1.0
                total_ai += result["ai_score"] * weight
                total_human += result["human_score"] * weight
                weights_sum += weight
            
            if weights_sum > 0:
                ensemble_ai = total_ai / weights_sum
                ensemble_human = total_human / weights_sum
            else:
                ensemble_ai = ensemble_human = 50
            
            # Adjust based on metrics
            # High perplexity suggests human text
            if results["metrics"]["perplexity"] > 100:
                ensemble_human += 5
                ensemble_ai -= 5
            elif results["metrics"]["perplexity"] < 30:
                ensemble_ai += 5
                ensemble_human -= 5
            
            # High burstiness suggests human text
            if results["metrics"]["burstiness"] > 0.8:
                ensemble_human += 5
                ensemble_ai -= 5
            elif results["metrics"]["burstiness"] < 0.3:
                ensemble_ai += 5
                ensemble_human -= 5
            
            # Pattern analysis adjustment
            pattern_adjustment = (results["pattern_analysis"]["ai_patterns_found"] - 
                                 results["pattern_analysis"]["human_patterns_found"]) * 3
            ensemble_ai += pattern_adjustment
            ensemble_human -= pattern_adjustment
            
            # Normalize to 100%
            total = ensemble_ai + ensemble_human
            if total > 0:
                ensemble_ai = (ensemble_ai / total) * 100
                ensemble_human = (ensemble_human / total) * 100
            
            # Determine most likely AI model
            if ensemble_ai > ensemble_human and modernbert_results:
                predicted_model = modernbert_results[0]["predicted_model"]
            else:
                predicted_model = "Human"
            
            results["ensemble_result"] = {
                "ai_percentage": round(min(max(ensemble_ai, 0), 100), 2),
                "human_percentage": round(min(max(ensemble_human, 0), 100), 2),
                "predicted_model": predicted_model,
                "confidence": round(max(ensemble_ai, ensemble_human), 2),
                "is_human": ensemble_human > ensemble_ai,
                "models_used": len(results["individual_models"])
            }
        
        return results

# =====================================================
# 🧹 دوال التنظيف والمعالجة
# =====================================================
def clean_text(text: str) -> str:
    """تنظيف النص من المسافات الزائدة"""
    text = re.sub(r'\s{2,}', ' ', text)
    text = re.sub(r'\s+([,.;:?!])', r'\1', text)
    return text.strip()

def split_into_paragraphs(text: str) -> List[str]:
    """تقسيم النص إلى فقرات"""
    paragraphs = re.split(r'\n\s*\n', text.strip())
    return [p.strip() for p in paragraphs if p.strip()]

# =====================================================
# 🌐 FastAPI Application
# =====================================================
app = FastAPI(
    title="Enhanced ModernBERT AI Detector",
    description="Advanced AI detection with multiple models, perplexity, and burstiness analysis",
    version="3.0.0"
)

# إضافة CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# إنشاء مدير الموديلات المحسن
model_manager = EnhancedModelManager()

# =====================================================
# 📝 نماذج البيانات (Pydantic Models)
# =====================================================
class TextInput(BaseModel):
    text: str
    analyze_paragraphs: Optional[bool] = False
    return_individual_scores: Optional[bool] = True

class SimpleTextInput(BaseModel):
    text: str

class EnhancedDetectionResult(BaseModel):
    success: bool
    code: int
    message: str
    data: Dict

# =====================================================
# 🎯 API Endpoints
# =====================================================
@app.on_event("startup")
async def startup_event():
    """تحميل الموديلات عند بداية التشغيل"""
    logger.info("=" * 50)
    logger.info("🚀 Starting Enhanced ModernBERT AI Detector...")
    logger.info(f"🐍 Python version: {sys.version}")
    logger.info(f"🔥 PyTorch version: {torch.__version__}")
    logger.info("=" * 50)
    
    # Load models
    max_modernbert = int(os.environ.get("MAX_MODERNBERT_MODELS", "2"))
    load_additional = os.environ.get("LOAD_ADDITIONAL_MODELS", "true").lower() == "true"
    
    success = model_manager.load_all_models(
        max_modernbert=max_modernbert,
        load_additional=load_additional
    )
    
    if success:
        logger.info("✅ Application ready with enhanced features!")
    else:
        logger.error("⚠️ Failed to load models - API will return errors")

@app.get("/")
async def root():
    """الصفحة الرئيسية"""
    models_info = {
        "modernbert_models": len(model_manager.modernbert_models),
        "additional_models": list(model_manager.additional_models.keys())
    }
    
    return {
        "message": "Enhanced ModernBERT AI Text Detector API",
        "status": "online" if model_manager.models_loaded else "initializing",
        "models": models_info,
        "device": str(device),
        "features": [
            "Multiple AI detection models",
            "Perplexity analysis",
            "Burstiness analysis",
            "Pattern detection",
            "Individual model scores",
            "Ensemble predictions"
        ],
        "endpoints": {
            "analyze": "/analyze",
            "simple": "/analyze-simple",
            "health": "/health",
            "docs": "/docs"
        }
    }

@app.get("/health")
async def health_check():
    """فحص صحة الخدمة"""
    memory_info = {}
    if torch.cuda.is_available():
        memory_info = {
            "gpu_allocated_gb": round(torch.cuda.memory_allocated() / 1024**3, 2),
            "gpu_reserved_gb": round(torch.cuda.memory_reserved() / 1024**3, 2)
        }
    
    return {
        "status": "healthy" if model_manager.models_loaded else "unhealthy",
        "modernbert_models": len(model_manager.modernbert_models),
        "additional_models": len(model_manager.additional_models),
        "total_models": len(model_manager.modernbert_models) + len(model_manager.additional_models),
        "device": str(device),
        "cuda_available": torch.cuda.is_available(),
        "memory_info": memory_info
    }

@app.post("/analyze", response_model=EnhancedDetectionResult)
async def analyze_text_enhanced(data: TextInput):
    """
    Enhanced analysis with multiple models and metrics
    """
    try:
        # Validate input
        text = data.text.strip()
        if not text:
            return EnhancedDetectionResult(
                success=False,
                code=400,
                message="Empty input text",
                data={}
            )
        
        # Ensure models are loaded
        if not model_manager.models_loaded:
            if not model_manager.load_all_models():
                return EnhancedDetectionResult(
                    success=False,
                    code=503,
                    message="Models not available",
                    data={}
                )
        
        # Comprehensive analysis
        analysis_result = model_manager.comprehensive_analysis(text)
        
        # Basic stats
        total_words = len(text.split())
        ai_percentage = analysis_result["ensemble_result"]["ai_percentage"]
        human_percentage = analysis_result["ensemble_result"]["human_percentage"]
        ai_words = int(total_words * (ai_percentage / 100))
        
        # Paragraph analysis if requested
        paragraphs_analysis = []
        if data.analyze_paragraphs:
            paragraphs = split_into_paragraphs(text)
            for para in paragraphs[:10]:
                if para.strip():
                    try:
                        para_result = model_manager.comprehensive_analysis(para)
                        para_words = len(para.split())
                        
                        paragraphs_analysis.append({
                            "paragraph": para[:200] + "..." if len(para) > 200 else para,
                            "ai_generated_score": para_result["ensemble_result"]["ai_percentage"] / 100,
                            "human_written_score": para_result["ensemble_result"]["human_percentage"] / 100,
                            "predicted_model": para_result["ensemble_result"]["predicted_model"],
                            "metrics": {
                                "perplexity": para_result["metrics"]["perplexity"],
                                "burstiness": para_result["metrics"]["burstiness"]
                            }
                        })
                    except Exception as e:
                        logger.warning(f"Failed to analyze paragraph: {e}")
        
        # Prepare response
        response_data = {
            "fakePercentage": ai_percentage,
            "isHuman": human_percentage,
            "textWords": total_words,
            "aiWords": ai_words,
            "predicted_model": analysis_result["ensemble_result"]["predicted_model"],
            "feedback": "Most of Your Text is AI/GPT Generated" if ai_percentage > 50 else "Most of Your Text Appears Human-Written",
            "confidence": analysis_result["ensemble_result"]["confidence"],
            "models_used": analysis_result["ensemble_result"]["models_used"],
            
            # New: Metrics
            "metrics": analysis_result["metrics"],
            
            # New: Pattern analysis
            "pattern_analysis": analysis_result["pattern_analysis"],
            
            # Paragraphs if requested
            "paragraphs": paragraphs_analysis,
            
            # Text preview
            "input_text": text[:500] + "..." if len(text) > 500 else text,
            "detected_language": "en"
        }
        
        # Add individual model scores if requested
        if data.return_individual_scores:
            response_data["individual_models"] = analysis_result["individual_models"]
        
        return EnhancedDetectionResult(
            success=True,
            code=200,
            message="Enhanced analysis completed",
            data=response_data
        )
        
    except Exception as e:
        logger.error(f"Analysis error: {e}", exc_info=True)
        return EnhancedDetectionResult(
            success=False,
            code=500,
            message=f"Analysis failed: {str(e)}",
            data={}
        )

@app.post("/analyze-simple")
async def analyze_simple(data: SimpleTextInput):
    """
    Simple analysis - returns basic results only
    """
    try:
        text = data.text.strip()
        if not text:
            raise HTTPException(status_code=400, detail="Empty text")
        
        if not model_manager.models_loaded:
            if not model_manager.load_all_models():
                raise HTTPException(status_code=503, detail="Models not available")
        
        result = model_manager.comprehensive_analysis(text)
        ensemble = result["ensemble_result"]
        
        return {
            "is_ai": ensemble["ai_percentage"] > 50,
            "ai_score": ensemble["ai_percentage"],
            "human_score": ensemble["human_percentage"],
            "detected_model": ensemble["predicted_model"],
            "confidence": ensemble["confidence"],
            "perplexity": result["metrics"]["perplexity"],
            "burstiness": result["metrics"]["burstiness"]
        }
        
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Simple analysis error: {e}")
        raise HTTPException(status_code=500, detail=str(e))

# =====================================================
# 🏃 تشغيل التطبيق
# =====================================================
if __name__ == "__main__":
    import uvicorn
    
    port = int(os.environ.get("PORT", 8000))
    host = os.environ.get("HOST", "0.0.0.0")
    workers = int(os.environ.get("WORKERS", 1))
    
    logger.info("=" * 50)
    logger.info(f"🌐 Starting enhanced server on {host}:{port}")
    logger.info(f"👷 Workers: {workers}")
    logger.info(f"📚 Documentation: http://{host}:{port}/docs")
    logger.info("=" * 50)
    
    uvicorn.run(
        "app_enhanced:app",
        host=host,
        port=port,
        reload=False,
        workers=workers,
        log_level="info"
    )