Spaces:
Sleeping
Sleeping
Update app.py
#1
by
munnae
- opened
app.py
CHANGED
|
@@ -1,45 +1,51 @@
|
|
| 1 |
-
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
# Load model
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
def home():
|
| 15 |
-
return "Food Image Classifier API is running!"
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return jsonify({"error": "No file provided"}), 400
|
| 21 |
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
results = classifier(image)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
|
| 6 |
+
# Set Streamlit page config
|
| 7 |
+
st.set_page_config(page_title="Food Image Classifier", layout="centered")
|
| 8 |
|
| 9 |
+
# Load the model
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_model():
|
| 12 |
+
st.text("Loading model...")
|
| 13 |
+
model = pipeline("image-classification", model="Xenova/mobilenet_v2_1.0_224")
|
| 14 |
+
st.text("Model loaded successfully!")
|
| 15 |
+
return model
|
| 16 |
|
| 17 |
+
classifier = load_model()
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Streamlit UI
|
| 20 |
+
st.title("🍕🥖 Food Image Classifier")
|
| 21 |
+
st.write("Upload an image of **roti, pizza, naan, or tofu** to classify.")
|
|
|
|
| 22 |
|
| 23 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 24 |
|
| 25 |
+
if uploaded_file is not None:
|
| 26 |
+
# Convert file to PIL image
|
| 27 |
+
image = Image.open(uploaded_file)
|
| 28 |
|
| 29 |
+
# Display the uploaded image
|
| 30 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 31 |
+
|
| 32 |
+
# Classify the image
|
| 33 |
+
with st.spinner("Classifying..."):
|
| 34 |
results = classifier(image)
|
| 35 |
|
| 36 |
+
# Display results
|
| 37 |
+
if results:
|
| 38 |
+
label = results[0]['label']
|
| 39 |
+
confidence = results[0]['score'] * 100 # Convert to percentage
|
| 40 |
+
|
| 41 |
+
st.success(f"**Prediction:** {label}")
|
| 42 |
+
st.info(f"**Confidence:** {confidence:.2f}%")
|
| 43 |
+
|
| 44 |
+
# Option to classify another image
|
| 45 |
+
st.button("Classify Another Image", on_click=lambda: st.experimental_rerun())
|
| 46 |
+
|
| 47 |
+
# Footer
|
| 48 |
+
st.markdown("---")
|
| 49 |
+
st.markdown("Made by **Muneeb Sahaf** | Final Year Project 2025")
|
| 50 |
+
|
| 51 |
+
|