cmckinle commited on
Commit
f88c19d
·
verified ·
1 Parent(s): 275549c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -159
app.py CHANGED
@@ -6,7 +6,6 @@ import zipfile
6
  import shutil
7
  import matplotlib.pyplot as plt
8
  from sklearn.metrics import accuracy_score, roc_auc_score, confusion_matrix, classification_report, roc_curve, auc
9
- from tqdm import tqdm
10
  from PIL import Image
11
  import uuid
12
  import tempfile
@@ -16,16 +15,9 @@ import numpy as np
16
  from sklearn.metrics import ConfusionMatrixDisplay
17
  import urllib.request
18
 
19
- # Define models
20
- models = [
21
- "umm-maybe/AI-image-detector",
22
- "Organika/sdxl-detector",
23
- "cmckinle/sdxl-flux-detector",
24
- ]
25
-
26
- pipe0 = pipeline("image-classification", f"{models[0]}")
27
- pipe1 = pipeline("image-classification", f"{models[1]}")
28
- pipe2 = pipeline("image-classification", f"{models[2]}")
29
 
30
  fin_sum = []
31
  uid = uuid.uuid4()
@@ -35,98 +27,25 @@ def softmax(vector):
35
  e = exp(vector - vector.max()) # for numerical stability
36
  return e / e.sum()
37
 
38
- # Single image classification functions
39
- def image_classifier0(image):
40
  labels = ["AI", "Real"]
41
- outputs = pipe0(image)
42
  results = {}
43
  for idx, result in enumerate(outputs):
44
- results[labels[idx]] = float(outputs[idx]['score']) # Convert to float
45
  fin_sum.append(results)
46
  return results
47
 
48
- def image_classifier1(image):
49
- labels = ["AI", "Real"]
50
- outputs = pipe1(image)
51
- results = {}
52
- for idx, result in enumerate(outputs):
53
- results[labels[idx]] = float(outputs[idx]['score']) # Convert to float
54
- fin_sum.append(results)
55
- return results
56
-
57
- def image_classifier2(image):
58
- labels = ["AI", "Real"]
59
- outputs = pipe2(image)
60
- results = {}
61
- for idx, result in enumerate(outputs):
62
- results[labels[idx]] = float(outputs[idx]['score']) # Convert to float
63
- fin_sum.append(results)
64
- return results
65
-
66
- def aiornot0(image):
67
- labels = ["AI", "Real"]
68
- mod = models[0]
69
- feature_extractor0 = AutoFeatureExtractor.from_pretrained(mod)
70
- model0 = AutoModelForImageClassification.from_pretrained(mod)
71
- input = feature_extractor0(image, return_tensors="pt")
72
- with torch.no_grad():
73
- outputs = model0(**input)
74
- logits = outputs.logits
75
- probability = softmax(logits) # Apply softmax on logits
76
- px = pd.DataFrame(probability.numpy())
77
- prediction = logits.argmax(-1).item()
78
- label = labels[prediction]
79
-
80
- html_out = f"""
81
- <h1>This image is likely: {label}</h1><br><h3>
82
- Probabilities:<br>
83
- Real: {float(px[1][0]):.4f}<br>
84
- AI: {float(px[0][0]):.4f}"""
85
-
86
- results = {
87
- "Real": float(px[1][0]),
88
- "AI": float(px[0][0])
89
- }
90
- fin_sum.append(results)
91
- return gr.HTML.update(html_out), results
92
-
93
- def aiornot1(image):
94
  labels = ["AI", "Real"]
95
- mod = models[1]
96
- feature_extractor1 = AutoFeatureExtractor.from_pretrained(mod)
97
- model1 = AutoModelForImageClassification.from_pretrained(mod)
98
- input = feature_extractor1(image, return_tensors="pt")
99
  with torch.no_grad():
100
- outputs = model1(**input)
101
  logits = outputs.logits
