Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -43,42 +43,62 @@ class AIDetector:
|
|
43 |
|
44 |
def process_zip(zip_file):
|
45 |
temp_dir = tempfile.mkdtemp()
|
46 |
-
with zipfile.ZipFile(zip_file.name, 'r') as z:
|
47 |
-
z.extractall(temp_dir)
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
76 |
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
|
81 |
-
|
82 |
|
83 |
def format_classification_report(labels, preds):
|
84 |
# Convert the report string to a dictionary
|
@@ -260,7 +280,7 @@ detector = AIDetector()
|
|
260 |
|
261 |
def create_gradio_interface():
|
262 |
with gr.Blocks() as app:
|
263 |
-
gr.Markdown("""<center><h1>AI Image Detector<br><h4>(
|
264 |
|
265 |
with gr.Tabs():
|
266 |
with gr.Tab("Single Image Detection"):
|
@@ -279,7 +299,12 @@ def create_gradio_interface():
|
|
279 |
output_label = gr.Label(label="Output")
|
280 |
|
281 |
with gr.Tab("Batch Image Processing"):
|
282 |
-
zip_file = gr.File(
|
|
|
|
|
|
|
|
|
|
|
283 |
batch_btn = gr.Button("Process Batch")
|
284 |
|
285 |
with gr.Group():
|
@@ -300,7 +325,8 @@ def create_gradio_interface():
|
|
300 |
batch_btn.click(
|
301 |
process_zip,
|
302 |
zip_file,
|
303 |
-
[output_acc, output_roc, output_report, output_plots, output_fp_fn]
|
|
|
304 |
)
|
305 |
|
306 |
return app
|
|
|
43 |
|
44 |
def process_zip(zip_file):
|
45 |
temp_dir = tempfile.mkdtemp()
|
|
|
|
|
46 |
|
47 |
+
try:
|
48 |
+
# Validate zip structure
|
49 |
+
with zipfile.ZipFile(zip_file.name, 'r') as z:
|
50 |
+
file_list = z.namelist()
|
51 |
+
if not ('real/' in file_list and 'ai/' in file_list):
|
52 |
+
raise ValueError("Zip file must contain 'real' and 'ai' folders")
|
53 |
+
|
54 |
+
z.extractall(temp_dir)
|
55 |
+
|
56 |
+
labels, preds, images = [], [], []
|
57 |
+
false_positives, false_negatives = [], []
|
58 |
+
detector = AIDetector()
|
59 |
+
|
60 |
+
total_images = sum(len(files) for _, _, files in os.walk(temp_dir))
|
61 |
+
processed_images = 0
|
62 |
+
|
63 |
+
for folder_name, ground_truth_label in [('real', 1), ('ai', 0)]:
|
64 |
+
folder_path = os.path.join(temp_dir, folder_name)
|
65 |
+
if not os.path.exists(folder_path):
|
66 |
+
raise ValueError(f"Folder not found: {folder_path}")
|
67 |
+
|
68 |
+
for img_name in os.listdir(folder_path):
|
69 |
+
img_path = os.path.join(folder_path, img_name)
|
70 |
+
try:
|
71 |
+
with Image.open(img_path).convert("RGB") as img:
|
72 |
+
_, prediction = detector.predict(img)
|
73 |
+
|
74 |
+
pred_label = 0 if prediction["AI"] > prediction["Real"] else 1
|
75 |
+
|
76 |
+
preds.append(pred_label)
|
77 |
+
labels.append(ground_truth_label)
|
78 |
+
images.append(img_name)
|
79 |
|
80 |
+
# Collect false positives and false negatives with image data
|
81 |
+
if pred_label != ground_truth_label:
|
82 |
+
with open(img_path, "rb") as img_file:
|
83 |
+
img_data = base64.b64encode(img_file.read()).decode()
|
84 |
+
if pred_label == 1 and ground_truth_label == 0:
|
85 |
+
false_positives.append((img_name, img_data))
|
86 |
+
elif pred_label == 0 and ground_truth_label == 1:
|
87 |
+
false_negatives.append((img_name, img_data))
|
88 |
|
89 |
+
except Exception as e:
|
90 |
+
print(f"Error processing image {img_name}: {e}")
|
91 |
+
|
92 |
+
processed_images += 1
|
93 |
+
gr.Progress(processed_images / total_images)
|
94 |
+
|
95 |
+
return evaluate_model(labels, preds, false_positives, false_negatives)
|
96 |
+
|
97 |
+
except Exception as e:
|
98 |
+
raise gr.Error(f"Error processing zip file: {str(e)}")
|
99 |
|
100 |
+
finally:
|
101 |
+
shutil.rmtree(temp_dir)
|
102 |
|
103 |
def format_classification_report(labels, preds):
|
104 |
# Convert the report string to a dictionary
|
|
|
280 |
|
281 |
def create_gradio_interface():
|
282 |
with gr.Blocks() as app:
|
283 |
+
gr.Markdown("""<center><h1>AI Image Detector<br><h4>(Demo)</h4></h1></center>""")
|
284 |
|
285 |
with gr.Tabs():
|
286 |
with gr.Tab("Single Image Detection"):
|
|
|
299 |
output_label = gr.Label(label="Output")
|
300 |
|
301 |
with gr.Tab("Batch Image Processing"):
|
302 |
+
zip_file = gr.File(
|
303 |
+
label="Upload Zip (Zip file must contain 2 folders, one labeled 'real' and one labled 'ai' )",
|
304 |
+
file_types=[".zip"],
|
305 |
+
file_count="single",
|
306 |
+
max_file_size=1024 # Increased to 1024 MB (1 GB)
|
307 |
+
)
|
308 |
batch_btn = gr.Button("Process Batch")
|
309 |
|
310 |
with gr.Group():
|
|
|
325 |
batch_btn.click(
|
326 |
process_zip,
|
327 |
zip_file,
|
328 |
+
[output_acc, output_roc, output_report, output_plots, output_fp_fn],
|
329 |
+
api_name="batch_process"
|
330 |
)
|
331 |
|
332 |
return app
|