Update app/gradio_app.py
Browse files- app/gradio_app.py +99 -86
app/gradio_app.py
CHANGED
|
@@ -1,87 +1,100 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
-
from .model import predict
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
gr.
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
inputs=
|
| 75 |
-
outputs=
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from .model import predict
|
| 4 |
+
import os
|
| 5 |
+
import random
|
| 6 |
+
from datasets import load_dataset
|
| 7 |
+
|
| 8 |
+
# Load Hugging Face dataset locally
|
| 9 |
+
ds = load_dataset("AIOmarRehan/Cats_and_Dogs", split="train") # adjust split if needed
|
| 10 |
+
|
| 11 |
+
model_path = os.path.join(
|
| 12 |
+
os.path.dirname(os.path.dirname(__file__)),
|
| 13 |
+
"saved_model",
|
| 14 |
+
"InceptionV3_Dogs_and_Cats_Classification.h5"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
def classify_image(image):
|
| 18 |
+
if image is None:
|
| 19 |
+
return None, {"error": "Please upload an image"}
|
| 20 |
+
try:
|
| 21 |
+
label, confidence, probs = predict(image)
|
| 22 |
+
results = {
|
| 23 |
+
"Predicted Class": label,
|
| 24 |
+
"Confidence": f"{confidence * 100:.2f}%",
|
| 25 |
+
"Cat Probability": f"{probs['Cat'] * 100:.2f}%",
|
| 26 |
+
"Dog Probability": f"{probs['Dog'] * 100:.2f}%"
|
| 27 |
+
}
|
| 28 |
+
return image, results
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return image, {"error": f"Classification failed: {str(e)}"}
|
| 31 |
+
|
| 32 |
+
def get_random_image():
|
| 33 |
+
# Pick a random sample from the dataset
|
| 34 |
+
sample = ds.shuffle(seed=random.randint(0, 9999))[0]
|
| 35 |
+
img = Image.open(sample["image"]).convert("RGB")
|
| 36 |
+
return img
|
| 37 |
+
|
| 38 |
+
with gr.Blocks(title="Cats vs Dogs Classifier", theme=gr.themes.Soft()) as demo:
|
| 39 |
+
gr.Markdown(
|
| 40 |
+
"""
|
| 41 |
+
# Cats vs Dogs Classifier
|
| 42 |
+
|
| 43 |
+
Upload an image of a cat or dog, and the InceptionV3 model will classify it!
|
| 44 |
+
|
| 45 |
+
**Model:** InceptionV3 (Transfer Learning)
|
| 46 |
+
**Classes:** Cat | Dog
|
| 47 |
+
**Image Size:** 256x256 pixels
|
| 48 |
+
"""
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
with gr.Column():
|
| 53 |
+
gr.Markdown("### Upload Image")
|
| 54 |
+
image_input = gr.Image(
|
| 55 |
+
type="pil",
|
| 56 |
+
label="Upload Image",
|
| 57 |
+
sources=["upload", "webcam"],
|
| 58 |
+
interactive=True
|
| 59 |
+
)
|
| 60 |
+
random_btn = gr.Button("Random Image from Dataset")
|
| 61 |
+
with gr.Column():
|
| 62 |
+
gr.Markdown("### Prediction Results")
|
| 63 |
+
output = gr.JSON(label="Classification Results")
|
| 64 |
+
|
| 65 |
+
submit_btn = gr.Button("Classify Image", variant="primary", scale=1)
|
| 66 |
+
submit_btn.click(
|
| 67 |
+
fn=classify_image,
|
| 68 |
+
inputs=image_input,
|
| 69 |
+
outputs=[image_input, output]
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
random_btn.click(
|
| 73 |
+
fn=get_random_image,
|
| 74 |
+
inputs=[],
|
| 75 |
+
outputs=image_input
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
gr.Markdown("### Examples")
|
| 79 |
+
gr.Examples(
|
| 80 |
+
examples=[
|
| 81 |
+
["examples/cat1.jpg"],
|
| 82 |
+
["examples/cat2.jpg"],
|
| 83 |
+
["examples/cat3.jpg"],
|
| 84 |
+
["examples/dog1.jpg"],
|
| 85 |
+
["examples/dog2.jpg"]
|
| 86 |
+
],
|
| 87 |
+
inputs=image_input,
|
| 88 |
+
outputs=[image_input, output],
|
| 89 |
+
fn=classify_image,
|
| 90 |
+
run_on_click=True,
|
| 91 |
+
label="Example Images (Click to run)"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
demo.launch(
|
| 96 |
+
server_name="0.0.0.0",
|
| 97 |
+
server_port=7860,
|
| 98 |
+
share=False,
|
| 99 |
+
show_error=True
|
| 100 |
)
|