zachwormgoor@gmail.com commited on
Commit
71fac2a
1 Parent(s): eb84283

fast.ai 2.7.11 update

Browse files
Files changed (1) hide show
  1. app.py +20 -21
app.py CHANGED
@@ -3,7 +3,7 @@
3
 
4
 
5
 
6
- __all__ = ['install', 'learn', 'categories', 'classify_image', 'image', 'label', 'examples', 'intf', 'classify_image_url', 'intf_text_url']
7
 
8
 
9
 
@@ -51,8 +51,26 @@ categories = ('amateur', 'stock')
51
  #add probs to UI somewhere
52
 
53
  def classify_image(img):
54
- pred,idx,probs = learn.predict(img)
 
55
  return dict(zip(categories, map(float,probs)))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
 
58
  image = gr.inputs.Image(shape=(192, 192))
@@ -74,25 +92,6 @@ intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=exa
74
  # intf.launch(inline=False)
75
 
76
 
77
-
78
- from fastdownload import download_url
79
- from fastai.vision.all import *
80
- import os
81
-
82
-
83
- def classify_image_url(url_text):
84
- try:
85
- dest = 'temp.jpg'
86
- download_url(url_text, dest, show_progress=False)
87
- im = Image.open(dest)
88
- img = im.to_thumb(256,256)
89
- #resize_images(dest, max_size=400, dest=dest)
90
- os.remove(dest)
91
- return classify_image(img)
92
- except:
93
- # in case there is any error, invalid URL or invalid image, etc., not sure how Gradio will handle a runtime exception so catching it to be safe
94
- return { categories[0]: 0.0, categories[1]: 0.0 }
95
-
96
  intf_text_url = gr.Interface(fn=classify_image_url, inputs="text", outputs=gr.outputs.Label())#, examples=example_urls)
97
  # intf_text_url.launch(inline=False)
98
 
 
3
 
4
 
5
 
6
+ __all__ = ['install', 'learn', 'categories', 'classify_image', 'classify_image_url', 'image', 'label', 'examples', 'intf', 'intf_text_url']
7
 
8
 
9
 
 
51
  #add probs to UI somewhere
52
 
53
  def classify_image(img):
54
+ tens = tensor(img) #fix apparently needed after fastai 2.7.11 released
55
+ pred,idx,probs = learn.predict(tens)
56
  return dict(zip(categories, map(float,probs)))
57
+
58
+ from fastdownload import download_url
59
+ import os
60
+
61
+ def classify_image_url(url_text):
62
+ try:
63
+ dest = 'temp.jpg'
64
+ download_url(url_text, dest, show_progress=False)
65
+ im = Image.open(dest)
66
+ img = im.to_thumb(256,256)
67
+ #resize_images(dest, max_size=400, dest=dest)
68
+ os.remove(dest)
69
+ return classify_image(img)
70
+ except:
71
+ # in case there is any error, invalid URL or invalid image, etc., not sure how Gradio will handle a runtime exception so catching it to be safe
72
+ return { categories[0]: 0.0, categories[1]: 0.0 }
73
+
74
 
75
 
76
  image = gr.inputs.Image(shape=(192, 192))
 
92
  # intf.launch(inline=False)
93
 
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  intf_text_url = gr.Interface(fn=classify_image_url, inputs="text", outputs=gr.outputs.Label())#, examples=example_urls)
96
  # intf_text_url.launch(inline=False)
97