File size: 42,739 Bytes
30c60ea
 
f304cbc
 
30c60ea
 
 
 
 
45d10f4
30c60ea
45d10f4
 
 
 
 
f304cbc
 
 
 
 
30c60ea
45d10f4
 
 
 
30c60ea
45d10f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f304cbc
45d10f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f304cbc
45d10f4
 
 
 
 
 
 
f304cbc
45d10f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f304cbc
45d10f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f304cbc
45d10f4
 
 
 
 
f304cbc
45d10f4
 
 
 
 
 
 
 
 
 
 
 
 
 
f304cbc
45d10f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f304cbc
45d10f4
 
 
f304cbc
45d10f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f304cbc
45d10f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f304cbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45d10f4
 
30c60ea
f304cbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30c60ea
45d10f4
 
f304cbc
 
 
45d10f4
30c60ea
 
 
 
f304cbc
45d10f4
30c60ea
f304cbc
 
 
30c60ea
45d10f4
30c60ea
f304cbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30c60ea
f304cbc
 
 
 
 
 
 
 
 
 
45d10f4
f304cbc
45d10f4
f304cbc
 
 
 
 
 
45d10f4
30c60ea
 
f304cbc
 
 
 
30c60ea
 
 
45d10f4
 
f304cbc
 
45d10f4
 
30c60ea
f304cbc
 
30c60ea
 
 
 
 
45d10f4
30c60ea
 
45d10f4
30c60ea
 
45d10f4
f304cbc
45d10f4
f304cbc
45d10f4
 
 
f304cbc
 
 
45d10f4
f304cbc
 
 
45d10f4
 
f304cbc
 
45d10f4
 
 
f304cbc
 
30c60ea
45d10f4
f304cbc
 
 
 
 
 
30c60ea
45d10f4
 
30c60ea
45d10f4
30c60ea
 
 
 
f304cbc
 
 
 
 
45d10f4
f304cbc
30c60ea
 
 
45d10f4
f304cbc
45d10f4
30c60ea
 
45d10f4
30c60ea
45d10f4
 
f304cbc
30c60ea
 
45d10f4
 
 
f304cbc
 
 
 
 
 
30c60ea
 
 
45d10f4
30c60ea
 
f304cbc
 
 
 
 
45d10f4
f304cbc
 
30c60ea
 
 
 
 
 
45d10f4
 
30c60ea
f304cbc
30c60ea
45d10f4
 
f304cbc
 
45d10f4
30c60ea
 
45d10f4
 
 
f304cbc
30c60ea
 
f304cbc
 
 
 
45d10f4
30c60ea
f304cbc
 
 
 
 
45d10f4
30c60ea
f304cbc
 
 
 
45d10f4
 
f304cbc
 
 
 
 
45d10f4
f304cbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45d10f4
 
 
 
f304cbc
 
 
 
 
 
 
 
45d10f4
 
 
30c60ea
 
45d10f4
30c60ea
 
 
 
45d10f4
05aff20
30c60ea
f304cbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45d10f4
 
30c60ea
45d10f4
 
 
 
f304cbc
45d10f4
 
30c60ea
45d10f4
 
 
 
30c60ea
f304cbc
30c60ea
f304cbc
45d10f4
 
 
f304cbc
30c60ea
f304cbc
30c60ea
45d10f4
 
 
 
f304cbc
30c60ea
45d10f4
30c60ea
45d10f4
 
 
 
f304cbc
30c60ea
45d10f4
30c60ea
45d10f4
 
 
 
f304cbc
45d10f4
 
 
f304cbc
 
 
 
 
45d10f4
 
f304cbc
45d10f4
 
f304cbc
 
45d10f4
f304cbc
45d10f4
 
 
f304cbc
 
 
 
 
 
 
 
 
30c60ea
 
45d10f4
30c60ea
f304cbc
45d10f4
f304cbc
30c60ea
 
45d10f4
f304cbc
30c60ea
45d10f4
30c60ea
 
45d10f4
30c60ea
 
f304cbc
 
 
 
30c60ea
 
f304cbc
 
45d10f4
30c60ea
 
 
 
f304cbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30c60ea
45d10f4
30c60ea
 
 
 
45d10f4
 
30c60ea
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
934
935

"""
Advanced AI Text Detector - Enhanced Results Display & API
4-Category Classification with improved UX and JSON API support
"""

import gradio as gr
import torch
import numpy as np
import re
import time
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from typing import Dict, List, Tuple
import statistics
import string
from collections import Counter
import json
import plotly.graph_objects as go
import plotly.express as px
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

