hitchhiker3010 commited on
Commit
f4655c9
1 Parent(s): 3ed0a71

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from fastai.vision.all import *
3
+ from PIL import Image
4
+
5
+ import fastai.losses
6
+ import fastai.layers
7
+
8
+ fastai.layers.BaseLoss = fastai.losses.BaseLoss
9
+ fastai.layers.CrossEntropyLossFlat = fastai.losses.CrossEntropyLossFlat
10
+ fastai.layers.BCEWithLogitsLossFlat = fastai.losses.BCEWithLogitsLossFlat
11
+ fastai.layers.BCELossFlat = fastai.losses.BCELossFlat
12
+ fastai.layers.MSELossFlat = fastai.losses.MSELossFlat
13
+ fastai.layers.L1LossFlat = fastai.losses.L1LossFlat
14
+ fastai.layers.LabelSmoothingCrossEntropy = fastai.losses.LabelSmoothingCrossEntropy
15
+ fastai.layers.LabelSmoothingCrossEntropyFlat = fastai.losses.LabelSmoothingCrossEntropyFlat
16
+
17
+ model = load_learner("model.pkl")
18
+ def predict(im):
19
+ image_file = PILImage(PILImage.create((255-im)))
20
+ pred,pred_idx,probs = model.predict(image_file)
21
+ vals, indx = torch.topk(probs,2)
22
+
23
+ return {model.dls.vocab[i]: prob.item() for prob,i in zip(vals,indx)}
24
+
25
+ input_widget = gr.inputs.Image(image_mode="L", source="canvas", shape=((224,224)), invert_colors=True)
26
+ #
27
+ # <b><span style="color:#fab114">some *blue* text</span>.</b>
28
+ classes = ",".join(model.dls.vocab)
29
+ article = f'currently supports <b><span style="color:#fab114">{classes}</span>.</b>'
30
+ interface = gr.Interface(predict, title="Quickdraw", inputs=input_widget, outputs='label', live=True,article=article)
31
+
32
+ interface.launch(debug=True)