Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoFeatureExtractor, AutoModelForImageClassification, pipeline
|
|
|
|
|
|
|
| 4 |
from numpy import exp
|
| 5 |
import pandas as pd
|
| 6 |
from PIL import Image
|
|
@@ -8,20 +10,64 @@ import urllib.request
|
|
| 8 |
import uuid
|
| 9 |
uid=uuid.uuid4()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def softmax(vector):
|
| 14 |
e = exp(vector)
|
| 15 |
return e / e.sum()
|
| 16 |
|
| 17 |
-
|
| 18 |
-
models=[
|
| 19 |
-
"Nahrawy/AIorNot",
|
| 20 |
-
"umm-maybe/AI-image-detector",
|
| 21 |
-
"arnolfokam/ai-generated-image-detector",
|
| 22 |
|
| 23 |
-
]
|
| 24 |
-
fin_sum=[]
|
| 25 |
def aiornot0(image):
|
| 26 |
labels = ["Real", "AI"]
|
| 27 |
mod=models[0]
|
|
@@ -39,8 +85,8 @@ def aiornot0(image):
|
|
| 39 |
<h1>This image is likely: {label}</h1><br><h3>
|
| 40 |
|
| 41 |
Probabilites:<br>
|
| 42 |
-
Real: {px[
|
| 43 |
-
AI: {px[
|
| 44 |
results = {}
|
| 45 |
for idx,result in enumerate(px):
|
| 46 |
results[labels[idx]] = px[idx][0]
|
|
@@ -48,7 +94,7 @@ def aiornot0(image):
|
|
| 48 |
fin_sum.append(results)
|
| 49 |
return gr.HTML.update(html_out),results
|
| 50 |
def aiornot1(image):
|
| 51 |
-
labels = ["
|
| 52 |
mod=models[1]
|
| 53 |
feature_extractor1 = AutoFeatureExtractor.from_pretrained(mod)
|
| 54 |
model1 = AutoModelForImageClassification.from_pretrained(mod)
|
|
@@ -64,8 +110,8 @@ def aiornot1(image):
|
|
| 64 |
<h1>This image is likely: {label}</h1><br><h3>
|
| 65 |
|
| 66 |
Probabilites:<br>
|
| 67 |
-
Real: {px[
|
| 68 |
-
AI: {px[
|
| 69 |
results = {}
|
| 70 |
for idx,result in enumerate(px):
|
| 71 |
results[labels[idx]] = px[idx][0]
|
|
@@ -73,9 +119,9 @@ def aiornot1(image):
|
|
| 73 |
fin_sum.append(results)
|
| 74 |
return gr.HTML.update(html_out),results
|
| 75 |
def aiornot2(image):
|
| 76 |
-
labels = ["
|
| 77 |
mod=models[2]
|
| 78 |
-
feature_extractor2 = AutoFeatureExtractor.from_pretrained(
|
| 79 |
model2 = AutoModelForImageClassification.from_pretrained(mod)
|
| 80 |
input = feature_extractor2(image, return_tensors="pt")
|
| 81 |
with torch.no_grad():
|
|
@@ -89,8 +135,8 @@ def aiornot2(image):
|
|
| 89 |
<h1>This image is likely: {label}</h1><br><h3>
|
| 90 |
|
| 91 |
Probabilites:<br>
|
| 92 |
-
Real: {px[
|
| 93 |
-
AI: {px[
|
| 94 |
|
| 95 |
results = {}
|
| 96 |
for idx,result in enumerate(px):
|
|
@@ -114,15 +160,15 @@ def load_url(url):
|
|
| 114 |
|
| 115 |
def tot_prob():
|
| 116 |
try:
|
| 117 |
-
fin_out = fin_sum[0]["Real"]+fin_sum[1]["Real"]+fin_sum[2]["Real"]
|
| 118 |
-
fin_out = fin_out/
|
| 119 |
fin_sub = 1-fin_out
|
| 120 |
out={
|
| 121 |
"Real":f"{fin_out}",
|
| 122 |
"AI":f"{fin_sub}"
|
| 123 |
}
|
| 124 |
#fin_sum.clear()
|
| 125 |
-
print (fin_out)
|
| 126 |
return out
|
| 127 |
except Exception as e:
|
| 128 |
pass
|
|
@@ -134,7 +180,7 @@ def fin_clear():
|
|
| 134 |
with gr.Blocks() as app:
|
| 135 |
gr.Markdown("""<center><h1>AI Image Detector<br><h4>(Test Demo - accuracy varies by model)""")
|
| 136 |
with gr.Column():
|
| 137 |
-
inp = gr.Image(type='
|
| 138 |
in_url=gr.Textbox(label="Image URL")
|
| 139 |
with gr.Row():
|
| 140 |
load_btn=gr.Button("Load URL")
|
|
@@ -164,11 +210,36 @@ with gr.Blocks() as app:
|
|
| 164 |
with gr.Box():
|
| 165 |
n_out2=gr.Label(label="Output")
|
| 166 |
outp2 = gr.HTML("""""")
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
load_btn.click(load_url,in_url,[inp,mes])
|
| 170 |
-
btn.click(aiornot0,[inp],[outp0,n_out0]).then(tot_prob,None,fin)
|
| 171 |
-
btn.click(aiornot1,[inp],[outp1,n_out1]).then(tot_prob,None,fin)
|
| 172 |
-
btn.click(aiornot2,[inp],[outp2,n_out2]).then(tot_prob,None,fin)
|
| 173 |
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoFeatureExtractor, AutoModelForImageClassification, pipeline
|
| 4 |
+
#from transformers import pipeline
|
| 5 |
+
import os
|
| 6 |
from numpy import exp
|
| 7 |
import pandas as pd
|
| 8 |
from PIL import Image
|
|
|
|
| 10 |
import uuid
|
| 11 |
uid=uuid.uuid4()
|
| 12 |
|
| 13 |
+
models=[
|
| 14 |
+
"Nahrawy/AIorNot",
|
| 15 |
+
"umm-maybe/AI-image-detector",
|
| 16 |
+
"arnolfokam/ai-generated-image-detector",
|
| 17 |
+
]
|
| 18 |
|
| 19 |
+
pipe0 = pipeline("image-classification", f"{models[0]}")
|
| 20 |
+
pipe1 = pipeline("image-classification", f"{models[1]}")
|
| 21 |
+
pipe2 = pipeline("image-classification", f"{models[2]}")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
fin_sum=[]
|
| 25 |
+
def image_classifier0(image):
|
| 26 |
+
labels = ["AI","Real"]
|
| 27 |
+
outputs = pipe0(image)
|
| 28 |
+
results = {}
|
| 29 |
+
result_test={}
|
| 30 |
+
for idx,result in enumerate(outputs):
|
| 31 |
+
results[labels[idx]] = outputs[idx]['score']
|
| 32 |
+
#print (result_test)
|
| 33 |
+
#for result in outputs:
|
| 34 |
+
# results[result['label']] = result['score']
|
| 35 |
+
#print (results)
|
| 36 |
+
fin_sum.append(results)
|
| 37 |
+
return results
|
| 38 |
+
def image_classifier1(image):
|
| 39 |
+
labels = ["AI","Real"]
|
| 40 |
+
outputs = pipe1(image)
|
| 41 |
+
results = {}
|
| 42 |
+
result_test={}
|
| 43 |
+
for idx,result in enumerate(outputs):
|
| 44 |
+
results[labels[idx]] = outputs[idx]['score']
|
| 45 |
+
#print (result_test)
|
| 46 |
+
#for result in outputs:
|
| 47 |
+
# results[result['label']] = result['score']
|
| 48 |
+
#print (results)
|
| 49 |
+
fin_sum.append(results)
|
| 50 |
+
return results
|
| 51 |
+
def image_classifier2(image):
|
| 52 |
+
labels = ["AI","Real"]
|
| 53 |
+
outputs = pipe2(image)
|
| 54 |
+
results = {}
|
| 55 |
+
result_test={}
|
| 56 |
+
for idx,result in enumerate(outputs):
|
| 57 |
+
results[labels[idx]] = outputs[idx]['score']
|
| 58 |
+
#print (result_test)
|
| 59 |
+
#for result in outputs:
|
| 60 |
+
# results[result['label']] = result['score']
|
| 61 |
+
#print (results)
|
| 62 |
+
fin_sum.append(results)
|
| 63 |
+
return results
|
| 64 |
|
| 65 |
def softmax(vector):
|
| 66 |
e = exp(vector)
|
| 67 |
return e / e.sum()
|
| 68 |
|
| 69 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
|
|
|
|
|
|
| 71 |
def aiornot0(image):
|
| 72 |
labels = ["Real", "AI"]
|
| 73 |
mod=models[0]
|
|
|
|
| 85 |
<h1>This image is likely: {label}</h1><br><h3>
|
| 86 |
|
| 87 |
Probabilites:<br>
|
| 88 |
+
Real: {px[1][0]}<br>
|
| 89 |
+
AI: {px[0][0]}"""
|
| 90 |
results = {}
|
| 91 |
for idx,result in enumerate(px):
|
| 92 |
results[labels[idx]] = px[idx][0]
|
|
|
|
| 94 |
fin_sum.append(results)
|
| 95 |
return gr.HTML.update(html_out),results
|
| 96 |
def aiornot1(image):
|
| 97 |
+
labels = ["AI", "Real"]
|
| 98 |
mod=models[1]
|
| 99 |
feature_extractor1 = AutoFeatureExtractor.from_pretrained(mod)
|
| 100 |
model1 = AutoModelForImageClassification.from_pretrained(mod)
|
|
|
|
| 110 |
<h1>This image is likely: {label}</h1><br><h3>
|
| 111 |
|
| 112 |
Probabilites:<br>
|
| 113 |
+
Real: {px[1][0]}<br>
|
| 114 |
+
AI: {px[0][0]}"""
|
| 115 |
results = {}
|
| 116 |
for idx,result in enumerate(px):
|
| 117 |
results[labels[idx]] = px[idx][0]
|
|
|
|
| 119 |
fin_sum.append(results)
|
| 120 |
return gr.HTML.update(html_out),results
|
| 121 |
def aiornot2(image):
|
| 122 |
+
labels = ["Real", "AI"]
|
| 123 |
mod=models[2]
|
| 124 |
+
feature_extractor2 = AutoFeatureExtractor.from_pretrained("microsoft/resnet-50")
|
| 125 |
model2 = AutoModelForImageClassification.from_pretrained(mod)
|
| 126 |
input = feature_extractor2(image, return_tensors="pt")
|
| 127 |
with torch.no_grad():
|
|
|
|
| 135 |
<h1>This image is likely: {label}</h1><br><h3>
|
| 136 |
|
| 137 |
Probabilites:<br>
|
| 138 |
+
Real: {px[0][0]}<br>
|
| 139 |
+
AI: {px[1][0]}"""
|
| 140 |
|
| 141 |
results = {}
|
| 142 |
for idx,result in enumerate(px):
|
|
|
|
| 160 |
|
| 161 |
def tot_prob():
|
| 162 |
try:
|
| 163 |
+
fin_out = fin_sum[0]["Real"]+fin_sum[1]["Real"]+fin_sum[2]["Real"]+fin_sum[3]["Real"]+fin_sum[4]["Real"]+fin_sum[5]["Real"]
|
| 164 |
+
fin_out = fin_out/6
|
| 165 |
fin_sub = 1-fin_out
|
| 166 |
out={
|
| 167 |
"Real":f"{fin_out}",
|
| 168 |
"AI":f"{fin_sub}"
|
| 169 |
}
|
| 170 |
#fin_sum.clear()
|
| 171 |
+
#print (fin_out)
|
| 172 |
return out
|
| 173 |
except Exception as e:
|
| 174 |
pass
|
|
|
|
| 180 |
with gr.Blocks() as app:
|
| 181 |
gr.Markdown("""<center><h1>AI Image Detector<br><h4>(Test Demo - accuracy varies by model)""")
|
| 182 |
with gr.Column():
|
| 183 |
+
inp = gr.Image(type='pil')
|
| 184 |
in_url=gr.Textbox(label="Image URL")
|
| 185 |
with gr.Row():
|
| 186 |
load_btn=gr.Button("Load URL")
|
|
|
|
| 210 |
with gr.Box():
|
| 211 |
n_out2=gr.Label(label="Output")
|
| 212 |
outp2 = gr.HTML("""""")
|
| 213 |
+
with gr.Row():
|
| 214 |
+
with gr.Box():
|
| 215 |
+
n_out3=gr.Label(label="Output")
|
| 216 |
+
outp3 = gr.HTML("""""")
|
| 217 |
+
with gr.Box():
|
| 218 |
+
n_out4=gr.Label(label="Output")
|
| 219 |
+
outp4 = gr.HTML("""""")
|
| 220 |
+
with gr.Box():
|
| 221 |
+
n_out5=gr.Label(label="Output")
|
| 222 |
+
outp5 = gr.HTML("""""")
|
| 223 |
+
hid_box=gr.Textbox(visible=False)
|
| 224 |
+
|
| 225 |
+
def upd(image):
|
| 226 |
+
rand_im = uuid.uuid4()
|
| 227 |
+
image.save(f"{rand_im}-vid_tmp_proc.png")
|
| 228 |
+
#out = os.path.abspath(f"{rand_im}-vid_tmp_proc.png")
|
| 229 |
+
#out_url = f'https://omnibus_AI_or_Not_dev.hf.space/file={out}'
|
| 230 |
+
out_url = f"{rand_im}-vid_tmp_proc.png"
|
| 231 |
+
return out_url
|
| 232 |
+
#inp.change(upd,inp,inp)
|
| 233 |
+
|
| 234 |
+
btn.click(fin_clear,None,fin,show_progress=False)
|
| 235 |
load_btn.click(load_url,in_url,[inp,mes])
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
+
btn.click(aiornot0,[inp],[outp0,n_out0]).then(tot_prob,None,fin,show_progress=False)
|
| 238 |
+
btn.click(aiornot1,[inp],[outp1,n_out1]).then(tot_prob,None,fin,show_progress=False)
|
| 239 |
+
btn.click(aiornot2,[inp],[outp2,n_out2]).then(tot_prob,None,fin,show_progress=False)
|
| 240 |
+
|
| 241 |
+
btn.click(image_classifier0,[inp],[n_out3]).then(tot_prob,None,fin,show_progress=False)
|
| 242 |
+
btn.click(image_classifier1,[inp],[n_out4]).then(tot_prob,None,fin,show_progress=False)
|
| 243 |
+
btn.click(image_classifier2,[inp],[n_out5]).then(tot_prob,None,fin,show_progress=False)
|
| 244 |
+
|
| 245 |
+
app.queue(concurrency_count=60).launch()
|