Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ from PIL import Image
|
|
7 |
import pandas as pd
|
8 |
import warnings
|
9 |
import math
|
|
|
10 |
|
11 |
# Suppress warnings
|
12 |
warnings.filterwarnings("ignore", category=UserWarning, message="Using a slow image processor as `use_fast` is unset")
|
@@ -26,8 +27,8 @@ clf_2 = pipeline("image-classification", model=model_2_path)
|
|
26 |
|
27 |
# Load additional models
|
28 |
models = ["Organika/sdxl-detector", "cmckinle/sdxl-flux-detector"]
|
29 |
-
pipe0 = pipeline("image-classification", model=models[0]
|
30 |
-
pipe1 = pipeline("image-classification", model=models[1]
|
31 |
|
32 |
# Define class names for all models
|
33 |
class_names_1 = ['artificial', 'real']
|
@@ -36,7 +37,7 @@ class_names_3 = ['AI', 'Real']
|
|
36 |
class_names_4 = ['AI', 'Real']
|
37 |
|
38 |
def softmax(vector):
|
39 |
-
e =
|
40 |
return e / e.sum()
|
41 |
|
42 |
@spaces.GPU(duration=10)
|
@@ -94,12 +95,13 @@ def predict_image(img, confidence_threshold):
|
|
94 |
except Exception as e:
|
95 |
label_2 = f"Error: {str(e)}"
|
96 |
|
97 |
-
# Predict using the third model
|
98 |
try:
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
103 |
|
104 |
# Ensure the result dictionary contains all class names
|
105 |
for class_name in class_names_3:
|
@@ -116,12 +118,13 @@ def predict_image(img, confidence_threshold):
|
|
116 |
except Exception as e:
|
117 |
label_3 = f"Error: {str(e)}"
|
118 |
|
119 |
-
# Predict using the fourth model
|
120 |
try:
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
125 |
|
126 |
# Ensure the result dictionary contains all class names
|
127 |
for class_name in class_names_4:
|
|
|
7 |
import pandas as pd
|
8 |
import warnings
|
9 |
import math
|
10 |
+
import numpy as np
|
11 |
|
12 |
# Suppress warnings
|
13 |
warnings.filterwarnings("ignore", category=UserWarning, message="Using a slow image processor as `use_fast` is unset")
|
|
|
27 |
|
28 |
# Load additional models
|
29 |
models = ["Organika/sdxl-detector", "cmckinle/sdxl-flux-detector"]
|
30 |
+
pipe0 = pipeline("image-classification", model=models[0])
|
31 |
+
pipe1 = pipeline("image-classification", model=models[1])
|
32 |
|
33 |
# Define class names for all models
|
34 |
class_names_1 = ['artificial', 'real']
|
|
|
37 |
class_names_4 = ['AI', 'Real']
|
38 |
|
39 |
def softmax(vector):
|
40 |
+
e = np.exp(vector - np.max(vector)) # for numerical stability
|
41 |
return e / e.sum()
|
42 |
|
43 |
@spaces.GPU(duration=10)
|
|
|
95 |
except Exception as e:
|
96 |
label_2 = f"Error: {str(e)}"
|
97 |
|
98 |
+
# Predict using the third model with softmax
|
99 |
try:
|
100 |
+
with torch.no_grad():
|
101 |
+
outputs = pipe0(img_pil)
|
102 |
+
logits = outputs[0].logits if isinstance(outputs, list) else outputs.logits
|
103 |
+
probabilities = softmax(logits.cpu().numpy())
|
104 |
+
result_3 = {class_names_3[idx]: float(probabilities[idx]) for idx in range(len(class_names_3))}
|
105 |
|
106 |
# Ensure the result dictionary contains all class names
|
107 |
for class_name in class_names_3:
|
|
|
118 |
except Exception as e:
|
119 |
label_3 = f"Error: {str(e)}"
|
120 |
|
121 |
+
# Predict using the fourth model with softmax
|
122 |
try:
|
123 |
+
with torch.no_grad():
|
124 |
+
outputs = pipe1(img_pil)
|
125 |
+
logits = outputs[0].logits if isinstance(outputs, list) else outputs.logits
|
126 |
+
probabilities = softmax(logits.cpu().numpy())
|
127 |
+
result_4 = {class_names_4[idx]: float(probabilities[idx]) for idx in range(len(class_names_4))}
|
128 |
|
129 |
# Ensure the result dictionary contains all class names
|
130 |
for class_name in class_names_4:
|