Spaces:
Sleeping
Sleeping
egecandrsn
commited on
Commit
•
a852064
1
Parent(s):
d33fccb
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
from utils import load_model, predict
|
3 |
+
import torch
|
4 |
+
import json
|
5 |
+
import requests
|
6 |
+
import io
|
7 |
+
from PIL import Image
|
8 |
+
import streamlit as st
|
9 |
+
|
10 |
+
st.title('Dog Breed Classification')
|
11 |
+
|
12 |
+
# set header
|
13 |
+
st.header('Please upload a image')
|
14 |
+
|
15 |
+
# upload file
|
16 |
+
file = st.file_uploader('', type=['jpeg', 'jpg', 'png'])
|
17 |
+
url = st.text_input("Or enter image URL:")
|
18 |
+
st.button("Load Image")
|
19 |
+
|
20 |
+
# load classifier
|
21 |
+
model = load_model('model_scripted.pt')
|
22 |
+
|
23 |
+
if file is not None:
|
24 |
+
image = Image.open(file).convert('RGB')
|
25 |
+
st.image(image, use_column_width=True)
|
26 |
+
|
27 |
+
# classify image
|
28 |
+
predicted, probability = predict(model, image)
|
29 |
+
|
30 |
+
# write classification
|
31 |
+
st.write("## {}".format(predicted))
|
32 |
+
st.write("### Probability: {}%".format(int(probability * 100)))
|
33 |
+
|
34 |
+
if url != '':
|
35 |
+
try:
|
36 |
+
response = requests.get(url)
|
37 |
+
image = Image.open(io.BytesIO(response.content)).convert('RGB')
|
38 |
+
st.image(image, caption="Image from URL", use_column_width=True)
|
39 |
+
|
40 |
+
# classify image
|
41 |
+
predicted, probability = predict(model, image)
|
42 |
+
|
43 |
+
# write classification
|
44 |
+
st.write("## {}".format(predicted))
|
45 |
+
st.write("### score: {}%".format(int(probability * 100)))
|
46 |
+
|
47 |
+
except Exception as e:
|
48 |
+
st.error(f"Error loading image from URL: {e}")
|