SiwakornMe commited on
Commit
b58ae7b
·
1 Parent(s): 05f3ebf
Files changed (6) hide show
  1. app.py +75 -0
  2. export.pkl +3 -0
  3. images/chicken.jpg +0 -0
  4. images/dog.jpg +0 -0
  5. images/fish.jpg +0 -0
  6. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastai.vision.all import *
2
+ import gradio as gr
3
+ import pathlib
4
+ import os
5
+ import platform
6
+
7
+ # The below is for testing your app.py on a Windows laptop e.g. Visual Studio Code
8
+ print(platform.system())
9
+ if platform.system() == 'Windows':
10
+ # NotImplementedError: cannot instantiate 'PosixPath' on your system
11
+ # workaround for Windows where path seperator is '/'. Linux is '\'.
12
+ posix_backup = pathlib.PosixPath # remember the original path thingy
13
+ try:
14
+ pathlib.PosixPath = pathlib.WindowsPath # change to Windows
15
+ path = Path() # get current path in your runtime environment e.g. laptop, Colab, HuggingFace, Kaggle
16
+ learn = load_learner(path/'export.pkl')
17
+ finally: # when program is finished, switch back to original path thingy
18
+ pathlib.PosixPath = posix_backup
19
+ else: # Darwin aka MacOS, Linux, etc...
20
+ path = Path()
21
+ learn = load_learner(path/'export.pkl')
22
+
23
+ # Not needed, since we don't return a dict below
24
+ # same as bear_types = ['grizzly','black','teddy']
25
+ # bear_types = learn.dls.vocab
26
+ # categories = bear_types
27
+
28
+ def classify_image(img):
29
+ pred,idx,probs = learn.predict(img)
30
+ prob = float(probs[idx]) * 100
31
+ return f"This is a {pred}.\n Confidence Level : {prob:.4f}%"
32
+ # return dict(zip(categories, map(float,probs))) # not really needed here
33
+
34
+ # Define example images
35
+ # These must be local images in your repository.
36
+ # URLs to images don't seem to work well ?
37
+ # Image source: Wikipedia
38
+ examples = ['images/chicken.jpg',
39
+ 'images/dog.jpg',
40
+ 'images/fish.jpg']
41
+
42
+ # Define input component for image upload
43
+ image_input = gr.Image()
44
+
45
+ # Define output component for displaying text
46
+ text_output = gr.Textbox(type="text", label="Output")
47
+
48
+ # Define Gradio Interface
49
+ iface = gr.Interface(
50
+ fn=classify_image, # the function we defined above
51
+ inputs=image_input,
52
+ outputs=text_output,
53
+ # live=True means you click on any element
54
+ # and all other elements update immediately.
55
+ # Therefore, there is no Submit button needed anymore.
56
+ # Works fine, but may break Clear button,
57
+ # since there is no image uploaded yet to predict -> Error
58
+ # AssertionError: Expected an input
59
+ # live=False will give you a Submit button.
60
+ live=True,
61
+ examples=examples
62
+ )
63
+
64
+ # Run the interface
65
+ # To create a public link, set `share=True` in `launch()`.
66
+ # iface.launch(share=True)
67
+
68
+ # Note: share is for notebooks
69
+ # When deploying to HuggingFace leave it out, or else -> Error ?
70
+ iface.launch()
71
+
72
+ # Use CTRL-C to stop server in Visual Studio Code
73
+
74
+ # ^CKeyboard interruption in main thread... closing server.
75
+ # Killing tunnel 127.0.0.1:7860 <> https://6479f1bbea54b008f.gradio.live
export.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8323e255ec239fd5b12295f07a800012b31d691ffafe76118b2cf6b167cafb75
3
+ size 46968015
images/chicken.jpg ADDED
images/dog.jpg ADDED
images/fish.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ fastai