102
- probability = softmax(logits) # Apply softmax on logits
103
- px = pd.DataFrame(probability.numpy())
104
- prediction = logits.argmax(-1).item()
105
- label = labels[prediction]
106
-
107
- html_out = f"""
108
- <h1>This image is likely: {label}</h1><br><h3>
109
- Probabilities:<br>
110
- Real: {float(px[1][0]):.4f}<br>
111
- AI: {float(px[0][0]):.4f}"""
112
-
113
- results = {
114
- "Real": float(px[1][0]),
115
- "AI": float(px[0][0])
116
- }
117
- fin_sum.append(results)
118
- return gr.HTML.update(html_out), results
119
-
120
- def aiornot2(image):
121
- labels = ["AI", "Real"]
122
- mod = models[2]
123
- feature_extractor2 = AutoFeatureExtractor.from_pretrained(mod)
124
- model2 = AutoModelForImageClassification.from_pretrained(mod)
125
- input = feature_extractor2(image, return_tensors="pt")
126
- with torch.no_grad():
127
- outputs = model2(**input)
128
- logits = outputs.logits
129
- probability = softmax(logits) # Apply softmax on logits
130
  px = pd.DataFrame(probability.numpy())
131
  prediction = logits.argmax(-1).item()
132
  label = labels[prediction]
@@ -146,13 +65,13 @@ def aiornot2(image):
146
 
147
  # Function to extract images from zip
148
  def extract_zip(zip_file):
149
- temp_dir = tempfile.mkdtemp() # Temporary directory
150
  with zipfile.ZipFile(zip_file, 'r') as z:
151
  z.extractall(temp_dir)
152
  return temp_dir
153
 
154
  # Function to classify images in a folder
155
- def classify_images(image_dir, model_pipeline, model_idx):
156
  images = []
157
  labels = []
158
  preds = []
@@ -165,18 +84,16 @@ def classify_images(image_dir, model_pipeline, model_idx):
165
  img_path = os.path.join(folder_path, img_name)
166
  try:
167
  img = Image.open(img_path).convert("RGB")
168
-
169
- # Ensure that each image is being processed by the correct model pipeline
170
- pred = model_pipeline(img)
171
- pred_label = 0 if pred[0]['label'] == 'AI' else 1 # Assuming 'AI' is label 0 and 'Real' is label 1
172
 
173
  preds.append(pred_label)
174
  labels.append(ground_truth_label)
175
  images.append(img_name)
176
  except Exception as e:
177
- print(f"Error processing image {img_name} in model {model_idx}: {e}")
178
 
179
- print(f"Model {model_idx} processed {len(images)} images")
180
  return labels, preds, images
181
 
182
  # Function to generate evaluation metrics
@@ -206,46 +123,13 @@ def evaluate_model(labels, preds):
206
 
207
  return accuracy, roc_score, report, fig, fig_roc
208
 
209
- # Batch processing for all models
210
  def process_zip(zip_file):
211
  extracted_dir = extract_zip(zip_file.name)
212
-
213
- # Run classification for each model
214
- results = {}
215
- for idx in range(len(models)):
216
- print(f"Processing with model {models[idx]}") # Debugging to show which model is being used
217
-
218
- # Create a new pipeline for each model within the loop
219
- pipe = pipeline("image-classification", f"{models[idx]}")
220
- print(f"Initialized pipeline for {models[idx]}") # Confirm pipeline is initialized correctly
221
-
222
- # Classify images with the correct pipeline per model
223
- labels, preds, images = classify_images(extracted_dir, pipe, idx)
224
-
225
- # Debugging: Print the predictions to ensure they're different
226
- print(f"Predictions for model {models[idx]}: {preds}")
227
-
228
- accuracy, roc_score, report, cm_fig, roc_fig = evaluate_model(labels, preds)
229
-
230
- # Store results for each model
231
- results[f'Model_{idx}_accuracy'] = accuracy
232
- results[f'Model_{idx}_roc_score'] = roc_score
233
- results[f'Model_{idx}_report'] = report
234
- results[f'Model_{idx}_cm_fig'] = cm_fig
235
- results[f'Model_{idx}_roc_fig'] = roc_fig
236
-
237
  shutil.rmtree(extracted_dir) # Clean up extracted files
