VictorM-Coder commited on
Commit
370410f
·
verified ·
1 Parent(s): 91f4244

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -7
app.py CHANGED
@@ -60,13 +60,14 @@ def sentence_split(text: str):
60
 
61
  # -----------------------------
62
  # CLASSIFY SENTENCE-BY-SENTENCE (Fakespot: id2label[1] == "AI")
 
63
  # -----------------------------
64
  def classify_sentence_by_sentence(text, threshold=0.70, max_len=512):
65
  sents = sentence_split(text)
66
  if not sents:
67
- return "⚠️ Please paste some text.", None, None
68
 
69
- # (Optional) light cleaning similar to model card guidance
70
  clean_sents = [re.sub(r"\s+", " ", s).strip() for s in sents]
71
 
72
  inputs = tokenizer(
@@ -77,10 +78,15 @@ def classify_sentence_by_sentence(text, threshold=0.70, max_len=512):
77
  logits = model(**inputs).logits
78
  probs = F.softmax(logits, dim=-1) # [:,0]=Human, [:,1]=AI
79
 
 
 
 
 
 
80
  rows = []
81
  highlights = []
82
  for i, orig in enumerate(sents, start=1):
83
- ai_p = float(probs[i-1, 1].item()) # Fakespot: 1 == AI
84
  label = "AI" if ai_p >= threshold else "Human"
85
  pct = f"{ai_p*100:.1f}%"
86
 
@@ -101,10 +107,12 @@ def classify_sentence_by_sentence(text, threshold=0.70, max_len=512):
101
 
102
  html = "\n".join(highlights)
103
  df = pd.DataFrame(rows, columns=["#", "Sentence", "AI_Prob", "Label"])
104
- return "Done ✅ (Fakespot detector)", html, df
 
 
105
 
106
  # -----------------------------
107
- # GRADIO UI (minimal)
108
  # -----------------------------
109
  with gr.Blocks() as demo:
110
  gr.Markdown("### 🧠 Sentence-by-Sentence AI Check (Fakespot)")
@@ -114,11 +122,16 @@ with gr.Blocks() as demo:
114
  btn = gr.Button("Analyze")
115
 
116
  status = gr.Label(label="Status")
 
 
117
  highlights = gr.HTML(label="Per-Sentence Highlights")
118
  table = gr.Dataframe(headers=["#", "Sentence", "AI_Prob", "Label"], wrap=True)
119
 
120
- btn.click(classify_sentence_by_sentence, inputs=[text_input, threshold],
121
- outputs=[status, highlights, table])
 
 
 
122
 
123
  if __name__ == "__main__":
124
  demo.launch()
 
60
 
61
  # -----------------------------
62
  # CLASSIFY SENTENCE-BY-SENTENCE (Fakespot: id2label[1] == "AI")
63
+ # Also compute an overall Likely-AI score (mean AI prob)
64
  # -----------------------------
65
  def classify_sentence_by_sentence(text, threshold=0.70, max_len=512):
66
  sents = sentence_split(text)
67
  if not sents:
68
+ return "⚠️ Please paste some text.", "", "", None, None
69
 
70
+ # light cleaning similar to model card guidance
71
  clean_sents = [re.sub(r"\s+", " ", s).strip() for s in sents]
72
 
73
  inputs = tokenizer(
 
78
  logits = model(**inputs).logits
79
  probs = F.softmax(logits, dim=-1) # [:,0]=Human, [:,1]=AI
80
 
81
+ ai_probs = probs[:, 1].detach().cpu().tolist()
82
+ overall_ai = sum(ai_probs) / len(ai_probs) # mean AI probability across sentences
83
+ overall_pct = f"{overall_ai * 100:.1f}%"
84
+ overall_label = "🤖 Likely AI" if overall_ai >= threshold else "🧒 Likely Human"
85
+
86
  rows = []
87
  highlights = []
88
  for i, orig in enumerate(sents, start=1):
89
+ ai_p = float(ai_probs[i-1]) # Fakespot: 1 == AI
90
  label = "AI" if ai_p >= threshold else "Human"
91
  pct = f"{ai_p*100:.1f}%"
92
 
 
107
 
108
  html = "\n".join(highlights)
109
  df = pd.DataFrame(rows, columns=["#", "Sentence", "AI_Prob", "Label"])
110
+
111
+ status = "Done ✅ (Fakespot detector)"
112
+ return status, overall_label, overall_pct, html, df
113
 
114
  # -----------------------------
115
+ # GRADIO UI (minimal, with overall score)
116
  # -----------------------------
117
  with gr.Blocks() as demo:
118
  gr.Markdown("### 🧠 Sentence-by-Sentence AI Check (Fakespot)")
 
122
  btn = gr.Button("Analyze")
123
 
124
  status = gr.Label(label="Status")
125
+ overall_label = gr.Label(label="Overall Verdict")
126
+ overall_score = gr.Label(label="Likely AI-generated Score (avg)")
127
  highlights = gr.HTML(label="Per-Sentence Highlights")
128
  table = gr.Dataframe(headers=["#", "Sentence", "AI_Prob", "Label"], wrap=True)
129
 
130
+ btn.click(
131
+ classify_sentence_by_sentence,
132
+ inputs=[text_input, threshold],
133
+ outputs=[status, overall_label, overall_score, highlights, table]
134
+ )
135
 
136
  if __name__ == "__main__":
137
  demo.launch()