| from fastai.vision.all import * | |
| import gradio as gr | |
| import glob | |
| learn = load_learner('model.pkl') | |
| labels = learn.dls.vocab | |
| def predict(img): | |
| img = PILImage.create(img) | |
| _, _, probs = learn.predict(img) | |
| return {labels[i]: float(probs[i]) for i in range(len(labels))} | |
| title='Japanese Food Classifier' | |
| description = ('Japanese food classifier trained on duckduckgo image search generated custom dataset, fastai library and fine-tuned with the resnet18 model') | |
| article="<p style='text-align: center'><a href='https://github.com/seamusbarnes/learn-in-public/blob/main/fastai/lesson01.ipynb' target='_blank'>Trained model</a></p>" | |
| subfolder = Path('images') | |
| search_pattern = str(subfolder/'*.jpg') | |
| jpg_files = glob.glob(search_pattern) | |
| intf = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(height=512, width=512), | |
| outputs=gr.Label(num_top_classes=10), | |
| title=title, | |
| description=description, | |
| article=article, | |
| examples=jpg_files, | |
| examples_per_page=37 | |
| ) | |
| intf.launch() |