DEV Community

TextSight AI
TextSight AI

Posted on AI-assisted

How to detect AI-generated text in Python (3 lines of code)

Pasting text into a website one essay at a time gets old fast. If you need to run an AI check from a script, here's the short version.

I build TextSight, an AI detector, and we just published a small open-source Python client for our API. It has no dependencies, so the whole thing takes about 3 lines.

Install

pip install textsight
Enter fullscreen mode Exit fullscreen mode

Works on Python 3.8 and up. Package is on PyPI and the code is on GitHub (MIT).

Check a piece of text

from textsight import TextSight

ts = TextSight(api_key="sk_live_...")   # or set TEXTSIGHT_API_KEY
r = ts.detect(open("essay.txt").read())

print(r["verdict"])              # "human", "mixed" or "ai"
print(r["humanization_score"])   # 0-100, higher = more human
Enter fullscreen mode Exit fullscreen mode

The part I actually find useful is the sentence breakdown. A whole-document score doesn't tell you much when only one paragraph was pasted from ChatGPT:

for s in r["sentences"]:
    if s["label"] == "ai":
        print(round(s["score"], 2), s["text"])
Enter fullscreen mode Exit fullscreen mode

Checking a folder of files

If you're a teacher with 40 submissions, or an agency checking writer drafts, you probably want something like this:

from pathlib import Path
from textsight import TextSight

ts = TextSight()
for path in Path("submissions").glob("*.txt"):
    r = ts.score(path.read_text())    # lighter call, no sentence list
    print(f"{path.name}: {r['humanization_score']}")
Enter fullscreen mode Exit fullscreen mode

score() skips the sentence list so it's a bit faster. The client retries on rate limits and short outages by itself, so you don't have to write that loop.

Rewriting what got flagged

Same API can also rewrite the AI-sounding parts:

out = ts.rewrite(text, tone="academic", strength=3, preserve=["Smith (2021)"])
print(out["rewritten"])
Enter fullscreen mode Exit fullscreen mode

Tones are conversational, professional, academic, blog and email. preserve keeps things like citations, names and numbers untouched, which matters a lot for academic text.

Please don't treat the score as proof

This is the bit I care about most. No AI detector is right every time, ours included. Short texts (under ~200 words) and writing from non-native English speakers get flagged more often than they should. We wrote up why detectors get it wrong if you want the details.

Use the score as a reason to look closer, not as a verdict on someone.

Node.js version

There's a JS/TypeScript client in the same repo too. I put together a Node.js guide with an Express example.

Getting a key

You need an API key from app.textsight.ai (Settings, then API Keys). If you just want to check a few texts by hand, the free AI detector works in the browser without any code.

If you try it and something breaks, open an issue on the repo, I read all of them.

Top comments (0)