Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,54 @@
|
|
| 1 |
-
import os
|
| 2 |
-
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # Suppress all TensorFlow warnings
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
| 5 |
import tensorflow as tf
|
| 6 |
from tensorflow import keras
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
try:
|
| 10 |
-
siamese = keras.models.load_model("my_siamese.keras")
|
| 11 |
except Exception as e:
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
# Load
|
| 15 |
def load_stored_images():
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
np.expand_dims(tf.keras.utils.load_img("bash_example.jpg").resize((160, 160)), 0),
|
| 20 |
-
np.expand_dims(tf.keras.utils.load_img("usa_example.jpg").resize((160, 160)), 0)
|
| 21 |
-
]
|
| 22 |
except Exception as e:
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
stored_imgs = load_stored_images()
|
| 26 |
|
| 27 |
# Inference function
|
| 28 |
def check_membership(uploaded_image):
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
predictions = [
|
| 35 |
-
siamese.predict([uploaded_image, img])[0][0]
|
| 36 |
-
for img in stored_imgs
|
| 37 |
-
]
|
| 38 |
-
|
| 39 |
if predictions[0] < 0.5:
|
| 40 |
-
return "
|
| 41 |
elif predictions[1] < 0.5:
|
| 42 |
-
return "
|
| 43 |
elif predictions[2] < 0.5:
|
| 44 |
-
return "
|
| 45 |
else:
|
| 46 |
-
return "
|
| 47 |
except Exception as e:
|
| 48 |
-
return f"Error: {
|
| 49 |
|
| 50 |
-
#
|
| 51 |
iface = gr.Interface(
|
| 52 |
fn=check_membership,
|
| 53 |
-
inputs=gr.Image(shape=(
|
| 54 |
outputs="text",
|
| 55 |
-
title="
|
| 56 |
-
examples=["mun_example.jpg", "bash_example.jpg", "usa_example.jpg"]
|
| 57 |
)
|
| 58 |
|
| 59 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
| 4 |
from tensorflow import keras
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Suppress oneDNN warnings (optional)
|
| 8 |
+
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
| 9 |
|
| 10 |
+
# Load the model
|
| 11 |
try:
|
| 12 |
+
siamese = keras.models.load_model("my_siamese.keras", compile=False)
|
| 13 |
except Exception as e:
|
| 14 |
+
print(f"Error loading model: {e}")
|
| 15 |
+
raise
|
| 16 |
|
| 17 |
+
# Load stored images
|
| 18 |
def load_stored_images():
|
| 19 |
try:
|
| 20 |
+
stored_images = [np.zeros((256, 256, 3)) for _ in range(3)] # Mock images
|
| 21 |
+
return np.array([np.expand_dims(img, axis=0) for img in stored_images])
|
|
|
|
|
|
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
+
print(f"Error loading stored images: {e}")
|
| 24 |
+
raise
|
| 25 |
|
| 26 |
stored_imgs = load_stored_images()
|
| 27 |
|
| 28 |
# Inference function
|
| 29 |
def check_membership(uploaded_image):
|
| 30 |
try:
|
| 31 |
+
uploaded_image = np.expand_dims(uploaded_image, axis=0).astype("float32")
|
| 32 |
+
|
| 33 |
+
predictions = [siamese.predict([uploaded_image, img])[0][0] for img in stored_imgs]
|
| 34 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if predictions[0] < 0.5:
|
| 36 |
+
return "You are welcome Munzali"
|
| 37 |
elif predictions[1] < 0.5:
|
| 38 |
+
return "You are welcome Ahmad"
|
| 39 |
elif predictions[2] < 0.5:
|
| 40 |
+
return "You are welcome Usama"
|
| 41 |
else:
|
| 42 |
+
return "You are not a member"
|
| 43 |
except Exception as e:
|
| 44 |
+
return f"Error: {e}"
|
| 45 |
|
| 46 |
+
# Gradio interface
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=check_membership,
|
| 49 |
+
inputs=gr.Image(shape=(256, 256), image_mode="RGB"),
|
| 50 |
outputs="text",
|
| 51 |
+
title="Siamese Network Membership Check"
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
+
iface.launch()
|