Spaces:
Runtime error
Runtime error
Download app.py from bogdanned/deep_learning_week_2: direct link, hf CLI and curl.
- Browser
- Download file 1.16 kB
-
https://huggingface.co/spaces/bogdanned/deep_learning_week_2/resolve/main/app.py
- Command line
-
hf download hf://spaces/bogdanned/deep_learning_week_2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/bogdanned/deep_learning_week_2/resolve/main/app.py
1.16 kB
| import gradio as gr | |
| import os | |
| from fastai.vision.all import * | |
| # get current directory | |
| current_directory = os.getcwd() | |
| modelpath = os.path.join(current_directory, "models", "dogs_vs_cats.pkl") | |
| def buildAssetPath(path): | |
| return os.path.join(current_directory, "examples", path) | |
| # somehow the model needs this function - was not explained in the tutorial why | |
| def is_cat(x): return x[0].isupper() | |
| learn = load_learner(modelpath) | |
| labels = learn.dls.vocab | |
| def predict(img): | |
| img = PILImage.create(img) | |
| pred, pred_idx, probs = learn.predict(img) | |
| return {labels[i]: float(probs[i]) for i in range(len(labels))} | |
| # Interface customization | |
| title = "Cat v.s. Docs" | |
| description = "A cat vs Dog Classifier. Created as a demo for Gradio and HuggingFace Spaces." | |
| examples = [buildAssetPath('dog.jpeg'), buildAssetPath('cat.jpeg')] | |
| interpretation = 'default' | |
| enable_queue = True | |
| gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(512, 512)), outputs=gr.outputs.Label(num_top_classes=3, label="Cat:"), title=title, | |
| description=description, examples=examples, interpretation=interpretation, enable_queue=enable_queue).launch(share=True) | |