Spaces:
Sleeping
Sleeping
web app
Browse files- app.py +61 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
import base64
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
|
| 8 |
+
def get_prediction(image_data):
|
| 9 |
+
#replace your image classification ai service URL
|
| 10 |
+
url = 'https://askai.aiclub.world/9e64ab8b-95e4-40fa-9529-b13d9e1b4761'
|
| 11 |
+
r = requests.post(url, data=image_data)
|
| 12 |
+
st.write(r)
|
| 13 |
+
response = r.json()['predicted_label']
|
| 14 |
+
score = r.json()['score']
|
| 15 |
+
#print("Predicted_label: {} and confidence_score: {}".format(response,score))
|
| 16 |
+
return response, score
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
#creating the web app
|
| 20 |
+
|
| 21 |
+
#setting up the title
|
| 22 |
+
st.title("Cats and Dogs Image Classifier")#change according to your project
|
| 23 |
+
#setting up the subheader
|
| 24 |
+
st.subheader("File Uploader")#change according to your project
|
| 25 |
+
|
| 26 |
+
#file uploader
|
| 27 |
+
image = st.file_uploader(label="Upload an image",accept_multiple_files=False, help="Upload an image to classify them")
|
| 28 |
+
if image:
|
| 29 |
+
#converting the image to bytes
|
| 30 |
+
img = Image.open(image)
|
| 31 |
+
buf = io.BytesIO()
|
| 32 |
+
img.save(buf,format = 'JPEG')
|
| 33 |
+
byte_im = buf.getvalue()
|
| 34 |
+
|
| 35 |
+
#converting bytes to b64encoding
|
| 36 |
+
payload = base64.b64encode(byte_im)
|
| 37 |
+
|
| 38 |
+
#file details
|
| 39 |
+
file_details = {
|
| 40 |
+
"file name": image.name,
|
| 41 |
+
"file type": image.type,
|
| 42 |
+
"file size": image.size
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
#write file details
|
| 46 |
+
st.write(file_details)
|
| 47 |
+
|
| 48 |
+
#setting up the image
|
| 49 |
+
st.image(img)
|
| 50 |
+
|
| 51 |
+
#predictions
|
| 52 |
+
response, scores = get_prediction(payload)
|
| 53 |
+
|
| 54 |
+
col1, col2 = st.columns(2)
|
| 55 |
+
with col1:
|
| 56 |
+
st.metric("Prediction Label",response)
|
| 57 |
+
with col2:
|
| 58 |
+
st.metric("Confidence Score", max(scores))
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
requirements.txt
ADDED
|
Binary file (3.39 kB). View file
|
|
|