Spaces:
Sleeping
Sleeping
| # AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb. | |
| # %% auto 0 | |
| __all__ = ['path', 'learn_inf', 'image', 'label', 'examples', 'intf', 'on_click_classify'] | |
| # %% app.ipynb 2 | |
| from fastai.vision.all import * | |
| import gradio as gr | |
| from fastai.vision.widgets import * | |
| # %% app.ipynb 12 | |
| path = Path('.') | |
| learn_inf = load_learner(path/'export.pkl') | |
| # %% app.ipynb 14 | |
| from PIL import Image | |
| import ipywidgets as widgets | |
| # Optional: Import display only if in an IPython environment | |
| try: | |
| from IPython.display import display | |
| can_display = True | |
| except ImportError: | |
| can_display = False | |
| def on_click_classify(img_array): | |
| # Convert numpy array to PIL Image | |
| img = Image.fromarray(img_array.astype('uint8'), 'RGB') | |
| out_pl = widgets.Output() | |
| out_pl.clear_output() | |
| if can_display: | |
| # Use display if available | |
| with out_pl: | |
| display(img.to_thumb(128, 128)) | |
| else: | |
| # Save to a file if display is not available | |
| img.to_thumb(128, 128).save('output_thumbnail.png') | |
| print("Thumbnail saved to 'output_thumbnail.png'.") | |
| # Assuming learn_inf is already defined and loaded elsewhere in your code | |
| pred, pred_idx, probs = learn_inf.predict(img) | |
| return f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}' | |
| # %% app.ipynb 17 | |
| image = gr.Image() | |
| label = gr.Label() | |
| examples = ['Adi_trainers.jpg', 'Nike_trainers.jpg', 'Puma_trainers.jpg', 'Adidas_trainers.jpg'] | |
| intf = gr.Interface(fn=on_click_classify, inputs=image, outputs=label, examples=examples) | |
| intf.launch(inline=False) | |