zachwormgoor@gmail.com commited on
Commit
f16cc96
1 Parent(s): 6f50425

Attempt to add URL text box interface

Browse files
Files changed (1) hide show
  1. app.py +25 -3
app.py CHANGED
@@ -3,7 +3,7 @@
3
 
4
 
5
 
6
- __all__ = ['install', 'learn', 'categories', 'classify_image', 'image', 'label', 'examples', 'intf']
7
 
8
 
9
 
@@ -55,8 +55,8 @@ def classify_image(img):
55
  return dict(zip(categories, map(float,probs)))
56
 
57
 
58
- image = gr.inputs.Image(shape=(192, 192))
59
- label = gr.outputs.Label()
60
  examples = ['stock.jpg', 'stock2.jpg', 'stock-easy.jpg', 'stock-easy2.jpg', 'combine-stock.jpg', 'washing-machine-stock.jpg', 'glasses-amateur.jpg', 'amateur.jpg', 'amateur2.jpg', 'amateur-easy.jpg', 'amateur-easy2.jpg', 'unsure.jpg']
61
  # stock: https://cdn.pixabay.com/photo/2022/02/11/12/12/pforphoto-7007231_1280.jpg
62
  # stock2: https://cdn.pixabay.com/photo/2019/09/25/13/57/business-4503747_960_720.jpg
@@ -75,6 +75,28 @@ intf.launch(inline=False)
75
 
76
 
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
 
80
 
 
3
 
4
 
5
 
6
+ __all__ = ['install', 'learn', 'categories', 'classify_image', 'image', 'label', 'examples', 'intf', 'classify_image_url', 'intf_text_url']
7
 
8
 
9
 
 
55
  return dict(zip(categories, map(float,probs)))
56
 
57
 
58
+ image = gr.inputs.Image(shape=(192, 192))
59
+ label = gr.outputs.Label()
60
  examples = ['stock.jpg', 'stock2.jpg', 'stock-easy.jpg', 'stock-easy2.jpg', 'combine-stock.jpg', 'washing-machine-stock.jpg', 'glasses-amateur.jpg', 'amateur.jpg', 'amateur2.jpg', 'amateur-easy.jpg', 'amateur-easy2.jpg', 'unsure.jpg']
61
  # stock: https://cdn.pixabay.com/photo/2022/02/11/12/12/pforphoto-7007231_1280.jpg
62
  # stock2: https://cdn.pixabay.com/photo/2019/09/25/13/57/business-4503747_960_720.jpg
 
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=label, examples=examples)
97
+ intf_text_url.launch(inline=False)
98
+
99
+
100
 
101
 
102