Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
from duckduckgo_search import DDGS
|
| 4 |
from fastcore.all import *
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
| 6 |
ddgs = DDGS()
|
| 7 |
-
|
| 8 |
def search_images(term, max_images=30):
|
| 9 |
print(f"Searching for '{term}'")
|
| 10 |
return L(ddgs.images(term, max_results=max_images)).itemgot('image')
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
urls[0]
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
download_url(urls[0], dest, show_progress=False)
|
| 22 |
-
|
| 23 |
-
from fastai.vision.all import *
|
| 24 |
-
im = Image.open(dest)
|
| 25 |
-
im.to_thumb(256,256)
|
| 26 |
-
|
| 27 |
-
download_url(search_images('platypus photo', max_images=1)[0], 'platypus.jpg', show_progress=False)
|
| 28 |
-
Image.open('platypus.jpg').to_thumb(256,256)
|
| 29 |
-
|
| 30 |
-
download_url(search_images('platypus photo', max_images=1)[0], 'platypus.jpg', show_progress=False)
|
| 31 |
-
Image.open('platypus.jpg').to_thumb(256,256)
|
| 32 |
-
|
| 33 |
|
|
|
|
| 34 |
failed = verify_images(get_image_files(path))
|
| 35 |
failed.map(Path.unlink)
|
| 36 |
-
len(failed)
|
| 37 |
-
|
| 38 |
|
|
|
|
| 39 |
dls = DataBlock(
|
| 40 |
blocks=(ImageBlock, CategoryBlock),
|
| 41 |
get_items=get_image_files,
|
|
@@ -44,16 +40,19 @@ dls = DataBlock(
|
|
| 44 |
item_tfms=[Resize(192, method='squish')]
|
| 45 |
).dataloaders(path, bs=32)
|
| 46 |
|
|
|
|
| 47 |
dls.show_batch(max_n=6)
|
| 48 |
|
| 49 |
-
|
| 50 |
learn = vision_learner(dls, resnet18, metrics=error_rate)
|
| 51 |
learn.fine_tune(3)
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
print(f"Probability it's a beaver: {probs[0]:.4f}")
|
| 57 |
-
|
| 58 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 59 |
demo.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from duckduckgo_search import DDGS
|
| 3 |
from fastcore.all import *
|
| 4 |
+
from fastdownload import download_url
|
| 5 |
+
from fastai.vision.all import *
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from pathlib import Path
|
| 8 |
|
| 9 |
+
# Define search function using DuckDuckGo
|
| 10 |
ddgs = DDGS()
|
|
|
|
| 11 |
def search_images(term, max_images=30):
|
| 12 |
print(f"Searching for '{term}'")
|
| 13 |
return L(ddgs.images(term, max_results=max_images)).itemgot('image')
|
| 14 |
|
| 15 |
+
# Create a folder for storing images
|
| 16 |
+
path = Path('images')
|
| 17 |
+
path.mkdir(exist_ok=True)
|
|
|
|
| 18 |
|
| 19 |
+
# Download example image: beaver
|
| 20 |
+
beaver_url = search_images('beaver photo', max_images=1)[0]
|
| 21 |
+
download_url(beaver_url, path/'beaver.jpg', show_progress=False)
|
| 22 |
|
| 23 |
+
# Download another example image: platypus
|
| 24 |
+
platypus_url = search_images('platypus photo', max_images=1)[0]
|
| 25 |
+
download_url(platypus_url, path/'platypus.jpg', show_progress=False)
|
| 26 |
|
| 27 |
+
# Show a thumbnail of the platypus image
|
| 28 |
+
Image.open(path/'platypus.jpg').thumbnail((256,256))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Remove any corrupt images
|
| 31 |
failed = verify_images(get_image_files(path))
|
| 32 |
failed.map(Path.unlink)
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Prepare DataLoaders (make sure `images/` has subfolders of labeled images)
|
| 35 |
dls = DataBlock(
|
| 36 |
blocks=(ImageBlock, CategoryBlock),
|
| 37 |
get_items=get_image_files,
|
|
|
|
| 40 |
item_tfms=[Resize(192, method='squish')]
|
| 41 |
).dataloaders(path, bs=32)
|
| 42 |
|
| 43 |
+
# Show a sample batch
|
| 44 |
dls.show_batch(max_n=6)
|
| 45 |
|
| 46 |
+
# Train a model
|
| 47 |
learn = vision_learner(dls, resnet18, metrics=error_rate)
|
| 48 |
learn.fine_tune(3)
|
| 49 |
|
| 50 |
+
# Prediction function
|
| 51 |
+
def predict_species(img):
|
| 52 |
+
is_sheep, _, probs = learn.predict(img)
|
| 53 |
+
return f"This looks like a: {is_sheep}. Probability it's a beaver: {probs[0]:.4f}"
|
| 54 |
|
| 55 |
+
# Define Gradio interface
|
| 56 |
+
demo = gr.Interface(fn=predict_species, inputs=gr.Image(type="pil"), outputs="text")
|
|
|
|
|
|
|
|
|
|
| 57 |
demo.launch()
|
| 58 |
+
|