| import gradio as gr |
| import pickle |
| import numpy as np |
| from PIL import Image |
|
|
| |
| with open("model.pkl", "rb") as f: |
| model = pickle.load(f) |
|
|
| |
| def preprocess_image(img: Image.Image): |
| |
| img = img.resize((64, 64)) |
| img_array = np.array(img) |
| if img_array.ndim == 3 and img_array.shape[2] == 3: |
| img_array = img_array.mean(axis=2) |
| img_flat = img_array.flatten() |
| return img_flat |
|
|
| |
| 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)}" |
|
|
| |
| 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() |
|
|
|
|