Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,8 @@ import warnings
|
|
| 9 |
import math
|
| 10 |
import numpy as np
|
| 11 |
from utils.goat import call_inference
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Suppress warnings
|
| 14 |
warnings.filterwarnings("ignore", category=UserWarning, message="Using a slow image processor as `use_fast` is unset")
|
|
@@ -45,13 +47,16 @@ labels_4 = ['AI', 'Real']
|
|
| 45 |
def softmax(vector):
|
| 46 |
e = np.exp(vector - np.max(vector)) # for numerical stability
|
| 47 |
return e / e.sum()
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
@spaces.GPU(duration=10)
|
| 50 |
def predict_image(img, confidence_threshold):
|
| 51 |
|
| 52 |
-
response5_raw = call_inference(img)
|
| 53 |
-
response5 = response5_raw.json()
|
| 54 |
-
|
| 55 |
# Ensure the image is a PIL Image
|
| 56 |
if not isinstance(img, Image.Image):
|
| 57 |
raise ValueError(f"Expected a PIL Image, but got {type(img)}")
|
|
@@ -161,14 +166,13 @@ def predict_image(img, confidence_threshold):
|
|
| 161 |
except Exception as e:
|
| 162 |
label_4 = f"Error: {str(e)}"
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
# label_3 = f"Error: {str(e)}"
|
| 172 |
|
| 173 |
|
| 174 |
# Combine results
|
|
@@ -177,7 +181,7 @@ def predict_image(img, confidence_threshold):
|
|
| 177 |
"ViT/AI-vs-Real": label_2,
|
| 178 |
"Swin/SDXL": label_3,
|
| 179 |
"Swin/SDXL-FLUX": label_4,
|
| 180 |
-
|
| 181 |
}
|
| 182 |
|
| 183 |
return combined_results
|
|
|
|
| 9 |
import math
|
| 10 |
import numpy as np
|
| 11 |
from utils.goat import call_inference
|
| 12 |
+
import io
|
| 13 |
+
import sys
|
| 14 |
|
| 15 |
# Suppress warnings
|
| 16 |
warnings.filterwarnings("ignore", category=UserWarning, message="Using a slow image processor as `use_fast` is unset")
|
|
|
|
| 47 |
def softmax(vector):
|
| 48 |
e = np.exp(vector - np.max(vector)) # for numerical stability
|
| 49 |
return e / e.sum()
|
| 50 |
+
|
| 51 |
+
def convert_pil_to_bytes(image, format='JPEG'):
|
| 52 |
+
img_byte_arr = io.BytesIO()
|
| 53 |
+
image.save(img_byte_arr, format=format)
|
| 54 |
+
img_byte_arr = img_byte_arr.getvalue()
|
| 55 |
+
return img_byte_arr
|
| 56 |
+
|
| 57 |
@spaces.GPU(duration=10)
|
| 58 |
def predict_image(img, confidence_threshold):
|
| 59 |
|
|
|
|
|
|
|
|
|
|
| 60 |
# Ensure the image is a PIL Image
|
| 61 |
if not isinstance(img, Image.Image):
|
| 62 |
raise ValueError(f"Expected a PIL Image, but got {type(img)}")
|
|
|
|
| 166 |
except Exception as e:
|
| 167 |
label_4 = f"Error: {str(e)}"
|
| 168 |
|
| 169 |
+
try:
|
| 170 |
+
img_bytes = convert_pil_to_bytes(img_pil)
|
| 171 |
+
response5_raw = call_inference(img_bytes)
|
| 172 |
+
response5 = response5_raw.json()
|
| 173 |
+
print(response5)
|
| 174 |
+
except Exception as e:
|
| 175 |
+
label_5 = f"Error: {str(e)}"
|
|
|
|
| 176 |
|
| 177 |
|
| 178 |
# Combine results
|
|
|
|
| 181 |
"ViT/AI-vs-Real": label_2,
|
| 182 |
"Swin/SDXL": label_3,
|
| 183 |
"Swin/SDXL-FLUX": label_4,
|
| 184 |
+
"GOAT": label_5
|
| 185 |
}
|
| 186 |
|
| 187 |
return combined_results
|