File size: 1,509 Bytes
5d40c09
7aaa31b
5d40c09
7aaa31b
 
 
5d40c09
7aaa31b
 
 
5d40c09
7aaa31b
5d40c09
7aaa31b
 
 
5d40c09
7aaa31b
 
 
 
 
 
 
 
5d40c09
 
 
 
7aaa31b
 
5d40c09
 
7aaa31b
 
5d40c09
7aaa31b
 
 
5d40c09
 
 
 
 
7aaa31b
 
5d40c09
7aaa31b
 
5d40c09
 
 
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
import gradio as gr
from transformers import pipeline

# 使用公开可用的AI文本检测模型
# 这个模型专门用于检测AI生成文本
detector = pipeline("text-classification", model="Xenova/distilbert-base-ai-generated-text-detection")

def detect_ai_text(text):
    if not text or len(text.strip()) < 50:
        return {"error": "文本太短,无法可靠检测"}
    
    result = detector(text)
    
    # 提取结果
    label = result[0]["label"]
    score = result[0]["score"]
    
    # 格式化为人类可读结果
    if "ai" in label.lower():  # AI生成
        ai_probability = score
    else:  # 人类撰写
        ai_probability = 1 - score
    
    # 分析特征
    features = analyze_text_features(text)
    
    return {
        "ai_probability": float(ai_probability),
        "features": features,
        "confidence": float(score),
        "label": label
    }

def analyze_text_features(text):
    # 简单文本特征分析
    features = {}
    features["length"] = len(text)
    features["avg_word_length"] = sum(len(word) for word in text.split()) / max(1, len(text.split()))
    features["unique_words_ratio"] = len(set(text.lower().split())) / max(1, len(text.split()))
    
    return features

# 创建Gradio界面
iface = gr.Interface(
    fn=detect_ai_text,
    inputs=gr.Textbox(lines=10, placeholder="粘贴要检测的文本..."),
    outputs=gr.JSON(),
    title="AI文本检测API",
    description="检测文本是否由AI生成"
)

iface.launch()