christinapeng commited on
Commit
91fb4f9
·
verified ·
1 Parent(s): f4b990d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -40
app.py CHANGED
@@ -1,55 +1,56 @@
1
- # '''PyTorch Food Classifier - FastAI 2022- Mostly Good For Pastries
2
- # and trained on ResNet 34'''
3
 
4
- # import streamlit as st
5
- # import os
6
- # from PIL import Image
7
- # import time
8
- # from fastai.vision.all import *
9
- # from fastai.learner import load_learner
10
 
11
- # def is_penguin(img):
12
- # return img.split('-')[0]
13
 
14
- # #Load the Learner (Exported from ipnyb file with learn.export() )
15
- # learn = load_learner('model.pkl')
16
 
17
- # categories = ('Penguin', 'Puffin')
18
 
19
- # #Classify image
20
- # def classify_image(cl_img):
21
- # img = Image.open(cl_img)
22
- # st.image(img)
23
- # pred, _ , prob = learn.predict(img)
24
- # return dict(zip(categories, map(float, prob)))
 
25
 
26
- # st.set_page_config(page_title="Penguin vs Puffin Classifier - FastAI 2025", page_icon=":robot:")
27
- # st.header("Penguin vs Puffin Classifier")
28
 
29
- # file_up = st.file_uploader("Upload Your Image Below", type=["jpg","png"])
30
 
31
- # if st.button('Run Model'):
32
- # st.write("Button Pressed")
33
- # cl_done = classify_image(file_up)
34
- # st.write(f"Your food is: {cl_done}")
35
 
36
- # st.write('This classifier is trained on Resnet-34 and specializes in differentiating penguins from puffins).')
37
 
38
- from fastai.vision.all import *
39
- from fastai.learner import load_learner
40
- import gradio as gr
41
 
42
- learn = load_learner('model.pkl')
43
 
44
- categories = ('Penguin', 'Puffin')
45
 
46
- def classifyImage(img):
47
- pred, idx, prob = learn.predict(img)
48
- return dict(zip(categories, map(float, prob)))
49
 
50
- image = gr.Image(shape=(192, 192))
51
- label = gr.Label()
52
- examples = ['penguin.jpg', 'puffin.png', 'razorbill.jpg']
53
 
54
- intf = gr.Interface(fn=classifyImage, inputs=image, outputs=label, examples=examples)
55
- intf.launch(inline=False)
 
1
+ '''PyTorch Food Classifier - FastAI 2022- Mostly Good For Pastries
2
+ and trained on ResNet 34'''
3
 
4
+ import streamlit as st
5
+ import os
6
+ from PIL import Image
7
+ import time
8
+ from fastai.vision.all import *
9
+ from fastai.learner import load_learner
10
 
11
+ def is_penguin(img):
12
+ return img.split('-')[0]
13
 
14
+ #Load the Learner (Exported from ipnyb file with learn.export() )
15
+ learn = load_learner('model.pkl')
16
 
17
+ categories = ('Penguin', 'Puffin')
18
 
19
+ #Classify image
20
+ def classify_image(cl_img):
21
+ img = Image.open(cl_img)
22
+ st.image(img)
23
+ pred, pred_idx, prob = learn.predict(img)
24
+ confidence = prob[pred_idx].item() * 100
25
+ return pred, prob
26
 
27
+ st.set_page_config(page_title="Penguin vs Puffin Classifier - FastAI 2025", page_icon=":robot:")
28
+ st.header("Penguin vs Puffin Classifier")
29
 
30
+ file_up = st.file_uploader("Upload Your Image Below", type=["jpg","png"])
31
 
32
+ if st.button('Run Model'):
33
+ st.write("Button Pressed")
34
+ pred_label, confidence = classify_image(file_up)
35
+ st.write(f"The model predicts {pred_label} with {confidence} confidence.")
36
 
37
+ st.write('This classifier is trained on Resnet-34 and specializes in differentiating penguins from puffins).')
38
 
39
+ # from fastai.vision.all import *
40
+ # from fastai.learner import load_learner
41
+ # import gradio as gr
42
 
43
+ # learn = load_learner('model.pkl')
44
 
45
+ # categories = ('Penguin', 'Puffin')
46
 
47
+ # def classifyImage(img):
48
+ # pred, idx, prob = learn.predict(img)
49
+ # return dict(zip(categories, map(float, prob)))
50
 
51
+ # image = gr.Image(shape=(192, 192))
52
+ # label = gr.Label()
53
+ # examples = ['penguin.jpg', 'puffin.png', 'razorbill.jpg']
54
 
55
+ # intf = gr.Interface(fn=classifyImage, inputs=image, outputs=label, examples=examples)
56
+ # intf.launch(inline=False)