from fastai.vision.all import *
import gradio as gr
import os
from pathlib import Path
import pathlib

def is_cat(x): return x[0].isupper()

temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath

path = Path(os.path.dirname(os.path.abspath(__file__)))
model_path = path / 'model.pkl'  # Construct the Windows-style path

learn = load_learner(model_path)

categories = ('Dog', 'Cat')

def classify_image(img):
  pred, idx, probs = learn.predict(img)
  return dict(zip(categories, map(float, probs)))

input_component = gr.components.Image(shape=(192,192))
output_component = gr.components.Label()
example = ['dog.jpg']

intf = gr.Interface(fn=classify_image, inputs=input_component, outputs=output_component, examples=example)
intf.launch(share=True)