Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import platform
|
| 2 |
+
import pathlib
|
| 3 |
+
|
| 4 |
+
plt = platform.system()
|
| 5 |
+
if plt == 'Windows':
|
| 6 |
+
pathlib.WindowsPath = pathlib.PosixPath
|
| 7 |
+
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from fastai.vision.all import *
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
# Load your trained/saved model
|
| 13 |
+
learn = load_learner('model.pkl')
|
| 14 |
+
|
| 15 |
+
def classify_bear(image):
|
| 16 |
+
# convert PIL image to fastai image object
|
| 17 |
+
img = PILImage.create(image)
|
| 18 |
+
|
| 19 |
+
# get predictions
|
| 20 |
+
pred, idx, probs = learn.predict(img)
|
| 21 |
+
|
| 22 |
+
return {learn.dls.vocab[i]: float(probs[i]) for i in range(len(probs))}
|
| 23 |
+
|
| 24 |
+
example_images = [
|
| 25 |
+
'images/black.jpg',
|
| 26 |
+
'images/teddy.jpg',
|
| 27 |
+
'images/grizzly.jpg',
|
| 28 |
+
'images/black2.jpg',
|
| 29 |
+
'images/grizzly2.jpg'
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
iface = gr.Interface(
|
| 33 |
+
fn=classify_bear,
|
| 34 |
+
inputs=gr.Image(type='pil'),
|
| 35 |
+
outputs=gr.Label(num_top_classes=3),
|
| 36 |
+
examples=example_images,
|
| 37 |
+
description="Classify bear images as grizzly, black or teddy:"
|
| 38 |
+
)
|
| 39 |
+
iface.launch()
|