Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,6 +19,9 @@ models=[
|
|
| 19 |
"Nahrawy/AIorNot",
|
| 20 |
"umm-maybe/AI-image-detector",
|
| 21 |
"arnolfokam/ai-generated-image-detector",
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
]
|
| 24 |
|
|
@@ -95,6 +98,79 @@ def aiornot2(image):
|
|
| 95 |
results[labels[idx]] = px[idx][0]
|
| 96 |
#results[labels['label']] = result['score']
|
| 97 |
return gr.HTML.update(html_out),results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
def load_url(url):
|
| 99 |
try:
|
| 100 |
urllib.request.urlretrieve(
|
|
@@ -128,9 +204,25 @@ with gr.Blocks() as app:
|
|
| 128 |
with gr.Box():
|
| 129 |
lab2 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[2]}'>{models[2]}</a></b>""")
|
| 130 |
n_out2=gr.Label(label="Output")
|
| 131 |
-
outp2 = gr.HTML("""""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
load_btn.click(load_url,in_url,[inp,mes])
|
| 133 |
btn.click(aiornot0,[inp],[outp0,n_out0])
|
| 134 |
btn.click(aiornot1,[inp],[outp1,n_out1])
|
| 135 |
btn.click(aiornot2,[inp],[outp2,n_out2])
|
|
|
|
|
|
|
|
|
|
| 136 |
app.launch()
|
|
|
|
| 19 |
"Nahrawy/AIorNot",
|
| 20 |
"umm-maybe/AI-image-detector",
|
| 21 |
"arnolfokam/ai-generated-image-detector",
|
| 22 |
+
"Binyamin/Hybrid_1",
|
| 23 |
+
"HuggingSara/model_soups",
|
| 24 |
+
"psyne/AIResnetClone",
|
| 25 |
|
| 26 |
]
|
| 27 |
|
|
|
|
| 98 |
results[labels[idx]] = px[idx][0]
|
| 99 |
#results[labels['label']] = result['score']
|
| 100 |
return gr.HTML.update(html_out),results
|
| 101 |
+
def aiornot3(image):
|
| 102 |
+
labels = ["Real", "AI"]
|
| 103 |
+
mod=models[3]
|
| 104 |
+
feature_extractor3 = AutoFeatureExtractor.from_pretrained(mod)
|
| 105 |
+
model3 = AutoModelForImageClassification.from_pretrained(mod)
|
| 106 |
+
input = feature_extractor3(image, return_tensors="pt")
|
| 107 |
+
with torch.no_grad():
|
| 108 |
+
outputs = model3(**input)
|
| 109 |
+
logits = outputs.logits
|
| 110 |
+
probability = softmax(logits)
|
| 111 |
+
px = pd.DataFrame(probability.numpy())
|
| 112 |
+
prediction = logits.argmax(-1).item()
|
| 113 |
+
label = labels[prediction]
|
| 114 |
+
html_out = f"""
|
| 115 |
+
<h1>This image is likely: {label}</h1><br><h3>
|
| 116 |
+
|
| 117 |
+
Probabilites:<br>
|
| 118 |
+
Real: {px[0][0]}<br>
|
| 119 |
+
AI: {px[1][0]}"""
|
| 120 |
+
results = {}
|
| 121 |
+
for idx,result in enumerate(px):
|
| 122 |
+
results[labels[idx]] = px[idx][0]
|
| 123 |
+
#results[labels['label']] = result['score']
|
| 124 |
+
return gr.HTML.update(html_out),results
|
| 125 |
+
def aiornot4(image):
|
| 126 |
+
labels = ["Real", "AI"]
|
| 127 |
+
mod=models[4]
|
| 128 |
+
feature_extractor4 = AutoFeatureExtractor.from_pretrained(mod)
|
| 129 |
+
model4 = AutoModelForImageClassification.from_pretrained(mod)
|
| 130 |
+
input = feature_extractor4(image, return_tensors="pt")
|
| 131 |
+
with torch.no_grad():
|
| 132 |
+
outputs = model4(**input)
|
| 133 |
+
logits = outputs.logits
|
| 134 |
+
probability = softmax(logits)
|
| 135 |
+
px = pd.DataFrame(probability.numpy())
|
| 136 |
+
prediction = logits.argmax(-1).item()
|
| 137 |
+
label = labels[prediction]
|
| 138 |
+
html_out = f"""
|
| 139 |
+
<h1>This image is likely: {label}</h1><br><h3>
|
| 140 |
+
|
| 141 |
+
Probabilites:<br>
|
| 142 |
+
Real: {px[0][0]}<br>
|
| 143 |
+
AI: {px[1][0]}"""
|
| 144 |
+
results = {}
|
| 145 |
+
for idx,result in enumerate(px):
|
| 146 |
+
results[labels[idx]] = px[idx][0]
|
| 147 |
+
#results[labels['label']] = result['score']
|
| 148 |
+
return gr.HTML.update(html_out),results
|
| 149 |
+
def aiornot5(image):
|
| 150 |
+
labels = ["AI", "Real"]
|
| 151 |
+
mod=models[5]
|
| 152 |
+
feature_extractor5 = AutoFeatureExtractor.from_pretrained(mod)
|
| 153 |
+
model5 = AutoModelForImageClassification.from_pretrained(mod)
|
| 154 |
+
input = feature_extractor5(image, return_tensors="pt")
|
| 155 |
+
with torch.no_grad():
|
| 156 |
+
outputs = model5(**input)
|
| 157 |
+
logits = outputs.logits
|
| 158 |
+
probability = softmax(logits)
|
| 159 |
+
px = pd.DataFrame(probability.numpy())
|
| 160 |
+
prediction = logits.argmax(-1).item()
|
| 161 |
+
label = labels[prediction]
|
| 162 |
+
html_out = f"""
|
| 163 |
+
<h1>This image is likely: {label}</h1><br><h3>
|
| 164 |
+
|
| 165 |
+
Probabilites:<br>
|
| 166 |
+
Real: {px[1][0]}<br>
|
| 167 |
+
AI: {px[0][0]}"""
|
| 168 |
+
|
| 169 |
+
results = {}
|
| 170 |
+
for idx,result in enumerate(px):
|
| 171 |
+
results[labels[idx]] = px[idx][0]
|
| 172 |
+
#results[labels['label']] = result['score']
|
| 173 |
+
return gr.HTML.update(html_out),results
|
| 174 |
def load_url(url):
|
| 175 |
try:
|
| 176 |
urllib.request.urlretrieve(
|
|
|
|
| 204 |
with gr.Box():
|
| 205 |
lab2 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[2]}'>{models[2]}</a></b>""")
|
| 206 |
n_out2=gr.Label(label="Output")
|
| 207 |
+
outp2 = gr.HTML("""""")
|
| 208 |
+
with gr.Row():
|
| 209 |
+
with gr.Box():
|
| 210 |
+
lab3 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[3]}'>{models[3]}</a></b>""")
|
| 211 |
+
n_out3=gr.Label(label="Output")
|
| 212 |
+
outp3 = gr.HTML("""""")
|
| 213 |
+
with gr.Box():
|
| 214 |
+
lab4 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[4]}'>{models[4]}</a></b>""")
|
| 215 |
+
n_out4=gr.Label(label="Output")
|
| 216 |
+
outp4 = gr.HTML("""""")
|
| 217 |
+
with gr.Box():
|
| 218 |
+
lab5 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[5]}'>{models[5]}</a></b>""")
|
| 219 |
+
n_out5=gr.Label(label="Output")
|
| 220 |
+
outp5 = gr.HTML("""""")
|
| 221 |
load_btn.click(load_url,in_url,[inp,mes])
|
| 222 |
btn.click(aiornot0,[inp],[outp0,n_out0])
|
| 223 |
btn.click(aiornot1,[inp],[outp1,n_out1])
|
| 224 |
btn.click(aiornot2,[inp],[outp2,n_out2])
|
| 225 |
+
btn.click(aiornot3,[inp],[outp3,n_out3])
|
| 226 |
+
btn.click(aiornot4,[inp],[outp4,n_out4])
|
| 227 |
+
btn.click(aiornot5,[inp],[outp5,n_out5])
|
| 228 |
app.launch()
|