from transformers import pipeline import streamlit as st from PIL import Image # Initialize the classifier pipeline classifier = pipeline("image-classification") # Streamlit interface st.title("Image Classification with Hugging Face") # Upload image uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png"]) if uploaded_image: try: # Open the image as a PIL object image = Image.open(uploaded_image).convert("RGB") # Convert to RGB to handle all formats # Display the uploaded image st.image(image, caption="Uploaded Image", use_column_width=True) # Pass the PIL image directly to the classifier results = classifier(image) # Display the results st.write("Classification Results:") for result in results: st.write(f"Label: {result['label']}, Score: {result['score']:.4f}") except Exception as e: st.error(f"Error processing the image: {e}")