Spaces:
No application file
No application file
| import requests | |
| import torch | |
| from io import BytesIO | |
| from PIL import Image | |
| from transformers import ViTImageProcessor, ViTForImageClassification | |
| import streamlit as st | |
| st.title("๋์ด๋ฅผ ์์ธกํด๋ด ์๋ค!") | |
| def get_model_transformers(): | |
| model = ViTForImageClassification.from_pretrained('nateraw/vit-age-classifier') | |
| transforms = ViTImageProcessor.from_pretrained('nateraw/vit-age-classifier') | |
| return model, transforms | |
| uploaded_file = st.file_uploader("๋์ด๋ฅผ ์์ธกํ ์ฌ๋์ ์ด๋ฏธ์ง๋ฅผ ์ ๋ก๋ํ์ธ์.", type=["jpg", "jpeg", "png", 'gif', 'webp']) | |
| if uploaded_file: | |
| #st.write(f'์ ๋ก๋๋ ํ์ผ ์ด๋ฆ: {uploaded_file}') | |
| st.image(uploaded_file, caption="Uploaded Image", use_column_width=True) | |
| ########### | |
| # Get example image from official fairface repo + read it in as an image | |
| #r = requests.get('https://github.com/dchen236/FairFace/blob/master/detected_faces/race_Asian_face0.jpg?raw=true') | |
| #im = Image.open(BytesIO(r.content)) | |
| im = Image.open(uploaded_file) | |
| # Init model, transforms | |
| #model = ViTForImageClassification.from_pretrained('nateraw/vit-age-classifier') | |
| model, transforms = get_model_transformers() | |
| #transforms = ViTImageProcessor.from_pretrained('nateraw/vit-age-classifier') | |
| # Transform our image and pass it through the model | |
| inputs = transforms(im, return_tensors='pt') | |
| output = model(**inputs) | |
| # Predicted Class probabilities | |
| proba = output.logits.softmax(1) | |
| values, indices = torch.topk(proba, k=5) | |
| result_dict = {model.config.id2label[i.item()]: v.item() for i, v in zip(indices.numpy()[0], values.detach().numpy()[0])} | |
| first_result = list(result_dict.keys())[0] | |
| print(f'predicted result:{result_dict}') | |
| print(f'first_result: {first_result}') | |
| st.header('๊ฒฐ๊ณผ') | |
| st.subheader(f'์์ธก๋ ๋์ด๋ {first_result} ์ ๋๋ค') | |
| for key, value in result_dict.items(): | |
| st.write(f'{key}: {value}') | |
| for key, value in result_dict.items(): | |
| st.write(f'{key}: {value * 100:.2f}%') | |