AIOmarRehan commited on
Commit
2756fe4
Β·
verified Β·
1 Parent(s): 82b71ae

Update app/gradio_app.py

Browse files
Files changed (1) hide show
  1. 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
- model_path = os.path.join(
7
- os.path.dirname(os.path.dirname(__file__)),
8
- "saved_model",
9
- "InceptionV3_Dogs_and_Cats_Classification.h5"
10
- )
11
-
12
- def classify_image(image):
13
- if image is None:
14
- return None, {"error": "Please upload an image"}
15
-
16
- try:
17
- label, confidence, probs = predict(image)
18
- results = {
19
- "Predicted Class": label,
20
- "Confidence": f"{confidence * 100:.2f}%",
21
- "Cat Probability": f"{probs['Cat'] * 100:.2f}%",
22
- "Dog Probability": f"{probs['Dog'] * 100:.2f}%"
23
- }
24
- return image, results
25
-
26
- except Exception as e:
27
- return image, {"error": f"Classification failed: {str(e)}"}
28
-
29
- with gr.Blocks(title="Cats vs Dogs Classifier", theme=gr.themes.Soft()) as demo:
30
- gr.Markdown(
31
- """
32
- # Cats vs Dogs Classifier
33
-
34
- Upload an image of a cat or dog, and the InceptionV3 model will classify it!
35
-
36
- **Model:** InceptionV3 (Transfer Learning)
37
- **Classes:** Cat | Dog
38
- **Image Size:** 256x256 pixels
39
-
40
- **NOTE:**
41
- - You can upload an image from your device, just press "X" icon and start uploading.
42
- """
43
- )
44
-
45
- with gr.Row():
46
- with gr.Column():
47
- gr.Markdown("### Upload Image")
48
- image_input = gr.Image(
49
- type="pil",
50
- label="Upload Image",
51
- sources=["upload", "webcam"],
52
- interactive=True
53
- )
54
- with gr.Column():
55
- gr.Markdown("### Prediction Results")
56
- output = gr.JSON(label="Classification Results")
57
-
58
- submit_btn = gr.Button("Classify Image", variant="primary", scale=1)
59
- submit_btn.click(
60
- fn=classify_image,
61
- inputs=image_input,
62
- outputs=[image_input, output]
63
- )
64
-
65
- gr.Markdown("### Examples")
66
- gr.Examples(
67
- examples=[
68
- ["examples/cat1.jpg"],
69
- ["examples/cat2.jpg"],
70
- ["examples/cat3.jpg"],
71
- ["examples/dog1.jpg"],
72
- ["examples/dog2.jpg"]
73
- ],
74
- inputs=image_input,
75
- outputs=[image_input, output],
76
- fn=classify_image,
77
- run_on_click=True,
78
- label="Example Images (Click to run)"
79
- )
80
-
81
- if __name__ == "__main__":
82
- demo.launch(
83
- server_name="0.0.0.0",
84
- server_port=7860,
85
- share=False,
86
- show_error=True
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  )