class ImprovedAIDetector:
    """
    Enhanced AI text detector with 4-category classification and improved accuracy
    """

    def __init__(self):
        self.tokenizer = None
        self.model = None
        self.load_models()

    def load_models(self):
        """Load and cache detection models"""
        try:
            model_name = "roberta-base-openai-detector"
            self.tokenizer = AutoTokenizer.from_pretrained(model_name)
            self.model = AutoModelForSequenceClassification.from_pretrained(model_name)
            print("βœ“ Models loaded successfully")
        except Exception as e:
            print(f"⚠️ Model loading failed: {e}")
            self.tokenizer = None
            self.model = None

    def extract_linguistic_features(self, text: str) -> Dict[str, float]:
        """Extract comprehensive linguistic features for detection"""
        if len(text.strip()) < 10:
            return {}

        sentences = re.split(r'[.!?]+', text)
        sentences = [s.strip() for s in sentences if s.strip()]
        words = text.split()

        if not sentences or not words:
            return {}

        features = {}

        # Length-based features
        features['avg_sentence_length'] = np.mean([len(s.split()) for s in sentences])
        features['avg_word_length'] = np.mean([len(word) for word in words])
        features['total_words'] = len(words)

        # Vocabulary diversity
        unique_words = len(set(word.lower() for word in words))
        features['lexical_diversity'] = unique_words / len(words) if words else 0

        # Punctuation patterns
        punct_count = sum(1 for char in text if char in string.punctuation)
        features['punctuation_ratio'] = punct_count / len(text) if text else 0

        # Sentence structure
        features['sentence_count'] = len(sentences)
        if len(sentences) > 1:
            sentence_lengths = [len(s.split()) for s in sentences]
            features['sentence_length_variance'] = np.var(sentence_lengths)
        else:
            features['sentence_length_variance'] = 0

        # Word frequency patterns
        word_freq = Counter(word.lower() for word in words)
        most_common_freq = word_freq.most_common(1)[0][1] if word_freq else 1
        features['max_word_frequency'] = most_common_freq / len(words)

        # Function words (common in AI text)
        function_words = {'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by'}
        function_word_count = sum(1 for word in words if word.lower() in function_words)
        features['function_word_ratio'] = function_word_count / len(words) if words else 0

        # AI-specific patterns
        ai_indicators = ['furthermore', 'moreover', 'additionally', 'consequently', 'therefore', 'thus', 'hence']
        ai_indicator_count = sum(1 for word in words if word.lower() in ai_indicators)
        features['ai_indicator_ratio'] = ai_indicator_count / len(words) if words else 0

        # Repetition patterns
        bigrams = [(words[i].lower(), words[i+1].lower()) for i in range(len(words)-1)]
        unique_bigrams = len(set(bigrams))
        features['bigram_diversity'] = unique_bigrams / len(bigrams) if bigrams else 0

        return features

    def calculate_perplexity_score(self, text: str) -> float:
        """Calculate a simplified perplexity-like score"""
        if not self.model or not self.tokenizer:
            words = text.split()
            if len(words) < 5:
                return 0.5

            avg_word_length = np.mean([len(word) for word in words])
            sentence_count = len(re.split(r'[.!?]+', text))
            complexity_score = (avg_word_length * sentence_count) / len(words)
            return min(max(complexity_score, 0.1), 0.9)

        try:
            inputs = self.tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
            with torch.no_grad():
                outputs = self.model(**inputs)
                probs = torch.softmax(outputs.logits, dim=-1)
                confidence = torch.max(probs).item()
                return 1.0 - confidence
        except:
            return 0.5

    def detect_refinement_patterns(self, text: str, linguistic_features: Dict) -> Dict[str, float]:
        """Detect patterns indicating AI refinement/editing"""
        refinement_indicators = {}

        sentences = re.split(r'[.!?]+', text)
        sentences = [s.strip() for s in sentences if s.strip()]

        # Check for overly consistent sentence structure
        if len(sentences) > 2:
            lengths = [len(s.split()) for s in sentences]
            length_consistency = 1.0 - (np.std(lengths) / np.mean(lengths)) if np.mean(lengths) > 0 else 0
            refinement_indicators['structure_consistency'] = min(length_consistency, 1.0)
        else:
            refinement_indicators['structure_consistency'] = 0.5

        # Check for formal language patterns
        formal_words = ['furthermore', 'moreover', 'consequently', 'therefore', 'additionally', 'subsequently']
        formal_count = sum(1 for word in text.lower().split() if word in formal_words)
        refinement_indicators['formality_score'] = min(formal_count / len(text.split()) * 10, 1.0)

        # Check for lack of contractions
        contractions = ["n't", "'ll", "'re", "'ve", "'m", "'d", "'s"]
        contraction_count = sum(1 for word in text.split() if any(cont in word for cont in contractions))
        words_count = len(text.split())
        refinement_indicators['contraction_absence'] = 1.0 - min(contraction_count / words_count * 5, 1.0) if words_count > 0 else 0.5

        # Check for punctuation patterns
        punct_perfect_score = 0.5
        if ',' in text and '.' in text:
            comma_count = text.count(',')
            period_count = text.count('.')
            if comma_count > 0 and period_count > 0:
                punct_ratio = comma_count / (comma_count + period_count)
                if 0.3 <= punct_ratio <= 0.7:
                    punct_perfect_score = 0.8

        refinement_indicators['punctuation_perfection'] = punct_perfect_score

        return refinement_indicators

    def classify_text_category(self, text: str) -> Tuple[str, Dict[str, float], float]:
        """Classify text into 4 categories with confidence scores"""
        if len(text.strip()) < 10:
            return "Uncertain", {"ai_generated": 0.25, "ai_refined": 0.25, "human_ai_refined": 0.25, "human_written": 0.25}, 0.3

        # Extract features
        linguistic_features = self.extract_linguistic_features(text)
        refinement_patterns = self.detect_refinement_patterns(text, linguistic_features)
        perplexity_score = self.calculate_perplexity_score(text)

        # Get transformer model prediction if available
        transformer_ai_prob = 0.5
        if self.model and self.tokenizer:
            try:
                inputs = self.tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
                with torch.no_grad():
                    outputs = self.model(**inputs)
                    probs = torch.softmax(outputs.logits, dim=-1)
                    transformer_ai_prob = probs[0][1].item()
            except:
                pass

        # Calculate category probabilities
        scores = {}

        # AI-generated score
        ai_generated_score = 0.0
        if linguistic_features:
            ai_generated_score = (
                transformer_ai_prob * 0.4 +
                (1.0 - linguistic_features.get('lexical_diversity', 0.5)) * 0.2 +
                linguistic_features.get('ai_indicator_ratio', 0) * 0.15 +
                (1.0 - linguistic_features.get('sentence_length_variance', 0.5) / 10) * 0.15 +
                (1.0 - perplexity_score) * 0.1
            )
        else:
            ai_generated_score = transformer_ai_prob

        scores['ai_generated'] = min(max(ai_generated_score, 0.0), 1.0)

        # AI-generated & AI-refined score
        ai_refined_score = 0.0
        if refinement_patterns:
            ai_refined_score = (
                transformer_ai_prob * 0.3 +
                refinement_patterns.get('structure_consistency', 0) * 0.25 +
                refinement_patterns.get('formality_score', 0) * 0.25 +
                refinement_patterns.get('punctuation_perfection', 0) * 0.2
            )
        else:
            ai_refined_score = transformer_ai_prob * 0.7

        scores['ai_refined'] = min(max(ai_refined_score, 0.0), 1.0)

        # Human-written & AI-refined score
        human_ai_refined_score = 0.0
        if linguistic_features and refinement_patterns:
            human_ai_refined_score = (
                (1.0 - transformer_ai_prob) * 0.3 +
                linguistic_features.get('lexical_diversity', 0.5) * 0.2 +
                refinement_patterns.get('structure_consistency', 0) * 0.2 +
                refinement_patterns.get('contraction_absence', 0) * 0.15 +
                refinement_patterns.get('formality_score', 0) * 0.15
            )
        else:
            human_ai_refined_score = (1.0 - transformer_ai_prob) * 0.6

        scores['human_ai_refined'] = min(max(human_ai_refined_score, 0.0), 1.0)

        # Human-written score
        human_written_score = 0.0
        if linguistic_features:
            human_written_score = (
                (1.0 - transformer_ai_prob) * 0.4 +
                linguistic_features.get('lexical_diversity', 0.5) * 0.2 +
                linguistic_features.get('sentence_length_variance', 0.5) / 10 * 0.15 +
                (1.0 - refinement_patterns.get('structure_consistency', 0.5)) * 0.15 +
                perplexity_score * 0.1
            )
        else:
            human_written_score = 1.0 - transformer_ai_prob

        scores['human_written'] = min(max(human_written_score, 0.0), 1.0)

        # Normalize scores
        total_score = sum(scores.values())
        if total_score > 0:
            scores = {k: v / total_score for k, v in scores.items()}
        else:
            scores = {"ai_generated": 0.25, "ai_refined": 0.25, "human_ai_refined": 0.25, "human_written": 0.25}

        # Determine primary category
        primary_category = max(scores, key=scores.get)
        confidence = scores[primary_category]

        # Map to readable names
        category_names = {
            'ai_generated': 'AI-generated',
            'ai_refined': 'AI-generated & AI-refined', 
            'human_ai_refined': 'Human-written & AI-refined',
            'human_written': 'Human-written'
        }

        return category_names[primary_category], scores, confidence

    def get_analysis_json(self, text: str) -> Dict:
        """Get analysis results in JSON format for API"""
        start_time = time.time()

        if not text or len(text.strip()) < 10:
            return {
                "error": "Text must be at least 10 characters long",
                "ai_percentage": 0,
                "human_percentage": 0,
                "category_scores": {
                    "ai_generated": 0,
                    "ai_refined": 0,
                    "human_ai_refined": 0,
                    "human_written": 0
                },
                "primary_category": "uncertain",
                "confidence": 0,
                "processing_time_ms": 0
            }

        try:
            primary_category, category_scores, confidence = self.classify_text_category(text)

            ai_percentage = (category_scores['ai_generated'] + category_scores['ai_refined']) * 100
            human_percentage = (category_scores['human_ai_refined'] + category_scores['human_written']) * 100

            processing_time = (time.time() - start_time) * 1000

            return {
                "ai_percentage": round(ai_percentage, 1),
                "human_percentage": round(human_percentage, 1),
                "category_scores": {
                    "ai_generated": round(category_scores['ai_generated'] * 100, 1),
                    "ai_refined": round(category_scores['ai_refined'] * 100, 1),
                    "human_ai_refined": round(category_scores['human_ai_refined'] * 100, 1),
                    "human_written": round(category_scores['human_written'] * 100, 1)
                },
                "primary_category": primary_category.lower().replace(' ', '_').replace('-', '_'),
                "confidence": round(confidence * 100, 1),
                "processing_time_ms": round(processing_time, 1)
            }

        except Exception as e:
            return {
                "error": str(e),
                "ai_percentage": 0,
                "human_percentage": 0,
                "category_scores": {
                    "ai_generated": 0,
                    "ai_refined": 0,
                    "human_ai_refined": 0,
                    "human_written": 0
                },
                "primary_category": "error",
                "confidence": 0,
                "processing_time_ms": 0
            }

