Spaces:
Paused
Paused
File size: 11,636 Bytes
a5bf95a bd3af38 a5bf95a bd3af38 a5bf95a bd3af38 a5bf95a | 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 | """
AI Writing Analyzer — sentence-level heat map for human vs. AI-generated text.
Built for classroom use. Loads a RoBERTa-based ChatGPT detector from
Hugging Face and runs it on each sentence independently, then renders the
input text with per-sentence color coding indicating the probability that
the sentence was AI-generated.
Runs comfortably on the free CPU tier.
"""
import re
import html
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
# ---------------------------------------------------------------------------
# Model
# ---------------------------------------------------------------------------
# Hello-SimpleAI's RoBERTa detector — small, CPU-friendly, widely used.
MODEL_NAME = "Hello-SimpleAI/chatgpt-detector-roberta"
print(f"Loading model: {MODEL_NAME}")
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
model.eval()
# The model's label order: index 0 = Human, index 1 = ChatGPT/AI.
# (Confirmed from the model card's id2label.)
AI_INDEX = 1
# ---------------------------------------------------------------------------
# Sentence splitting
# ---------------------------------------------------------------------------
_SENT_SPLIT_RE = re.compile(r"(?<=[.!?])\s+(?=[A-Z\"'\(\[])")
def split_sentences(text: str):
"""Lightweight sentence splitter — no NLTK download needed on free CPU."""
text = text.strip()
if not text:
return []
# First split on paragraph breaks to preserve structure, then sentences.
chunks = []
for para in re.split(r"\n\s*\n", text):
para = para.strip()
if not para:
continue
parts = _SENT_SPLIT_RE.split(para)
parts = [p.strip() for p in parts if p.strip()]
chunks.extend(parts)
return chunks
# ---------------------------------------------------------------------------
# Scoring
# ---------------------------------------------------------------------------
@torch.no_grad()
def score_sentence(sentence: str) -> float:
"""Return probability that `sentence` is AI-generated (0.0 – 1.0)."""
inputs = tokenizer(
sentence,
return_tensors="pt",
truncation=True,
max_length=512,
)
logits = model(**inputs).logits
probs = torch.softmax(logits, dim=-1)[0]
return float(probs[AI_INDEX].item())
# ---------------------------------------------------------------------------
# Rendering
# ---------------------------------------------------------------------------
def prob_to_color(p: float) -> str:
"""
Map probability 0..1 to a background color.
Low (human) -> cool teal
Mid -> amber
High (AI) -> warm red
"""
# Interpolate between three stops in RGB.
if p < 0.5:
t = p / 0.5
r = int(56 + (245 - 56) * t)
g = int(189 + (191 - 189) * t)
b = int(248 + (66 - 248) * t)
else:
t = (p - 0.5) / 0.5
r = int(245 + (248 - 245) * t)
g = int(191 + (80 - 191) * t)
b = int(66 + (80 - 66) * t)
# Higher opacity for a vivid highlight; text is forced light on top.
return f"rgba({r}, {g}, {b}, 0.42)"
def border_color(p: float) -> str:
if p < 0.5:
t = p / 0.5
r = int(56 + (245 - 56) * t)
g = int(189 + (191 - 189) * t)
b = int(248 + (66 - 248) * t)
else:
t = (p - 0.5) / 0.5
r = int(245 + (248 - 245) * t)
g = int(191 + (80 - 191) * t)
b = int(66 + (80 - 66) * t)
return f"rgba({r}, {g}, {b}, 0.95)"
def render_heatmap(sentences, scores) -> str:
if not sentences:
return (
"<div style='color:#94a3b8; font-style:italic; padding:1rem;'>"
"Paste some writing above and click <b>Analyze</b> to see a "
"sentence-by-sentence breakdown.</div>"
)
pieces = []
for sent, p in zip(sentences, scores):
bg = prob_to_color(p)
bd = border_color(p)
pct = int(round(p * 100))
safe = html.escape(sent)
pieces.append(
f"<span class='awa-sent' title='AI likelihood: {pct}%' "
f"style='background:{bg} !important; "
f"border-bottom:2px solid {bd} !important; "
f"color:#f8fafc !important; "
f"text-shadow:0 1px 2px rgba(0,0,0,0.65); "
f"padding:3px 6px; margin:2px 1px; border-radius:5px; "
f"box-decoration-break:clone; -webkit-box-decoration-break:clone; "
f"line-height:2.3;'>{safe} "
f"<span style='font-size:0.72em; color:#f1f5f9 !important; "
f"font-weight:600; vertical-align:super; "
f"text-shadow:0 1px 2px rgba(0,0,0,0.7);'>{pct}%</span></span>"
)
body = " ".join(pieces)
avg = sum(scores) / len(scores)
verdict, vcolor = classify_overall(avg)
summary = (
f"<div style='display:flex; align-items:center; gap:1rem; "
f"margin-bottom:1.25rem; padding:1rem 1.25rem; "
f"background:#0f172a; border:1px solid #1e293b; border-radius:12px;'>"
f"<div style='font-size:0.78rem; letter-spacing:0.12em; "
f"text-transform:uppercase; color:#94a3b8;'>Overall assessment</div>"
f"<div style='font-size:1.15rem; font-weight:600; color:{vcolor};'>"
f"{verdict}</div>"
f"<div style='margin-left:auto; color:#cbd5e1; font-variant-numeric:tabular-nums;'>"
f"Avg. AI likelihood: <b style='color:#f1f5f9;'>{int(round(avg*100))}%</b> "
f" · Sentences: <b style='color:#f1f5f9;'>{len(sentences)}</b></div>"
f"</div>"
)
legend = (
"<div style='display:flex; gap:0.75rem; align-items:center; "
"margin-top:1.25rem; font-size:0.82rem; color:#94a3b8;'>"
"<span>Legend:</span>"
"<span style='background:rgba(56,189,248,0.28); padding:2px 10px; "
"border-radius:4px; border-bottom:2px solid rgba(56,189,248,0.95);'>Likely human</span>"
"<span style='background:rgba(245,191,66,0.28); padding:2px 10px; "
"border-radius:4px; border-bottom:2px solid rgba(245,191,66,0.95);'>Uncertain</span>"
"<span style='background:rgba(248,80,80,0.28); padding:2px 10px; "
"border-radius:4px; border-bottom:2px solid rgba(248,80,80,0.95);'>Likely AI</span>"
"</div>"
)
return (
f"<div style='font-family: -apple-system, BlinkMacSystemFont, "
f"\"Segoe UI\", Inter, sans-serif; color:#e2e8f0;'>"
f"{summary}"
f"<div style='padding:1.25rem 1.5rem; background:#0b1220; "
f"border:1px solid #1e293b; border-radius:12px; font-size:1rem; "
f"line-height:2.1;'>{body}</div>"
f"{legend}"
f"</div>"
)
def classify_overall(avg: float):
if avg < 0.25:
return "Likely human-written", "#38bdf8"
if avg < 0.5:
return "Leaning human", "#7dd3fc"
if avg < 0.75:
return "Leaning AI", "#fbbf24"
return "Likely AI-generated", "#f87171"
# ---------------------------------------------------------------------------
# Main analyze function
# ---------------------------------------------------------------------------
def analyze(text: str):
if not text or not text.strip():
return render_heatmap([], [])
sentences = split_sentences(text)
if not sentences:
return render_heatmap([], [])
scores = [score_sentence(s) for s in sentences]
return render_heatmap(sentences, scores)
# ---------------------------------------------------------------------------
# UI
# ---------------------------------------------------------------------------
CUSTOM_CSS = """
:root, .gradio-container, body {
background: #060912 !important;
color: #e2e8f0 !important;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, system-ui, sans-serif !important;
}
.gradio-container {
max-width: 960px !important;
margin: 0 auto !important;
padding-top: 2.5rem !important;
}
#app-header {
text-align: left;
margin-bottom: 1.75rem;
padding: 1.75rem 2rem;
background: linear-gradient(135deg, #0f172a 0%, #111827 100%);
border: 1px solid #1e293b;
border-radius: 16px;
}
#app-header h1 {
margin: 0 0 0.5rem 0;
font-size: 1.9rem;
font-weight: 700;
letter-spacing: -0.02em;
background: linear-gradient(90deg, #38bdf8 0%, #a78bfa 100%);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}
#app-header p {
margin: 0;
color: #94a3b8;
font-size: 0.98rem;
line-height: 1.55;
max-width: 62ch;
}
textarea {
background: #0b1220 !important;
border: 1px solid #1e293b !important;
color: #e2e8f0 !important;
border-radius: 12px !important;
font-size: 0.98rem !important;
line-height: 1.6 !important;
}
textarea:focus {
border-color: #38bdf8 !important;
box-shadow: 0 0 0 3px rgba(56,189,248,0.15) !important;
}
label span {
color: #cbd5e1 !important;
font-weight: 500 !important;
}
button.primary, .primary button {
background: linear-gradient(135deg, #38bdf8 0%, #6366f1 100%) !important;
border: none !important;
color: #0b1220 !important;
font-weight: 600 !important;
border-radius: 10px !important;
}
button.secondary, .secondary button {
background: #1e293b !important;
border: 1px solid #334155 !important;
color: #e2e8f0 !important;
border-radius: 10px !important;
}
footer { display: none !important; }
/* Force light text inside our custom HTML output — Gradio 6's prose styles
otherwise darken anything rendered inside gr.HTML. */
.gradio-container .prose,
.gradio-container .prose * ,
.gradio-container .html-container,
.gradio-container .html-container * {
color: #e2e8f0 !important;
}
.gradio-container .awa-sent,
.gradio-container .awa-sent * {
color: #f8fafc !important;
}
"""
HEADER_HTML = """
<div id="app-header">
<h1>AI Writing Analyzer</h1>
<p>A classroom tool for examining student writing sentence by sentence. Paste a
passage below and this tool will highlight each sentence with a color-coded
heat map showing how likely it is to have been generated by an AI model.
Use it as a starting point for conversation — not as a verdict.</p>
</div>
"""
EXAMPLE_TEXT = (
"The old lighthouse had stood on that cliff for nearly two centuries, "
"its white paint worn thin by salt and wind. Every evening, Marta climbed "
"the spiral stairs with a cup of tea balanced in one hand. "
"In conclusion, lighthouses serve as vital navigational aids that have "
"played a crucial role in maritime safety throughout history. "
"Furthermore, they represent an important cultural and architectural heritage "
"that must be preserved for future generations."
)
with gr.Blocks(css=CUSTOM_CSS, title="AI Writing Analyzer", theme=gr.themes.Base()) as demo:
gr.HTML(HEADER_HTML)
with gr.Row():
input_box = gr.Textbox(
label="Student writing",
placeholder="Paste a passage of writing here…",
lines=10,
value=EXAMPLE_TEXT,
)
with gr.Row():
analyze_btn = gr.Button("Analyze", variant="primary")
clear_btn = gr.Button("Clear", variant="secondary")
output = gr.HTML(value=render_heatmap([], []))
analyze_btn.click(fn=analyze, inputs=input_box, outputs=output)
clear_btn.click(
fn=lambda: ("", render_heatmap([], [])),
inputs=None,
outputs=[input_box, output],
)
if __name__ == "__main__":
demo.launch() |