File size: 1,231 Bytes
e1d1b36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel

from ai_score import PTTScorer

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=False,
    allow_methods=["*"],
    allow_headers=["*"],
)

class Req(BaseModel):
    text: str

# 啟動時只載入一次模型
scorer = PTTScorer()

@app.post("/detect")
def detect(req: Req):
    out = scorer.score(req.text)

    # 給 Chrome extension 用的顯示文字
    label_map = {
        "human_like": "Human-like",
        "maybe_human_like": "Mixed",
        "ai_slop_like": "AI-like",
        "unknown": "Unknown",
    }

    return {
        "label": label_map.get(out["label"], out["label"]),
        "cleanowl_label": out["label"],
        "score": out.get("score", 0),
        "raw": out.get("raw", 0),
        "mean": out.get("mean", 0),
        "var": out.get("var", 0),
        "peak": out.get("peak", 0),
        "len_var": out.get("len_var", 0),
        "continuity": out.get("continuity", 0),
        "punct_ratio": out.get("punct_ratio", 0),
        "formal_punct_ratio": out.get("formal_punct_ratio", 0),
        "tokens": out.get("tokens", [])[:30],
    }