Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,8 @@ 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")
|
|
@@ -47,6 +49,10 @@ def softmax(vector):
|
|
| 47 |
|
| 48 |
@spaces.GPU(duration=10)
|
| 49 |
def predict_image(img, confidence_threshold):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
# Ensure the image is a PIL Image
|
| 51 |
if not isinstance(img, Image.Image):
|
| 52 |
raise ValueError(f"Expected a PIL Image, but got {type(img)}")
|
|
@@ -72,9 +78,9 @@ def predict_image(img, confidence_threshold):
|
|
| 72 |
|
| 73 |
# Check if either class meets the confidence threshold
|
| 74 |
if result_1['artificial'] >= confidence_threshold:
|
| 75 |
-
label_1 = f"
|
| 76 |
elif result_1['real'] >= confidence_threshold:
|
| 77 |
-
label_1 = f"
|
| 78 |
else:
|
| 79 |
label_1 = "Uncertain Classification"
|
| 80 |
except Exception as e:
|
|
@@ -92,9 +98,9 @@ def predict_image(img, confidence_threshold):
|
|
| 92 |
|
| 93 |
# Check if either class meets the confidence threshold
|
| 94 |
if result_2['AI Image'] >= confidence_threshold:
|
| 95 |
-
label_2 = f"
|
| 96 |
elif result_2['Real Image'] >= confidence_threshold:
|
| 97 |
-
label_2 = f"
|
| 98 |
else:
|
| 99 |
label_2 = "Uncertain Classification"
|
| 100 |
except Exception as e:
|
|
@@ -120,9 +126,9 @@ def predict_image(img, confidence_threshold):
|
|
| 120 |
|
| 121 |
# Check if either class meets the confidence threshold
|
| 122 |
if result_3['AI'] >= confidence_threshold:
|
| 123 |
-
label_3 = f"
|
| 124 |
elif result_3['Real'] >= confidence_threshold:
|
| 125 |
-
label_3 = f"
|
| 126 |
else:
|
| 127 |
label_3 = "Uncertain Classification"
|
| 128 |
except Exception as e:
|
|
@@ -148,9 +154,9 @@ def predict_image(img, confidence_threshold):
|
|
| 148 |
|
| 149 |
# Check if either class meets the confidence threshold
|
| 150 |
if result_4['AI'] >= confidence_threshold:
|
| 151 |
-
label_4 = f"
|
| 152 |
elif result_4['Real'] >= confidence_threshold:
|
| 153 |
-
label_4 = f"
|
| 154 |
else:
|
| 155 |
label_4 = "Uncertain Classification"
|
| 156 |
except Exception as e:
|
|
@@ -168,10 +174,10 @@ def predict_image(img, confidence_threshold):
|
|
| 168 |
|
| 169 |
# Combine results
|
| 170 |
combined_results = {
|
| 171 |
-
"SwinV2": label_1,
|
| 172 |
-
"AI-vs-Real
|
| 173 |
-
"
|
| 174 |
-
"
|
| 175 |
# "ALSv": label_5
|
| 176 |
}
|
| 177 |
|
|
|
|
| 8 |
import warnings
|
| 9 |
import math
|
| 10 |
import numpy as np
|
| 11 |
+
from utils import call_inference
|
| 12 |
+
|
| 13 |
|
| 14 |
# Suppress warnings
|
| 15 |
warnings.filterwarnings("ignore", category=UserWarning, message="Using a slow image processor as `use_fast` is unset")
|
|
|
|
| 49 |
|
| 50 |
@spaces.GPU(duration=10)
|
| 51 |
def predict_image(img, confidence_threshold):
|
| 52 |
+
|
| 53 |
+
response5_raw = call_inference(img)
|
| 54 |
+
response5 = response5_raw.json()
|
| 55 |
+
|
| 56 |
# Ensure the image is a PIL Image
|
| 57 |
if not isinstance(img, Image.Image):
|
| 58 |
raise ValueError(f"Expected a PIL Image, but got {type(img)}")
|
|
|
|
| 78 |
|
| 79 |
# Check if either class meets the confidence threshold
|
| 80 |
if result_1['artificial'] >= confidence_threshold:
|
| 81 |
+
label_1 = f"AI, Confidence: {result_1['artificial']:.4f}"
|
| 82 |
elif result_1['real'] >= confidence_threshold:
|
| 83 |
+
label_1 = f"Real, Confidence: {result_1['real']:.4f}"
|
| 84 |
else:
|
| 85 |
label_1 = "Uncertain Classification"
|
| 86 |
except Exception as e:
|
|
|
|
| 98 |
|
| 99 |
# Check if either class meets the confidence threshold
|
| 100 |
if result_2['AI Image'] >= confidence_threshold:
|
| 101 |
+
label_2 = f"AI, Confidence: {result_2['AI Image']:.4f}"
|
| 102 |
elif result_2['Real Image'] >= confidence_threshold:
|
| 103 |
+
label_2 = f"Real, Confidence: {result_2['Real Image']:.4f}"
|
| 104 |
else:
|
| 105 |
label_2 = "Uncertain Classification"
|
| 106 |
except Exception as e:
|
|
|
|
| 126 |
|
| 127 |
# Check if either class meets the confidence threshold
|
| 128 |
if result_3['AI'] >= confidence_threshold:
|
| 129 |
+
label_3 = f"AI, Confidence: {result_3['AI']:.4f}"
|
| 130 |
elif result_3['Real'] >= confidence_threshold:
|
| 131 |
+
label_3 = f"Real, Confidence: {result_3['Real']:.4f}"
|
| 132 |
else:
|
| 133 |
label_3 = "Uncertain Classification"
|
| 134 |
except Exception as e:
|
|
|
|
| 154 |
|
| 155 |
# Check if either class meets the confidence threshold
|
| 156 |
if result_4['AI'] >= confidence_threshold:
|
| 157 |
+
label_4 = f"AI, Confidence: {result_4['AI']:.4f}"
|
| 158 |
elif result_4['Real'] >= confidence_threshold:
|
| 159 |
+
label_4 = f"Real, Confidence: {result_4['Real']:.4f}"
|
| 160 |
else:
|
| 161 |
label_4 = "Uncertain Classification"
|
| 162 |
except Exception as e:
|
|
|
|
| 174 |
|
| 175 |
# Combine results
|
| 176 |
combined_results = {
|
| 177 |
+
"SwinV2/detect": label_1,
|
| 178 |
+
"ViT/AI-vs-Real": label_2,
|
| 179 |
+
"Swin/SDXL": label_3,
|
| 180 |
+
"Swin/SDXL-FLUX": label_4,
|
| 181 |
# "ALSv": label_5
|
| 182 |
}
|
| 183 |
|