Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load the model and feature extractor
|
| 7 |
+
model_name = "google/vit-base-patch16-224"
|
| 8 |
+
model = ViTForImageClassification.from_pretrained(model_name)
|
| 9 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
# Streamlit app
|
| 12 |
+
st.title("Image Classifier")
|
| 13 |
+
st.write("Upload an image to classify it into categories.")
|
| 14 |
+
|
| 15 |
+
# File uploader
|
| 16 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 17 |
+
|
| 18 |
+
if uploaded_file:
|
| 19 |
+
# Load and display the image
|
| 20 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 21 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 22 |
+
|
| 23 |
+
# Preprocess the image
|
| 24 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 25 |
+
|
| 26 |
+
# Perform inference
|
| 27 |
+
with torch.no_grad():
|
| 28 |
+
outputs = model(**inputs)
|
| 29 |
+
logits = outputs.logits
|
| 30 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 31 |
+
|
| 32 |
+
# Get classification label
|
| 33 |
+
label = model.config.id2label[predicted_class_idx]
|
| 34 |
+
|
| 35 |
+
# Display results
|
| 36 |
+
st.write(f"Prediction: **{label}**")
|