Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
import streamlit as st
|
3 |
+
from transformers import pipeline
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Function to classify age from an image
|
7 |
+
def classify_age(image_path):
|
8 |
+
# Load the age classification model
|
9 |
+
age_classifier = pipeline("image-classification", model="nateraw/vit-age-classifier")
|
10 |
+
# Predict age
|
11 |
+
result = age_classifier(image_path)
|
12 |
+
return result
|
13 |
+
|
14 |
+
# Main part of the app
|
15 |
+
st.set_page_config(page_title="Age Classifier", page_icon="👶")
|
16 |
+
st.header("Age Classification using nateraw/vit-age-classifier")
|
17 |
+
uploaded_file = st.file_uploader("Upload an image of a person...", type=["jpg", "jpeg", "png"])
|
18 |
+
|
19 |
+
if uploaded_file is not None:
|
20 |
+
# Save the uploaded file temporarily
|
21 |
+
with open(uploaded_file.name, "wb") as file:
|
22 |
+
file.write(uploaded_file.getvalue())
|
23 |
+
|
24 |
+
# Display the uploaded image
|
25 |
+
st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
|
26 |
+
|
27 |
+
# Classify age
|
28 |
+
st.text('Classifying age...')
|
29 |
+
age_result = classify_age(uploaded_file.name)
|
30 |
+
|
31 |
+
# Display the result
|
32 |
+
st.write("Age Classification Result:")
|
33 |
+
for res in age_result:
|
34 |
+
st.write(f"{res['label']}: {res['score']:.2f}")
|