File size: 27,697 Bytes
850a7ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import random
import re
import warnings
import math
from collections import Counter
warnings.filterwarnings("ignore")

# Import NLTK with error handling
try:
    import nltk
    import textstat
    from nltk.corpus import wordnet
    from nltk.tokenize import sent_tokenize, word_tokenize
    NLTK_AVAILABLE = True
    
    # Download required NLTK data
    try:
        nltk.data.find('tokenizers/punkt_tab')
    except LookupError:
        nltk.download('punkt_tab')
    try:
        nltk.data.find('tokenizers/punkt')
    except LookupError:
        nltk.download('punkt')
    try:
        nltk.data.find('corpora/wordnet')
    except LookupError:
        nltk.download('wordnet')
    try:
        nltk.data.find('corpora/omw-1.4')
    except LookupError:
        nltk.download('omw-1.4')
        
except ImportError as e:
    print(f"NLTK import error: {e}")
    NLTK_AVAILABLE = False
    import textstat

class AdvancedHumanizer:
    def __init__(self):
        self.transition_words = [
            "However", "Nevertheless", "Furthermore", "Moreover", "Additionally",
            "Consequently", "Therefore", "Thus", "In contrast", "Similarly",
            "On the other hand", "Meanwhile", "Subsequently", "Notably",
            "Importantly", "Significantly", "Interestingly", "Remarkably"
        ]
        
        self.hedging_phrases = [
            "appears to", "seems to", "tends to", "suggests that", "indicates that",
            "may well", "might be", "could be", "potentially", "presumably",
            "arguably", "to some extent", "in many cases", "generally speaking"
        ]
        
        self.academic_connectors = [
            "In light of this", "Building upon this", "This finding suggests",
            "It is worth noting that", "This observation", "These results",
            "The evidence indicates", "This approach", "The data reveals"
        ]
        
        # Enhanced vocabulary replacements for better humanization
        self.vocabulary_replacements = {
            "significant": ["notable", "considerable", "substantial", "important", "remarkable"],
            "demonstrate": ["show", "illustrate", "reveal", "display", "indicate"],
            "utilize": ["use", "employ", "apply", "implement", "make use of"],
            "implement": ["apply", "use", "put into practice", "carry out", "execute"],
            "generate": ["create", "produce", "develop", "form", "make"],
            "facilitate": ["help", "enable", "assist", "support", "aid"],
            "optimize": ["improve", "enhance", "refine", "perfect", "better"],
            "analyze": ["examine", "study", "investigate", "assess", "evaluate"],
            "therefore": ["thus", "hence", "consequently", "as a result", "for this reason"],
            "however": ["nevertheless", "nonetheless", "yet", "on the other hand", "but"],
            "furthermore": ["moreover", "additionally", "in addition", "what is more", "besides"],
            "substantial": ["significant", "considerable", "notable", "important", "major"],
            "subsequently": ["later", "then", "afterward", "following this", "next"],
            "approximately": ["about", "roughly", "around", "nearly", "close to"],
            "numerous": ["many", "several", "multiple", "various", "a number of"],
            "encompasses": ["includes", "covers", "contains", "involves", "comprises"],
            "methodology": ["method", "approach", "technique", "procedure", "process"],
            "comprehensive": ["complete", "thorough", "extensive", "detailed", "full"],
            "indicates": ["shows", "suggests", "points to", "reveals", "demonstrates"],
            "established": ["set up", "created", "formed", "developed", "built"]
        }

    def split_into_sentences(self, text):
        """Smart sentence splitting with NLTK fallback"""
        if NLTK_AVAILABLE:
            return sent_tokenize(text)
        else:
            # Enhanced fallback sentence splitting
            sentences = []
            current = ""
            
            for char in text:
                current += char
                if char == '.' and len(current) > 10:
                    # Check if this looks like end of sentence
                    remaining = text[text.find(current) + len(current):]
                    if remaining and (remaining[0].isupper() or remaining.strip().startswith(('The ', 'This ', 'A '))):
                        sentences.append(current.strip())
                        current = ""
            
            if current.strip():
                sentences.append(current.strip())
            
            return [s for s in sentences if len(s.strip()) > 5]

    def add_natural_variations(self, text):
        """Add natural linguistic variations to make text less robotic"""
        sentences = self.split_into_sentences(text)
        varied_sentences = []
        
        for i, sentence in enumerate(sentences):
            sentence = sentence.strip()
            if not sentence.endswith('.'):
                sentence += '.'
                
            # Randomly add hedging language
            if random.random() < 0.3 and not any(phrase in sentence.lower() for phrase in self.hedging_phrases):
                hedge = random.choice(self.hedging_phrases)
                if sentence.startswith("The ") or sentence.startswith("This "):
                    words = sentence.split()
                    if len(words) > 2:
                        words.insert(2, hedge)
                        sentence = " ".join(words)
            
            # Add transitional phrases for flow
            if i > 0 and random.random() < 0.4:
                connector = random.choice(self.academic_connectors)
                sentence = f"{connector}, {sentence.lower()}"
            
            varied_sentences.append(sentence)
        
        return " ".join(varied_sentences)

    def diversify_vocabulary(self, text):
        """Replace common words with synonyms for variation"""
        if NLTK_AVAILABLE:
            words = word_tokenize(text)
            result = []
            
            for word in words:
                if word.isalpha() and len(word) > 4 and random.random() < 0.2:
                    synonyms = []
                    for syn in wordnet.synsets(word):
                        for lemma in syn.lemmas():
                            if lemma.name() != word and '_' not in lemma.name():
                                synonyms.append(lemma.name())
                    
                    if synonyms:
                        replacement = random.choice(synonyms[:3])
                        result.append(replacement)
                    else:
                        result.append(word)
                else:
                    result.append(word)
            
            return " ".join(result)
        else:
            # Enhanced fallback with more replacements
            result = text
            for original, alternatives in self.vocabulary_replacements.items():
                if original.lower() in result.lower():
                    replacement = random.choice(alternatives)
                    pattern = re.compile(re.escape(original), re.IGNORECASE)
                    result = pattern.sub(replacement, result, count=1)
            
            return result

    def adjust_sentence_structure(self, text):
        """Modify sentence structures for more natural flow"""
        sentences = self.split_into_sentences(text)
        modified = []
        
        for sentence in sentences:
            words = sentence.split()
            
            # For long sentences, sometimes break them up
            if len(words) > 20 and random.random() < 0.4:
                # Find a good break point
                break_words = ['and', 'but', 'which', 'that', 'because', 'since', 'while']
                for i, word in enumerate(words[8:18], 8):  # Look in middle section
                    if word.lower() in break_words:
                        part1 = " ".join(words[:i]) + "."
                        part2 = " ".join(words[i+1:])
                        if len(part2) > 5:  # Only if second part is substantial
                            part2 = part2[0].upper() + part2[1:] if part2 else part2
                            modified.extend([part1, part2])
                            break
                else:
                    modified.append(sentence)
            else:
                modified.append(sentence)
        
        return " ".join(modified)

    def clean_and_format(self, text):
        """Clean up the text formatting"""
        # Remove extra spaces
        text = re.sub(r'\s+', ' ', text)
        text = re.sub(r'\s+([.,!?;:])', r'\1', text)
        
        # Fix capitalization
        sentences = self.split_into_sentences(text)
        formatted = []
        
        for sentence in sentences:
            sentence = sentence.strip()
            if sentence:
                # Capitalize first letter
                sentence = sentence[0].upper() + sentence[1:] if len(sentence) > 1 else sentence.upper()
                
                # Ensure proper ending
                if not sentence.endswith(('.', '!', '?')):
                    sentence += '.'
                
                formatted.append(sentence)
        
        return " ".join(formatted)

    def humanize_text(self, text, intensity="medium"):
        """Main humanization function"""
        if not text or len(text.strip()) < 10:
            return "Please enter substantial text to humanize (at least 10 characters)."
        
        result = text.strip()
        
        try:
            # Apply different levels of humanization
            if intensity.lower() in ["light", "low"]:
                # Just vocabulary changes
                result = self.diversify_vocabulary(result)
                
            elif intensity.lower() in ["medium", "moderate"]:
                # Vocabulary + natural flow
                result = self.diversify_vocabulary(result)
                result = self.add_natural_variations(result)
                
            elif intensity.lower() in ["heavy", "high", "maximum"]:
                # All techniques
                result = self.diversify_vocabulary(result)
                result = self.add_natural_variations(result)
                result = self.adjust_sentence_structure(result)
            
            # Always clean up formatting
            result = self.clean_and_format(result)
            
            return result if result and len(result) > 10 else text
            
        except Exception as e:
            print(f"Humanization error: {e}")
            return "Error processing text. Please try again with different input."

