Spaces:
Sleeping
Sleeping
| import re | |
| import random | |
| import nltk | |
| import numpy as np | |
| from typing import List, Dict, Optional, Tuple | |
| import time | |
| import math | |
| from collections import Counter, defaultdict | |
| import statistics | |
| # Download required NLTK data | |
| def ensure_nltk_data(): | |
| try: | |
| nltk.data.find('tokenizers/punkt') | |
| except LookupError: | |
| nltk.download('punkt', quiet=True) | |
| try: | |
| nltk.data.find('corpora/wordnet') | |
| except LookupError: | |
| nltk.download('wordnet', quiet=True) | |
| try: | |
| nltk.data.find('corpora/omw-1.4') | |
| except LookupError: | |
| nltk.download('omw-1.4', quiet=True) | |
| try: | |
| nltk.data.find('taggers/averaged_perceptron_tagger') | |
| except LookupError: | |
| nltk.download('averaged_perceptron_tagger', quiet=True) | |
| ensure_nltk_data() | |
| from nltk.tokenize import sent_tokenize, word_tokenize | |
| from nltk import pos_tag | |
| from nltk.corpus import wordnet | |
| # Advanced imports with fallbacks | |
| def safe_import_with_detailed_fallback(module_name, component=None, max_retries=2): | |
| """Import with fallbacks and detailed error reporting""" | |
| for attempt in range(max_retries): | |
| try: | |
| if component: | |
| module = __import__(module_name, fromlist=[component]) | |
| return getattr(module, component), True | |
| else: | |
| return __import__(module_name), True | |
| except ImportError as e: | |
| if attempt == max_retries - 1: | |
| print(f"β Could not import {module_name}.{component if component else ''}: {e}") | |
| return None, False | |
| except Exception as e: | |
| print(f"β Error importing {module_name}: {e}") | |
| return None, False | |
| return None, False | |
| # Advanced model imports | |
| print("π§ Loading Advanced AI Text Humanizer...") | |
| SentenceTransformer, SENTENCE_TRANSFORMERS_AVAILABLE = safe_import_with_detailed_fallback('sentence_transformers', 'SentenceTransformer') | |
| pipeline, TRANSFORMERS_AVAILABLE = safe_import_with_detailed_fallback('transformers', 'pipeline') | |
| try: | |
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| from sklearn.metrics.pairwise import cosine_similarity as sklearn_cosine_similarity | |
| SKLEARN_AVAILABLE = True | |
| except ImportError: | |
| SKLEARN_AVAILABLE = False | |
| try: | |
| import torch | |
| TORCH_AVAILABLE = True | |
| except ImportError: | |
| TORCH_AVAILABLE = False | |
| class AdvancedAITextHumanizer: | |
| """ | |
| Advanced AI Text Humanizer based on research from QuillBot, ChatGPT, and BypassGPT | |
| Implements cutting-edge techniques to make AI text undetectable | |
| """ | |
| def __init__(self, enable_gpu=True, aggressive_mode=False): | |
| print("π Initializing Advanced AI Text Humanizer...") | |
| print("π Based on research from QuillBot, BypassGPT, and academic papers") | |
| self.enable_gpu = enable_gpu and TORCH_AVAILABLE | |
| self.aggressive_mode = aggressive_mode | |
| # Initialize advanced models | |
| self._load_advanced_models() | |
| self._initialize_humanization_database() | |
| self._setup_detection_evasion_patterns() | |
| print("β Advanced AI Text Humanizer ready!") | |
| self._print_capabilities() | |
| def _load_advanced_models(self): | |
| """Load advanced NLP models for humanization""" | |
| self.similarity_model = None | |
| self.paraphraser = None | |
| # Load sentence transformer for semantic analysis | |
| if SENTENCE_TRANSFORMERS_AVAILABLE: | |
| try: | |
| print("π₯ Loading advanced similarity model...") | |
| device = 'cuda' if self.enable_gpu and TORCH_AVAILABLE and torch.cuda.is_available() else 'cpu' | |
| self.similarity_model = SentenceTransformer('all-MiniLM-L6-v2', device=device) | |
| print("β Advanced similarity model loaded") | |
| except Exception as e: | |
| print(f"β οΈ Could not load similarity model: {e}") | |
| # Load paraphrasing model | |
| if TRANSFORMERS_AVAILABLE: | |
| try: | |
| print("π₯ Loading advanced paraphrasing model...") | |
| device = 0 if self.enable_gpu and TORCH_AVAILABLE and torch.cuda.is_available() else -1 | |
| self.paraphraser = pipeline( | |
| "text2text-generation", | |
| model="google/flan-t5-base", # Larger model for better quality | |
| device=device, | |
| max_length=512 | |
| ) | |
| print("β Advanced paraphrasing model loaded") | |
| except Exception as e: | |
| print(f"β οΈ Could not load paraphrasing model, trying smaller model: {e}") | |
| try: | |
| self.paraphraser = pipeline( | |
| "text2text-generation", | |
| model="google/flan-t5-small", | |
| device=device, | |
| max_length=512 | |
| ) | |
| print("β Fallback paraphrasing model loaded") | |
| except Exception as e2: | |
| print(f"β οΈ Could not load any paraphrasing model: {e2}") | |
| # Initialize fallback TF-IDF | |
| if SKLEARN_AVAILABLE: | |
| self.tfidf_vectorizer = TfidfVectorizer( | |
| stop_words='english', | |
| ngram_range=(1, 3), | |
| max_features=10000 | |
| ) | |
| else: | |
| self.tfidf_vectorizer = None | |
| def _initialize_humanization_database(self): | |
| """Initialize comprehensive humanization patterns based on research""" | |
| # Extended formal-to-casual mappings (QuillBot style) | |
| self.formal_to_casual = { | |
| # Academic/business formal words | |
| "utilize": ["use", "employ", "apply"], | |
| "demonstrate": ["show", "prove", "reveal", "display"], | |
| "facilitate": ["help", "enable", "assist", "make easier"], | |
| "implement": ["do", "carry out", "execute", "put in place"], | |
| "consequently": ["so", "therefore", "as a result", "thus"], | |
| "furthermore": ["also", "plus", "additionally", "what's more"], | |
| "moreover": ["also", "besides", "furthermore", "on top of that"], | |
| "nevertheless": ["but", "however", "still", "yet"], | |
| "subsequently": ["then", "later", "after that", "next"], | |
| "accordingly": ["so", "therefore", "thus", "hence"], | |
| "regarding": ["about", "concerning", "on", "as for"], | |
| "pertaining": ["about", "related to", "concerning", "regarding"], | |
| "approximately": ["about", "around", "roughly", "nearly"], | |
| "endeavor": ["try", "attempt", "effort", "work"], | |
| "commence": ["start", "begin", "kick off", "get going"], | |
| "terminate": ["end", "stop", "finish", "conclude"], | |
| "obtain": ["get", "acquire", "receive", "secure"], | |
| "purchase": ["buy", "get", "acquire", "pick up"], | |
| "examine": ["look at", "check", "study", "review"], | |
| "analyze": ["study", "examine", "look into", "break down"], | |
| "construct": ["build", "make", "create", "put together"], | |
| "establish": ["set up", "create", "form", "start"], | |
| # Advanced academic terms | |
| "methodology": ["method", "approach", "way", "process"], | |
| "systematic": ["organized", "structured", "methodical", "orderly"], | |
| "comprehensive": ["complete", "thorough", "full", "extensive"], | |
| "significant": ["important", "major", "big", "notable"], | |
| "substantial": ["large", "considerable", "major", "significant"], | |
| "optimal": ["best", "ideal", "perfect", "top"], | |
| "sufficient": ["enough", "adequate", "plenty", "satisfactory"], | |
| "adequate": ["enough", "sufficient", "acceptable", "decent"], | |
| "exceptional": ["amazing", "outstanding", "remarkable", "extraordinary"], | |
| "predominant": ["main", "primary", "chief", "leading"], | |
| "fundamental": ["basic", "essential", "core", "key"], | |
| "essential": ["key", "vital", "crucial", "important"], | |
| "crucial": ["key", "vital", "essential", "critical"], | |
| "paramount": ["most important", "crucial", "vital", "key"], | |
| "imperative": ["essential", "crucial", "vital", "necessary"], | |
| "mandatory": ["required", "necessary", "compulsory", "obligatory"], | |
| # Technical jargon | |
| "optimization": ["improvement", "enhancement", "betterment", "upgrade"], | |
| "enhancement": ["improvement", "upgrade", "boost", "betterment"], | |
| "implementation": ["execution", "carrying out", "putting in place", "doing"], | |
| "utilization": ["use", "usage", "employment", "application"], | |
| "evaluation": ["assessment", "review", "analysis", "examination"], | |
| "assessment": ["evaluation", "review", "analysis", "check"], | |
| "validation": ["confirmation", "verification", "proof", "checking"], | |
| "verification": ["confirmation", "validation", "checking", "proof"], | |
| "consolidation": ["combining", "merging", "uniting", "bringing together"], | |
| "integration": ["combining", "merging", "blending", "bringing together"], | |
| "transformation": ["change", "conversion", "shift", "alteration"], | |
| "modification": ["change", "alteration", "adjustment", "tweak"], | |
| "alteration": ["change", "modification", "adjustment", "shift"] | |
| } | |
| # AI-specific phrase patterns (BypassGPT research) | |
| self.ai_phrases = { | |
| "it's important to note that": ["by the way", "worth mentioning", "interestingly", "note that"], | |
| "it should be emphasized that": ["importantly", "remember", "keep in mind", "crucially"], | |
| "it is worth mentioning that": ["by the way", "also", "incidentally", "note that"], | |
| "it is crucial to understand that": ["importantly", "remember", "you should know", "crucially"], | |
| "from a practical standpoint": ["practically speaking", "in practice", "realistically", "in real terms"], | |
| "from an analytical perspective": ["analytically", "looking at it closely", "from analysis", "examining it"], | |
| "in terms of implementation": ["when implementing", "for implementation", "practically", "in practice"], | |
| "with respect to the aforementioned": ["regarding what was mentioned", "about that", "concerning this", "as for that"], | |
| "as previously mentioned": ["as I said", "like I mentioned", "as noted before", "earlier I said"], | |
| "in light of this": ["because of this", "given this", "considering this", "with this in mind"], | |
| "it is imperative to understand": ["you must understand", "it's crucial to know", "importantly", "you need to know"], | |
| "one must consider": ["you should think about", "consider", "think about", "keep in mind"], | |
| "it is evident that": ["clearly", "obviously", "it's clear that", "you can see that"], | |
| "it can be observed that": ["you can see", "it's clear", "obviously", "evidently"], | |
| "upon careful consideration": ["thinking about it", "considering this", "looking at it closely", "after thinking"], | |
| "in the final analysis": ["ultimately", "in the end", "finally", "when all is said and done"] | |
| } | |
| # Advanced contraction patterns | |
| self.contractions = { | |
| "do not": "don't", "does not": "doesn't", "did not": "didn't", | |
| "will not": "won't", "would not": "wouldn't", "should not": "shouldn't", | |
| "could not": "couldn't", "cannot": "can't", "is not": "isn't", | |
| "are not": "aren't", "was not": "wasn't", "were not": "weren't", | |
| "have not": "haven't", "has not": "hasn't", "had not": "hadn't", | |
| "I am": "I'm", "you are": "you're", "he is": "he's", "she is": "she's", | |
| "it is": "it's", "we are": "we're", "they are": "they're", | |
| "I have": "I've", "you have": "you've", "we have": "we've", | |
| "they have": "they've", "I will": "I'll", "you will": "you'll", | |
| "he will": "he'll", "she will": "she'll", "it will": "it'll", | |
| "we will": "we'll", "they will": "they'll", | |
| "would have": "would've", "should have": "should've", | |
| "could have": "could've", "might have": "might've", | |
| "must have": "must've", "need not": "needn't", | |
| "ought not": "oughtn't", "dare not": "daren't" | |
| } | |
| # Human-like transition words | |
| self.human_transitions = [ | |
| "Look,", "Listen,", "Here's the thing:", "You know what?", | |
| "Actually,", "Honestly,", "Frankly,", "To be honest,", | |
| "In my opinion,", "I think", "I believe", "It seems to me", | |
| "From what I can tell,", "As I see it,", "The way I look at it,", | |
| "Let me put it this way:", "Here's what I mean:", "In other words,", | |
| "What I'm saying is,", "The point is,", "Bottom line,", | |
| "At the end of the day,", "When it comes down to it,", | |
| "The truth is,", "Real talk,", "Between you and me,", | |
| "If you ask me,", "In my experience,", "From my perspective," | |
| ] | |
| # Sentence starters that add personality | |
| self.personality_starters = [ | |
| "You know,", "I mean,", "Well,", "So,", "Now,", "Look,", | |
| "Listen,", "Hey,", "Sure,", "Yeah,", "Okay,", "Right,", | |
| "Basically,", "Essentially,", "Obviously,", "Clearly,", | |
| "Apparently,", "Surprisingly,", "Interestingly,", "Funny thing is," | |
| ] | |
| # Filler words and natural imperfections | |
| self.filler_words = [ | |
| "like", "you know", "I mean", "sort of", "kind of", | |
| "basically", "actually", "literally", "really", "pretty much", | |
| "more or less", "somewhat", "rather", "quite", "fairly" | |
| ] | |
| def _setup_detection_evasion_patterns(self): | |
| """Setup patterns to evade AI detection based on research""" | |
| # Patterns that trigger AI detection (to avoid) | |
| self.ai_detection_triggers = { | |
| 'repetitive_sentence_structure': r'^(The|This|It|That)\s+\w+\s+(is|are|was|were)\s+', | |
| 'overuse_of_furthermore': r'\b(Furthermore|Moreover|Additionally|Subsequently|Consequently)\b', | |
| 'perfect_grammar': r'^\s*[A-Z][^.!?]*[.!?]\s*$', | |
| 'uniform_sentence_length': True, # Check programmatically | |
| 'lack_of_contractions': True, # Check programmatically | |
| 'overuse_of_passive_voice': r'\b(is|are|was|were|been|being)\s+\w+ed\b', | |
| 'technical_jargon_clusters': True, # Check programmatically | |
| 'lack_of_personality': True # Check programmatically | |
| } | |
| # Burstiness patterns (sentence length variation) | |
| self.burstiness_targets = { | |
| 'short_sentence_ratio': 0.3, # 30% short sentences (1-10 words) | |
| 'medium_sentence_ratio': 0.5, # 50% medium sentences (11-20 words) | |
| 'long_sentence_ratio': 0.2 # 20% long sentences (21+ words) | |
| } | |
| # Perplexity enhancement techniques | |
| self.perplexity_enhancers = [ | |
| 'unexpected_word_choices', | |
| 'colloquial_expressions', | |
| 'regional_variations', | |
| 'emotional_language', | |
| 'metaphors_and_analogies' | |
| ] | |
| def calculate_perplexity(self, text: str) -> float: | |
| """Calculate text perplexity (predictability measure)""" | |
| words = word_tokenize(text.lower()) | |
| if len(words) < 2: | |
| return 1.0 | |
| # Simple n-gram based perplexity calculation | |
| word_counts = Counter(words) | |
| total_words = len(words) | |
| # Calculate probability of each word | |
| perplexity_sum = 0 | |
| for i, word in enumerate(words[1:], 1): | |
| prev_word = words[i-1] | |
| # Probability based on frequency | |
| prob = word_counts[word] / total_words | |
| if prob > 0: | |
| perplexity_sum += -math.log2(prob) | |
| return perplexity_sum / len(words) if words else 1.0 | |
| def calculate_burstiness(self, text: str) -> float: | |
| """Calculate text burstiness (sentence length variation)""" | |
| sentences = sent_tokenize(text) | |
| if len(sentences) < 2: | |
| return 0.0 | |
| # Calculate sentence lengths | |
| lengths = [len(word_tokenize(sent)) for sent in sentences] | |
| # Calculate coefficient of variation (std dev / mean) | |
| mean_length = statistics.mean(lengths) | |
| if mean_length == 0: | |
| return 0.0 | |
| std_dev = statistics.stdev(lengths) if len(lengths) > 1 else 0 | |
| burstiness = std_dev / mean_length | |
| return burstiness | |
| def enhance_perplexity(self, text: str, intensity: float = 0.3) -> str: | |
| """Enhance text perplexity by adding unexpected elements""" | |
| sentences = sent_tokenize(text) | |
| enhanced_sentences = [] | |
| for sentence in sentences: | |
| if random.random() < intensity: | |
| # Add unexpected elements | |
| words = word_tokenize(sentence) | |
| # Occasionally add filler words | |
| if len(words) > 5 and random.random() < 0.4: | |
| insert_pos = random.randint(1, len(words)-1) | |
| filler = random.choice(self.filler_words) | |
| words.insert(insert_pos, filler) | |
| # Occasionally use unexpected synonyms | |
| if random.random() < 0.3: | |
| for i, word in enumerate(words): | |
| if word.lower() in self.formal_to_casual: | |
| alternatives = self.formal_to_casual[word.lower()] | |
| words[i] = random.choice(alternatives) | |
| sentence = ' '.join(words) | |
| enhanced_sentences.append(sentence) | |
| return ' '.join(enhanced_sentences) | |
| def enhance_burstiness(self, text: str, intensity: float = 0.7) -> str: | |
| """Enhance text burstiness by varying sentence structure""" | |
| sentences = sent_tokenize(text) | |
| enhanced_sentences = [] | |
| for i, sentence in enumerate(sentences): | |
| words = word_tokenize(sentence) | |
| # Determine target sentence type based on position and randomness | |
| if random.random() < 0.3: # Short sentence | |
| # Break long sentences or keep short ones | |
| if len(words) > 15: | |
| # Find a natural break point | |
| break_points = [j for j, word in enumerate(words) | |
| if word.lower() in ['and', 'but', 'or', 'so', 'because', 'when', 'where', 'which']] | |
| if break_points: | |
| break_point = random.choice(break_points) | |
| first_part = ' '.join(words[:break_point]) | |
| second_part = ' '.join(words[break_point+1:]) | |
| if second_part: | |
| second_part = second_part[0].upper() + second_part[1:] if len(second_part) > 1 else second_part.upper() | |
| enhanced_sentences.append(first_part + '.') | |
| sentence = second_part | |
| elif random.random() < 0.2: # Very short sentence for emphasis | |
| if len(words) > 8: | |
| # Create a short, punchy version | |
| key_words = [w for w in words if w.lower() not in ['the', 'a', 'an', 'is', 'are', 'was', 'were']][:4] | |
| sentence = ' '.join(key_words) + '.' | |
| # Add personality starters occasionally | |
| if random.random() < intensity * 0.3: | |
| starter = random.choice(self.personality_starters) | |
| sentence = starter + ' ' + sentence.lower() | |
| enhanced_sentences.append(sentence) | |
| return ' '.join(enhanced_sentences) | |
| def apply_advanced_word_replacement(self, text: str, intensity: float = 0.8) -> str: | |
| """Apply advanced word replacement using multiple strategies""" | |
| words = word_tokenize(text) | |
| modified_words = [] | |
| for i, word in enumerate(words): | |
| word_lower = word.lower().strip('.,!?;:"') | |
| replaced = False | |
| # Strategy 1: Direct formal-to-casual mapping | |
| if word_lower in self.formal_to_casual and random.random() < intensity: | |
| alternatives = self.formal_to_casual[word_lower] | |
| replacement = random.choice(alternatives) | |
| # Preserve case | |
| if word.isupper(): | |
| replacement = replacement.upper() | |
| elif word.istitle(): | |
| replacement = replacement.title() | |
| modified_words.append(replacement) | |
| replaced = True | |
| # Strategy 2: Contextual synonym replacement using WordNet | |
| elif not replaced and len(word) > 4 and random.random() < intensity * 0.4: | |
| try: | |
| synsets = wordnet.synsets(word_lower) | |
| if synsets: | |
| # Get synonyms | |
| synonyms = [] | |
| for syn in synsets[:2]: # Check first 2 synsets | |
| for lemma in syn.lemmas(): | |
| synonym = lemma.name().replace('_', ' ') | |
| if synonym != word_lower and len(synonym) <= len(word) + 3: | |
| synonyms.append(synonym) | |
| if synonyms: | |
| replacement = random.choice(synonyms) | |
| if word.isupper(): | |
| replacement = replacement.upper() | |
| elif word.istitle(): | |
| replacement = replacement.title() | |
| modified_words.append(replacement) | |
| replaced = True | |
| except: | |
| pass | |
| if not replaced: | |
| modified_words.append(word) | |
| # Reconstruct text with proper spacing | |
| result = "" | |
| for i, word in enumerate(modified_words): | |
| if i > 0 and word not in ".,!?;:\"')": | |
| result += " " | |
| result += word | |
| return result | |
| def apply_advanced_contractions(self, text: str, intensity: float = 0.8) -> str: | |
| """Apply contractions with natural frequency""" | |
| # Sort contractions by length (longest first) | |
| sorted_contractions = sorted(self.contractions.items(), key=lambda x: len(x[0]), reverse=True) | |
| for formal, contracted in sorted_contractions: | |
| if random.random() < intensity: | |
| # Use word boundaries for accurate replacement | |
| pattern = r'\b' + re.escape(formal) + r'\b' | |
| text = re.sub(pattern, contracted, text, flags=re.IGNORECASE) | |
| return text | |
| def replace_ai_phrases(self, text: str, intensity: float = 0.9) -> str: | |
| """Replace AI-specific phrases with human alternatives""" | |
| for ai_phrase, alternatives in self.ai_phrases.items(): | |
| if ai_phrase in text.lower(): | |
| if random.random() < intensity: | |
| replacement = random.choice(alternatives) | |
| # Preserve case of first letter | |
| if ai_phrase[0].isupper() or text.find(ai_phrase.title()) != -1: | |
| replacement = replacement.capitalize() | |
| text = text.replace(ai_phrase, replacement) | |
| text = text.replace(ai_phrase.title(), replacement.title()) | |
| text = text.replace(ai_phrase.upper(), replacement.upper()) | |
| return text | |
| def add_natural_imperfections(self, text: str, intensity: float = 0.2) -> str: | |
| """Add subtle imperfections that humans naturally make""" | |
| sentences = sent_tokenize(text) | |
| imperfect_sentences = [] | |
| for sentence in sentences: | |
| if random.random() < intensity: | |
| # Type of imperfection to add | |
| imperfection_type = random.choice([ | |
| 'start_with_conjunction', | |
| 'end_without_period', | |
| 'add_hesitation', | |
| 'use_incomplete_thought' | |
| ]) | |
| if imperfection_type == 'start_with_conjunction': | |
| conjunctions = ['And', 'But', 'Or', 'So', 'Yet'] | |
| if not sentence.split()[0] in conjunctions: | |
| sentence = random.choice(conjunctions) + ' ' + sentence.lower() | |
| elif imperfection_type == 'end_without_period': | |
| if sentence.endswith('.'): | |
| sentence = sentence[:-1] | |
| elif imperfection_type == 'add_hesitation': | |
| hesitations = ['um,', 'uh,', 'well,', 'you know,'] | |
| words = sentence.split() | |
| if len(words) > 3: | |
| insert_pos = random.randint(1, len(words)-1) | |
| words.insert(insert_pos, random.choice(hesitations)) | |
| sentence = ' '.join(words) | |
| elif imperfection_type == 'use_incomplete_thought': | |
| if len(sentence.split()) > 10: | |
| sentence = sentence + '... you know what I mean?' | |
| imperfect_sentences.append(sentence) | |
| return ' '.join(imperfect_sentences) | |
| def apply_advanced_paraphrasing(self, text: str, intensity: float = 0.4) -> str: | |
| """Apply advanced paraphrasing using transformer models""" | |
| if not self.paraphraser: | |
| return text | |
| sentences = sent_tokenize(text) | |
| paraphrased_sentences = [] | |
| for sentence in sentences: | |
| if len(sentence.split()) > 8 and random.random() < intensity: | |
| try: | |
| # Multiple paraphrasing strategies | |
| strategies = [ | |
| f"Rewrite this naturally: {sentence}", | |
| f"Make this more conversational: {sentence}", | |
| f"Simplify this: {sentence}", | |
| f"Rephrase casually: {sentence}", | |
| f"Say this differently: {sentence}" | |
| ] | |
| prompt = random.choice(strategies) | |
| result = self.paraphraser( | |
| prompt, | |
| max_length=min(200, len(sentence) + 50), | |
| min_length=max(10, len(sentence) // 2), | |
| num_return_sequences=1, | |
| temperature=0.8, | |
| do_sample=True | |
| ) | |
| paraphrased = result[0]['generated_text'] | |
| paraphrased = paraphrased.replace(prompt, '').strip().strip('"\'') | |
| # Quality checks | |
| if (paraphrased and | |
| len(paraphrased) > 5 and | |
| len(paraphrased) < len(sentence) * 2.5 and | |
| not paraphrased.lower().startswith(('i cannot', 'sorry', 'i can\'t'))): | |
| paraphrased_sentences.append(paraphrased) | |
| else: | |
| paraphrased_sentences.append(sentence) | |
| except Exception as e: | |
| print(f"β οΈ Paraphrasing failed: {e}") | |
| paraphrased_sentences.append(sentence) | |
| else: | |
| paraphrased_sentences.append(sentence) | |
| return ' '.join(paraphrased_sentences) | |
| def calculate_advanced_similarity(self, text1: str, text2: str) -> float: | |
| """Calculate semantic similarity using advanced methods""" | |
| if self.similarity_model: | |
| try: | |
| embeddings1 = self.similarity_model.encode([text1]) | |
| embeddings2 = self.similarity_model.encode([text2]) | |
| similarity = np.dot(embeddings1[0], embeddings2[0]) / ( | |
| np.linalg.norm(embeddings1[0]) * np.linalg.norm(embeddings2[0]) | |
| ) | |
| return float(similarity) | |
| except Exception as e: | |
| print(f"β οΈ Advanced similarity failed: {e}") | |
| # Fallback to TF-IDF | |
| if self.tfidf_vectorizer and SKLEARN_AVAILABLE: | |
| try: | |
| tfidf_matrix = self.tfidf_vectorizer.fit_transform([text1, text2]) | |
| similarity = sklearn_cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])[0][0] | |
| return float(similarity) | |
| except Exception as e: | |
| print(f"β οΈ TF-IDF similarity failed: {e}") | |
| # Basic word overlap similarity | |
| words1 = set(word_tokenize(text1.lower())) | |
| words2 = set(word_tokenize(text2.lower())) | |
| if not words1 or not words2: | |
| return 1.0 if text1 == text2 else 0.0 | |
| intersection = words1.intersection(words2) | |
| union = words1.union(words2) | |
| return len(intersection) / len(union) if union else 1.0 | |
| def humanize_text_advanced(self, | |
| text: str, | |
| style: str = "natural", | |
| intensity: float = 0.8, | |
| bypass_detection: bool = True, | |
| preserve_meaning: bool = True, | |
| quality_threshold: float = 0.7) -> Dict: | |
| """ | |
| Advanced text humanization with cutting-edge techniques | |
| Args: | |
| text: Input text to humanize | |
| style: 'natural', 'casual', 'conversational', 'academic' | |
| intensity: Transformation intensity (0.0 to 1.0) | |
| bypass_detection: Enable AI detection bypass techniques | |
| preserve_meaning: Maintain semantic similarity | |
| quality_threshold: Minimum similarity to preserve | |
| """ | |
| if not text.strip(): | |
| return { | |
| "original_text": text, | |
| "humanized_text": text, | |
| "similarity_score": 1.0, | |
| "perplexity_score": 1.0, | |
| "burstiness_score": 0.0, | |
| "changes_made": [], | |
| "processing_time_ms": 0.0, | |
| "detection_evasion_score": 1.0, | |
| "quality_metrics": {} | |
| } | |
| start_time = time.time() | |
| original_text = text | |
| humanized_text = text | |
| changes_made = [] | |
| # Calculate initial metrics | |
| initial_perplexity = self.calculate_perplexity(text) | |
| initial_burstiness = self.calculate_burstiness(text) | |
| # Phase 1: AI Detection Bypass (if enabled) | |
| if bypass_detection and intensity > 0.2: | |
| # Replace AI-specific phrases first | |
| before_ai_phrases = humanized_text | |
| humanized_text = self.replace_ai_phrases(humanized_text, intensity * 0.9) | |
| if humanized_text != before_ai_phrases: | |
| changes_made.append("Removed AI-specific phrases") | |
| # Phase 2: Advanced Word Replacement | |
| if intensity > 0.3: | |
| before_words = humanized_text | |
| humanized_text = self.apply_advanced_word_replacement(humanized_text, intensity * 0.8) | |
| if humanized_text != before_words: | |
| changes_made.append("Applied advanced word replacement") | |
| # Phase 3: Contraction Enhancement | |
| if intensity > 0.4: | |
| before_contractions = humanized_text | |
| humanized_text = self.apply_advanced_contractions(humanized_text, intensity * 0.7) | |
| if humanized_text != before_contractions: | |
| changes_made.append("Enhanced with natural contractions") | |
| # Phase 4: Perplexity Enhancement | |
| if intensity > 0.5: | |
| before_perplexity = humanized_text | |
| humanized_text = self.enhance_perplexity(humanized_text, intensity * 0.4) | |
| if humanized_text != before_perplexity: | |
| changes_made.append("Enhanced text perplexity") | |
| # Phase 5: Burstiness Enhancement | |
| if intensity > 0.6: | |
| before_burstiness = humanized_text | |
| humanized_text = self.enhance_burstiness(humanized_text, intensity * 0.6) | |
| if humanized_text != before_burstiness: | |
| changes_made.append("Enhanced sentence burstiness") | |
| # Phase 6: Advanced Paraphrasing | |
| if intensity > 0.7 and self.paraphraser: | |
| before_paraphrasing = humanized_text | |
| humanized_text = self.apply_advanced_paraphrasing(humanized_text, intensity * 0.3) | |
| if humanized_text != before_paraphrasing: | |
| changes_made.append("Applied AI-powered paraphrasing") | |
| # Phase 7: Natural Imperfections (for aggressive mode) | |
| if self.aggressive_mode and style in ["casual", "conversational"] and intensity > 0.8: | |
| before_imperfections = humanized_text | |
| humanized_text = self.add_natural_imperfections(humanized_text, intensity * 0.2) | |
| if humanized_text != before_imperfections: | |
| changes_made.append("Added natural imperfections") | |
| # Quality Control | |
| similarity_score = self.calculate_advanced_similarity(original_text, humanized_text) | |
| if preserve_meaning and similarity_score < quality_threshold: | |
| print(f"β οΈ Quality threshold not met (similarity: {similarity_score:.3f})") | |
| humanized_text = original_text | |
| similarity_score = 1.0 | |
| changes_made = ["Quality threshold not met - reverted to original"] | |
| # Calculate final metrics | |
| final_perplexity = self.calculate_perplexity(humanized_text) | |
| final_burstiness = self.calculate_burstiness(humanized_text) | |
| processing_time = (time.time() - start_time) * 1000 | |
| # Calculate detection evasion score | |
| detection_evasion_score = self._calculate_detection_evasion_score( | |
| original_text, humanized_text, changes_made | |
| ) | |
| return { | |
| "original_text": original_text, | |
| "humanized_text": humanized_text, | |
| "similarity_score": similarity_score, | |
| "perplexity_score": final_perplexity, | |
| "burstiness_score": final_burstiness, | |
| "changes_made": changes_made, | |
| "processing_time_ms": processing_time, | |
| "detection_evasion_score": detection_evasion_score, | |
| "quality_metrics": { | |
| "perplexity_improvement": final_perplexity - initial_perplexity, | |
| "burstiness_improvement": final_burstiness - initial_burstiness, | |
| "word_count_change": len(humanized_text.split()) - len(original_text.split()), | |
| "character_count_change": len(humanized_text) - len(original_text), | |
| "sentence_count": len(sent_tokenize(humanized_text)) | |
| } | |
| } | |
| def _calculate_detection_evasion_score(self, original: str, humanized: str, changes: List[str]) -> float: | |
| """Calculate how well the text evades AI detection""" | |
| score = 0.0 | |
| # Score based on changes made | |
| if "Removed AI-specific phrases" in changes: | |
| score += 0.25 | |
| if "Enhanced text perplexity" in changes: | |
| score += 0.20 | |
| if "Enhanced sentence burstiness" in changes: | |
| score += 0.20 | |
| if "Applied advanced word replacement" in changes: | |
| score += 0.15 | |
| if "Enhanced with natural contractions" in changes: | |
| score += 0.10 | |
| if "Applied AI-powered paraphrasing" in changes: | |
| score += 0.10 | |
| # Bonus for variety | |
| if len(changes) > 3: | |
| score += 0.1 | |
| return min(1.0, score) | |
| def _print_capabilities(self): | |
| """Print current capabilities""" | |
| print("\nπ ADVANCED HUMANIZER CAPABILITIES:") | |
| print("-" * 45) | |
| print(f"π§ Advanced Similarity: {'β ENABLED' if self.similarity_model else 'β DISABLED'}") | |
| print(f"π€ AI Paraphrasing: {'β ENABLED' if self.paraphraser else 'β DISABLED'}") | |
| print(f"π TF-IDF Fallback: {'β ENABLED' if self.tfidf_vectorizer else 'β DISABLED'}") | |
| print(f"π GPU Acceleration: {'β ENABLED' if self.enable_gpu else 'β DISABLED'}") | |
| print(f"β‘ Aggressive Mode: {'β ENABLED' if self.aggressive_mode else 'β DISABLED'}") | |
| print(f"π― Detection Bypass: β ENABLED") | |
| print(f"π Word Mappings: β ENABLED ({len(self.formal_to_casual)} mappings)") | |
| print(f"π€ AI Phrase Detection: β ENABLED ({len(self.ai_phrases)} patterns)") | |
| print(f"π Perplexity Enhancement: β ENABLED") | |
| print(f"π Burstiness Enhancement: β ENABLED") | |
| # Calculate feature completeness | |
| total_features = 8 | |
| enabled_features = sum([ | |
| bool(self.similarity_model), | |
| bool(self.paraphraser), | |
| bool(self.tfidf_vectorizer), | |
| True, # Word mappings | |
| True, # AI phrase detection | |
| True, # Perplexity enhancement | |
| True, # Burstiness enhancement | |
| True # Detection bypass | |
| ]) | |
| completeness = (enabled_features / total_features) * 100 | |
| print(f"π― Feature Completeness: {completeness:.1f}%") | |
| if completeness >= 90: | |
| print("π ADVANCED HUMANIZER READY!") | |
| elif completeness >= 70: | |
| print("β οΈ Most features ready - some advanced capabilities limited") | |
| else: | |
| print("β Limited functionality - install additional dependencies") | |
| # Convenience function for backward compatibility | |
| def AITextHumanizer(): | |
| """Factory function for backward compatibility""" | |
| return AdvancedAITextHumanizer() | |
| # Test the advanced humanizer | |
| if __name__ == "__main__": | |
| humanizer = AdvancedAITextHumanizer(aggressive_mode=True) | |
| test_cases = [ | |
| { | |
| "text": "Furthermore, it is important to note that artificial intelligence systems demonstrate significant capabilities in natural language processing tasks. Subsequently, these systems can analyze and generate text with remarkable accuracy. Nevertheless, it is crucial to understand that human oversight remains essential for optimal performance.", | |
| "style": "conversational", | |
| "intensity": 0.9 | |
| }, | |
| { | |
| "text": "The implementation of comprehensive methodologies will facilitate optimization and enhance operational efficiency. Moreover, the utilization of systematic approaches demonstrates substantial improvements in performance metrics. Therefore, organizations should endeavor to establish frameworks that utilize these technologies effectively.", | |
| "style": "casual", | |
| "intensity": 0.8 | |
| } | |
| ] | |
| print("\nπ§ͺ TESTING ADVANCED HUMANIZER") | |
| print("=" * 40) | |
| for i, test_case in enumerate(test_cases, 1): | |
| print(f"\n㪠Test {i}: {test_case['style'].title()} style") | |
| print("-" * 50) | |
| print(f"π Original: {test_case['text'][:100]}...") | |
| result = humanizer.humanize_text_advanced(**test_case) | |
| print(f"β¨ Humanized: {result['humanized_text'][:100]}...") | |
| print(f"π Similarity: {result['similarity_score']:.3f}") | |
| print(f"π― Perplexity: {result['perplexity_score']:.3f}") | |
| print(f"π Burstiness: {result['burstiness_score']:.3f}") | |
| print(f"π‘οΈ Detection Evasion: {result['detection_evasion_score']:.3f}") | |
| print(f"β‘ Processing: {result['processing_time_ms']:.1f}ms") | |
| print(f"π§ Changes: {', '.join(result['changes_made'])}") | |
| print(f"\nπ Advanced testing completed!") | |
| print(f"π This humanizer uses cutting-edge techniques from QuillBot, BypassGPT research!") |