Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -151,33 +151,6 @@ def extract_zip(zip_file):
|
|
151 |
z.extractall(temp_dir)
|
152 |
return temp_dir
|
153 |
|
154 |
-
# Function to classify images in a folder
|
155 |
-
# Function to classify images in a folder
|
156 |
-
def classify_images(image_dir, model_pipeline, model_idx):
|
157 |
-
images = []
|
158 |
-
labels = []
|
159 |
-
preds = []
|
160 |
-
for folder_name, ground_truth_label in [('real', 1), ('ai', 0)]:
|
161 |
-
folder_path = os.path.join(image_dir, folder_name)
|
162 |
-
if not os.path.exists(folder_path):
|
163 |
-
continue
|
164 |
-
for img_name in os.listdir(folder_path):
|
165 |
-
img_path = os.path.join(folder_path, img_name)
|
166 |
-
try:
|
167 |
-
img = Image.open(img_path).convert("RGB")
|
168 |
-
|
169 |
-
# Now use the specific model pipeline passed in
|
170 |
-
pred = model_pipeline(img)
|
171 |
-
pred_label = np.argmax([x['score'] for x in pred])
|
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 |
-
return labels, preds, images
|
179 |
-
|
180 |
-
|
181 |
# Function to classify images in a folder
|
182 |
def classify_images(image_dir, model_pipeline, model_idx):
|
183 |
images = []
|
@@ -203,6 +176,33 @@ def classify_images(image_dir, model_pipeline, model_idx):
|
|
203 |
print(f"Error processing image {img_name} in model {model_idx}: {e}")
|
204 |
return labels, preds, images
|
205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
# Batch processing for all models
|
207 |
def process_zip(zip_file):
|
208 |
extracted_dir = extract_zip(zip_file.name)
|
@@ -237,8 +237,6 @@ def process_zip(zip_file):
|
|
237 |
results['Model_2_cm_fig'], results['Model_2_roc_fig'])
|
238 |
|
239 |
|
240 |
-
|
241 |
-
|
242 |
# Single image section
|
243 |
def load_url(url):
|
244 |
try:
|
|
|
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 = []
|
|
|
176 |
print(f"Error processing image {img_name} in model {model_idx}: {e}")
|
177 |
return labels, preds, images
|
178 |
|
179 |
+
# Function to generate evaluation metrics
|
180 |
+
def evaluate_model(labels, preds):
|
181 |
+
cm = confusion_matrix(labels, preds)
|
182 |
+
accuracy = accuracy_score(labels, preds)
|
183 |
+
roc_score = roc_auc_score(labels, preds)
|
184 |
+
report = classification_report(labels, preds)
|
185 |
+
fpr, tpr, _ = roc_curve(labels, preds)
|
186 |
+
roc_auc = auc(fpr, tpr)
|
187 |
+
|
188 |
+
fig, ax = plt.subplots()
|
189 |
+
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=["AI", "Real"])
|
190 |
+
disp.plot(cmap=plt.cm.Blues, ax=ax)
|
191 |
+
plt.close(fig)
|
192 |
+
|
193 |
+
fig_roc, ax_roc = plt.subplots()
|
194 |
+
ax_roc.plot(fpr, tpr, color='blue', lw=2, label=f'ROC curve (area = {roc_auc:.2f})')
|
195 |
+
ax_roc.plot([0, 1], [0, 1], color='gray', linestyle='--')
|
196 |
+
ax_roc.set_xlim([0.0, 1.0])
|
197 |
+
ax_roc.set_ylim([0.0, 1.05])
|
198 |
+
ax_roc.set_xlabel('False Positive Rate')
|
199 |
+
ax_roc.set_ylabel('True Positive Rate')
|
200 |
+
ax_roc.set_title('Receiver Operating Characteristic (ROC) Curve')
|
201 |
+
ax_roc.legend(loc="lower right")
|
202 |
+
plt.close(fig_roc)
|
203 |
+
|
204 |
+
return accuracy, roc_score, report, fig, fig_roc
|
205 |
+
|
206 |
# Batch processing for all models
|
207 |
def process_zip(zip_file):
|
208 |
extracted_dir = extract_zip(zip_file.name)
|
|
|
237 |
results['Model_2_cm_fig'], results['Model_2_roc_fig'])
|
238 |
|
239 |
|
|
|
|
|
240 |
# Single image section
|
241 |
def load_url(url):
|
242 |
try:
|