cmckinle commited on
Commit
a870a21
1 Parent(s): 050a6c5

Update app.py

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