Commit
·
86b08a1
1
Parent(s):
3060ef9
First Push
Browse filesUpload Folder
- app.py +73 -0
- bear_classifier.pkl +3 -0
- black.jpg +0 -0
- cat.jpg +0 -0
- cat_dog.pkl +3 -0
- dog.jpg +0 -0
- grizzly.jpg +0 -0
- requirements.txt +3 -0
- teddy.jpg +0 -0
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../drive/MyDrive/Codici/Python/Apps/Gradio_App/Image Classification/simlpe_image_models.ipynb.
|
2 |
+
|
3 |
+
# %% auto 0
|
4 |
+
__all__ = ['cat_dog_model', 'bear_model', 'input_model', 'input_image', 'output_label', 'bear_examples', 'cat_examples',
|
5 |
+
'examples', 'intf', 'get_model', 'classify_image']
|
6 |
+
|
7 |
+
# %% ../drive/MyDrive/Codici/Python/Apps/Gradio_App/Image Classification/simlpe_image_models.ipynb 2
|
8 |
+
!pip install -Uqq fastai
|
9 |
+
!pip install -Uqq fastbook
|
10 |
+
!pip install gradio
|
11 |
+
|
12 |
+
# %% ../drive/MyDrive/Codici/Python/Apps/Gradio_App/Image Classification/simlpe_image_models.ipynb 3
|
13 |
+
import fastbook
|
14 |
+
# fastbook.setup_book()
|
15 |
+
from fastbook import *
|
16 |
+
from fastai.vision.widgets import *
|
17 |
+
from fastai.vision.all import *
|
18 |
+
import gradio as gr
|
19 |
+
|
20 |
+
# %% ../drive/MyDrive/Codici/Python/Apps/Gradio_App/Image Classification/simlpe_image_models.ipynb 4
|
21 |
+
cat_dog_model = load_learner('cat_dog.pkl')
|
22 |
+
bear_model = load_learner("bear_classifier.pkl")
|
23 |
+
|
24 |
+
# %% ../drive/MyDrive/Codici/Python/Apps/Gradio_App/Image Classification/simlpe_image_models.ipynb 7
|
25 |
+
# Define a function to load the appropriate model
|
26 |
+
def get_model(model_name):
|
27 |
+
if model_name == 'Cat vs Dog Model':
|
28 |
+
return cat_dog_model
|
29 |
+
elif model_name == 'Bear Model':
|
30 |
+
return bear_model
|
31 |
+
else:
|
32 |
+
raise ValueError("Model not found")
|
33 |
+
|
34 |
+
# Classification Function
|
35 |
+
def classify_image(model_name, img):
|
36 |
+
model = get_model(model_name)
|
37 |
+
categories = model.dls.vocab
|
38 |
+
pred, idx, probs = model.predict(img)
|
39 |
+
return dict(zip(categories, map(float, probs)))
|
40 |
+
|
41 |
+
# %% ../drive/MyDrive/Codici/Python/Apps/Gradio_App/Image Classification/simlpe_image_models.ipynb 10
|
42 |
+
#INPUTS
|
43 |
+
input_model = gr.Dropdown(['Cat vs Dog Model', 'Bear Model'])
|
44 |
+
input_image = gr.components.Image(width=192, height=192)
|
45 |
+
|
46 |
+
#OUTPUT
|
47 |
+
output_label = gr.components.Label()
|
48 |
+
|
49 |
+
#EXAMPLES
|
50 |
+
bear_examples = ['black.jpg', 'grizzly.jpg', 'teddy.jpg']
|
51 |
+
cat_examples = ['cat.jpg', 'dog.jpg']
|
52 |
+
|
53 |
+
examples = [
|
54 |
+
['Bear Model',bear_examples[0]], # added model name
|
55 |
+
['Bear Model',bear_examples[1]], # added model name
|
56 |
+
['Bear Model',bear_examples[2]], # added model name
|
57 |
+
['Cat vs Dog Model', cat_examples[0]], # added model name
|
58 |
+
['Cat vs Dog Model', cat_examples[1]], # added model name
|
59 |
+
]
|
60 |
+
|
61 |
+
#INTERFACE
|
62 |
+
intf = gr.Interface(fn=classify_image,
|
63 |
+
inputs=[
|
64 |
+
input_model,
|
65 |
+
input_image],
|
66 |
+
outputs=output_label,
|
67 |
+
examples=examples,
|
68 |
+
theme=gr.themes.Ocean())
|
69 |
+
|
70 |
+
#LAUNCH APP
|
71 |
+
intf.launch(inline=False,
|
72 |
+
# share=True
|
73 |
+
)
|
bear_classifier.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9d8c34f0e2837c4c29df2c35f55b47a31c3c81b21cea2261046b8aa3742998f6
|
3 |
+
size 46979320
|
black.jpg
ADDED
![]() |
cat.jpg
ADDED
![]() |
cat_dog.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:77ef29713bc84552a98d2db2faf48b75c4ab5db6303a4b7ce54a6ace7d4dc030
|
3 |
+
size 46974608
|
dog.jpg
ADDED
![]() |
grizzly.jpg
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
fastai==2.7.19
|
2 |
+
gradio==5.21.0
|
3 |
+
gradio_client==1.7.2
|
teddy.jpg
ADDED
![]() |