File size: 5,645 Bytes
c96e0d1
 
 
 
 
 
 
3ba7ae0
 
 
 
 
c96e0d1
 
78cdd85
c96e0d1
 
 
 
 
3ba7ae0
 
 
 
 
 
 
 
 
 
c96e0d1
3ba7ae0
 
 
 
78cdd85
3ba7ae0
c96e0d1
3ba7ae0
 
c96e0d1
78cdd85
 
 
3ba7ae0
78cdd85
3ba7ae0
c96e0d1
 
 
3ba7ae0
78cdd85
 
3ba7ae0
 
 
 
78cdd85
c96e0d1
 
3ba7ae0
c96e0d1
3ba7ae0
 
c96e0d1
 
3ba7ae0
c96e0d1
 
3ba7ae0
 
 
 
 
 
c96e0d1
 
3ba7ae0
78cdd85
3ba7ae0
78cdd85
3ba7ae0
 
 
 
 
c96e0d1
 
 
3ba7ae0
 
c96e0d1
 
 
 
 
 
 
 
3ba7ae0
 
78cdd85
c96e0d1
 
 
3ba7ae0
 
c96e0d1
 
 
 
 
 
 
 
 
3ba7ae0
 
 
78cdd85
c96e0d1
 
 
 
78cdd85
 
 
 
c96e0d1
 
 
 
78cdd85
 
 
c96e0d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78cdd85
 
c96e0d1
 
78cdd85
c96e0d1
 
 
 
 
 
78cdd85
 
3ba7ae0
78cdd85
3ba7ae0
78cdd85
 
c96e0d1
3ba7ae0
c96e0d1
 
1
2
3
4
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
from huggingface_hub import hf_hub_download
import os
import pandas as pd
import logging

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Configuration
MODEL_REPO = "Ahmedhassan54/Image-Classification"
MODEL_FILE = "best_model.h5"

# Download model from Hugging Face Hub
def load_model_from_hf():
    try:
        logger.info("Attempting to load model...")
        
        # Check if model exists in cache
        model_path = hf_hub_download(
            repo_id=MODEL_REPO,
            filename=MODEL_FILE,
            cache_dir=".",
            force_download=True  # Ensure fresh download
        )
        logger.info(f"Model downloaded to: {model_path}")
        
        # Load model
        logger.info("Loading model...")
        model = tf.keras.models.load_model(model_path)
        logger.info("Model loaded successfully!")
        return model
        
    except Exception as e:
        logger.error(f"Model loading failed: {str(e)}")
        raise gr.Error(f"⚠️ Model loading failed: {str(e)}. Check the logs for details.")

# Load model when the app starts
try:
    model = load_model_from_hf()
except Exception as e:
    model = None
    logger.error(f"Proceeding without model due to: {str(e)}")

def classify_image(image):
    try:
        logger.info("\nClassification started...")
        
        # Debug: Check input type
        logger.info(f"Input type: {type(image)}")
        
        if image is None:
            raise ValueError("No image provided")
        
        # Convert image if needed
        if isinstance(image, np.ndarray):
            logger.info("Converting numpy array to PIL Image")
            image = Image.fromarray(image)
        elif not isinstance(image, Image.Image):
            raise ValueError(f"Unexpected image type: {type(image)}")
        
        # Preprocess image
        logger.info("Preprocessing image...")
        image = image.resize((150, 150))
        image_array = np.array(image) / 255.0
        
        # Add batch dimension
        if len(image_array.shape) == 3:
            image_array = np.expand_dims(image_array, axis=0)
        
        logger.info(f"Image array shape: {image_array.shape}")
        
        # Make prediction
        logger.info("Making prediction...")
        if model is None:
            raise gr.Error("Model failed to load. Cannot make predictions.")
        
        prediction = model.predict(image_array, verbose=0)
        logger.info(f"Raw prediction: {prediction}")
        
        confidence = float(prediction[0][0])
        logger.info(f"Confidence score: {confidence}")
        
        # Format outputs
        label_output = {
            "Cat": round(1 - confidence, 4),
            "Dog": round(confidence, 4)
        }
        
        # Create dataframe for bar plot
        plot_data = pd.DataFrame({
            'Class': ['Cat', 'Dog'],
            'Confidence': [1 - confidence, confidence]
        })
        
        logger.info("Classification successful!")
        logger.info(f"Results: {label_output}")
        
        return label_output, plot_data
        
    except Exception as e:
        logger.error(f"Error during classification: {str(e)}", exc_info=True)
        raise gr.Error(f"🔴 Classification failed: {str(e)}")

# Custom CSS
css = """

.gradio-container {

    background: linear-gradient(to right, #f5f7fa, #c3cfe2);

}

footer {

    visibility: hidden

}

.error-message {

    color: red !important;

    font-weight: bold !important;

}

"""

# Build the interface
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
    gr.Markdown("""

    # 🐾 Cat vs Dog Classifier 🦮

    Upload an image to classify whether it's a cat or dog

    """)
    
    with gr.Row():
        with gr.Column():
            image_input = gr.Image(label="Upload Image", type="pil")
            with gr.Row():
                submit_btn = gr.Button("Classify", variant="primary")
                clear_btn = gr.Button("Clear")
        
        with gr.Column():
            label_output = gr.Label(label="Predictions", num_top_classes=2)
            confidence_bar = gr.BarPlot(
                pd.DataFrame({'Class': ['Cat', 'Dog'], 'Confidence': [0.5, 0.5]}),
                x="Class",
                y="Confidence",
                y_lim=[0,1],
                title="Confidence Scores",
                width=400,
                height=300,
                container=False
            )
    
    # Example images
    gr.Examples(
        examples=[
            ["https://upload.wikimedia.org/wikipedia/commons/1/15/Cat_August_2010-4.jpg"],
            ["https://upload.wikimedia.org/wikipedia/commons/d/d9/Collage_of_Nine_Dogs.jpg"]
        ],
        inputs=image_input,
        outputs=[label_output, confidence_bar],
        fn=classify_image,
        cache_examples=True,
        label="Try these examples:"
    )
    
    # Button actions
    submit_btn.click(
        fn=classify_image,
        inputs=image_input,
        outputs=[label_output, confidence_bar],
        api_name="classify"
    )
    
    clear_btn.click(
        fn=lambda: [None, pd.DataFrame({'Class': ['Cat', 'Dog'], 'Confidence': [0.5, 0.5]})],
        inputs=None,
        outputs=[image_input, confidence_bar],
        show_progress=False
    )

# Launch the app
if __name__ == "__main__":
    demo.launch(debug=True)