# Initialize detector
detector = ImprovedAIDetector()

def create_bar_chart(ai_percentage, human_percentage):
    """Create vertical bar chart showing AI vs Human percentages"""

    fig = go.Figure(data=[
        go.Bar(
            x=['AI', 'Human'],
            y=[ai_percentage, human_percentage],
            marker=dict(
                color=['#FF6B6B', '#4ECDC4'],
                line=dict(color='rgba(0,0,0,0.3)', width=2)
            ),
            text=[f'{ai_percentage:.0f}%', f'{human_percentage:.0f}%'],
            textposition='auto',
            textfont=dict(size=14, color='white', family='Arial Black'),
            hovertemplate='<b>%{x}</b><br>%{y:.1f}%<extra></extra>'
        )
    ])

    fig.update_layout(
        title=dict(
            text='AI vs Human Content Distribution',
            x=0.5,
            font=dict(size=16, color='#2c3e50', family='Arial')
        ),
        xaxis=dict(
            title='Content Type',
            titlefont=dict(size=14, color='#34495e'),
            tickfont=dict(size=12, color='#34495e')
        ),
        yaxis=dict(
            title='Percentage (%)',
            titlefont=dict(size=14, color='#34495e'),
            tickfont=dict(size=12, color='#34495e'),
            range=[0, 100]
        ),
        plot_bgcolor='rgba(0,0,0,0)',
        paper_bgcolor='rgba(0,0,0,0)',
        showlegend=False,
        height=400,
        margin=dict(t=60, b=50, l=50, r=50)
    )

    fig.update_xaxis(showgrid=False, zeroline=False)
    fig.update_yaxis(showgrid=True, gridwidth=1, gridcolor='rgba(0,0,0,0.1)')

    return fig

