aaappp7878's picture
Update app.py
7aaa31b verified
raw
history blame
1.51 kB
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()