Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -19,16 +19,9 @@ models=[
|
|
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 |
-
|
28 |
-
fin_sum=[]
|
29 |
-
#fin_res={f'{uid}':''}
|
30 |
-
#fin_sum.append(fin_res)
|
31 |
-
#tmp_res=
|
32 |
def aiornot0(image):
|
33 |
labels = ["Real", "AI"]
|
34 |
mod=models[0]
|
@@ -52,7 +45,6 @@ def aiornot0(image):
|
|
52 |
for idx,result in enumerate(px):
|
53 |
results[labels[idx]] = px[idx][0]
|
54 |
#results[labels['label']] = result['score']
|
55 |
-
#tmp_res={f'{uid}-0':results}
|
56 |
fin_sum.append(results)
|
57 |
return gr.HTML.update(html_out),results
|
58 |
def aiornot1(image):
|
@@ -78,8 +70,7 @@ def aiornot1(image):
|
|
78 |
for idx,result in enumerate(px):
|
79 |
results[labels[idx]] = px[idx][0]
|
80 |
#results[labels['label']] = result['score']
|
81 |
-
|
82 |
-
fin_sum.append(results)
|
83 |
return gr.HTML.update(html_out),results
|
84 |
def aiornot2(image):
|
85 |
labels = ["AI", "Real"]
|
@@ -101,85 +92,14 @@ def aiornot2(image):
|
|
101 |
Real: {px[1][0]}<br>
|
102 |
AI: {px[0][0]}"""
|
103 |
|
104 |
-
results = {}
|
105 |
-
for idx,result in enumerate(px):
|
106 |
-
results[labels[idx]] = px[idx][0]
|
107 |
-
#tmp_res={f'{uid}-2':results}
|
108 |
-
fin_sum.append(results)
|
109 |
-
return gr.HTML.update(html_out),results
|
110 |
-
def aiornot3(image):
|
111 |
-
labels = ["Real", "AI"]
|
112 |
-
mod=models[3]
|
113 |
-
feature_extractor3 = AutoFeatureExtractor.from_pretrained(mod)
|
114 |
-
model3 = AutoModelForImageClassification.from_pretrained(mod)
|
115 |
-
input = feature_extractor3(image, return_tensors="pt")
|
116 |
-
with torch.no_grad():
|
117 |
-
outputs = model3(**input)
|
118 |
-
logits = outputs.logits
|
119 |
-
probability = softmax(logits)
|
120 |
-
px = pd.DataFrame(probability.numpy())
|
121 |
-
prediction = logits.argmax(-1).item()
|
122 |
-
label = labels[prediction]
|
123 |
-
html_out = f"""
|
124 |
-
<h1>This image is likely: {label}</h1><br><h3>
|
125 |
-
|
126 |
-
Probabilites:<br>
|
127 |
-
Real: {px[0][0]}<br>
|
128 |
-
AI: {px[1][0]}"""
|
129 |
results = {}
|
130 |
for idx,result in enumerate(px):
|
131 |
results[labels[idx]] = px[idx][0]
|
132 |
#results[labels['label']] = result['score']
|
|
|
|
|
133 |
return gr.HTML.update(html_out),results
|
134 |
-
def aiornot4(image):
|
135 |
-
labels = ["Real", "AI"]
|
136 |
-
mod=models[4]
|
137 |
-
feature_extractor4 = AutoFeatureExtractor.from_pretrained(mod)
|
138 |
-
model4 = AutoModelForImageClassification.from_pretrained(mod)
|
139 |
-
input = feature_extractor4(image, return_tensors="pt")
|
140 |
-
with torch.no_grad():
|
141 |
-
outputs = model4(**input)
|
142 |
-
logits = outputs.logits
|
143 |
-
probability = softmax(logits)
|
144 |
-
px = pd.DataFrame(probability.numpy())
|
145 |
-
prediction = logits.argmax(-1).item()
|
146 |
-
label = labels[prediction]
|
147 |
-
html_out = f"""
|
148 |
-
<h1>This image is likely: {label}</h1><br><h3>
|
149 |
-
|
150 |
-
Probabilites:<br>
|
151 |
-
Real: {px[0][0]}<br>
|
152 |
-
AI: {px[1][0]}"""
|
153 |
-
results = {}
|
154 |
-
for idx,result in enumerate(px):
|
155 |
-
results[labels[idx]] = px[idx][0]
|
156 |
-
#results[labels['label']] = result['score']
|
157 |
-
return gr.HTML.update(html_out),results
|
158 |
-
def aiornot5(image):
|
159 |
-
labels = ["AI", "Real"]
|
160 |
-
mod=models[5]
|
161 |
-
feature_extractor5 = AutoFeatureExtractor.from_pretrained(mod)
|
162 |
-
model5 = AutoModelForImageClassification.from_pretrained(mod)
|
163 |
-
input = feature_extractor5(image, return_tensors="pt")
|
164 |
-
with torch.no_grad():
|
165 |
-
outputs = model5(**input)
|
166 |
-
logits = outputs.logits
|
167 |
-
probability = softmax(logits)
|
168 |
-
px = pd.DataFrame(probability.numpy())
|
169 |
-
prediction = logits.argmax(-1).item()
|
170 |
-
label = labels[prediction]
|
171 |
-
html_out = f"""
|
172 |
-
<h1>This image is likely: {label}</h1><br><h3>
|
173 |
-
|
174 |
-
Probabilites:<br>
|
175 |
-
Real: {px[1][0]}<br>
|
176 |
-
AI: {px[0][0]}"""
|
177 |
|
178 |
-
results = {}
|
179 |
-
for idx,result in enumerate(px):
|
180 |
-
results[labels[idx]] = px[idx][0]
|
181 |
-
#results[labels['label']] = result['score']
|
182 |
-
return gr.HTML.update(html_out),results
|
183 |
def load_url(url):
|
184 |
try:
|
185 |
urllib.request.urlretrieve(
|
@@ -201,45 +121,53 @@ def tot_prob():
|
|
201 |
"Real":f"{fin_out}",
|
202 |
"AI":f"{fin_sub}"
|
203 |
}
|
|
|
204 |
print (fin_out)
|
205 |
return out
|
206 |
except Exception as e:
|
207 |
-
|
208 |
pass
|
209 |
-
print (
|
210 |
-
|
211 |
-
def clear_fin():
|
212 |
fin_sum.clear()
|
213 |
|
214 |
with gr.Blocks() as app:
|
215 |
-
|
216 |
-
|
217 |
-
in_url=gr.Textbox(label="Image URL")
|
218 |
-
with gr.Row():
|
219 |
-
load_btn=gr.Button("Load URL")
|
220 |
-
btn = gr.Button("Detect AI")
|
221 |
-
mes = gr.HTML("""""")
|
222 |
inp = gr.Pil()
|
223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
with gr.Row():
|
225 |
with gr.Box():
|
226 |
lab0 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[0]}'>{models[0]}</a></b>""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
n_out0=gr.Label(label="Output")
|
228 |
outp0 = gr.HTML("""""")
|
229 |
with gr.Box():
|
230 |
-
lab1 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[1]}'>{models[1]}</a></b>""")
|
231 |
n_out1=gr.Label(label="Output")
|
232 |
outp1 = gr.HTML("""""")
|
233 |
with gr.Box():
|
234 |
-
lab2 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[2]}'>{models[2]}</a></b>""")
|
235 |
n_out2=gr.Label(label="Output")
|
236 |
outp2 = gr.HTML("""""")
|
237 |
-
|
238 |
-
|
239 |
-
fin.change(clear_fin,None,None)
|
240 |
load_btn.click(load_url,in_url,[inp,mes])
|
241 |
btn.click(aiornot0,[inp],[outp0,n_out0]).then(tot_prob,None,fin)
|
242 |
btn.click(aiornot1,[inp],[outp1,n_out1]).then(tot_prob,None,fin)
|
243 |
btn.click(aiornot2,[inp],[outp2,n_out2]).then(tot_prob,None,fin)
|
244 |
-
|
245 |
app.queue(concurrency_count=20).launch()
|
|
|
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]
|
|
|
45 |
for idx,result in enumerate(px):
|
46 |
results[labels[idx]] = px[idx][0]
|
47 |
#results[labels['label']] = result['score']
|
|
|
48 |
fin_sum.append(results)
|
49 |
return gr.HTML.update(html_out),results
|
50 |
def aiornot1(image):
|
|
|
70 |
for idx,result in enumerate(px):
|
71 |
results[labels[idx]] = px[idx][0]
|
72 |
#results[labels['label']] = result['score']
|
73 |
+
fin_sum.append(results)
|
|
|
74 |
return gr.HTML.update(html_out),results
|
75 |
def aiornot2(image):
|
76 |
labels = ["AI", "Real"]
|
|
|
92 |
Real: {px[1][0]}<br>
|
93 |
AI: {px[0][0]}"""
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
results = {}
|
96 |
for idx,result in enumerate(px):
|
97 |
results[labels[idx]] = px[idx][0]
|
98 |
#results[labels['label']] = result['score']
|
99 |
+
fin_sum.append(results)
|
100 |
+
|
101 |
return gr.HTML.update(html_out),results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
|
|
|
|
|
|
|
|
|
|
103 |
def load_url(url):
|
104 |
try:
|
105 |
urllib.request.urlretrieve(
|
|
|
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
|
129 |
+
print (e)
|
130 |
+
def fin_clear():
|
|
|
131 |
fin_sum.clear()
|
132 |
|
133 |
with gr.Blocks() as app:
|
134 |
+
gr.Markdown("""<center><h1>AI Image Detector<br><h4>(Test Demo - accuracy varies by model)""")
|
135 |
+
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
136 |
inp = gr.Pil()
|
137 |
+
in_url=gr.Textbox(label="Image URL")
|
138 |
+
with gr.Row():
|
139 |
+
load_btn=gr.Button("Load URL")
|
140 |
+
btn = gr.Button("Detect AI")
|
141 |
+
mes = gr.HTML("""""")
|
142 |
+
with gr.Group():
|
143 |
+
with gr.Row():
|
144 |
+
fin=gr.Label(label="Final Probability")
|
145 |
with gr.Row():
|
146 |
with gr.Box():
|
147 |
lab0 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[0]}'>{models[0]}</a></b>""")
|
148 |
+
nun0 = gr.HTML("""""")
|
149 |
+
with gr.Box():
|
150 |
+
lab1 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[1]}'>{models[1]}</a></b>""")
|
151 |
+
nun1 = gr.HTML("""""")
|
152 |
+
with gr.Box():
|
153 |
+
lab2 = gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{models[2]}'>{models[2]}</a></b>""")
|
154 |
+
nun2 = gr.HTML("""""")
|
155 |
+
|
156 |
+
with gr.Row():
|
157 |
+
with gr.Box():
|
158 |
n_out0=gr.Label(label="Output")
|
159 |
outp0 = gr.HTML("""""")
|
160 |
with gr.Box():
|
|
|
161 |
n_out1=gr.Label(label="Output")
|
162 |
outp1 = gr.HTML("""""")
|
163 |
with gr.Box():
|
|
|
164 |
n_out2=gr.Label(label="Output")
|
165 |
outp2 = gr.HTML("""""")
|
166 |
+
|
167 |
+
fin.change(fin_clear,None,None)
|
|
|
168 |
load_btn.click(load_url,in_url,[inp,mes])
|
169 |
btn.click(aiornot0,[inp],[outp0,n_out0]).then(tot_prob,None,fin)
|
170 |
btn.click(aiornot1,[inp],[outp1,n_out1]).then(tot_prob,None,fin)
|
171 |
btn.click(aiornot2,[inp],[outp2,n_out2]).then(tot_prob,None,fin)
|
172 |
+
|
173 |
app.queue(concurrency_count=20).launch()
|