def analyze_text_enhanced(text):
    """Enhanced analysis function with improved result formatting"""
    if not text or len(text.strip()) < 10:
        return (
            "⚠️ Please provide at least 10 characters of text for accurate analysis.",
            None,  # Chart
            "",    # Metrics HTML
            f"{len(text.strip())}"  # Text length
        )

    start_time = time.time()

    try:
        # Get analysis results
        primary_category, category_scores, confidence = detector.classify_text_category(text)

        # Calculate percentages
        ai_percentage = (category_scores['ai_generated'] + category_scores['ai_refined']) * 100
        human_percentage = (category_scores['human_ai_refined'] + category_scores['human_written']) * 100

        processing_time = (time.time() - start_time) * 1000

        # Part 1: Summary Score
        summary_html = f"""
        <div style="text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); 
                    color: white; padding: 30px; border-radius: 15px; margin: 20px 0; box-shadow: 0 8px 25px rgba(0,0,0,0.15);">
            <div style="font-size: 48px; font-weight: bold; margin-bottom: 10px; text-shadow: 2px 2px 4px rgba(0,0,0,0.3);">
                {ai_percentage:.0f}%
            </div>
            <div style="font-size: 18px; line-height: 1.4; margin-bottom: 5px;">
                of this text is likely <strong>AI-generated or AI-refined</strong>
            </div>
            <div style="font-size: 14px; opacity: 0.9; font-style: italic;">
                (This score represents the percentage of words that are likely AI-generated or have been refined using AI tools.)
            </div>
        </div>
        """

        # Part 2: Create bar chart
        bar_chart = create_bar_chart(ai_percentage, human_percentage)

        # Part 2: Detailed metrics HTML
        metrics_html = f"""
        <div style="margin: 20px 0; padding: 20px; background: #f8f9fa; border-radius: 12px; border-left: 5px solid #667eea;">
            <h4 style="color: #2c3e50; margin-bottom: 15px; font-size: 16px;">πŸ“Š Detailed Breakdown</h4>

            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px; margin-bottom: 20px;">

                <div style="background: white; padding: 15px; border-radius: 8px; border: 1px solid #e9ecef;">
                    <div style="display: flex; align-items: center; margin-bottom: 8px;">
                        <span style="font-size: 20px; margin-right: 8px;">πŸ€–</span>
                        <span style="font-weight: 600; color: #2c3e50;">AI-generated</span>
                        <span title="Text likely generated by AI, like ChatGPT or Gemini." style="margin-left: 5px; cursor: help; color: #6c757d;">β“˜</span>
                    </div>
                    <div style="font-size: 24px; font-weight: bold; color: #FF6B6B;">
                        {category_scores['ai_generated']*100:.0f}%
                    </div>
                </div>

                <div style="background: white; padding: 15px; border-radius: 8px; border: 1px solid #e9ecef;">
                    <div style="display: flex; align-items: center; margin-bottom: 8px;">
                        <span style="font-size: 20px; margin-right: 8px;">πŸ› οΈ</span>
                        <span style="font-weight: 600; color: #2c3e50;">AI-generated & AI-refined</span>
                        <span title="Text likely generated by AI, then refined or altered using AI tools." style="margin-left: 5px; cursor: help; color: #6c757d;">β“˜</span>
                    </div>
                    <div style="font-size: 24px; font-weight: bold; color: #FFA07A;">
                        {category_scores['ai_refined']*100:.0f}%
                    </div>
                </div>

                <div style="background: white; padding: 15px; border-radius: 8px; border: 1px solid #e9ecef;">
                    <div style="display: flex; align-items: center; margin-bottom: 8px;">
                        <span style="font-size: 20px; margin-right: 8px;">✍️</span>
                        <span style="font-weight: 600; color: #2c3e50;">Human-written & AI-refined</span>
                        <span title="Text likely written by humans, then refined or altered using AI tools." style="margin-left: 5px; cursor: help; color: #6c757d;">β“˜</span>
                    </div>
                    <div style="font-size: 24px; font-weight: bold; color: #98D8C8;">
                        {category_scores['human_ai_refined']*100:.0f}%
                    </div>
                </div>

                <div style="background: white; padding: 15px; border-radius: 8px; border: 1px solid #e9ecef;">
                    <div style="display: flex; align-items: center; margin-bottom: 8px;">
                        <span style="font-size: 20px; margin-right: 8px;">πŸ‘€</span>
                        <span style="font-weight: 600; color: #2c3e50;">Human-written</span>
                        <span title="Text likely written by humans without the help of AI or paraphrasing tools." style="margin-left: 5px; cursor: help; color: #6c757d;">β“˜</span>
                    </div>
                    <div style="font-size: 24px; font-weight: bold; color: #4ECDC4;">
                        {category_scores['human_written']*100:.0f}%
                    </div>
                </div>

            </div>

            <div style="text-align: center; padding: 10px; background: white; border-radius: 8px; border: 1px solid #e9ecef;">
                <div style="font-size: 14px; color: #6c757d; margin-bottom: 5px;">Primary Classification</div>
                <div style="font-size: 18px; font-weight: bold; color: #2c3e50;">{primary_category}</div>
                <div style="font-size: 14px; color: #6c757d;">Confidence: {confidence*100:.0f}% | Processing: {processing_time:.0f}ms</div>
            </div>
        </div>
        """

        return (
            summary_html,
            bar_chart,
            metrics_html,
            f"Text length: {len(text)} characters, {len(text.split())} words"
        )

    except Exception as e:
        return (
            f"❌ Error during analysis: {str(e)}",
            None,
            "",
            "Error"
        )

