Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load your trained model and processor
|
| 7 |
+
model_name = "mjpsm/confidence-image-classifier"
|
| 8 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 9 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
# Label mapping
|
| 12 |
+
id2label = {
|
| 13 |
+
0: "Confident",
|
| 14 |
+
1: "No Confidence",
|
| 15 |
+
2: "Somewhat Confident"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
# Streamlit app title
|
| 19 |
+
st.title("Confidence Detector 📸")
|
| 20 |
+
|
| 21 |
+
st.write("Take a picture or upload one, and the AI will predict your confidence level!")
|
| 22 |
+
|
| 23 |
+
# Upload or capture an image
|
| 24 |
+
uploaded_file = st.camera_input("Take a picture") # Opens your webcam
|
| 25 |
+
# You could also use st.file_uploader("Upload a picture", type=["jpg", "jpeg", "png"])
|
| 26 |
+
|
| 27 |
+
if uploaded_file is not None:
|
| 28 |
+
# Load the image
|
| 29 |
+
image = Image.open(uploaded_file)
|
| 30 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 31 |
+
|
| 32 |
+
# Preprocess
|
| 33 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 34 |
+
|
| 35 |
+
# Predict
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
outputs = model(**inputs)
|
| 38 |
+
|
| 39 |
+
logits = outputs.logits
|
| 40 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 41 |
+
|
| 42 |
+
# Map prediction
|
| 43 |
+
predicted_label = id2label[predicted_class_idx]
|
| 44 |
+
|
| 45 |
+
# Show result
|
| 46 |
+
st.markdown(f"## Prediction: **{predicted_label}** 🎯")
|