Spaces:
Sleeping
Sleeping
Create content
Browse files- app.py +59 -4
- export.pkl +3 -0
app.py
CHANGED
@@ -1,7 +1,62 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def
|
4 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from fastbook import *
|
3 |
+
from fastai.vision.widgets import *
|
4 |
+
from fastcore.all import *
|
5 |
+
from urllib.error import URLError,HTTPError
|
6 |
+
import json
|
7 |
+
import matplotlib as mpl, pkgutil, requests, time
|
8 |
|
9 |
+
def search_images_ddg(term, max_images=200):
|
10 |
+
"Search for `term` with DuckDuckGo and return a unique urls of about `max_images` images"
|
11 |
+
assert max_images<1000
|
12 |
+
url = 'https://duckduckgo.com/'
|
13 |
+
res = urlread(url,data={'q':term})
|
14 |
+
searchObj = re.search(r'vqd=([\d-]+)\&', res)
|
15 |
+
assert searchObj
|
16 |
+
requestUrl = url + 'i.js'
|
17 |
+
params = dict(l='us-en', o='json', q=term, vqd=searchObj.group(1), f=',,,', p='1', v7exp='a')
|
18 |
+
urls,data = set(),{'next':1}
|
19 |
+
headers = dict(referer='https://duckduckgo.com/')
|
20 |
+
while len(urls)<max_images and 'next' in data:
|
21 |
+
try:
|
22 |
+
res = urlread(requestUrl, data=params, headers=headers)
|
23 |
+
data = json.loads(res) if res else {}
|
24 |
+
urls.update(L(data['results']).itemgot('image'))
|
25 |
+
requestUrl = url + data['next']
|
26 |
+
except (URLError,HTTPError): pass
|
27 |
+
time.sleep(1)
|
28 |
+
return L(urls)[:max_images]
|
29 |
|
30 |
+
btn_upload = widgets.FileUpload()
|
31 |
+
btn_upload
|
32 |
+
|
33 |
+
img = PILImage.create(btn_upload.data[-1])
|
34 |
+
img
|
35 |
+
|
36 |
+
out_pl = widgets.Output()
|
37 |
+
out_pl.clear_output()
|
38 |
+
with out_pl: display(img.to_thumb(128,128))
|
39 |
+
out_pl
|
40 |
+
|
41 |
+
pred,pred_idx,probs = learn_inf.predict(img)
|
42 |
+
|
43 |
+
lbl_pred = widgets.Label()
|
44 |
+
lbl_pred.value = f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}'
|
45 |
+
lbl_pred
|
46 |
+
|
47 |
+
btn_run = widgets.Button(description='Classify')
|
48 |
+
btn_run
|
49 |
+
|
50 |
+
def on_click_classify(change):
|
51 |
+
img = PILImage.create(btn_upload.data[-1])
|
52 |
+
out_pl.clear_output()
|
53 |
+
with out_pl: display(img.to_thumb(128,128))
|
54 |
+
pred,pred_idx,probs = learn_inf.predict(img)
|
55 |
+
lbl_pred.value = f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}'
|
56 |
+
|
57 |
+
btn_run.on_click(on_click_classify)
|
58 |
+
|
59 |
+
btn_upload = widgets.FileUpload()
|
60 |
+
|
61 |
+
VBox([widgets.Label('Select your bear!'),
|
62 |
+
btn_upload, btn_run, out_pl, lbl_pred])
|
export.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:eb16d427d20e5ed16d133a595707da69ef882e33dad6a5918a529e8faffa4762
|
3 |
+
size 46973374
|