juunho commited on
Commit
15e62f5
ยท
1 Parent(s): 054598f

Add application file

Browse files
Files changed (1) hide show
  1. get-age.py +63 -0
get-age.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import torch
3
+ from io import BytesIO
4
+ from PIL import Image
5
+ from transformers import ViTImageProcessor, ViTForImageClassification
6
+
7
+
8
+ import streamlit as st
9
+
10
+
11
+ st.title("๋‚˜์ด๋ฅผ ์˜ˆ์ธกํ•ด๋ด…์‹œ๋‹ค!")
12
+
13
+ @st.cache_resource
14
+ def get_model_transformers():
15
+ model = ViTForImageClassification.from_pretrained('nateraw/vit-age-classifier')
16
+ transforms = ViTImageProcessor.from_pretrained('nateraw/vit-age-classifier')
17
+ return model, transforms
18
+
19
+
20
+ uploaded_file = st.file_uploader("๋‚˜์ด๋ฅผ ์˜ˆ์ธกํ•  ์‚ฌ๋žŒ์˜ ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜์„ธ์š”.", type=["jpg", "jpeg", "png", 'gif', 'webp'])
21
+ if uploaded_file:
22
+ #st.write(f'์—…๋กœ๋“œ๋œ ํŒŒ์ผ ์ด๋ฆ„: {uploaded_file}')
23
+ st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
24
+
25
+
26
+ ###########
27
+ # Get example image from official fairface repo + read it in as an image
28
+ #r = requests.get('https://github.com/dchen236/FairFace/blob/master/detected_faces/race_Asian_face0.jpg?raw=true')
29
+ #im = Image.open(BytesIO(r.content))
30
+ im = Image.open(uploaded_file)
31
+
32
+ # Init model, transforms
33
+ #model = ViTForImageClassification.from_pretrained('nateraw/vit-age-classifier')
34
+ model, transforms = get_model_transformers()
35
+ #transforms = ViTImageProcessor.from_pretrained('nateraw/vit-age-classifier')
36
+
37
+ # Transform our image and pass it through the model
38
+ inputs = transforms(im, return_tensors='pt')
39
+ output = model(**inputs)
40
+
41
+ # Predicted Class probabilities
42
+ proba = output.logits.softmax(1)
43
+
44
+ values, indices = torch.topk(proba, k=5)
45
+
46
+ result_dict = {model.config.id2label[i.item()]: v.item() for i, v in zip(indices.numpy()[0], values.detach().numpy()[0])}
47
+ first_result = list(result_dict.keys())[0]
48
+
49
+ print(f'predicted result:{result_dict}')
50
+ print(f'first_result: {first_result}')
51
+
52
+ st.header('๊ฒฐ๊ณผ')
53
+ st.subheader(f'์˜ˆ์ธก๋œ ๋‚˜์ด๋Š” {first_result} ์ž…๋‹ˆ๋‹ค')
54
+
55
+ for key, value in result_dict.items():
56
+ st.write(f'{key}: {value}')
57
+
58
+ for key, value in result_dict.items():
59
+ st.write(f'{key}: {value * 100:.2f}%')
60
+
61
+
62
+
63
+