238
-
239
- # Return results for all models
240
- return (results['Model_0_accuracy'], results['Model_0_roc_score'], results['Model_0_report'],
241
- results['Model_0_cm_fig'], results['Model_0_roc_fig'],
242
- results['Model_1_accuracy'], results['Model_1_roc_score'], results['Model_1_report'],
243
- results['Model_1_cm_fig'], results['Model_1_roc_fig'],
244
- results['Model_2_accuracy'], results['Model_2_roc_score'], results['Model_2_report'],
245
- results['Model_2_cm_fig'], results['Model_2_roc_fig'])
246
-
247
-
248
-
249
 
250
  # Single image section
251
  def load_url(url):
@@ -294,18 +178,15 @@ with gr.Blocks() as app:
294
  with gr.Row():
295
  fin = gr.Label(label="Final Probability")
296
  with gr.Row():
297
- for i, model in enumerate(models):
298
- with gr.Box():
299
- gr.HTML(f"""<b>Testing on Model {i}: <a href='https://huggingface.co/{model}'>{model}</a></b>""")
300
- globals()[f'outp{i}'] = gr.HTML("""""")
301
- globals()[f'n_out{i}'] = gr.Label(label="Output")
302
 
303
  btn.click(fin_clear, None, fin, show_progress=False)
304
  load_btn.click(load_url, in_url, [inp, mes])
305
 
306
- btn.click(aiornot0, [inp], [outp0, n_out0]).then(
307
- aiornot1, [inp], [outp1, n_out1]).then(
308
- aiornot2, [inp], [outp2, n_out2]).then(
309
  tot_prob, None, fin, show_progress=False)
310
 
311
  # Tab for batch processing
@@ -313,19 +194,16 @@ with gr.Blocks() as app:
313
  zip_file = gr.File(label="Upload Zip (two folders: real, ai)")
314
  batch_btn = gr.Button("Process Batch")
315
 
316
- for i, model in enumerate(models):
317
- with gr.Group():
318
- gr.Markdown(f"### Results for {model}")
319
- globals()[f'output_acc{i}'] = gr.Label(label=f"Model {i} Accuracy")
320
- globals()[f'output_roc{i}'] = gr.Label(label=f"Model {i} ROC Score")
321
- globals()[f'output_report{i}'] = gr.Textbox(label=f"Model {i} Classification Report", lines=10)
322
- globals()[f'output_cm{i}'] = gr.Plot(label=f"Model {i} Confusion Matrix")
323
- globals()[f'output_roc_plot{i}'] = gr.Plot(label=f"Model {i} ROC Curve")
324
 
325
  # Connect batch processing
326
  batch_btn.click(process_zip, zip_file,
327
- [output_acc0, output_roc0, output_report0, output_cm0, output_roc_plot0,
328
- output_acc1, output_roc1, output_report1, output_cm1, output_roc_plot1,
329
- output_acc2, output_roc2, output_report2, output_cm2, output_roc_plot2])
330
 
331
- app.launch(show_api=False, max_threads=24)
 
6
  import shutil
7
  import matplotlib.pyplot as plt
8
  from sklearn.metrics import accuracy_score, roc_auc_score, confusion_matrix, classification_report, roc_curve, auc
 
9
  from PIL import Image
10
  import uuid
11
  import tempfile
 
15
  from sklearn.metrics import ConfusionMatrixDisplay
16
  import urllib.request
17
 
18
+ # Define model
19
+ model = "cmckinle/sdxl-flux-detector"
20
+ pipe = pipeline("image-classification", model)
 
 
 
 
 
 
 
21
 
22
  fin_sum = []
23
  uid = uuid.uuid4()
 
27
  e = exp(vector - vector.max()) # for numerical stability
28
  return e / e.sum()
29
 
30
+ # Single image classification function
31
+ def image_classifier(image):
32
  labels = ["AI", "Real"]
33
+ outputs = pipe(image)
34
  results = {}
