Spaces:
Sleeping
Sleeping
File size: 1,361 Bytes
220d160 6f9efe3 220d160 |
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 37 38 39 40 41 42 43 44 45 46 |
import streamlit as st
from transformers import AutoImageProcessor, AutoModelForImageClassification
from PIL import Image
import torch
# Load your trained model and processor
model_name = "mjpsm/confidence-image-classifier"
model = AutoModelForImageClassification.from_pretrained(model_name)
processor = AutoImageProcessor.from_pretrained(model_name)
# Label mapping
id2label = {
0: "Confident",
1: "No Confidence",
2: "Somewhat Confident"
}
# Streamlit app title
st.title("Confidence Detector ๐ธ")
st.write("Take a picture or upload one, and the AI will predict your confidence level!")
# Upload or capture an image
uploaded_file = st.camera_input("Take a picture") # Opens your webcam
# You could also use st.file_uploader("Upload a picture", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Load the image
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_container_width=True)
# Preprocess
inputs = processor(images=image, return_tensors="pt")
# Predict
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predicted_class_idx = logits.argmax(-1).item()
# Map prediction
predicted_label = id2label[predicted_class_idx]
# Show result
st.markdown(f"## Prediction: **{predicted_label}** ๐ฏ") |