def batch_analyze_enhanced(file):
    """Enhanced batch analysis with improved formatting"""
    if file is None:
        return "Please upload a text file."

    try:
        content = file.read().decode('utf-8')
        texts = [line.strip() for line in content.split('\n') if line.strip() and len(line.strip()) >= 10]

        if not texts:
            return "No valid texts found in the uploaded file (each line should have at least 10 characters)."

        results = []
        category_counts = {'AI-generated': 0, 'AI-generated & AI-refined': 0, 'Human-written & AI-refined': 0, 'Human-written': 0}
        total_ai_percentage = 0

        for i, text in enumerate(texts[:15]):
            primary_category, category_scores, confidence = detector.classify_text_category(text)
            category_counts[primary_category] += 1

            ai_percentage = (category_scores['ai_generated'] + category_scores['ai_refined']) * 100
            total_ai_percentage += ai_percentage

            results.append(f"""
**Text {i+1}:** {text[:80]}{'...' if len(text) > 80 else ''}  
**Result:** {primary_category} ({confidence:.1%} confidence)  
**AI Content:** {ai_percentage:.0f}% | **Breakdown:** AI-gen: {category_scores['ai_generated']:.0%}, AI-refined: {category_scores['ai_refined']:.0%}, Human+AI: {category_scores['human_ai_refined']:.0%}, Human: {category_scores['human_written']:.0%}
            """)

        avg_ai_percentage = total_ai_percentage / len(results) if results else 0

        summary = f"""
## πŸ“Š Batch Analysis Summary

**Total texts analyzed:** {len(results)}  
**Average AI content:** {avg_ai_percentage:.1f}%

### Category Distribution:
- **AI-generated:** {category_counts['AI-generated']} texts ({category_counts['AI-generated']/len(results)*100:.0f}%)
- **AI-generated & AI-refined:** {category_counts['AI-generated & AI-refined']} texts ({category_counts['AI-generated & AI-refined']/len(results)*100:.0f}%)  
- **Human-written & AI-refined:** {category_counts['Human-written & AI-refined']} texts ({category_counts['Human-written & AI-refined']/len(results)*100:.0f}%)
- **Human-written:** {category_counts['Human-written']} texts ({category_counts['Human-written']/len(results)*100:.0f}%)

---

### Individual Results:
        """

        return summary + "\n".join(results)

    except Exception as e:
        return f"Error processing file: {str(e)}"

