alonshini commited on
Commit
01e58e1
·
1 Parent(s): 6a6ff99

new script 2

Browse files
Files changed (1) hide show
  1. app.py +25 -35
app.py CHANGED
@@ -1,37 +1,27 @@
1
- # gradio
 
 
 
2
  import gradio as gr
3
 
4
- # fast.ai
5
- import fastbook
6
- fastbook.setup_book()
7
- from fastbook import *
8
- from fastai.vision.widgets import *
9
-
10
- # pathlib
11
- import pathlib
12
- temp = pathlib.PosixPath
13
- pathlib.PosixPath = pathlib.WindowsPath
14
- path = Path()
15
- # load model
16
- learn_inf = load_learner(path/'model.pkl')
17
-
18
- # Output of the uploaded image
19
- # Resizes the image to 128x128
20
- out_pl = widgets.Output()
21
- out_pl.clear_output()
22
- with out_pl: display(img.to_thumb(128,128))
23
- out_pl
24
-
25
- # Label Function that will disply our prediction data to the user
26
- lbl_pred = widgets.Label()
27
-
28
- # Logic function that will run the image classifier on the classify button click
29
- def on_click_classify(input_img):
30
- img = PILImage.create(input_img)
31
- with out_pl: display(img.to_thumb(128,128))
32
- pred,pred_idx,probs = learn_inf.predict(img)
33
- lbl_pred.value = f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}'
34
- return lbl_pred.value
35
-
36
- demo = gr.Interface(on_click_classify, gr.Image(shape=(512, 512)), "text")
37
- demo.launch()
 
1
+ __all__ = ['learn', 'categories', 'examples', 'interface', 'classify_image']
2
+
3
+ # %% 02_app.ipynb 1
4
+ from fastai.vision.all import *
5
  import gradio as gr
6
 
7
+ print(gr.__version__)
8
+
9
+ # %% 02_app.ipynb 2
10
+ learn = load_learner('model.pkl')
11
+
12
+ # %% 02_app.ipynb 4
13
+ categories = ('black', 'grizzly', 'teddy')
14
+
15
+ def classify_image(img):
16
+ pred, pred_idx, probs = learn.predict(img)
17
+ return dict(zip(categories, map(float, probs)))
18
+
19
+ # %% 02_app.ipynb 5
20
+ # create a gradio interface
21
+
22
+ # image = gr.inputs.Image(shape=(192,192))
23
+ # label = gr.outputs.Label()
24
+ examples = ['examples/black.jpg', 'examples/grizzly.jpg', 'examples/teddy.jpg']
25
+
26
+ interface = gr.Interface(fn=classify_image, inputs="image", outputs="label", examples=examples)
27
+ interface.launch(inline=False)