aaappp7878 commited on
Commit
7aaa31b
·
verified ·
1 Parent(s): aea7f4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -42
app.py CHANGED
@@ -1,64 +1,52 @@
1
  import gradio as gr
2
- import torch
3
- from PIL import Image
4
- import numpy as np
5
- from transformers import AutoFeatureExtractor, AutoModelForImageClassification
6
 
7
- # 加载预训练的AI图像检测器
8
- model_name = "SehwanHong/Stable-Diffusion-Detector"
9
- feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
10
- model = AutoModelForImageClassification.from_pretrained(model_name)
11
 
12
- def detect_ai_image(image):
13
- # 处理图像
14
- inputs = feature_extractor(images=image, return_tensors="pt")
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
- probabilities = torch.nn.functional.softmax(logits, dim=-1)
24
- ai_probability = probabilities[0][1].item() # 假设索引1是AI生成类
25
 
26
- # 分析图像特征
27
- features = analyze_image_features(image)
 
 
 
 
 
 
28
 
29
  return {
30
  "ai_probability": float(ai_probability),
31
  "features": features,
32
- "predicted_class": model.config.id2label[predicted_class_idx]
 
33
  }
34
 
35
- def analyze_image_features(image):
36
- # 简单图像特征分析
37
  features = {}
38
-
39
- # 转换为numpy数组
40
- img_array = np.array(image)
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=detect_ai_image,
58
- inputs=gr.Image(type="pil"),
59
  outputs=gr.JSON(),
60
- title="AI图像检测API",
61
- description="检测图像是否由AI生成"
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()