Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load model and feature extractor
|
| 7 |
+
MODEL_NAME = "google/vit-base-patch16-224"
|
| 8 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(MODEL_NAME)
|
| 9 |
+
model = ViTForImageClassification.from_pretrained(MODEL_NAME)
|
| 10 |
+
|
| 11 |
+
# Streamlit UI
|
| 12 |
+
st.title("Animal Recognition App")
|
| 13 |
+
st.write("Upload an image, and the model will identify the animal.")
|
| 14 |
+
|
| 15 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
| 16 |
+
|
| 17 |
+
if uploaded_file is not None:
|
| 18 |
+
image = Image.open(uploaded_file)
|
| 19 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 20 |
+
|
| 21 |
+
# Preprocess image
|
| 22 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 23 |
+
|
| 24 |
+
# Predict
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
outputs = model(**inputs)
|
| 27 |
+
logits = outputs.logits
|
| 28 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 29 |
+
|
| 30 |
+
# Get label
|
| 31 |
+
label = model.config.id2label[predicted_class_idx]
|
| 32 |
+
st.success(f"Predicted Animal: **{label}**")
|