import gradio as gr import pickle import numpy as np from PIL import Image # Load your model with open("model.pkl", "rb") as f: model = pickle.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()