Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastai.vision.core import PILImageBW, TensorImageBW
|
2 |
+
from datasets import ClassLabel
|
3 |
+
import gradio as gr
|
4 |
+
from fastai.learner import load_learner
|
5 |
+
|
6 |
+
def get_image_attr(x): return x['image']
|
7 |
+
def get_target_attr(x): return x['target']
|
8 |
+
|
9 |
+
def img2tensor(im: Image.Image):
|
10 |
+
return TensorImageBW(array(im)).unsqueeze(0)
|
11 |
+
|
12 |
+
classLabel = ClassLabel(names=['T - shirt / top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'], id=None)
|
13 |
+
|
14 |
+
def add_target(x:dict):
|
15 |
+
x['target'] = classLabel.int2str(x['label'])
|
16 |
+
return x
|
17 |
+
|
18 |
+
learn = load_learner('export.pkl', cpu=True)
|
19 |
+
|
20 |
+
def classify(inp):
|
21 |
+
img = PILImageBW.create(inp)
|
22 |
+
item = dict(image=img)
|
23 |
+
pred, _, _ = learn.predict(item)
|
24 |
+
return classLabel.int2str(int(pred))
|
25 |
+
|
26 |
+
iface = gr.Interface(
|
27 |
+
fn=classify,
|
28 |
+
inputs=gr.inputs.Image(),
|
29 |
+
outputs="text",
|
30 |
+
title="Fashion Mnist Classifier",
|
31 |
+
description="fastai deployment in Gradio.",
|
32 |
+
).launch()
|