Upload app.py
Browse files
app.py
CHANGED
@@ -1,96 +1,118 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import tensorflow as tf
|
3 |
-
import numpy as np
|
4 |
-
from PIL import Image
|
5 |
-
from huggingface_hub import hf_hub_download
|
6 |
-
import os
|
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 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
gr.
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
import os
|
7 |
+
import pandas as pd
|
8 |
+
|
9 |
+
# Configuration
|
10 |
+
MODEL_REPO = "Ahmedhassan54/Image-Classification" # Changed to your actual repo
|
11 |
+
MODEL_FILE = "best_model.h5"
|
12 |
+
|
13 |
+
# Download model from Hugging Face Hub
|
14 |
+
def load_model_from_hf():
|
15 |
+
try:
|
16 |
+
if not os.path.exists(MODEL_FILE):
|
17 |
+
print("Downloading model from Hugging Face Hub...")
|
18 |
+
model_path = hf_hub_download(
|
19 |
+
repo_id=MODEL_REPO,
|
20 |
+
filename=MODEL_FILE,
|
21 |
+
cache_dir="."
|
22 |
+
)
|
23 |
+
os.system(f"cp {model_path} {MODEL_FILE}")
|
24 |
+
|
25 |
+
return tf.keras.models.load_model(MODEL_FILE)
|
26 |
+
except Exception as e:
|
27 |
+
raise gr.Error(f"Model loading failed: {str(e)}")
|
28 |
+
|
29 |
+
model = load_model_from_hf()
|
30 |
+
|
31 |
+
def classify_image(image):
|
32 |
+
try:
|
33 |
+
# Convert image if needed
|
34 |
+
if isinstance(image, np.ndarray):
|
35 |
+
image = Image.fromarray(image)
|
36 |
+
|
37 |
+
# Preprocess image
|
38 |
+
image = image.resize((150, 150))
|
39 |
+
image_array = np.array(image) / 255.0
|
40 |
+
image_array = np.expand_dims(image_array, axis=0)
|
41 |
+
|
42 |
+
# Make prediction
|
43 |
+
prediction = model.predict(image_array, verbose=0)
|
44 |
+
confidence = float(prediction[0][0])
|
45 |
+
|
46 |
+
# Format outputs
|
47 |
+
label_output = {
|
48 |
+
"Cat": 1 - confidence,
|
49 |
+
"Dog": confidence
|
50 |
+
}
|
51 |
+
|
52 |
+
# Create dataframe for bar plot
|
53 |
+
plot_data = pd.DataFrame({
|
54 |
+
'Class': ['Cat', 'Dog'],
|
55 |
+
'Confidence': [1 - confidence, confidence]
|
56 |
+
})
|
57 |
+
|
58 |
+
return label_output, plot_data
|
59 |
+
|
60 |
+
except Exception as e:
|
61 |
+
print(f"Error: {str(e)}") # Debug print
|
62 |
+
raise gr.Error(f"Classification error: {str(e)}")
|
63 |
+
|
64 |
+
# Custom CSS
|
65 |
+
css = """
|
66 |
+
.gradio-container {
|
67 |
+
background: linear-gradient(to right, #f5f7fa, #c3cfe2);
|
68 |
+
}
|
69 |
+
footer {
|
70 |
+
visibility: hidden
|
71 |
+
}
|
72 |
+
"""
|
73 |
+
|
74 |
+
# Build the interface
|
75 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
76 |
+
gr.Markdown("# 🐾 Cat vs Dog Classifier 🦮")
|
77 |
+
gr.Markdown("Upload an image to classify whether it's a cat or dog")
|
78 |
+
|
79 |
+
with gr.Row():
|
80 |
+
with gr.Column():
|
81 |
+
image_input = gr.Image(label="Upload Image", type="pil")
|
82 |
+
submit_btn = gr.Button("Classify", variant="primary")
|
83 |
+
|
84 |
+
with gr.Column():
|
85 |
+
label_output = gr.Label(label="Predictions", num_top_classes=2)
|
86 |
+
confidence_bar = gr.BarPlot(
|
87 |
+
pd.DataFrame({'Class': ['Cat', 'Dog'], 'Confidence': [0.5, 0.5]}),
|
88 |
+
x="Class",
|
89 |
+
y="Confidence",
|
90 |
+
y_lim=[0,1],
|
91 |
+
title="Confidence Scores",
|
92 |
+
width=400,
|
93 |
+
height=300,
|
94 |
+
container=False
|
95 |
+
)
|
96 |
+
|
97 |
+
# Example images
|
98 |
+
gr.Examples(
|
99 |
+
examples=[
|
100 |
+
["https://upload.wikimedia.org/wikipedia/commons/1/15/Cat_August_2010-4.jpg"],
|
101 |
+
["https://upload.wikimedia.org/wikipedia/commons/d/d9/Collage_of_Nine_Dogs.jpg"]
|
102 |
+
],
|
103 |
+
inputs=image_input,
|
104 |
+
outputs=[label_output, confidence_bar],
|
105 |
+
fn=classify_image,
|
106 |
+
cache_examples=True
|
107 |
+
)
|
108 |
+
|
109 |
+
# Button action
|
110 |
+
submit_btn.click(
|
111 |
+
fn=classify_image,
|
112 |
+
inputs=image_input,
|
113 |
+
outputs=[label_output, confidence_bar],
|
114 |
+
api_name="classify"
|
115 |
+
)
|
116 |
+
|
117 |
+
if __name__ == "__main__":
|
118 |
+
demo.launch(debug=True)
|