Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,16 +2,16 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
-
from transformers import
|
6 |
|
7 |
-
#
|
8 |
-
model_name = "
|
9 |
-
|
10 |
model = AutoModelForImageClassification.from_pretrained(model_name)
|
11 |
|
12 |
def detect_ai_image(image):
|
13 |
# 处理图像
|
14 |
-
inputs =
|
15 |
with torch.no_grad():
|
16 |
outputs = model(**inputs)
|
17 |
|
@@ -21,7 +21,11 @@ def detect_ai_image(image):
|
|
21 |
|
22 |
# 获取概率
|
23 |
probabilities = torch.nn.functional.softmax(logits, dim=-1)
|
24 |
-
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# 分析图像特征
|
27 |
features = analyze_image_features(image)
|
@@ -62,4 +66,3 @@ iface = gr.Interface(
|
|
62 |
)
|
63 |
|
64 |
iface.launch()
|
65 |
-
|
|
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
6 |
|
7 |
+
# 使用专门的AI图像检测模型
|
8 |
+
model_name = "umm-maybe/AI-image-detector"
|
9 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
10 |
model = AutoModelForImageClassification.from_pretrained(model_name)
|
11 |
|
12 |
def detect_ai_image(image):
|
13 |
# 处理图像
|
14 |
+
inputs = processor(images=image, return_tensors="pt")
|
15 |
with torch.no_grad():
|
16 |
outputs = model(**inputs)
|
17 |
|
|
|
21 |
|
22 |
# 获取概率
|
23 |
probabilities = torch.nn.functional.softmax(logits, dim=-1)
|
24 |
+
|
25 |
+
# 获取AI生成概率
|
26 |
+
# 通常索引1对应AI生成,索引0对应真实图像,但我们需要确认模型的标签
|
27 |
+
ai_index = 1 if "ai" in model.config.id2label[1].lower() else 0
|
28 |
+
ai_probability = probabilities[0][ai_index].item()
|
29 |
|
30 |
# 分析图像特征
|
31 |
features = analyze_image_features(image)
|
|
|
66 |
)
|
67 |
|
68 |
iface.launch()
|
|