Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from transformers import ViTFeatureExtractor, ViTForImageClassification | |
| from PIL import Image | |
| import torch | |
| # Load model and feature extractor | |
| MODEL_NAME = "google/vit-base-patch16-224" | |
| feature_extractor = ViTFeatureExtractor.from_pretrained(MODEL_NAME) | |
| model = ViTForImageClassification.from_pretrained(MODEL_NAME) | |
| # Streamlit UI | |
| st.title("Animal Recognition App") | |
| st.write("Upload an image, and the model will identify the animal.") | |
| uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"]) | |
| if uploaded_file is not None: | |
| image = Image.open(uploaded_file) | |
| st.image(image, caption="Uploaded Image", use_column_width=True) | |
| # Preprocess image | |
| inputs = feature_extractor(images=image, return_tensors="pt") | |
| # Predict | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| logits = outputs.logits | |
| predicted_class_idx = logits.argmax(-1).item() | |
| # Get label | |
| label = model.config.id2label[predicted_class_idx] | |
| st.success(f"Predicted Animal: **{label}**") | |