Spaces:
Sleeping
Sleeping
Commit
Β·
5234a81
1
Parent(s):
59f5880
fix detect
Browse files- app.py +108 -31
- test_detector.py +76 -0
app.py
CHANGED
@@ -192,6 +192,16 @@ class AdvancedAITextDetector:
|
|
192 |
starters = [s.split()[0].lower() for s in sentences if s.split()]
|
193 |
starter_diversity = len(set(starters)) / len(starters) if starters else 0
|
194 |
scores['starter_diversity'] = starter_diversity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
|
196 |
# 2. N-gram analysis
|
197 |
words = text.lower().split()
|
@@ -210,6 +220,7 @@ class AdvancedAITextDetector:
|
|
210 |
|
211 |
# 3. ChatGPT-specific patterns
|
212 |
chatgpt_score = 0
|
|
|
213 |
|
214 |
# Common ChatGPT phrases (weighted by specificity)
|
215 |
high_confidence_phrases = [
|
@@ -229,6 +240,15 @@ class AdvancedAITextDetector:
|
|
229 |
"in other words", "that being said", "that said"
|
230 |
]
|
231 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
text_lower = text.lower()
|
233 |
|
234 |
# Check high confidence phrases
|
@@ -241,6 +261,11 @@ class AdvancedAITextDetector:
|
|
241 |
if phrase in text_lower:
|
242 |
chatgpt_score += 0.08
|
243 |
|
|
|
|
|
|
|
|
|
|
|
244 |
# Check for structured lists (very common in ChatGPT)
|
245 |
has_numbered = bool(re.search(r'\n\s*\d+[\.\)]\s', text))
|
246 |
has_bullets = bool(re.search(r'\n\s*[-β’*]\s', text))
|
@@ -253,13 +278,26 @@ class AdvancedAITextDetector:
|
|
253 |
if has_colons:
|
254 |
chatgpt_score += 0.10
|
255 |
|
256 |
-
# Formal tone indicators
|
257 |
formal_words = ['utilize', 'implement', 'facilitate', 'enhance', 'optimize',
|
258 |
'comprehensive', 'significant', 'substantial', 'various', 'numerous']
|
|
|
|
|
|
|
259 |
formal_count = sum(1 for word in formal_words if word in text_lower)
|
|
|
|
|
260 |
chatgpt_score += min(formal_count * 0.05, 0.25)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
|
262 |
scores['chatgpt_patterns'] = min(chatgpt_score, 1.0)
|
|
|
263 |
|
264 |
# 4. Complexity uniformity (AI has uniform complexity)
|
265 |
if len(sentences) > 2:
|
@@ -283,15 +321,18 @@ class AdvancedAITextDetector:
|
|
283 |
scores['paragraph_consistency'] = 1 - min(para_cv, 1.0)
|
284 |
|
285 |
# Calculate final statistical score
|
286 |
-
# Weight the features based on their importance for
|
287 |
weights = {
|
288 |
-
'chatgpt_patterns': 0.
|
289 |
-
'
|
290 |
-
'
|
291 |
-
'
|
292 |
-
'
|
293 |
-
'
|
294 |
-
'
|
|
|
|
|
|
|
295 |
}
|
296 |
|
297 |
final_score = 0.5 # Start neutral
|
@@ -299,10 +340,19 @@ class AdvancedAITextDetector:
|
|
299 |
if feature in weights:
|
300 |
weight = weights[feature]
|
301 |
if weight < 0:
|
302 |
-
# Inverse relationship
|
303 |
final_score += abs(weight) * (1 - value)
|
304 |
else:
|
|
|
305 |
final_score += weight * value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
306 |
|
307 |
return min(max(final_score, 0), 1), scores
|
308 |
|
@@ -361,28 +411,33 @@ class AdvancedAITextDetector:
|
|
361 |
# Weighted average
|
362 |
final_score = sum(s * w for s, w in zip(all_scores, normalized_weights))
|
363 |
|
364 |
-
#
|
365 |
-
agreement_scores = [s for s in all_scores if s > 0.
|
366 |
if len(agreement_scores) >= 2:
|
367 |
avg_agreement = np.mean(agreement_scores)
|
368 |
-
if avg_agreement > 0.
|
369 |
-
final_score = min(final_score * 1.
|
370 |
-
elif avg_agreement < 0.
|
371 |
-
final_score = max(final_score * 0.
|
|
|
|
|
|
|
|
|
|
|
372 |
else:
|
373 |
final_score = 0.5
|
374 |
|
375 |
-
# 4. Classification with
|
376 |
-
if final_score >= 0.
|
377 |
classification = "AI-Generated (High Confidence)"
|
378 |
confidence = "HIGH"
|
379 |
-
elif final_score >= 0.
|
380 |
classification = "Likely AI-Generated"
|
381 |
confidence = "MEDIUM-HIGH"
|
382 |
-
elif final_score >= 0.
|
383 |
classification = "Uncertain"
|
384 |
confidence = "LOW"
|
385 |
-
elif final_score >= 0.
|
386 |
classification = "Likely Human-Written"
|
387 |
confidence = "MEDIUM"
|
388 |
else:
|
@@ -405,30 +460,30 @@ class AdvancedAITextDetector:
|
|
405 |
"""Create detailed explanation"""
|
406 |
exp = []
|
407 |
|
408 |
-
# Overall assessment
|
409 |
-
if score >= 0.
|
410 |
exp.append("π€ STRONG AI INDICATORS: The text exhibits multiple characteristics typical of AI-generated content.")
|
411 |
-
elif score >= 0.
|
412 |
exp.append("β οΈ PROBABLE AI: Several AI patterns detected, suggesting machine generation.")
|
413 |
-
elif score >= 0.
|
414 |
exp.append("β INCONCLUSIVE: Mixed signals - could be AI-assisted or edited content.")
|
415 |
-
elif score >= 0.
|
416 |
exp.append("βοΈ PROBABLE HUMAN: More human-like characteristics than AI patterns.")
|
417 |
else:
|
418 |
exp.append("π€ STRONG HUMAN INDICATORS: Text shows natural human writing patterns.")
|
419 |
|
420 |
# Model consensus
|
421 |
if model_results:
|
422 |
-
high_ai = [name for name, s in model_results.items() if s > 0.
|
423 |
-
high_human = [name for name, s in model_results.items() if s < 0.
|
424 |
|
425 |
if len(high_ai) >= 2:
|
426 |
exp.append(f"\n\nβ Multiple models detect AI: {', '.join(high_ai)}")
|
427 |
elif len(high_human) >= 2:
|
428 |
exp.append(f"\n\nβ Multiple models detect human writing: {', '.join(high_human)}")
|
429 |
|
430 |
-
#
|
431 |
-
if stat_details.get('chatgpt_patterns', 0) > 0.
|
432 |
exp.append("\n\nβ‘ High density of ChatGPT-style phrases and structures detected")
|
433 |
|
434 |
if stat_details.get('sent_length_std', 1) < 0.3:
|
@@ -436,6 +491,16 @@ class AdvancedAITextDetector:
|
|
436 |
|
437 |
if stat_details.get('trigram_repetition', 0) > 0.1:
|
438 |
exp.append("\nπ Repeated phrase patterns detected")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
439 |
|
440 |
return " ".join(exp)
|
441 |
|
@@ -493,6 +558,7 @@ def analyze_text(text):
|
|
493 |
|
494 |
analysis = result['statistical_analysis']
|
495 |
|
|
|
496 |
if 'chatgpt_patterns' in analysis:
|
497 |
output += f"\n- **ChatGPT Pattern Score:** {analysis['chatgpt_patterns']:.2f}/1.00"
|
498 |
if 'sent_length_std' in analysis:
|
@@ -501,6 +567,14 @@ def analyze_text(text):
|
|
501 |
output += f"\n- **Phrase Repetition:** {analysis['trigram_repetition']:.3f}"
|
502 |
if 'starter_diversity' in analysis:
|
503 |
output += f"\n- **Sentence Starter Diversity:** {analysis['starter_diversity']:.3f}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
504 |
|
505 |
# Visual representation
|
506 |
ai_prob = result['ai_probability']
|
@@ -561,9 +635,12 @@ interface = gr.Interface(
|
|
561 |
# ChatGPT example
|
562 |
["Artificial intelligence has revolutionized numerous industries in recent years. It's important to note that this technology offers both opportunities and challenges. Machine learning algorithms can process vast amounts of data, identify patterns, and make predictions with remarkable accuracy. Furthermore, AI applications span various domains including healthcare, finance, and transportation. However, it's crucial to consider the ethical implications. Issues such as bias in algorithms, job displacement, and privacy concerns require careful consideration. Additionally, the development of AI must be guided by responsible practices. In conclusion, while AI presents tremendous potential for innovation and progress, we must approach its implementation thoughtfully and ethically."],
|
563 |
|
564 |
-
# Human example
|
565 |
["So yesterday I'm at the coffee shop, right? And this guy next to me is having the LOUDEST phone conversation about his crypto investments. Like, dude, we get it, you bought Dogecoin. But here's the thing - he kept saying he was gonna be a millionaire by next week. Next week! I almost choked on my latte. The barista and I made eye contact and we both just tried not to laugh. I mean, good luck to him and all, but maybe don't count those chickens yet? Anyway, that's my coffee shop drama for the week. Still better than working from home where my cat judges me all day."],
|
566 |
|
|
|
|
|
|
|
567 |
# Mixed/edited example
|
568 |
["The impact of social media on society has been profound. Studies show that people spend an average of 2.5 hours daily on social platforms. But honestly, I think it's probably way more than that - I know I'm constantly checking my phone! These platforms have transformed how we communicate, share information, and even how we see ourselves. There are definitely benefits, like staying connected with friends and family across distances. However, we're also seeing rises in anxiety and depression linked to social media use, especially among teenagers. It's a complex issue that deserves our attention."]
|
569 |
],
|
|
|
192 |
starters = [s.split()[0].lower() for s in sentences if s.split()]
|
193 |
starter_diversity = len(set(starters)) / len(starters) if starters else 0
|
194 |
scores['starter_diversity'] = starter_diversity
|
195 |
+
|
196 |
+
# Human writing indicators - sentence length variety
|
197 |
+
short_sentences = sum(1 for length in sent_lengths if length < 8)
|
198 |
+
long_sentences = sum(1 for length in sent_lengths if length > 20)
|
199 |
+
scores['sentence_variety'] = (short_sentences + long_sentences) / len(sent_lengths)
|
200 |
+
|
201 |
+
# Conversational patterns (human indicators)
|
202 |
+
conversational_starters = ['so', 'well', 'actually', 'basically', 'like', 'you know', 'i mean', 'anyway']
|
203 |
+
conv_count = sum(1 for starter in starters if starter in conversational_starters)
|
204 |
+
scores['conversational_patterns'] = conv_count / len(starters) if starters else 0
|
205 |
|
206 |
# 2. N-gram analysis
|
207 |
words = text.lower().split()
|
|
|
220 |
|
221 |
# 3. ChatGPT-specific patterns
|
222 |
chatgpt_score = 0
|
223 |
+
human_score = 0
|
224 |
|
225 |
# Common ChatGPT phrases (weighted by specificity)
|
226 |
high_confidence_phrases = [
|
|
|
240 |
"in other words", "that being said", "that said"
|
241 |
]
|
242 |
|
243 |
+
# Human writing indicators
|
244 |
+
human_indicators = [
|
245 |
+
"i think", "i feel", "i believe", "i guess", "i suppose",
|
246 |
+
"honestly", "frankly", "personally", "in my opinion",
|
247 |
+
"you know", "right", "like", "um", "uh", "well",
|
248 |
+
"actually", "basically", "literally", "totally", "really",
|
249 |
+
"so", "anyway", "btw", "lol", "haha", "omg"
|
250 |
+
]
|
251 |
+
|
252 |
text_lower = text.lower()
|
253 |
|
254 |
# Check high confidence phrases
|
|
|
261 |
if phrase in text_lower:
|
262 |
chatgpt_score += 0.08
|
263 |
|
264 |
+
# Check human indicators
|
265 |
+
for phrase in human_indicators:
|
266 |
+
if phrase in text_lower:
|
267 |
+
human_score += 0.1
|
268 |
+
|
269 |
# Check for structured lists (very common in ChatGPT)
|
270 |
has_numbered = bool(re.search(r'\n\s*\d+[\.\)]\s', text))
|
271 |
has_bullets = bool(re.search(r'\n\s*[-β’*]\s', text))
|
|
|
278 |
if has_colons:
|
279 |
chatgpt_score += 0.10
|
280 |
|
281 |
+
# Formal tone indicators (AI) vs informal (human)
|
282 |
formal_words = ['utilize', 'implement', 'facilitate', 'enhance', 'optimize',
|
283 |
'comprehensive', 'significant', 'substantial', 'various', 'numerous']
|
284 |
+
informal_words = ['gonna', 'wanna', 'gotta', 'kinda', 'sorta', 'yeah', 'nah',
|
285 |
+
'awesome', 'cool', 'sucks', 'crazy', 'insane', 'ridiculous']
|
286 |
+
|
287 |
formal_count = sum(1 for word in formal_words if word in text_lower)
|
288 |
+
informal_count = sum(1 for word in informal_words if word in text_lower)
|
289 |
+
|
290 |
chatgpt_score += min(formal_count * 0.05, 0.25)
|
291 |
+
human_score += min(informal_count * 0.08, 0.3)
|
292 |
+
|
293 |
+
# Contractions and casual language
|
294 |
+
contractions = ['don\'t', 'won\'t', 'can\'t', 'isn\'t', 'aren\'t', 'wasn\'t', 'weren\'t',
|
295 |
+
'i\'m', 'you\'re', 'he\'s', 'she\'s', 'it\'s', 'we\'re', 'they\'re']
|
296 |
+
contraction_count = sum(1 for word in contractions if word in text_lower)
|
297 |
+
human_score += min(contraction_count * 0.05, 0.2)
|
298 |
|
299 |
scores['chatgpt_patterns'] = min(chatgpt_score, 1.0)
|
300 |
+
scores['human_patterns'] = min(human_score, 1.0)
|
301 |
|
302 |
# 4. Complexity uniformity (AI has uniform complexity)
|
303 |
if len(sentences) > 2:
|
|
|
321 |
scores['paragraph_consistency'] = 1 - min(para_cv, 1.0)
|
322 |
|
323 |
# Calculate final statistical score
|
324 |
+
# Weight the features based on their importance for detection
|
325 |
weights = {
|
326 |
+
'chatgpt_patterns': 0.25, # AI patterns
|
327 |
+
'human_patterns': -0.20, # Human patterns (negative weight)
|
328 |
+
'sent_length_std': -0.10, # Lower std = more AI
|
329 |
+
'starter_diversity': -0.08, # Lower diversity = more AI
|
330 |
+
'sentence_variety': -0.12, # More variety = more human
|
331 |
+
'conversational_patterns': -0.15, # More conversational = more human
|
332 |
+
'trigram_repetition': 0.10, # More repetition = more AI
|
333 |
+
'bigram_diversity': -0.08, # Lower diversity = more AI
|
334 |
+
'complexity_variance': -0.08, # Lower variance = more AI
|
335 |
+
'paragraph_consistency': 0.10 # More consistency = more AI
|
336 |
}
|
337 |
|
338 |
final_score = 0.5 # Start neutral
|
|
|
340 |
if feature in weights:
|
341 |
weight = weights[feature]
|
342 |
if weight < 0:
|
343 |
+
# Inverse relationship - human indicators reduce AI score
|
344 |
final_score += abs(weight) * (1 - value)
|
345 |
else:
|
346 |
+
# Direct relationship - AI indicators increase AI score
|
347 |
final_score += weight * value
|
348 |
+
|
349 |
+
# Apply confidence boost for strong human indicators
|
350 |
+
if scores.get('human_patterns', 0) > 0.3 and scores.get('conversational_patterns', 0) > 0.2:
|
351 |
+
final_score *= 0.7 # Reduce AI probability for strong human indicators
|
352 |
+
|
353 |
+
# Apply confidence boost for strong AI indicators
|
354 |
+
if scores.get('chatgpt_patterns', 0) > 0.4 and scores.get('paragraph_consistency', 0) > 0.7:
|
355 |
+
final_score = min(final_score * 1.2, 0.95) # Increase AI probability
|
356 |
|
357 |
return min(max(final_score, 0), 1), scores
|
358 |
|
|
|
411 |
# Weighted average
|
412 |
final_score = sum(s * w for s, w in zip(all_scores, normalized_weights))
|
413 |
|
414 |
+
# Apply model agreement boost with more conservative thresholds
|
415 |
+
agreement_scores = [s for s in all_scores if s > 0.75 or s < 0.25]
|
416 |
if len(agreement_scores) >= 2:
|
417 |
avg_agreement = np.mean(agreement_scores)
|
418 |
+
if avg_agreement > 0.75:
|
419 |
+
final_score = min(final_score * 1.05, 0.90) # More conservative boost
|
420 |
+
elif avg_agreement < 0.25:
|
421 |
+
final_score = max(final_score * 0.95, 0.10) # More conservative reduction
|
422 |
+
|
423 |
+
# Additional human text protection - if statistical analysis strongly suggests human
|
424 |
+
if stat_score < 0.3 and len([s for s in all_scores if s < 0.4]) >= 2:
|
425 |
+
final_score = max(final_score * 0.8, 0.15) # Strong protection for human text
|
426 |
+
|
427 |
else:
|
428 |
final_score = 0.5
|
429 |
|
430 |
+
# 4. Classification with improved thresholds to reduce false positives
|
431 |
+
if final_score >= 0.75:
|
432 |
classification = "AI-Generated (High Confidence)"
|
433 |
confidence = "HIGH"
|
434 |
+
elif final_score >= 0.60:
|
435 |
classification = "Likely AI-Generated"
|
436 |
confidence = "MEDIUM-HIGH"
|
437 |
+
elif final_score >= 0.40:
|
438 |
classification = "Uncertain"
|
439 |
confidence = "LOW"
|
440 |
+
elif final_score >= 0.25:
|
441 |
classification = "Likely Human-Written"
|
442 |
confidence = "MEDIUM"
|
443 |
else:
|
|
|
460 |
"""Create detailed explanation"""
|
461 |
exp = []
|
462 |
|
463 |
+
# Overall assessment with improved thresholds
|
464 |
+
if score >= 0.75:
|
465 |
exp.append("π€ STRONG AI INDICATORS: The text exhibits multiple characteristics typical of AI-generated content.")
|
466 |
+
elif score >= 0.60:
|
467 |
exp.append("β οΈ PROBABLE AI: Several AI patterns detected, suggesting machine generation.")
|
468 |
+
elif score >= 0.40:
|
469 |
exp.append("β INCONCLUSIVE: Mixed signals - could be AI-assisted or edited content.")
|
470 |
+
elif score >= 0.25:
|
471 |
exp.append("βοΈ PROBABLE HUMAN: More human-like characteristics than AI patterns.")
|
472 |
else:
|
473 |
exp.append("π€ STRONG HUMAN INDICATORS: Text shows natural human writing patterns.")
|
474 |
|
475 |
# Model consensus
|
476 |
if model_results:
|
477 |
+
high_ai = [name for name, s in model_results.items() if s > 0.70]
|
478 |
+
high_human = [name for name, s in model_results.items() if s < 0.30]
|
479 |
|
480 |
if len(high_ai) >= 2:
|
481 |
exp.append(f"\n\nβ Multiple models detect AI: {', '.join(high_ai)}")
|
482 |
elif len(high_human) >= 2:
|
483 |
exp.append(f"\n\nβ Multiple models detect human writing: {', '.join(high_human)}")
|
484 |
|
485 |
+
# AI-specific indicators
|
486 |
+
if stat_details.get('chatgpt_patterns', 0) > 0.4:
|
487 |
exp.append("\n\nβ‘ High density of ChatGPT-style phrases and structures detected")
|
488 |
|
489 |
if stat_details.get('sent_length_std', 1) < 0.3:
|
|
|
491 |
|
492 |
if stat_details.get('trigram_repetition', 0) > 0.1:
|
493 |
exp.append("\nπ Repeated phrase patterns detected")
|
494 |
+
|
495 |
+
# Human-specific indicators
|
496 |
+
if stat_details.get('human_patterns', 0) > 0.3:
|
497 |
+
exp.append("\n\n㪠Strong human conversational patterns detected")
|
498 |
+
|
499 |
+
if stat_details.get('conversational_patterns', 0) > 0.2:
|
500 |
+
exp.append("\nπ£οΈ Conversational language and casual expressions found")
|
501 |
+
|
502 |
+
if stat_details.get('sentence_variety', 0) > 0.4:
|
503 |
+
exp.append("\nπ Natural sentence length variation (human characteristic)")
|
504 |
|
505 |
return " ".join(exp)
|
506 |
|
|
|
558 |
|
559 |
analysis = result['statistical_analysis']
|
560 |
|
561 |
+
# AI indicators
|
562 |
if 'chatgpt_patterns' in analysis:
|
563 |
output += f"\n- **ChatGPT Pattern Score:** {analysis['chatgpt_patterns']:.2f}/1.00"
|
564 |
if 'sent_length_std' in analysis:
|
|
|
567 |
output += f"\n- **Phrase Repetition:** {analysis['trigram_repetition']:.3f}"
|
568 |
if 'starter_diversity' in analysis:
|
569 |
output += f"\n- **Sentence Starter Diversity:** {analysis['starter_diversity']:.3f}"
|
570 |
+
|
571 |
+
# Human indicators
|
572 |
+
if 'human_patterns' in analysis:
|
573 |
+
output += f"\n- **Human Pattern Score:** {analysis['human_patterns']:.2f}/1.00"
|
574 |
+
if 'conversational_patterns' in analysis:
|
575 |
+
output += f"\n- **Conversational Patterns:** {analysis['conversational_patterns']:.3f}"
|
576 |
+
if 'sentence_variety' in analysis:
|
577 |
+
output += f"\n- **Sentence Variety:** {analysis['sentence_variety']:.3f} (higher = more human-like)"
|
578 |
|
579 |
# Visual representation
|
580 |
ai_prob = result['ai_probability']
|
|
|
635 |
# ChatGPT example
|
636 |
["Artificial intelligence has revolutionized numerous industries in recent years. It's important to note that this technology offers both opportunities and challenges. Machine learning algorithms can process vast amounts of data, identify patterns, and make predictions with remarkable accuracy. Furthermore, AI applications span various domains including healthcare, finance, and transportation. However, it's crucial to consider the ethical implications. Issues such as bias in algorithms, job displacement, and privacy concerns require careful consideration. Additionally, the development of AI must be guided by responsible practices. In conclusion, while AI presents tremendous potential for innovation and progress, we must approach its implementation thoughtfully and ethically."],
|
637 |
|
638 |
+
# Human example - conversational
|
639 |
["So yesterday I'm at the coffee shop, right? And this guy next to me is having the LOUDEST phone conversation about his crypto investments. Like, dude, we get it, you bought Dogecoin. But here's the thing - he kept saying he was gonna be a millionaire by next week. Next week! I almost choked on my latte. The barista and I made eye contact and we both just tried not to laugh. I mean, good luck to him and all, but maybe don't count those chickens yet? Anyway, that's my coffee shop drama for the week. Still better than working from home where my cat judges me all day."],
|
640 |
|
641 |
+
# Human example - personal reflection
|
642 |
+
["I've been thinking about this whole social media thing lately. You know, I used to post everything - what I ate for breakfast, random thoughts, selfies. But now I'm kinda over it? Like, I still check Instagram and stuff, but I don't feel the need to share every little thing anymore. Maybe I'm getting old, or maybe I just realized that most people don't actually care about my lunch. It's weird how we went from sharing everything to being more private. I think it's actually healthier this way, but I miss the old days sometimes when social media felt more fun and less performative."],
|
643 |
+
|
644 |
# Mixed/edited example
|
645 |
["The impact of social media on society has been profound. Studies show that people spend an average of 2.5 hours daily on social platforms. But honestly, I think it's probably way more than that - I know I'm constantly checking my phone! These platforms have transformed how we communicate, share information, and even how we see ourselves. There are definitely benefits, like staying connected with friends and family across distances. However, we're also seeing rises in anxiety and depression linked to social media use, especially among teenagers. It's a complex issue that deserves our attention."]
|
646 |
],
|
test_detector.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
"""
|
3 |
+
Test script for the improved AI text detector
|
4 |
+
"""
|
5 |
+
|
6 |
+
import sys
|
7 |
+
import os
|
8 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
9 |
+
|
10 |
+
from app import AdvancedAITextDetector
|
11 |
+
|
12 |
+
def test_detector():
|
13 |
+
"""Test the detector with various text samples"""
|
14 |
+
print("Initializing detector...")
|
15 |
+
detector = AdvancedAITextDetector()
|
16 |
+
|
17 |
+
# Test cases
|
18 |
+
test_cases = [
|
19 |
+
{
|
20 |
+
"name": "Human - Conversational",
|
21 |
+
"text": "So yesterday I'm at the coffee shop, right? And this guy next to me is having the LOUDEST phone conversation about his crypto investments. Like, dude, we get it, you bought Dogecoin. But here's the thing - he kept saying he was gonna be a millionaire by next week. Next week! I almost choked on my latte.",
|
22 |
+
"expected": "human"
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"name": "AI - ChatGPT Style",
|
26 |
+
"text": "Artificial intelligence has revolutionized numerous industries in recent years. It's important to note that this technology offers both opportunities and challenges. Machine learning algorithms can process vast amounts of data, identify patterns, and make predictions with remarkable accuracy. Furthermore, AI applications span various domains including healthcare, finance, and transportation.",
|
27 |
+
"expected": "ai"
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"name": "Human - Personal Reflection",
|
31 |
+
"text": "I've been thinking about this whole social media thing lately. You know, I used to post everything - what I ate for breakfast, random thoughts, selfies. But now I'm kinda over it? Like, I still check Instagram and stuff, but I don't feel the need to share every little thing anymore.",
|
32 |
+
"expected": "human"
|
33 |
+
},
|
34 |
+
{
|
35 |
+
"name": "AI - Formal Academic",
|
36 |
+
"text": "The implementation of machine learning algorithms requires careful consideration of various factors. It is essential to understand that these systems must be designed with robust error handling mechanisms. Additionally, the development process should incorporate comprehensive testing protocols to ensure optimal performance.",
|
37 |
+
"expected": "ai"
|
38 |
+
}
|
39 |
+
]
|
40 |
+
|
41 |
+
print("\n" + "="*80)
|
42 |
+
print("TESTING IMPROVED AI TEXT DETECTOR")
|
43 |
+
print("="*80)
|
44 |
+
|
45 |
+
for i, test_case in enumerate(test_cases, 1):
|
46 |
+
print(f"\n{i}. {test_case['name']}")
|
47 |
+
print("-" * 50)
|
48 |
+
|
49 |
+
result = detector.detect(test_case['text'])
|
50 |
+
|
51 |
+
print(f"AI Probability: {result['ai_probability']}%")
|
52 |
+
print(f"Classification: {result['classification']}")
|
53 |
+
print(f"Confidence: {result['confidence']}")
|
54 |
+
|
55 |
+
# Check if result matches expectation
|
56 |
+
ai_prob = result['ai_probability']
|
57 |
+
if test_case['expected'] == 'human' and ai_prob < 40:
|
58 |
+
print("β
CORRECT - Human text correctly identified")
|
59 |
+
elif test_case['expected'] == 'ai' and ai_prob > 60:
|
60 |
+
print("β
CORRECT - AI text correctly identified")
|
61 |
+
else:
|
62 |
+
print("β INCORRECT - Misclassification detected")
|
63 |
+
|
64 |
+
# Show key indicators
|
65 |
+
if 'statistical_analysis' in result:
|
66 |
+
stats = result['statistical_analysis']
|
67 |
+
print(f"Human Patterns: {stats.get('human_patterns', 0):.2f}")
|
68 |
+
print(f"ChatGPT Patterns: {stats.get('chatgpt_patterns', 0):.2f}")
|
69 |
+
print(f"Conversational: {stats.get('conversational_patterns', 0):.2f}")
|
70 |
+
|
71 |
+
print("\n" + "="*80)
|
72 |
+
print("TEST COMPLETE")
|
73 |
+
print("="*80)
|
74 |
+
|
75 |
+
if __name__ == "__main__":
|
76 |
+
test_detector()
|