Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,52 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import numpy as np
|
| 5 |
-
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
with torch.no_grad():
|
| 16 |
-
outputs = model(**inputs)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
logits = outputs.logits
|
| 20 |
-
predicted_class_idx = logits.argmax(-1).item()
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
return {
|
| 30 |
"ai_probability": float(ai_probability),
|
| 31 |
"features": features,
|
| 32 |
-
"
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
-
def
|
| 36 |
-
#
|
| 37 |
features = {}
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
# 基本特征
|
| 43 |
-
features["width"] = image.width
|
| 44 |
-
features["height"] = image.height
|
| 45 |
-
features["aspect_ratio"] = image.width / max(1, image.height)
|
| 46 |
-
|
| 47 |
-
# 颜色分析
|
| 48 |
-
if len(img_array.shape) == 3: # 彩色图像
|
| 49 |
-
features["avg_red"] = float(np.mean(img_array[:,:,0]))
|
| 50 |
-
features["avg_green"] = float(np.mean(img_array[:,:,1]))
|
| 51 |
-
features["avg_blue"] = float(np.mean(img_array[:,:,2]))
|
| 52 |
|
| 53 |
return features
|
| 54 |
|
| 55 |
# 创建Gradio界面
|
| 56 |
iface = gr.Interface(
|
| 57 |
-
fn=
|
| 58 |
-
inputs=gr.
|
| 59 |
outputs=gr.JSON(),
|
| 60 |
-
title="AI
|
| 61 |
-
description="
|
| 62 |
)
|
| 63 |
|
| 64 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# 使用公开可用的AI文本检测模型
|
| 5 |
+
# 这个模型专门用于检测AI生成文本
|
| 6 |
+
detector = pipeline("text-classification", model="Xenova/distilbert-base-ai-generated-text-detection")
|
|
|
|
| 7 |
|
| 8 |
+
def detect_ai_text(text):
|
| 9 |
+
if not text or len(text.strip()) < 50:
|
| 10 |
+
return {"error": "文本太短,无法可靠检测"}
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
result = detector(text)
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# 提取结果
|
| 15 |
+
label = result[0]["label"]
|
| 16 |
+
score = result[0]["score"]
|
| 17 |
|
| 18 |
+
# 格式化为人类可读结果
|
| 19 |
+
if "ai" in label.lower(): # AI生成
|
| 20 |
+
ai_probability = score
|
| 21 |
+
else: # 人类撰写
|
| 22 |
+
ai_probability = 1 - score
|
| 23 |
+
|
| 24 |
+
# 分析特征
|
| 25 |
+
features = analyze_text_features(text)
|
| 26 |
|
| 27 |
return {
|
| 28 |
"ai_probability": float(ai_probability),
|
| 29 |
"features": features,
|
| 30 |
+
"confidence": float(score),
|
| 31 |
+
"label": label
|
| 32 |
}
|
| 33 |
|
| 34 |
+
def analyze_text_features(text):
|
| 35 |
+
# 简单文本特征分析
|
| 36 |
features = {}
|
| 37 |
+
features["length"] = len(text)
|
| 38 |
+
features["avg_word_length"] = sum(len(word) for word in text.split()) / max(1, len(text.split()))
|
| 39 |
+
features["unique_words_ratio"] = len(set(text.lower().split())) / max(1, len(text.split()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
return features
|
| 42 |
|
| 43 |
# 创建Gradio界面
|
| 44 |
iface = gr.Interface(
|
| 45 |
+
fn=detect_ai_text,
|
| 46 |
+
inputs=gr.Textbox(lines=10, placeholder="粘贴要检测的文本..."),
|
| 47 |
outputs=gr.JSON(),
|
| 48 |
+
title="AI文本检测API",
|
| 49 |
+
description="检测文本是否由AI生成"
|
| 50 |
)
|
| 51 |
|
| 52 |
iface.launch()
|