Spaces:
Sleeping
Sleeping
File size: 20,553 Bytes
6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 6ef9704 5b7b927 | 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 | import re
import random
import nltk
from typing import List, Dict, Optional
import numpy as np
# Download required NLTK data
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')
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import wordnet
# Try to import optional dependencies with fallbacks
try:
from sentence_transformers import SentenceTransformer
SENTENCE_TRANSFORMERS_AVAILABLE = True
except ImportError as e:
print(f"⚠️ Warning: sentence_transformers not available: {e}")
print("💡 Falling back to basic similarity calculation")
SENTENCE_TRANSFORMERS_AVAILABLE = False
try:
from transformers import pipeline
TRANSFORMERS_AVAILABLE = True
except ImportError as e:
print(f"⚠️ Warning: transformers not available: {e}")
print("💡 Paraphrasing will be disabled")
TRANSFORMERS_AVAILABLE = False
try:
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity as sklearn_cosine_similarity
SKLEARN_AVAILABLE = True
except ImportError as e:
print(f"⚠️ Warning: scikit-learn not available: {e}")
print("💡 Using basic similarity calculation")
SKLEARN_AVAILABLE = False
class AITextHumanizer:
def __init__(self):
"""Initialize the text humanizer with necessary models and data"""
print("Loading AI Text Humanizer...")
# Load sentence transformer for semantic similarity (optional)
self.similarity_model = None
if SENTENCE_TRANSFORMERS_AVAILABLE:
try:
print("📥 Loading sentence transformer...")
self.similarity_model = SentenceTransformer('all-MiniLM-L6-v2')
print("✅ Sentence transformer loaded")
except Exception as e:
print(f"⚠️ Warning: Could not load sentence transformer: {e}")
self.similarity_model = None
# Initialize paraphrasing pipeline (optional)
self.paraphraser = None
if TRANSFORMERS_AVAILABLE:
try:
print("📥 Loading paraphrasing model...")
self.paraphraser = pipeline("text2text-generation",
model="google/flan-t5-small",
max_length=512)
print("✅ Paraphrasing model loaded")
except Exception as e:
print(f"⚠️ Warning: Could not load paraphrasing model: {e}")
self.paraphraser = None
# Fallback TF-IDF vectorizer for similarity
self.tfidf_vectorizer = None
if SKLEARN_AVAILABLE:
self.tfidf_vectorizer = TfidfVectorizer(stop_words='english', ngram_range=(1, 2))
# Formal to casual word mappings
self.formal_to_casual = {
"utilize": "use",
"demonstrate": "show",
"facilitate": "help",
"implement": "do",
"consequently": "so",
"therefore": "so",
"nevertheless": "but",
"furthermore": "also",
"moreover": "also",
"subsequently": "then",
"accordingly": "so",
"regarding": "about",
"concerning": "about",
"pertaining": "about",
"approximately": "about",
"endeavor": "try",
"commence": "start",
"terminate": "end",
"obtain": "get",
"purchase": "buy",
"examine": "look at",
"analyze": "study",
"construct": "build",
"establish": "set up",
"magnitude": "size",
"comprehensive": "complete",
"significant": "big",
"substantial": "large",
"optimal": "best",
"sufficient": "enough",
"prior to": "before",
"in order to": "to",
"due to the fact that": "because",
"at this point in time": "now",
"in the event that": "if",
"it is important to note": "note that",
"it should be emphasized": "remember",
"it is worth mentioning": "by the way",
"it is crucial to understand": "importantly",
}
# Contractions mapping
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",
}
# AI-like transition words
self.ai_transition_words = [
"Furthermore,", "Moreover,", "Additionally,", "Subsequently,",
"Consequently,", "Therefore,", "Nevertheless,", "However,",
"In conclusion,", "To summarize,", "In summary,", "Overall,",
"It is important to note that", "It should be emphasized that",
"It is worth mentioning that", "It is crucial to understand that",
"It is essential to recognize that", "It must be acknowledged that"
]
# Natural alternatives
self.natural_transitions = [
"Also,", "Plus,", "And,", "Then,", "So,", "But,", "Still,",
"Anyway,", "By the way,", "Actually,", "Basically,",
"Look,", "Listen,", "Here's the thing:", "The point is,",
"What's more,", "On top of that,", "Another thing,",
"Now,", "Well,", "You know,", "I mean,", "Honestly,",
]
print("✅ AI Text Humanizer initialized successfully!")
def add_contractions(self, text: str) -> str:
"""Add contractions to make text sound more natural"""
for formal, casual in self.contractions.items():
# Case insensitive replacement but preserve original case
pattern = re.compile(re.escape(formal), re.IGNORECASE)
text = pattern.sub(casual, text)
return text
def replace_formal_words(self, text: str, replacement_rate: float = 0.7) -> str:
"""Replace formal words with casual alternatives"""
# Handle both word-level and phrase-level replacements
text_lower = text.lower()
# First handle multi-word phrases
for formal_phrase, casual_phrase in self.formal_to_casual.items():
if len(formal_phrase.split()) > 1: # Multi-word phrases
pattern = re.compile(re.escape(formal_phrase), re.IGNORECASE)
if random.random() < replacement_rate:
text = pattern.sub(casual_phrase, text)
# Then handle individual words
words = word_tokenize(text)
for i, word in enumerate(words):
word_lower = word.lower()
if word_lower in self.formal_to_casual and len(self.formal_to_casual[word_lower].split()) == 1:
if random.random() < replacement_rate:
# Preserve original case
if word.isupper():
words[i] = self.formal_to_casual[word_lower].upper()
elif word.istitle():
words[i] = self.formal_to_casual[word_lower].title()
else:
words[i] = self.formal_to_casual[word_lower]
# Reconstruct text with proper spacing
result = ""
for i, word in enumerate(words):
if i > 0 and word not in ".,!?;:":
result += " "
result += word
return result
def vary_sentence_structure(self, text: str) -> str:
"""Vary sentence structure to sound more natural"""
sentences = sent_tokenize(text)
varied_sentences = []
for sentence in sentences:
# Sometimes start sentences with connecting words
if random.random() < 0.3:
connectors = ["Well,", "So,", "Now,", "Look,", "Actually,", "Basically,"]
if not any(sentence.startswith(word) for word in connectors):
sentence = random.choice(connectors) + " " + sentence.lower()
# Occasionally break long sentences
if len(sentence.split()) > 20 and random.random() < 0.4:
words = sentence.split()
mid_point = len(words) // 2
# Find a natural break point near the middle
for i in range(max(0, mid_point - 3), min(mid_point + 3, len(words))):
if words[i].rstrip('.,!?;:') in ['and', 'but', 'or', 'so', 'then']:
sentence1 = ' '.join(words[:i+1])
sentence2 = ' '.join(words[i+1:])
if sentence2:
sentence2 = sentence2[0].upper() + sentence2[1:] if len(sentence2) > 1 else sentence2.upper()
varied_sentences.append(sentence1)
sentence = sentence2
break
varied_sentences.append(sentence)
return ' '.join(varied_sentences)
def replace_ai_transitions(self, text: str) -> str:
"""Replace AI-like transition words with natural alternatives"""
for ai_word in self.ai_transition_words:
if ai_word in text:
natural_replacement = random.choice(self.natural_transitions)
text = text.replace(ai_word, natural_replacement, 1) # Replace only first occurrence
return text
def add_natural_imperfections(self, text: str, imperfection_rate: float = 0.1) -> str:
"""Add subtle imperfections to make text more human-like"""
sentences = sent_tokenize(text)
modified_sentences = []
for sentence in sentences:
# Occasionally start with lowercase after punctuation (casual style)
if random.random() < imperfection_rate:
words = sentence.split()
if len(words) > 1 and words[0].lower() in ['and', 'but', 'or', 'so']:
sentence = words[0].lower() + ' ' + ' '.join(words[1:])
# Sometimes use informal punctuation
if random.random() < imperfection_rate:
if sentence.endswith('.'):
# Occasionally remove period for casual feel
sentence = sentence[:-1]
elif not sentence.endswith(('.', '!', '?')):
if random.random() < 0.5:
sentence += '.'
modified_sentences.append(sentence)
return ' '.join(modified_sentences)
def paraphrase_segments(self, text: str, paraphrase_rate: float = 0.3) -> str:
"""Paraphrase some segments using the transformer model"""
if not self.paraphraser:
return text
sentences = sent_tokenize(text)
paraphrased_sentences = []
for sentence in sentences:
if random.random() < paraphrase_rate and len(sentence.split()) > 8:
try:
# Create paraphrase prompt
prompt = f"Rewrite this in a more natural, conversational way: {sentence}"
result = self.paraphraser(prompt, max_length=150, num_return_sequences=1)
paraphrased = result[0]['generated_text']
# Clean up the result
paraphrased = paraphrased.replace(prompt, '').strip()
# Remove quotes if added
paraphrased = paraphrased.strip('"\'')
if paraphrased and len(paraphrased) > 10 and len(paraphrased) < len(sentence) * 2:
paraphrased_sentences.append(paraphrased)
else:
paraphrased_sentences.append(sentence)
except Exception as e:
print(f"⚠️ Paraphrasing failed for sentence: {e}")
paraphrased_sentences.append(sentence)
else:
paraphrased_sentences.append(sentence)
return ' '.join(paraphrased_sentences)
def calculate_similarity_basic(self, text1: str, text2: str) -> float:
"""Basic similarity calculation using word overlap"""
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 calculate_similarity_tfidf(self, text1: str, text2: str) -> float:
"""Calculate similarity using TF-IDF vectors"""
if not SKLEARN_AVAILABLE or not self.tfidf_vectorizer:
return self.calculate_similarity_basic(text1, text2)
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 calculation failed: {e}")
return self.calculate_similarity_basic(text1, text2)
def calculate_similarity(self, text1: str, text2: str) -> float:
"""Calculate semantic similarity between original and humanized text"""
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"⚠️ Sentence transformer similarity failed: {e}")
return self.calculate_similarity_tfidf(text1, text2)
else:
return self.calculate_similarity_tfidf(text1, text2)
def humanize_text(self,
text: str,
style: str = "natural",
intensity: float = 0.7) -> Dict:
"""
Main humanization function
Args:
text: Input text to humanize
style: Style of humanization ('natural', 'casual', 'conversational')
intensity: Intensity of humanization (0.0 to 1.0)
Returns:
Dictionary with humanized text and metadata
"""
if not text.strip():
return {
"original_text": text,
"humanized_text": text,
"similarity_score": 1.0,
"changes_made": [],
"style": style,
"intensity": intensity
}
changes_made = []
humanized_text = text
original_text = text
# Apply transformations based on intensity
if intensity > 0.2:
# Replace AI-like transitions first
before_transitions = humanized_text
humanized_text = self.replace_ai_transitions(humanized_text)
if humanized_text != before_transitions:
changes_made.append("Replaced AI-like transition words")
if intensity > 0.3:
# Replace formal words
before_formal = humanized_text
humanized_text = self.replace_formal_words(humanized_text, intensity * 0.8)
if humanized_text != before_formal:
changes_made.append("Replaced formal words with casual alternatives")
if intensity > 0.4:
# Add contractions
before_contractions = humanized_text
humanized_text = self.add_contractions(humanized_text)
if humanized_text != before_contractions:
changes_made.append("Added contractions")
if intensity > 0.5:
# Vary sentence structure
before_structure = humanized_text
humanized_text = self.vary_sentence_structure(humanized_text)
if humanized_text != before_structure:
changes_made.append("Varied sentence structure")
if intensity > 0.6 and style in ["casual", "conversational"]:
# Add natural imperfections
before_imperfections = humanized_text
humanized_text = self.add_natural_imperfections(humanized_text, intensity * 0.15)
if humanized_text != before_imperfections:
changes_made.append("Added natural imperfections")
if intensity > 0.7 and self.paraphraser:
# Paraphrase some segments
before_paraphrase = humanized_text
humanized_text = self.paraphrase_segments(humanized_text, intensity * 0.3)
if humanized_text != before_paraphrase:
changes_made.append("Paraphrased some segments")
# Calculate similarity
similarity_score = self.calculate_similarity(original_text, humanized_text)
# Ensure similarity is reasonable (between 0.7-1.0 for good humanization)
if similarity_score < 0.5:
print(f"⚠️ Low similarity score ({similarity_score:.3f}), using original text")
humanized_text = original_text
similarity_score = 1.0
changes_made = ["Similarity too low, reverted to original"]
return {
"original_text": original_text,
"humanized_text": humanized_text,
"similarity_score": similarity_score,
"changes_made": changes_made,
"style": style,
"intensity": intensity
}
# Test the humanizer
if __name__ == "__main__":
humanizer = AITextHumanizer()
# Test text
test_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.
Therefore, organizations should implement comprehensive strategies to utilize these
technologies effectively while maintaining quality standards.
"""
print("Original Text:")
print(test_text.strip())
print("\n" + "="*50 + "\n")
result = humanizer.humanize_text(test_text.strip(), style="conversational", intensity=0.8)
print("Humanized Text:")
print(result["humanized_text"])
print(f"\nSimilarity Score: {result['similarity_score']:.3f}")
print(f"Changes Made: {', '.join(result['changes_made']) if result['changes_made'] else 'None'}")
print(f"\nModel Status:")
print(f"- Sentence Transformers: {'✅ Available' if SENTENCE_TRANSFORMERS_AVAILABLE else '❌ Not available'}")
print(f"- Transformers: {'✅ Available' if TRANSFORMERS_AVAILABLE else '❌ Not available'}")
print(f"- Scikit-learn: {'✅ Available' if SKLEARN_AVAILABLE else '❌ Not available'}") |