Harsh-Jadhav commited on
Commit
bf5a90a
1 Parent(s): 27fb144

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -50
app.py CHANGED
@@ -1,58 +1,26 @@
1
- import streamlit as st
 
 
 
2
  from fastai.vision.all import *
 
3
 
4
- def is_cat(x):
5
- return x[0].isupper()
6
 
7
- # Load the pre-trained model
8
- model_path = "model.pkl" # Replace with the path to your model file
9
- learn = load_learner(model_path)
10
 
11
- # Define categories
12
- categories = ['Dog', 'Cat']
13
 
14
- # Function to classify an image
15
  def classify_image(img):
16
- pred, idx, probs = learn.predict(img)
17
- return dict(zip(categories, map(float, probs)))
18
-
19
- # Streamlit app
20
- st.title("Dog vs. Cat Classifier")
21
-
22
- # Upload an image
23
- uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "jfif", "png"])
24
-
25
- if uploaded_image is not None:
26
- # Display the uploaded image
27
- st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
28
-
29
- # Make predictions
30
- image = PILImage.create(uploaded_image)
31
- predictions = classify_image(image)
32
-
33
- # Display the prediction
34
- st.subheader("Prediction:")
35
- for category, probability in predictions.items():
36
- st.write(f"{category}: {probability:.2f}")
37
-
38
- # Example images
39
- st.sidebar.title("Example Images")
40
- example_images = {
41
- "Dog": "dog.jfif",
42
- "Cat": "cat.jfif"
43
- }
44
-
45
- selected_example = st.sidebar.selectbox("Select an Example Image", list(example_images.keys()))
46
-
47
- if selected_example:
48
- selected_image_path = example_images[selected_example]
49
- st.image(selected_image_path, caption=selected_example, use_column_width=True)
50
 
51
- # Make predictions for the example image
52
- example_image = PILImage.create(selected_image_path)
53
- example_predictions = classify_image(example_image)
 
54
 
55
- # Display the prediction
56
- st.sidebar.subheader("Prediction:")
57
- for category, probability in example_predictions.items():
58
- st.sidebar.write(f"{category}: {probability:.2f}")
 
1
+ # Dog v Cat script
2
+ __all__ = ['is_cat','learn','classify_image','categories','image','label','examples','intf']
3
+
4
+ # cell
5
  from fastai.vision.all import *
6
+ import gradio as gr
7
 
8
+ def is_cat(x): return x[0].isupper()
 
9
 
10
+ # cell
11
+ learn = load_learner('model.pkl')
 
12
 
13
+ # cell
14
+ categories = ('Dog','Cat')
15
 
 
16
  def classify_image(img):
17
+ pred,idx,probs = learn.predict(img)
18
+ return dict(zip(categories, map(float,probs)))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ # cell
21
+ image = gr.inputs.Image(shape=(192,192))
22
+ label = gr.outputs.Label()
23
+ examples = ['dog.jfif','cat.jfif']
24
 
25
+ intf = gr.Interface(fn=classify_image, inputs=image, outputs= label, examples=examples)
26
+ intf.launch(inline=False)