class AIDetector:
    def __init__(self):
        """Initialize AI detection patterns and thresholds"""
        self.ai_phrases = [
            "demonstrates significant", "substantial improvements", "comprehensive analysis",
            "furthermore", "moreover", "additionally", "consequently", "therefore",
            "implementation of", "utilization of", "optimization of", "enhancement of",
            "facilitate", "demonstrate", "indicate", "substantial", "comprehensive",
            "significant improvements", "notable enhancements", "effective approach",
            "robust methodology", "systematic approach", "extensive evaluation",
            "empirical results", "experimental validation", "performance metrics",
            "benchmark datasets", "state-of-the-art", "cutting-edge", "novel approach",
            "innovative solution", "groundbreaking", "revolutionary", "paradigm shift"
        ]
        
        self.overused_academic_words = [
            "significant", "substantial", "comprehensive", "extensive", "robust",
            "novel", "innovative", "efficient", "effective", "optimal", "superior",
            "enhanced", "improved", "advanced", "sophisticated", "cutting-edge",
            "state-of-the-art", "groundbreaking", "revolutionary", "paradigm"
        ]
        
        self.excessive_transitions = [
            "furthermore", "moreover", "additionally", "consequently", "therefore",
            "thus", "hence", "nevertheless", "nonetheless", "however"
        ]
        
        self.formal_patterns = [
            r"the implementation of \w+",
            r"the utilization of \w+",
            r"in order to \w+",
            r"it is important to note that",
            r"it should be emphasized that",
            r"it can be observed that",
            r"the results demonstrate that",
            r"the findings indicate that"
        ]

    def calculate_ai_probability(self, text):
        """Calculate the probability that text is AI-generated"""
        if not text or len(text.strip()) < 50:
            return {"probability": 0, "confidence": "Low", "details": {"error": "Text too short for analysis"}}
        
        scores = {}
        
        # Various AI detection checks
        scores['ai_phrases'] = self._check_ai_phrases(text)
        scores['vocab_repetition'] = self._check_vocabulary_repetition(text)
        scores['structure_patterns'] = self._check_structure_patterns(text)
        scores['transition_overuse'] = self._check_transition_overuse(text)
        scores['formal_patterns'] = self._check_formal_patterns(text)
        scores['sentence_consistency'] = self._check_sentence_consistency(text)
        scores['readability'] = self._check_readability_patterns(text)
        
        # Calculate weighted final score
        weights = {
            'ai_phrases': 0.2, 'vocab_repetition': 0.15, 'structure_patterns': 0.15,
            'transition_overuse': 0.15, 'formal_patterns': 0.15,
            'sentence_consistency': 0.1, 'readability': 0.1
        }
        
        final_score = sum(scores[key] * weights[key] for key in weights)
        final_score = min(100, max(0, final_score))
        
        # Determine confidence level
        if final_score >= 80:
            confidence, verdict = "Very High", "Likely AI-Generated"
        elif final_score >= 60:
            confidence, verdict = "High", "Probably AI-Generated"
        elif final_score >= 40:
            confidence, verdict = "Medium", "Possibly AI-Generated"
        elif final_score >= 20:
            confidence, verdict = "Low", "Probably Human-Written"
        else:
            confidence, verdict = "Very Low", "Likely Human-Written"
        
        return {
            "probability": round(final_score, 1),
            "confidence": confidence,
            "verdict": verdict,
            "details": {k: round(v, 1) for k, v in scores.items()}
        }
    
    def _check_ai_phrases(self, text):
        text_lower = text.lower()
        phrase_count = sum(1 for phrase in self.ai_phrases if phrase in text_lower)
        words = len(text.split())
        return min(100, (phrase_count / words) * 1000 * 10) if words > 0 else 0
    
    def _check_vocabulary_repetition(self, text):
        words = [word.lower().strip('.,!?;:') for word in text.split() if word.isalpha()]
        if len(words) < 10:
            return 0
        word_counts = Counter(words)
        overused_count = sum(1 for word in self.overused_academic_words if word_counts.get(word, 0) > 1)
        return min(100, (overused_count / len(self.overused_academic_words)) * 200)
    
    def _check_structure_patterns(self, text):
        if NLTK_AVAILABLE:
            sentences = sent_tokenize(text)
        else:
            sentences = [s.strip() for s in text.split('.') if s.strip()]
        
        if len(sentences) < 3:
            return 0
        
        starters = [s.split()[:3] for s in sentences if len(s.split()) >= 3]
        starter_counts = Counter([' '.join(starter) for starter in starters])
        repeated_starters = sum(1 for count in starter_counts.values() if count > 1)
        return min(100, (repeated_starters / len(sentences)) * 150) if sentences else 0
    
    def _check_transition_overuse(self, text):
        text_lower = text.lower()
        transition_count = sum(1 for transition in self.excessive_transitions if transition in text_lower)
        words = len(text.split())
        return min(100, (transition_count / words) * 100 * 20) if words > 0 else 0
    
    def _check_formal_patterns(self, text):
        pattern_count = sum(len(re.findall(pattern, text.lower())) for pattern in self.formal_patterns)
        words = len(text.split())
        return min(100, (pattern_count / words) * 1000 * 15) if words > 0 else 0
    
    def _check_sentence_consistency(self, text):
        if NLTK_AVAILABLE:
            sentences = sent_tokenize(text)
        else:
            sentences = [s.strip() for s in text.split('.') if s.strip()]
        
        if len(sentences) < 5:
            return 0
        
        lengths = [len(s.split()) for s in sentences]
        avg_length = sum(lengths) / len(lengths)
        variance = sum((length - avg_length) ** 2 for length in lengths) / len(lengths)
        std_dev = math.sqrt(variance)
        consistency_score = 100 - min(100, std_dev * 10)
        return max(0, consistency_score - 20)
    
    def _check_readability_patterns(self, text):
        try:
            words = text.split()
            sentences = len([s for s in text.split('.') if s.strip()])
            if sentences == 0:
                return 0
            avg_words_per_sentence = len(words) / sentences
            if 15 <= avg_words_per_sentence <= 25:
                return 30
            elif 25 < avg_words_per_sentence <= 35:
                return 50
            else:
                return 10
        except:
            return 0

