Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
-
from tensorflow.keras.preprocessing import image
|
| 4 |
import numpy as np
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
-
import os
|
| 7 |
|
|
|
|
| 8 |
def load_model_from_hub(repo_id, filename):
|
| 9 |
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 10 |
return tf.keras.models.load_model(model_path)
|
| 11 |
|
| 12 |
-
# Load
|
| 13 |
-
model1 = load_model_from_hub("arsath-sm/face_classification_model1", "
|
| 14 |
-
model2 = load_model_from_hub("arsath-sm/face_classification_model2", "
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
img =
|
| 19 |
-
img = image.img_to_array(img)
|
| 20 |
-
img = np.expand_dims(img, axis=0)
|
| 21 |
img = img / 255.0
|
| 22 |
-
return img
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
preprocessed_img = preprocess_image(img)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
confidence2 =
|
| 36 |
|
| 37 |
return {
|
| 38 |
"Model 1 Prediction": f"{result1} (Confidence: {confidence1:.2f})",
|
|
@@ -41,8 +38,8 @@ def predict(img):
|
|
| 41 |
|
| 42 |
# Create the Gradio interface
|
| 43 |
iface = gr.Interface(
|
| 44 |
-
fn=
|
| 45 |
-
inputs=gr.Image(
|
| 46 |
outputs={
|
| 47 |
"Model 1 Prediction": gr.Textbox(),
|
| 48 |
"Model 2 Prediction": gr.Textbox()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 5 |
|
| 6 |
+
# Function to load model from H5 file
|
| 7 |
def load_model_from_hub(repo_id, filename):
|
| 8 |
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 9 |
return tf.keras.models.load_model(model_path)
|
| 10 |
|
| 11 |
+
# Load models from Hugging Face Hub
|
| 12 |
+
model1 = load_model_from_hub("arsath-sm/face_classification_model1", "model.h5")
|
| 13 |
+
model2 = load_model_from_hub("arsath-sm/face_classification_model2", "model.h5")
|
| 14 |
|
| 15 |
+
def preprocess_image(image):
|
| 16 |
+
img = tf.convert_to_tensor(image)
|
| 17 |
+
img = tf.image.resize(img, (150, 150))
|
|
|
|
|
|
|
| 18 |
img = img / 255.0
|
| 19 |
+
return tf.expand_dims(img, 0)
|
| 20 |
|
| 21 |
+
def predict_image(image):
|
| 22 |
+
preprocessed_image = preprocess_image(image)
|
|
|
|
| 23 |
|
| 24 |
+
# Make predictions using both models
|
| 25 |
+
pred1 = model1.predict(preprocessed_image)[0][0]
|
| 26 |
+
pred2 = model2.predict(preprocessed_image)[0][0]
|
| 27 |
|
| 28 |
+
# Prepare results for each model
|
| 29 |
+
result1 = "Real" if pred1 > 0.5 else "Fake"
|
| 30 |
+
confidence1 = pred1 if pred1 > 0.5 else 1 - pred1
|
| 31 |
+
result2 = "Real" if pred2 > 0.5 else "Fake"
|
| 32 |
+
confidence2 = pred2 if pred2 > 0.5 else 1 - pred2
|
| 33 |
|
| 34 |
return {
|
| 35 |
"Model 1 Prediction": f"{result1} (Confidence: {confidence1:.2f})",
|
|
|
|
| 38 |
|
| 39 |
# Create the Gradio interface
|
| 40 |
iface = gr.Interface(
|
| 41 |
+
fn=predict_image,
|
| 42 |
+
inputs=gr.Image(),
|
| 43 |
outputs={
|
| 44 |
"Model 1 Prediction": gr.Textbox(),
|
| 45 |
"Model 2 Prediction": gr.Textbox()
|