File size: 1,078 Bytes
b3aa582
d7e902b
ebb9d97
 
b3aa582
ebb9d97
 
d7e902b
ebb9d97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3aa582
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
import gradio as gr
import joblib
import numpy as np
from PIL import Image

# Load your model
with open("model.pkl", "rb") as f:
    model = joblib.load(f)

# Preprocessing function
def preprocess_image(img: Image.Image):
    # Resize and convert to a flat array (adjust according to your model's needs)
    img = img.resize((64, 64))  # example size
    img_array = np.array(img)
    if img_array.ndim == 3 and img_array.shape[2] == 3:
        img_array = img_array.mean(axis=2)  # convert to grayscale if needed
    img_flat = img_array.flatten()
    return img_flat

# Prediction function
def predict(image):
    try:
        img_flat = preprocess_image(image)
        prediction = model.predict([img_flat])[0]
        return prediction
    except Exception as e:
        return f"Error: {str(e)}"

# Gradio interface
iface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="Cat vs Dog Classifier",
    description="Upload an image and the model will predict: cat, dog, or idk.",
)

if __name__ == "__main__":
    iface.launch()