File size: 1,161 Bytes
04f475a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import streamlit as st
from transformers import pipeline, AutoImageProcessor, AutoModelForImageClassification
from PIL import Image
import requests

# Load the model and processor
st.title("Food Image Classification with Hugging Face")
st.write("Upload an image to classify the type of food!")

# Load the model
@st.cache_resource
def load_pipeline():
    return pipeline("image-classification", model="Shresthadev403/food-image-classification")

pipe = load_pipeline()

# Upload image
uploaded_file = st.file_uploader("Choose a food image...", type=["jpg", "png", "jpeg"])

if uploaded_file is not None:
    # Display the uploaded image
    image = Image.open(uploaded_file)
    st.image(image, caption="Uploaded Image", use_column_width=True)
    st.write("Classifying...")
    
    # Make predictions
    predictions = pipe(image)
    
    # Display top prediction
    st.subheader("Top Prediction")
    st.write(f"**{predictions[0]['label']}** with confidence {predictions[0]['score']:.2f}")
    
    # Display other predictions
    st.subheader("Other Predictions")
    for pred in predictions[1:]:
        st.write(f"{pred['label']}: {pred['score']:.2f}")