Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import required libraries
|
2 |
+
import streamlit as st
|
3 |
+
from transformers import ViTForImageClassification, ViTFeatureExtractor
|
4 |
+
from PIL import Image
|
5 |
+
import torch
|
6 |
+
|
7 |
+
# Load the pre-trained model and feature extractor
|
8 |
+
model_name = "nateraw/vit-age-classifier"
|
9 |
+
model = ViTForImageClassification.from_pretrained(model_name)
|
10 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
|
11 |
+
|
12 |
+
# Set up Streamlit app
|
13 |
+
st.set_page_config(page_title="Age Classifier", page_icon="👶")
|
14 |
+
st.title("Age Classification using AI")
|
15 |
+
st.write("Upload an image of a person, and the model will predict their age group.")
|
16 |
+
|
17 |
+
# Upload image
|
18 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
19 |
+
|
20 |
+
if uploaded_file is not None:
|
21 |
+
# Open the uploaded image
|
22 |
+
image = Image.open(uploaded_file)
|
23 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
24 |
+
|
25 |
+
# Preprocess the image
|
26 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
27 |
+
|
28 |
+
# Perform inference
|
29 |
+
with torch.no_grad():
|
30 |
+
outputs = model(**inputs)
|
31 |
+
logits = outputs.logits
|
32 |
+
|
33 |
+
# Get the predicted class
|
34 |
+
predicted_class_idx = logits.argmax(-1).item()
|
35 |
+
predicted_age_group = model.config.id2label[predicted_class_idx]
|
36 |
+
|
37 |
+
# Display the result
|
38 |
+
st.write(f"**Predicted Age Group:** {predicted_age_group}")
|