Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,12 @@ import numpy as np
|
|
12 |
import urllib.request
|
13 |
import base64
|
14 |
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
MODEL_NAME = "cmckinle/sdxl-flux-detector"
|
17 |
LABELS = ["AI", "Real"]
|
@@ -41,10 +47,34 @@ class AIDetector:
|
|
41 |
|
42 |
return label, results
|
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()
|
@@ -87,14 +117,16 @@ def process_zip(zip_file):
|
|
87 |
false_negatives.append((img_name, img_data))
|
88 |
|
89 |
except Exception as e:
|
90 |
-
|
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:
|
@@ -313,7 +345,8 @@ def create_gradio_interface():
|
|
313 |
label="Upload Zip (must contain 'real' and 'ai' folders)",
|
314 |
file_types=[".zip"],
|
315 |
file_count="single",
|
316 |
-
max_file_size=1024 #
|
|
|
317 |
)
|
318 |
batch_btn = gr.Button("Process Batch", interactive=False)
|
319 |
|
@@ -333,23 +366,4 @@ def create_gradio_interface():
|
|
333 |
)
|
334 |
|
335 |
def enable_batch_btn(file):
|
336 |
-
return gr.Button.update(interactive=file is not None)
|
337 |
-
|
338 |
-
zip_file.upload(
|
339 |
-
enable_batch_btn,
|
340 |
-
zip_file,
|
341 |
-
batch_btn
|
342 |
-
)
|
343 |
-
|
344 |
-
batch_btn.click(
|
345 |
-
process_zip,
|
346 |
-
zip_file,
|
347 |
-
[output_acc, output_roc, output_report, output_plots, output_fp_fn],
|
348 |
-
api_name="batch_process"
|
349 |
-
)
|
350 |
-
|
351 |
-
return app
|
352 |
-
|
353 |
-
if __name__ == "__main__":
|
354 |
-
app = create_gradio_interface()
|
355 |
-
app.launch(show_api=False, max_threads=24)
|
|
|
12 |
import urllib.request
|
13 |
import base64
|
14 |
from io import BytesIO
|
15 |
+
import logging
|
16 |
+
from tqdm import tqdm
|
17 |
+
|
18 |
+
# Set up logging
|
19 |
+
logging.basicConfig(filename='app.log', level=logging.DEBUG,
|
20 |
+
format='%(asctime)s - %(levelname)s - %(message)s')
|
21 |
|
22 |
MODEL_NAME = "cmckinle/sdxl-flux-detector"
|
23 |
LABELS = ["AI", "Real"]
|
|
|
47 |
|
48 |
return label, results
|
49 |
|
50 |
+
def custom_upload_handler(file):
|
51 |
+
try:
|
52 |
+
logging.info(f"Starting upload of file: {file.name}")
|
53 |
+
file_size = os.path.getsize(file.name)
|
54 |
+
logging.info(f"File size: {file_size} bytes")
|
55 |
+
|
56 |
+
# Read and process the file in chunks
|
57 |
+
chunk_size = 1024 * 1024 # 1MB chunks
|
58 |
+
total_chunks = file_size // chunk_size + (1 if file_size % chunk_size > 0 else 0)
|
59 |
+
|
60 |
+
with open(file.name, 'rb') as f:
|
61 |
+
for chunk in tqdm(range(total_chunks), desc="Uploading"):
|
62 |
+
data = f.read(chunk_size)
|
63 |
+
if not data:
|
64 |
+
break
|
65 |
+
logging.debug(f"Processed chunk {chunk+1} of {total_chunks}")
|
66 |
+
|
67 |
+
logging.info("File upload completed successfully")
|
68 |
+
return file
|
69 |
+
except Exception as e:
|
70 |
+
logging.error(f"Error during file upload: {str(e)}")
|
71 |
+
raise gr.Error(f"Upload failed: {str(e)}")
|
72 |
+
|
73 |
def process_zip(zip_file):
|
74 |
temp_dir = tempfile.mkdtemp()
|
75 |
|
76 |
try:
|
77 |
+
logging.info(f"Starting to process zip file: {zip_file.name}")
|
78 |
# Validate zip structure
|
79 |
with zipfile.ZipFile(zip_file.name, 'r') as z:
|
80 |
file_list = z.namelist()
|
|
|
117 |
false_negatives.append((img_name, img_data))
|
118 |
|
119 |
except Exception as e:
|
120 |
+
logging.error(f"Error processing image {img_name}: {e}")
|
121 |
|
122 |
processed_images += 1
|
123 |
gr.Progress(processed_images / total_images)
|
124 |
|
125 |
+
logging.info("Zip file processing completed successfully")
|
126 |
return evaluate_model(labels, preds, false_positives, false_negatives)
|
127 |
|
128 |
except Exception as e:
|
129 |
+
logging.error(f"Error processing zip file: {str(e)}")
|
130 |
raise gr.Error(f"Error processing zip file: {str(e)}")
|
131 |
|
132 |
finally:
|
|
|
345 |
label="Upload Zip (must contain 'real' and 'ai' folders)",
|
346 |
file_types=[".zip"],
|
347 |
file_count="single",
|
348 |
+
max_file_size=1024 * 10, # 10240 MB (10 GB)
|
349 |
+
preprocess=custom_upload_handler
|
350 |
)
|
351 |
batch_btn = gr.Button("Process Batch", interactive=False)
|
352 |
|
|
|
366 |
)
|
367 |
|
368 |
def enable_batch_btn(file):
|
369 |
+
return gr.Button.update(interactive=file is not None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|