__all__ = ['install', 'learn', 'categories', 'classify_image', 'classify_image_url', 'image', 'label', 'examples', 'intf', 'intf_text_url'] # Kept getting "No module named 'fastai'" from huggingface..workaround: # https://stackoverflow.com/a/50255019 import subprocess import sys def install(package): subprocess.check_call([sys.executable, "-m", "pip", "install", package]) install("fastai") from fastai.vision.all import * #from fast.ai.vision.all import * import gradio as gr # def greet(name): # return "Hello " + name + "!!" # iface = gr.Interface(fn=greet, inputs="text", outputs="text") # iface.launch() # from: # https://course.fast.ai/Lessons/lesson2.html # https://github.com/fastai/fastbook/blob/master/02_production.ipynb #learn_inf = load_learner(path/'export.pkl') #learn_inf.predict('images/grizzly.jpg') #learn_inf.dls.vocab # model created from: https://www.kaggle.com/code/zachwormgoor/stock-photo-recognizer learn = load_learner('model - 712 images 3 epochs.pkl') categories = ('amateur', 'stock') #may have categories swapped? #add probs to UI somewhere def classify_image(img): tens = tensor(img) #fix apparently needed after fastai 2.7.11 released pred,idx,probs = learn.predict(tens) return dict(zip(categories, map(float,probs))) from fastdownload import download_url import os def classify_image_url(url_text): try: dest = 'temp.jpg' download_url(url_text, dest, show_progress=False) im = Image.open(dest) img = im.to_thumb(256,256) #resize_images(dest, max_size=400, dest=dest) os.remove(dest) return classify_image(img) except: # 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 return { categories[0]: 0.0, categories[1]: 0.0 } def classify_image_url_debug(url_text): try: dest = 'temp.jpg' download_url(url_text, dest, show_progress=False) im = Image.open(dest) img = im.to_thumb(256,256) #resize_images(dest, max_size=400, dest=dest) os.remove(dest) #return classify_image(img) return "Success" except Exception as ex: # 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 #return { categories[0]: 0.0, categories[1]: 0.0 } return ex.message image = gr.inputs.Image(shape=(192, 192)) label = gr.outputs.Label() 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'] # stock: https://cdn.pixabay.com/photo/2022/02/11/12/12/pforphoto-7007231_1280.jpg # stock2: https://cdn.pixabay.com/photo/2019/09/25/13/57/business-4503747_960_720.jpg # stock-easy: https://scienceline.org/wp-content/uploads/2015/10/apple-926456_1280.jpg # stock-easy2: https://ph-files.imgix.net/6b5cb0af-8bef-4700-b35f-8576e0ec58d8.jpeg?auto=format&fit=crop&frame=1&h=512&w=1024 # washing-machine-stock: https://live.staticflickr.com/7313/12775521983_e0a67aeb86_n.jpg # combine-stock: https://live.staticflickr.com/5765/22059912468_ec5c315be4_b.jpg # glasses-amateur: http://2.bp.blogspot.com/-dpygWXrSzRo/VfiIfzsWuxI/AAAAAAAAGvc/SNLnjV-IzO4/s1600/Green%2BTea%2Band%2BOrange%2BCollard%2BRecipe.jpg # amateur: https://cimg3.ibsrv.net/cimg/www.honda-tech.com/1024x768_85/479/AAApril-2-Control-Arms-01-58479.jpg # amateur2: https://www.iflash.xyz/images/diyMod/img7.jpg # unsure: https://en.wikipedia.org/wiki/Joshua_Bean intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples) # intf.launch(inline=False) intf_text_url = gr.Interface(fn=classify_image_url, inputs="text", outputs=gr.outputs.Label())#, examples=example_urls) intf_text_url_debug = gr.Interface(fn=classify_image_url_debug, inputs="text", outputs="text") # intf_text_url.launch(inline=False) # https://github.com/gradio-app/gradio/issues/450 gr.TabbedInterface( [intf, intf_text_url, intf_text_url_debug], ["Image file upload", "Image URL", "Image URL debug" ] ).launch()