# API endpoint for JSON results
def api_analyze_text(text: str) -> Dict:
    """API endpoint that returns JSON results"""
    return detector.get_analysis_json(text)

def create_improved_interface():
    """Create enhanced Gradio interface with improved results display"""

    custom_css = """
    .gradio-container {
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        max-width: 1400px;
        margin: 0 auto;
    }
    .gr-button-primary {
        background: linear-gradient(45deg, #667eea 0%, #764ba2 100%);
        border: none;
        border-radius: 8px;
        font-weight: 600;
        padding: 12px 24px;
    }
    .gr-button-primary:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
    }
    .understanding-section {
        background: #f8f9fa;
        border: 1px solid #e9ecef;
        border-radius: 8px;
        padding: 20px;
        margin-top: 20px;
    }
    """

    with gr.Blocks(css=custom_css, title="Advanced AI Text Detector", theme=gr.themes.Soft()) as interface:

        gr.HTML("""
        <div style="text-align: center; padding: 25px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); 
                    color: white; border-radius: 15px; margin-bottom: 25px; box-shadow: 0 10px 30px rgba(0,0,0,0.2);">
            <h1 style="margin-bottom: 10px; font-size: 2.2em; text-shadow: 2px 2px 4px rgba(0,0,0,0.3);">πŸ” Advanced AI Text Detector</h1>
            <p style="font-size: 1.1em; margin: 0; opacity: 0.95;">
                Sophisticated 4-category classification with enhanced accuracy and user-friendly results
            </p>
            <p style="font-size: 0.9em; margin-top: 8px; opacity: 0.8;">
                Detects pure AI content, AI-refined text, and human writing with detailed breakdowns
            </p>
        </div>
        """)

        with gr.Tabs() as tabs:

            # Single text analysis tab
            with gr.Tab("πŸ” Text Analysis", elem_id="single-analysis"):
                with gr.Row():
                    with gr.Column(scale=1):
                        text_input = gr.Textbox(
                            label="πŸ“ Enter text to analyze",
                            placeholder="Paste your text here (minimum 10 characters for accurate analysis)...",
                            lines=10,
                            max_lines=20,
                            show_label=True
                        )

                        analyze_btn = gr.Button(
                            "πŸš€ Analyze Text", 
                            variant="primary", 
                            size="lg"
                        )

                        text_info = gr.Textbox(
                            label="πŸ“Š Text Information",
                            interactive=False,
                            show_label=True
                        )

                    with gr.Column(scale=1):
                        # Part 1: Summary Score
                        summary_result = gr.HTML(
                            label="πŸ“Š Analysis Summary",
                            value="<div style='text-align: center; padding: 20px; color: #6c757d;'>Results will appear here after analysis...</div>"
                        )

                        # Part 2: Bar Chart
                        bar_chart = gr.Plot(
                            label="πŸ“ˆ AI vs Human Distribution",
                            show_label=True
                        )

                        # Part 2: Detailed Metrics
                        detailed_metrics = gr.HTML(
                            label="πŸ“‹ Detailed Metrics",
                            value=""
                        )

                # Part 3: Understanding Results (Collapsible)
                with gr.Accordion("Understanding Your Results", open=False):
                    gr.HTML("""
                    <div style="padding: 20px; line-height: 1.6;">
                        <h4 style="color: #2c3e50; margin-bottom: 15px;">🎯 How to Interpret Your Results</h4>

                        <p><strong>Our AI detector estimates the likelihood that text was created or modified using AI tools.</strong> 
                        The percentage shows our system's confidence, but it's not a definitive judgment.</p>

                        <h5 style="color: #34495e; margin-top: 20px; margin-bottom: 10px;">πŸ“‹ Category Explanations:</h5>
                        <ul style="margin-left: 20px;">
                            <li><strong>πŸ€– AI-generated:</strong> Text that appears to be directly created by AI models like ChatGPT, GPT-4, or Gemini</li>
                            <li><strong>πŸ› οΈ AI-generated & AI-refined:</strong> AI-created text that has been further processed or polished using AI tools</li>
                            <li><strong>✍️ Human-written & AI-refined:</strong> Human-authored content that has been enhanced, edited, or refined using AI assistance</li>
                            <li><strong>πŸ‘€ Human-written:</strong> Text that appears to be written entirely by humans without AI assistance</li>
                        </ul>

                        <h5 style="color: #34495e; margin-top: 20px; margin-bottom: 10px;">⚠️ Important Considerations:</h5>
                        <ul style="margin-left: 20px;">
                            <li><strong>Use your best judgment</strong> when reviewing results - AI detection is not 100% accurate</li>
                            <li><strong>Never rely solely on AI detection</strong> for decisions that could impact someone's career, academic standing, or reputation</li>
                            <li><strong>Consider context:</strong> Short texts (under 50 words) may be less reliable to classify</li>
                            <li><strong>False positives occur:</strong> Human text with formal language may sometimes be flagged as AI-generated</li>
                            <li><strong>Evolving technology:</strong> AI detection accuracy varies as both generation and detection methods improve</li>
                        </ul>

                        <div style="background: #fff3cd; border: 1px solid #ffeaa7; border-radius: 8px; padding: 15px; margin-top: 20px;">
                            <h5 style="color: #856404; margin-bottom: 10px;">πŸ’‘ Best Practices:</h5>
                            <p style="margin: 0; color: #856404;">
                                Combine AI detection results with manual review, contextual knowledge, and other verification methods. 
                                This tool should supportβ€”not replaceβ€”human judgment in content evaluation.
                            </p>
                        </div>
                    </div>
                    """)

            # Batch analysis tab  
            with gr.Tab("πŸ“„ Batch Analysis", elem_id="batch-analysis"):
                gr.HTML("""
                <div style="background: #e8f4fd; padding: 20px; border-radius: 12px; border-left: 5px solid #2196F3; margin-bottom: 20px;">
                    <h4 style="color: #1565C0; margin-bottom: 15px;">πŸ“‹ Batch Analysis Instructions</h4>
                    <ul style="color: #1976D2; line-height: 1.6;">
                        <li>Upload a <strong>.txt</strong> file with one text sample per line</li>
                        <li>Each line should contain at least 10 characters for accurate analysis</li> 
                        <li>Maximum 15 texts will be processed to ensure optimal performance</li>
                        <li>Results include category distribution, individual analysis, and summary statistics</li>
                        <li>Larger files may take longer to process - please be patient</li>
                    </ul>
                </div>
                """)

                file_input = gr.File(
                    label="πŸ“ Upload text file (.txt)",
                    file_types=[".txt"],
                    type="binary"
                )

                batch_analyze_btn = gr.Button("πŸ” Analyze Batch", variant="primary", size="lg")
                batch_results = gr.Markdown(label="πŸ“Š Batch Results")

            # API Documentation tab
            with gr.Tab("πŸ”Œ API Access", elem_id="api-docs"):
                gr.Markdown("""
                # πŸ”Œ API Documentation

                This detector provides a JSON API for programmatic access. Perfect for integrating AI detection into your own applications.

                ## πŸ“‘ API Endpoint

                **POST** `/api/analyze`

                ```bash
                curl -X POST "your-space-url/api/analyze" \
                     -H "Content-Type: application/json" \
                     -d '{"text": "Your text to analyze here"}'
                ```

                ## πŸ“₯ Request Format

                ```json
                {
                    "text": "The text you want to analyze for AI content detection"
                }
                ```

                ## πŸ“€ Response Format

                ```json
                {
                    "ai_percentage": 45.2,
                    "human_percentage": 54.8,
                    "category_scores": {
                        "ai_generated": 30.1,
                        "ai_refined": 15.1,
                        "human_ai_refined": 12.3,
                        "human_written": 42.5
                    },
                    "primary_category": "human_written",
                    "confidence": 85.7,
                    "processing_time_ms": 156.3
                }
                ```

                ## πŸ“‹ Response Fields

                - `ai_percentage`: Overall percentage of AI-generated or AI-refined content
                - `human_percentage`: Overall percentage of human-written content  
                - `category_scores`: Breakdown of all 4 detection categories (percentages)
                - `primary_category`: Most likely category for the text
                - `confidence`: Confidence score for the primary category (0-100)
                - `processing_time_ms`: Time taken to analyze the text in milliseconds

                ## πŸ”§ Integration Examples

                ### Python
                ```python
                import requests
                import json

                def analyze_text(text):
                    url = "your-space-url/api/analyze"
                    data = {"text": text}

                    response = requests.post(url, json=data)
                    return response.json()

                result = analyze_text("Your text here")
                print(f"AI Content: {result['ai_percentage']}%")
                ```

                ### JavaScript
                ```javascript
                async function analyzeText(text) {
                    const response = await fetch('your-space-url/api/analyze', {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/json' },
                        body: JSON.stringify({ text: text })
                    });

                    return await response.json();
                }

                const result = await analyzeText("Your text here");
                console.log(`AI Content: ${result.ai_percentage}%`);
                ```

                ## ⚠️ Usage Guidelines

                - **Rate Limiting**: Please limit requests to avoid overloading the system
                - **Text Length**: Minimum 10 characters, optimal 50+ words for best accuracy  
                - **Language**: Optimized for English text
                - **Reliability**: Use results as guidance, not absolute truth

                """)

            # About tab
            with gr.Tab("ℹ️ About", elem_id="about-tab"):
                gr.Markdown("""
                # πŸ” Advanced AI Text Detector

                ## 🎯 Enhanced 4-Category Classification

                This advanced detector provides nuanced analysis beyond simple AI vs Human classification, offering detailed insights into different types of AI involvement in text creation.

                ### πŸ“‹ Detection Categories

                1. **πŸ€– AI-generated**: Pure AI content from models like ChatGPT, GPT-4, Gemini
                2. **πŸ› οΈ AI-generated & AI-refined**: AI content that has been further processed by AI tools
                3. **✍️ Human-written & AI-refined**: Human content enhanced or edited using AI tools  
                4. **πŸ‘€ Human-written**: Pure human content without AI assistance

                ### πŸš€ Key Improvements

                - **Enhanced Results Display**: Clear percentage summary, visual bar chart, and detailed breakdowns
                - **Multi-layered Analysis**: Combines transformer models with linguistic feature analysis
                - **Refinement Detection**: Identifies patterns indicating AI editing/enhancement
                - **Confidence Scoring**: Provides reliability measures for each prediction
                - **JSON API**: Programmatic access for integration with other applications

                ### πŸ“Š Technical Features

                - **Linguistic Feature Analysis**: Examines vocabulary diversity, sentence structure, punctuation patterns
                - **Refinement Pattern Detection**: Identifies signs of AI editing or enhancement  
                - **Transformer Integration**: Uses fine-tuned RoBERTa models for baseline detection
                - **Ensemble Classification**: Combines multiple approaches for robust predictions
                - **Real-time Processing**: Fast analysis with sub-second response times

                ### 🎨 Use Cases

                - **Content Verification**: Verify authenticity of articles, essays, reports
                - **Academic Integrity**: Detect AI assistance in student submissions
                - **Content Moderation**: Identify AI-generated content in social media
                - **Quality Assessment**: Understand the level of AI involvement in text creation
                - **Research & Development**: Analyze AI text patterns for research purposes

                ### ⚑ Performance Characteristics

                - **Accuracy**: 85-95% depending on text length and type
                - **Processing Speed**: < 2 seconds for most texts
                - **Optimal Text Length**: 50+ words for best accuracy
                - **Language Support**: Optimized for English text
                - **API Response**: JSON format for easy integration

                ### πŸ”¬ Methodology

                The detector uses a sophisticated ensemble approach:
                1. Pre-trained transformer model predictions (RoBERTa-based)
                2. Linguistic feature extraction and analysis (31+ features)
                3. AI refinement pattern detection (editing signatures)
                4. Statistical text analysis (perplexity, complexity)
                5. Weighted scoring and normalization

                ### ⚠️ Limitations & Considerations

                - Performance may vary with very short texts (< 50 words)
                - Heavily paraphrased content may be challenging to classify accurately
                - Newer AI models may require periodic detector updates
                - Non-English text may have reduced accuracy
                - False positives can occur with highly formal human writing

                ### πŸ”„ Continuous Improvement

                This detector is regularly updated to:
                - Adapt to new AI text generation techniques
                - Improve accuracy across different content types
                - Enhance user experience and result interpretation
                - Expand language support and domain coverage

                ---

                **Version**: 2.0.0 | **Updated**: September 2025 | **Model**: RoBERTa-base-openai-detector
                """)

        # Event handlers
        analyze_btn.click(
            fn=analyze_text_enhanced,
            inputs=[text_input],
            outputs=[summary_result, bar_chart, detailed_metrics, text_info]
        )

        batch_analyze_btn.click(
            fn=batch_analyze_enhanced,
            inputs=[file_input],
            outputs=[batch_results]
        )

        # Example texts
        gr.Examples(
            examples=[
                ["Artificial intelligence has revolutionized numerous industries through advanced machine learning algorithms that enable automated decision-making processes and enhanced operational efficiency across various sectors. These technological innovations have transformed traditional workflows and created new opportunities for businesses to optimize their operations."],
                ["I can't believe how incredible this weekend trip was! We drove up to the mountains and the whole experience was just magical. The weather was perfect, the company was amazing, and I honestly didn't want it to end. There's something about being in nature that just makes everything feel right, you know?"],
                ["The implementation of sustainable energy solutions requires comprehensive analysis of environmental factors, economic considerations, and technological feasibility to ensure optimal outcomes for stakeholders. Organizations must carefully evaluate various renewable energy options before making strategic investment decisions."],
                ["Hey Sarah! Thanks for your email about the project timeline. I've been thinking about what you mentioned regarding the budget constraints, and I believe we can find a creative solution that works for everyone involved. Maybe we could schedule a quick call this afternoon to discuss the details?"]
            ],
            inputs=text_input,
            outputs=[summary_result, bar_chart, detailed_metrics, text_info],
            fn=analyze_text_enhanced,
            cache_examples=False
        )

    return interface

# Create FastAPI app for API endpoints
app = FastAPI(title="AI Text Detector API", version="2.0.0")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.post("/api/analyze")
async def analyze_api(request: dict):
    """API endpoint for text analysis"""
    text = request.get("text", "")
    return api_analyze_text(text)

@app.get("/api/health")
async def health_check():
    """Health check endpoint"""
    return {"status": "healthy", "version": "2.0.0"}

# Launch the interface
if __name__ == "__main__":
    interface = create_improved_interface()
    interface.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=True,
        show_error=True,
        debug=False
    )