35
  for idx, result in enumerate(outputs):
36
+ results[labels[idx]] = float(outputs[idx]['score'])
37
  fin_sum.append(results)
38
  return results
39
 
40
+ def aiornot(image):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  labels = ["AI", "Real"]
42
+ feature_extractor = AutoFeatureExtractor.from_pretrained(model)
43
+ model_cls = AutoModelForImageClassification.from_pretrained(model)
44
+ input = feature_extractor(image, return_tensors="pt")
 
45
  with torch.no_grad():
46
+ outputs = model_cls(**input)
47
  logits = outputs.logits
48
+ probability = softmax(logits)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  px = pd.DataFrame(probability.numpy())
50
  prediction = logits.argmax(-1).item()
51
  label = labels[prediction]
 
65
 
66
  # Function to extract images from zip
67
  def extract_zip(zip_file):
68
+ temp_dir = tempfile.mkdtemp()
69
  with zipfile.ZipFile(zip_file, 'r') as z:
70
  z.extractall(temp_dir)
71
  return temp_dir
72
 
73
  # Function to classify images in a folder
74
+ def classify_images(image_dir):
75
  images = []
76
  labels = []
77
  preds = []
 
84
  img_path = os.path.join(folder_path, img_name)
85
  try:
86
  img = Image.open(img_path).convert("RGB")
87
+ pred = pipe(img)
88
+ pred_label = 0 if pred[0]['label'] == 'AI' else 1
 
 
89
 
90
  preds.append(pred_label)
91
  labels.append(ground_truth_label)
92
  images.append(img_name)
93
  except Exception as e:
94
+ print(f"Error processing image {img_name}: {e}")
95
 
96
+ print(f"Processed {len(images)} images")
97
  return labels, preds, images
98
 
99
  # Function to generate evaluation metrics
 
123
 
124
  return accuracy, roc_score, report, fig, fig_roc
125
 
126
+ # Batch processing
127
  def process_zip(zip_file):
128
  extracted_dir = extract_zip(zip_file.name)
129
+ labels, preds, images = classify_images(extracted_dir)
130
+ accuracy, roc_score, report, cm_fig, roc_fig = evaluate_model(labels, preds)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  shutil.rmtree(extracted_dir) # Clean up extracted files
132
+ return accuracy, roc_score, report, cm_fig, roc_fig
 
 
 
 
 
 
 
 
 
 
133
 
134
  # Single image section
135
  def load_url(url):
 
178
  with gr.Row():
179
  fin = gr.Label(label="Final Probability")
180
  with gr.Row():
181
+ with gr.Box():
182
+ gr.HTML(f"""<b>Testing on Model: <a href='https://huggingface.co/{model}'>{model}</a></b>""")
183
+ outp = gr.HTML("""""")
184
+ n_out = gr.Label(label="Output")
 
185
 
186
  btn.click(fin_clear, None, fin, show_progress=False)
187
  load_btn.click(load_url, in_url, [inp, mes])
188
 
189
+ btn.click(aiornot, [inp], [outp, n_out]).then(
 
 
190
  tot_prob, None, fin, show_progress=False)
191
 
192
  # Tab for batch processing
 
194
  zip_file = gr.File(label="Upload Zip (two folders: real, ai)")
195
  batch_btn = gr.Button("Process Batch")
196
 
197
+ with gr.Group():
198
+ gr.Markdown(f"### Results for {model}")
199
+ output_acc = gr.Label(label="Accuracy")
200
+ output_roc = gr.Label(label="ROC Score")
201
+ output_report = gr.Textbox(label="Classification Report", lines=10)
202
+ output_cm = gr.Plot(label="Confusion Matrix")
203
+ output_roc_plot = gr.Plot(label="ROC Curve")
 
204
 
205
  # Connect batch processing
206
  batch_btn.click(process_zip, zip_file,
207
+ [output_acc, output_roc, output_report, output_cm, output_roc_plot])
 
 
208
 
209
+ app.launch(show_api=False, max_threads=24)