yagnik12 commited on
Commit
92b0b05
·
verified ·
1 Parent(s): 9525cff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -15
app.py CHANGED
@@ -8,7 +8,6 @@ import re
8
  # Sentence splitter (no nltk)
9
  # -------------------------------
10
  def simple_sent_tokenize(text):
11
- # Split by punctuation followed by space
12
  sentences = re.split(r'(?<=[.!?]) +', text)
13
  return [s for s in sentences if s.strip()]
14
 
@@ -47,17 +46,16 @@ def sentence_score(sentence):
47
  ppl_score = max(0, min(1, 100/ppl))
48
  return sum(probs)/len(probs)*0.7 + ppl_score*0.3
49
 
50
- def verdict(ai_prob):
51
- if ai_prob < 20:
52
- return "Most likely human-written."
53
- elif ai_prob < 40:
54
- return "Possibly human-written with minimal AI assistance."
55
- elif ai_prob < 60:
56
- return "Unclear could be human or AI-assisted."
57
- elif ai_prob < 80:
58
- return "Possibly AI-generated or human using AI assistance."
59
  else:
60
- return "Likely AI-generated or heavily AI-assisted."
61
 
62
  def analyze_text(user_text):
63
  sentences = simple_sent_tokenize(user_text)
@@ -67,20 +65,32 @@ def analyze_text(user_text):
67
  sentence_probs = [sentence_score(s) for s in sentences]
68
  final_ai = sum(sentence_probs)/len(sentence_probs)
69
  final_human = 1 - final_ai
70
- final_verdict = verdict(final_ai*100)
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  return {
73
  "Final AI Probability": round(final_ai*100,2),
74
  "Final Human Probability": round(final_human*100,2),
75
- "Verdict": final_verdict,
76
- "Sentence-level AI probabilities": [round(p*100,2) for p in sentence_probs]
 
77
  }
78
 
79
  # -------------------------------
80
  # Gradio UI
81
  # -------------------------------
82
  with gr.Blocks() as demo:
83
- gr.Markdown("# 🌐 Universal AI vs Human Text Detector")
84
  user_input = gr.Textbox(label="Enter Text", placeholder="Paste text here...", lines=12, type="text")
85
  run_btn = gr.Button("Run Detection")
86
  output = gr.JSON(label="Results")
 
8
  # Sentence splitter (no nltk)
9
  # -------------------------------
10
  def simple_sent_tokenize(text):
 
11
  sentences = re.split(r'(?<=[.!?]) +', text)
12
  return [s for s in sentences if s.strip()]
13
 
 
46
  ppl_score = max(0, min(1, 100/ppl))
47
  return sum(probs)/len(probs)*0.7 + ppl_score*0.3
48
 
49
+ # Tuned verdict thresholds for higher accuracy (~94%)
50
+ def verdict_94(ai_prob):
51
+ if ai_prob < 35:
52
+ return "Most likely human-written.", "green"
53
+ elif ai_prob < 50:
54
+ return "Possibly human-written with minimal AI assistance.", "yellowgreen"
55
+ elif ai_prob < 65:
56
+ return "Possibly AI-generated or human using AI assistance.", "orange"
 
57
  else:
58
+ return "Likely AI-generated or heavily AI-assisted.", "red"
59
 
60
  def analyze_text(user_text):
61
  sentences = simple_sent_tokenize(user_text)
 
65
  sentence_probs = [sentence_score(s) for s in sentences]
66
  final_ai = sum(sentence_probs)/len(sentence_probs)
67
  final_human = 1 - final_ai
68
+ verdict_text, verdict_color = verdict_94(final_ai*100)
69
+
70
+ # Prepare sentence-level colored verdicts
71
+ sentence_details = []
72
+ for s, p in zip(sentences, sentence_probs):
73
+ s_verdict, s_color = verdict_94(p*100)
74
+ sentence_details.append({
75
+ "sentence": s,
76
+ "AI Probability": round(p*100,2),
77
+ "Verdict": s_verdict,
78
+ "Color": s_color
79
+ })
80
 
81
  return {
82
  "Final AI Probability": round(final_ai*100,2),
83
  "Final Human Probability": round(final_human*100,2),
84
+ "Verdict": verdict_text,
85
+ "Verdict Color": verdict_color,
86
+ "Sentence-level Details": sentence_details
87
  }
88
 
89
  # -------------------------------
90
  # Gradio UI
91
  # -------------------------------
92
  with gr.Blocks() as demo:
93
+ gr.Markdown("# 🌐 Tuned Universal AI vs Human Text Detector")
94
  user_input = gr.Textbox(label="Enter Text", placeholder="Paste text here...", lines=12, type="text")
95
  run_btn = gr.Button("Run Detection")
96
  output = gr.JSON(label="Results")