# Initialize components
humanizer = AdvancedHumanizer()
ai_detector = AIDetector()

def process_text(input_text, humanization_level):
    """Process the input text"""
    return humanizer.humanize_text(input_text, humanization_level)

def detect_ai_text(input_text):
    """Detect if text is AI-generated"""
    if not input_text.strip():
        return "Please enter some text to analyze."
    
    result = ai_detector.calculate_ai_probability(input_text)
    
    return f"""
## πŸ€– AI Detection Analysis

**Overall Assessment:** {result['verdict']}
**AI Probability:** {result['probability']}%
**Confidence Level:** {result['confidence']}

### πŸ“Š Detailed Breakdown:
- **AI Phrases Score:** {result['details']['ai_phrases']}%
- **Vocabulary Repetition:** {result['details']['vocab_repetition']}%
- **Structure Patterns:** {result['details']['structure_patterns']}%
- **Transition Overuse:** {result['details']['transition_overuse']}%
- **Formal Patterns:** {result['details']['formal_patterns']}%
- **Sentence Consistency:** {result['details']['sentence_consistency']}%
- **Readability Score:** {result['details']['readability']}%

### πŸ’‘ Interpretation:
- **0-20%:** Likely human-written with natural variations
- **21-40%:** Possibly AI-generated or heavily edited
- **41-60%:** Probably AI-generated with some humanization
- **61-80%:** Likely AI-generated with minimal editing
- **81-100%:** Very likely raw AI-generated content
"""

