Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,257 +1,101 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
4 |
-
#from transformers import pipeline
|
5 |
import os
|
6 |
from numpy import exp
|
7 |
-
import pandas as
|
8 |
from PIL import Image
|
9 |
import urllib.request
|
10 |
import uuid
|
11 |
-
uid=uuid.uuid4()
|
12 |
|
13 |
-
|
|
|
|
|
14 |
"cmckinle/sdxl-flux-detector",
|
15 |
"umm-maybe/AI-image-detector",
|
16 |
"Organika/sdxl-detector",
|
17 |
-
#"arnolfokam/ai-generated-image-detector",
|
18 |
]
|
19 |
|
20 |
-
|
21 |
-
pipe1 = pipeline("image-classification", f"{models[1]}")
|
22 |
-
pipe2 = pipeline("image-classification", f"{models[2]}")
|
23 |
-
|
24 |
-
|
25 |
-
fin_sum=[]
|
26 |
-
def image_classifier0(image):
|
27 |
-
labels = ["AI","Real"]
|
28 |
-
outputs = pipe0(image)
|
29 |
-
results = {}
|
30 |
-
result_test={}
|
31 |
-
for idx,result in enumerate(outputs):
|
32 |
-
results[labels[idx]] = outputs[idx]['score']
|
33 |
-
#print (result_test)
|
34 |
-
#for result in outputs:
|
35 |
-
# results[result['label']] = result['score']
|
36 |
-
#print (results)
|
37 |
-
fin_sum.append(results)
|
38 |
-
return results
|
39 |
-
def image_classifier1(image):
|
40 |
-
labels = ["AI","Real"]
|
41 |
-
outputs = pipe1(image)
|
42 |
-
results = {}
|
43 |
-
result_test={}
|
44 |
-
for idx,result in enumerate(outputs):
|
45 |
-
results[labels[idx]] = outputs[idx]['score']
|
46 |
-
#print (result_test)
|
47 |
-
#for result in outputs:
|
48 |
-
# results[result['label']] = result['score']
|
49 |
-
#print (results)
|
50 |
-
fin_sum.append(results)
|
51 |
-
return results
|
52 |
-
def image_classifier2(image):
|
53 |
-
labels = ["AI","Real"]
|
54 |
-
outputs = pipe2(image)
|
55 |
-
results = {}
|
56 |
-
result_test={}
|
57 |
-
for idx,result in enumerate(outputs):
|
58 |
-
results[labels[idx]] = outputs[idx]['score']
|
59 |
-
#print (result_test)
|
60 |
-
#for result in outputs:
|
61 |
-
# results[result['label']] = result['score']
|
62 |
-
#print (results)
|
63 |
-
fin_sum.append(results)
|
64 |
-
return results
|
65 |
|
66 |
def softmax(vector):
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
|
72 |
-
def
|
73 |
labels = ["AI", "Real"]
|
74 |
-
mod=models[
|
75 |
-
|
76 |
-
|
77 |
-
input =
|
78 |
-
|
79 |
-
outputs = model0(**input)
|
80 |
-
logits = outputs.logits
|
81 |
-
probability = softmax(logits)
|
82 |
-
px = pd.DataFrame(probability.numpy())
|
83 |
-
prediction = logits.argmax(-1).item()
|
84 |
-
label = labels[prediction]
|
85 |
-
html_out = f"""
|
86 |
-
<h1>This image is likely: {label}</h1><br><h3>
|
87 |
-
|
88 |
-
Probabilites:<br>
|
89 |
-
Real: {px[1][0]}<br>
|
90 |
-
AI: {px[0][0]}"""
|
91 |
-
results = {}
|
92 |
-
for idx,result in enumerate(px):
|
93 |
-
results[labels[idx]] = px[idx][0]
|
94 |
-
#results[labels['label']] = result['score']
|
95 |
-
fin_sum.append(results)
|
96 |
-
return gr.HTML.update(html_out),results
|
97 |
-
def aiornot1(image):
|
98 |
-
labels = ["AI", "Real"]
|
99 |
-
mod=models[1]
|
100 |
-
feature_extractor1 = AutoFeatureExtractor.from_pretrained(mod)
|
101 |
-
model1 = AutoModelForImageClassification.from_pretrained(mod)
|
102 |
-
input = feature_extractor1(image, return_tensors="pt")
|
103 |
with torch.no_grad():
|
104 |
-
outputs =
|
105 |
logits = outputs.logits
|
106 |
probability = softmax(logits)
|
107 |
px = pd.DataFrame(probability.numpy())
|
|
|
108 |
prediction = logits.argmax(-1).item()
|
109 |
label = labels[prediction]
|
|
|
110 |
html_out = f"""
|
111 |
<h1>This image is likely: {label}</h1><br><h3>
|
112 |
-
|
113 |
Probabilites:<br>
|
114 |
Real: {px[1][0]}<br>
|
115 |
AI: {px[0][0]}"""
|
|
|
116 |
results = {}
|
117 |
-
for idx,result in enumerate(px):
|
118 |
-
results[labels[idx]] = px[idx][0]
|
119 |
-
#results[labels['label']] = result['score']
|
120 |
-
fin_sum.append(results)
|
121 |
-
return gr.HTML.update(html_out),results
|
122 |
-
def aiornot2(image):
|
123 |
-
labels = ["Real", "AI"]
|
124 |
-
mod=models[2]
|
125 |
-
feature_extractor2 = AutoFeatureExtractor.from_pretrained(mod)
|
126 |
-
#feature_extractor2 = AutoFeatureExtractor.from_pretrained("microsoft/resnet-50")
|
127 |
-
model2 = AutoModelForImageClassification.from_pretrained(mod)
|
128 |
-
input = feature_extractor2(image, return_tensors="pt")
|
129 |
-
with torch.no_grad():
|
130 |
-
outputs = model2(**input)
|
131 |
-
logits = outputs.logits
|
132 |
-
probability = softmax(logits)
|
133 |
-
px = pd.DataFrame(probability.numpy())
|
134 |
-
prediction = logits.argmax(-1).item()
|
135 |
-
label = labels[prediction]
|
136 |
-
html_out = f"""
|
137 |
-
<h1>This image is likely: {label}</h1><br><h3>
|
138 |
-
|
139 |
-
Probabilites:<br>
|
140 |
-
Real: {px[0][0]}<br>
|
141 |
-
AI: {px[1][0]}"""
|
142 |
-
|
143 |
-
results = {}
|
144 |
-
for idx,result in enumerate(px):
|
145 |
results[labels[idx]] = px[idx][0]
|
146 |
-
|
147 |
fin_sum.append(results)
|
148 |
-
|
149 |
-
return gr.HTML.update(html_out),results
|
150 |
|
151 |
def load_url(url):
|
152 |
try:
|
153 |
-
urllib.request.urlretrieve(
|
154 |
-
f'{url}',
|
155 |
-
f"{uid}tmp_im.png")
|
156 |
image = Image.open(f"{uid}tmp_im.png")
|
157 |
mes = "Image Loaded"
|
158 |
except Exception as e:
|
159 |
-
image=None
|
160 |
-
mes=f"Image not Found<br>Error: {e}"
|
161 |
-
return image,mes
|
162 |
|
163 |
def tot_prob():
|
164 |
try:
|
165 |
-
fin_out =
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
"
|
170 |
-
"AI":f"{fin_sub}"
|
171 |
}
|
172 |
-
#fin_sum.clear()
|
173 |
-
#print (fin_out)
|
174 |
return out
|
175 |
except Exception as e:
|
176 |
-
|
177 |
-
print (e)
|
178 |
return None
|
|
|
179 |
def fin_clear():
|
180 |
fin_sum.clear()
|
181 |
return None
|
182 |
|
183 |
-
def upd(image):
|
184 |
-
print (image)
|
185 |
-
rand_im = uuid.uuid4()
|
186 |
-
image.save(f"{rand_im}-vid_tmp_proc.png")
|
187 |
-
out = Image.open(f"{rand_im}-vid_tmp_proc.png")
|
188 |
-
|
189 |
-
#image.save(f"{rand_im}-vid_tmp_proc.png")
|
190 |
-
#out = os.path.abspath(f"{rand_im}-vid_tmp_proc.png")
|
191 |
-
#out_url = f'https://omnibus_AI_or_Not_dev.hf.space/file={out}'
|
192 |
-
#out_url = f"{rand_im}-vid_tmp_proc.png"
|
193 |
-
return out
|
194 |
-
|
195 |
-
|
196 |
with gr.Blocks() as app:
|
197 |
-
gr.Markdown("""<center><h1>AI Image Detector<br><h4>(Test Demo - accuracy varies by model)""")
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
btn = gr.Button("Detect AI")
|
204 |
-
mes = gr.HTML("""""")
|
205 |
-
with gr.Group():
|
206 |
-
with gr.Row():
|
207 |
-
fin=gr.Label(label="Final Probability")
|
208 |
-
with gr.Row():
|
209 |
-
with gr.Box():
|
210 |
-
lab0 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[0]}'>{models[0]}</a></b>""")
|
211 |
-
nun0 = gr.HTML("""""")
|
212 |
-
with gr.Box():
|
213 |
-
lab1 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[1]}'>{models[1]}</a></b>""")
|
214 |
-
nun1 = gr.HTML("""""")
|
215 |
-
with gr.Box():
|
216 |
-
lab2 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[2]}'>{models[2]}</a></b>""")
|
217 |
-
nun2 = gr.HTML("""""")
|
218 |
-
|
219 |
-
with gr.Row():
|
220 |
-
with gr.Box():
|
221 |
-
n_out0=gr.Label(label="Output")
|
222 |
-
outp0 = gr.HTML("""""")
|
223 |
-
with gr.Box():
|
224 |
-
n_out1=gr.Label(label="Output")
|
225 |
-
outp1 = gr.HTML("""""")
|
226 |
-
with gr.Box():
|
227 |
-
n_out2=gr.Label(label="Output")
|
228 |
-
outp2 = gr.HTML("""""")
|
229 |
-
with gr.Row():
|
230 |
-
with gr.Box():
|
231 |
-
n_out3=gr.Label(label="Output")
|
232 |
-
outp3 = gr.HTML("""""")
|
233 |
-
with gr.Box():
|
234 |
-
n_out4=gr.Label(label="Output")
|
235 |
-
outp4 = gr.HTML("""""")
|
236 |
-
with gr.Box():
|
237 |
-
n_out5=gr.Label(label="Output")
|
238 |
-
outp5 = gr.HTML("""""")
|
239 |
-
hid_box=gr.Textbox(visible=False)
|
240 |
-
hid_im = gr.Image(type="pil",visible=False)
|
241 |
-
def echo(inp):
|
242 |
-
return inp
|
243 |
-
|
244 |
-
#inp.change(echo,inp,hid_im).then(upd,hid_im,inp)
|
245 |
-
|
246 |
-
btn.click(fin_clear,None,fin,show_progress=False)
|
247 |
-
load_btn.click(load_url,in_url,[inp,mes])
|
248 |
|
249 |
-
|
250 |
-
|
251 |
-
|
|
|
|
|
|
|
252 |
|
253 |
-
btn.click(
|
254 |
-
btn.click(
|
255 |
-
btn.click(
|
|
|
256 |
|
257 |
-
app.launch(show_api=False,max_threads=24)
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
|
|
4 |
import os
|
5 |
from numpy import exp
|
6 |
+
import pandas as pd
|
7 |
from PIL import Image
|
8 |
import urllib.request
|
9 |
import uuid
|
|
|
10 |
|
11 |
+
uid = uuid.uuid4()
|
12 |
+
|
13 |
+
models = [
|
14 |
"cmckinle/sdxl-flux-detector",
|
15 |
"umm-maybe/AI-image-detector",
|
16 |
"Organika/sdxl-detector",
|
|
|
17 |
]
|
18 |
|
19 |
+
fin_sum = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def softmax(vector):
|
22 |
+
e = exp(vector)
|
23 |
+
return e / e.sum()
|
|
|
|
|
24 |
|
25 |
+
def aiornot(image, model_index):
|
26 |
labels = ["AI", "Real"]
|
27 |
+
mod = models[model_index]
|
28 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(mod)
|
29 |
+
model = AutoModelForImageClassification.from_pretrained(mod)
|
30 |
+
input = feature_extractor(image, return_tensors="pt")
|
31 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
with torch.no_grad():
|
33 |
+
outputs = model(**input)
|
34 |
logits = outputs.logits
|
35 |
probability = softmax(logits)
|
36 |
px = pd.DataFrame(probability.numpy())
|
37 |
+
|
38 |
prediction = logits.argmax(-1).item()
|
39 |
label = labels[prediction]
|
40 |
+
|
41 |
html_out = f"""
|
42 |
<h1>This image is likely: {label}</h1><br><h3>
|
|
|
43 |
Probabilites:<br>
|
44 |
Real: {px[1][0]}<br>
|
45 |
AI: {px[0][0]}"""
|
46 |
+
|
47 |
results = {}
|
48 |
+
for idx, result in enumerate(px):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
results[labels[idx]] = px[idx][0]
|
50 |
+
|
51 |
fin_sum.append(results)
|
52 |
+
return gr.HTML.update(html_out), results
|
|
|
53 |
|
54 |
def load_url(url):
|
55 |
try:
|
56 |
+
urllib.request.urlretrieve(f'{url}', f"{uid}tmp_im.png")
|
|
|
|
|
57 |
image = Image.open(f"{uid}tmp_im.png")
|
58 |
mes = "Image Loaded"
|
59 |
except Exception as e:
|
60 |
+
image = None
|
61 |
+
mes = f"Image not Found<br>Error: {e}"
|
62 |
+
return image, mes
|
63 |
|
64 |
def tot_prob():
|
65 |
try:
|
66 |
+
fin_out = sum([result["Real"] for result in fin_sum]) / len(fin_sum)
|
67 |
+
fin_sub = 1 - fin_out
|
68 |
+
out = {
|
69 |
+
"Real": f"{fin_out}",
|
70 |
+
"AI": f"{fin_sub}"
|
|
|
71 |
}
|
|
|
|
|
72 |
return out
|
73 |
except Exception as e:
|
74 |
+
print(e)
|
|
|
75 |
return None
|
76 |
+
|
77 |
def fin_clear():
|
78 |
fin_sum.clear()
|
79 |
return None
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
with gr.Blocks() as app:
|
82 |
+
gr.Markdown("""<center><h1>AI Image Detector<br><h4>(Test Demo - accuracy varies by model)</h4></center>""")
|
83 |
+
inp = gr.Image(type='pil')
|
84 |
+
in_url = gr.Textbox(label="Image URL")
|
85 |
+
load_btn = gr.Button("Load URL")
|
86 |
+
btn = gr.Button("Detect AI")
|
87 |
+
mes = gr.HTML("""""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
+
fin = gr.Label(label="Final Probability")
|
90 |
+
outp0 = gr.HTML("""""")
|
91 |
+
outp1 = gr.HTML("""""")
|
92 |
+
outp2 = gr.HTML("""""")
|
93 |
+
|
94 |
+
load_btn.click(load_url, in_url, [inp, mes])
|
95 |
|
96 |
+
btn.click(fin_clear, None, fin, show_progress=False)
|
97 |
+
btn.click(lambda img: aiornot(img, 0), inp, [outp0]).then(tot_prob, None, fin, show_progress=False)
|
98 |
+
btn.click(lambda img: aiornot(img, 1), inp, [outp1]).then(tot_prob, None, fin, show_progress=False)
|
99 |
+
btn.click(lambda img: aiornot(img, 2), inp, [outp2]).then(tot_prob, None, fin, show_progress=False)
|
100 |
|
101 |
+
app.launch(show_api=False, max_threads=24)
|