Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import cv2
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from tensorflow.keras.utils import img_to_array
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
+
|
| 7 |
+
# Load your pre-trained model
|
| 8 |
+
model = load_model(r'model.h5')
|
| 9 |
+
|
| 10 |
+
# Define the prediction function that takes an image as input and returns the predicted label
|
| 11 |
+
def predict_image(img):
|
| 12 |
+
# Preprocess the image if needed
|
| 13 |
+
x = img_to_array(img)
|
| 14 |
+
x = cv2.resize(x, (299, 299), interpolation=cv2.INTER_AREA)
|
| 15 |
+
x /= 255
|
| 16 |
+
x = np.expand_dims(x, axis=0)
|
| 17 |
+
image = np.vstack([x])
|
| 18 |
+
# Make a prediction using your model
|
| 19 |
+
prediction = model.predict(image)
|
| 20 |
+
# Assuming your model returns probabilities, get the label with the highest probability
|
| 21 |
+
predicted_label = "dog" if prediction > 0.5 else "cat"
|
| 22 |
+
return predicted_label
|
| 23 |
+
|
| 24 |
+
# Define the Gradio Interface with the desired title and description
|
| 25 |
+
description_html = """
|
| 26 |
+
<p>This model was trained by Moaz Eldsouky You can find more about me here:</p>
|
| 27 |
+
<p>GitHub: <a href="https://github.com/MoazEldsouky">GitHub Profile</a></p>
|
| 28 |
+
<p>LinkedIn: <a href="https://www.linkedin.com/in/moaz-eldesouky-762288251/">LinkedIn Profile</a></p>
|
| 29 |
+
<p>Kaggle: <a href="https://www.kaggle.com/moazeldsokyx">Kaggle Profile</a></p>
|
| 30 |
+
<p>This model was trained to predict whether an image contains a cat or a dog.</p>
|
| 31 |
+
<p>You can see how this model was trained on the following Kaggle Notebook:</p>
|
| 32 |
+
<p><a href="https://www.kaggle.com/code/moazeldsokyx/dogs-vs-cats-classification-with-xception">Kaggle Notebook</a></p>
|
| 33 |
+
<p>Upload a photo to see how the model predicts!</p>
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
# Example images for a dog and a cat
|
| 37 |
+
example_dog_image = "dog_.jpeg"
|
| 38 |
+
example_cat_image = "FELV-cat.jpg"
|
| 39 |
+
|
| 40 |
+
gr.Interface(
|
| 41 |
+
fn=predict_image,
|
| 42 |
+
inputs="image",
|
| 43 |
+
outputs="text",
|
| 44 |
+
title="Dogs vs Cats classification with Xception 🐶vs 😺",
|
| 45 |
+
description=description_html,
|
| 46 |
+
allow_flagging='never',
|
| 47 |
+
examples=[
|
| 48 |
+
[example_dog_image], # Example image for a dog
|
| 49 |
+
[example_cat_image], # Example image for a cat
|
| 50 |
+
]
|
| 51 |
+
).launch()
|