def combined_process(text, level):
    """Humanize text and then analyze it"""
    if not text.strip():
        return "Please enter text to process.", "No analysis available."
    
    humanized = process_text(text, level)
    analysis = detect_ai_text(humanized)
    return humanized, analysis

# Create Gradio interface
with gr.Blocks(theme="soft", title="AI Text Humanizer & Detector") as demo:
    gr.Markdown("""
    # πŸ€–βž‘οΈπŸ‘¨ AI Text Humanizer & Detector Pro
    
    **Complete solution for AI text processing - Humanize AND Detect AI-generated content**
    
    Transform robotic AI text into natural, human-like writing, then verify the results with our built-in AI detector.
    
    ⚠️ **Note:** This tool is for educational purposes. Please use responsibly and maintain academic integrity.
    """)
    
    with gr.Tabs():
        # Humanization Tab
        with gr.TabItem("🎭 Text Humanizer"):
            gr.Markdown("### Transform AI text into natural, human-like writing")
            
            with gr.Row():
                with gr.Column():
                    humanize_input = gr.Textbox(
                        lines=10,
                        placeholder="Enter machine-generated or robotic academic text here...",
                        label="Raw Input Text",
                        info="Paste your AI-generated text that needs to be humanized"
                    )
                    
                    humanization_level = gr.Radio(
                        choices=["Light", "Medium", "Heavy"],
                        value="Medium",
                        label="Humanization Level",
                        info="Light: Basic changes | Medium: Vocabulary + flow | Heavy: All techniques"
                    )
                    
                    humanize_btn = gr.Button("πŸš€ Humanize Text", variant="primary", size="lg")
                
                with gr.Column():
                    humanize_output = gr.Textbox(
                        label="Humanized Academic Output",
                        lines=10,
                        show_copy_button=True,
                        info="Copy this natural, human-like text"
                    )
            
            # Examples for humanizer
            gr.Examples(
                examples=[
                    [
                        "The implementation of artificial intelligence algorithms demonstrates significant improvements in computational efficiency and accuracy metrics across various benchmark datasets.",
                        "Medium"
                    ],
                    [
                        "Machine learning models exhibit superior performance characteristics when evaluated against traditional statistical approaches in predictive analytics applications.",
                        "Heavy"
                    ]
                ],
                inputs=[humanize_input, humanization_level],
                outputs=humanize_output
            )
        
        # AI Detection Tab
        with gr.TabItem("πŸ•΅οΈ AI Detector"):
            gr.Markdown("### Analyze text to detect if it's AI-generated")
            
            with gr.Row():
                with gr.Column():
                    detect_input = gr.Textbox(
                        lines=10,
                        placeholder="Paste text here to check if it's AI-generated...",
                        label="Text to Analyze",
                        info="Enter any text to check its AI probability"
                    )
                    
                    detect_btn = gr.Button("πŸ” Analyze Text", variant="secondary", size="lg")
                
                with gr.Column():
                    detect_output = gr.Markdown(
                        label="AI Detection Results",
                        value="Analysis results will appear here..."
                    )
            
            # Examples for detector
            gr.Examples(
                examples=[
                    ["The implementation of machine learning algorithms demonstrates significant improvements in computational efficiency and accuracy metrics across various benchmark datasets. Furthermore, these results indicate substantial enhancements in performance."],
                    ["I love going to the coffee shop on weekends. The barista there makes the best cappuccino I've ever had, and I always end up chatting with other customers about random stuff."],
                    ["The comprehensive analysis reveals that the optimization of neural network architectures facilitates enhanced performance characteristics in predictive analytics applications."]
                ],
                inputs=[detect_input],
                outputs=detect_output
            )
        
        # Combined Analysis Tab
        with gr.TabItem("πŸ”„ Humanize & Test"):
            gr.Markdown("### Humanize text and immediately test the results")
            
            with gr.Column():
                combined_input = gr.Textbox(
                    lines=8,
                    placeholder="Enter AI-generated text to humanize and test...",
                    label="Original AI Text",
                    info="This will be humanized and then tested for AI detection"
                )
                
                combined_level = gr.Radio(
                    choices=["Light", "Medium", "Heavy"],
                    value="Medium",
                    label="Humanization Level"
                )
                
                combined_btn = gr.Button("πŸ”„ Humanize & Analyze", variant="primary", size="lg")
            
            with gr.Row():
                with gr.Column():
                    combined_humanized = gr.Textbox(
                        label="Humanized Text",
                        lines=8,
                        show_copy_button=True
                    )
                
                with gr.Column():
                    combined_analysis = gr.Markdown(
                        label="AI Detection Analysis",
                        value="Analysis will appear here..."
                    )
        
        # Info Tab
        with gr.TabItem("ℹ️ Instructions"):
            gr.Markdown("""
            ### 🎯 How to Use:
            
            **Text Humanizer:**
            1. Paste your AI-generated text
            2. Choose humanization level
            3. Get natural, human-like output
            
            **AI Detector:**
            1. Paste any text
            2. Get detailed AI probability analysis
            3. See breakdown of detection factors
            
            **Combined Mode:**
            1. Humanize and test in one step
            2. Perfect for optimizing results
            3. Iterate until satisfied
            
            ### πŸ”§ Features:
            
            **Humanization Techniques:**
            - βœ… Advanced vocabulary variations
            - βœ… Natural sentence flow enhancement  
            - βœ… Academic tone preservation
            - βœ… Structure diversification
            - βœ… Linguistic pattern breaking
            
            **AI Detection:**
            - πŸ” 7-point analysis system
            - πŸ“Š Detailed scoring breakdown
            - 🎯 Confidence assessment
            - πŸ’‘ Improvement suggestions
            
            ### βš–οΈ Ethical Usage:
            This tool is designed for:
            - βœ… Improving writing quality
            - βœ… Learning natural language patterns
            - βœ… Educational purposes
            - βœ… Understanding AI detection
            
            **Please use responsibly:**
            - 🚫 Don't use for plagiarism
            - 🚫 Don't violate academic policies
            - 🚫 Don't misrepresent authorship
            - βœ… Maintain academic integrity
            """)
    
    # Event handlers
    humanize_btn.click(
        fn=process_text,
        inputs=[humanize_input, humanization_level],
        outputs=humanize_output
    )
    
    detect_btn.click(
        fn=detect_ai_text,
        inputs=[detect_input],
        outputs=detect_output
    )
    
    combined_btn.click(
        fn=combined_process,
        inputs=[combined_input, combined_level],
        outputs=[combined_humanized, combined_analysis]
    )

if __name__ == "__main__":
    demo.launch(
        share=True,  # Enable public sharing
        server_name="0.0.0.0",
        server_port=7860
    )