Spaces:
Runtime error
Runtime error
dennisjooo
commited on
Commit
•
839b7e0
1
Parent(s):
f9d48c9
Initial commit
Browse files- README.md +5 -2
- app.py +90 -0
- images/andrew.jpg +0 -0
- images/feifei.jpg +0 -0
- images/geoff.jpg +0 -0
- images/ilya.jpg +0 -0
- images/karpathy.jpg +0 -0
- images/lex.jpg +0 -0
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: Age Guesser ViT
|
3 |
-
emoji:
|
4 |
colorFrom: indigo
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
@@ -10,4 +10,7 @@ pinned: false
|
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
1 |
---
|
2 |
title: Age Guesser ViT
|
3 |
+
emoji: 👴🏻
|
4 |
colorFrom: indigo
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
|
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
+
A deployed Gradio for a project for REA Mastering AI course. Made by [Dennis Jonathan](dennisjooo.github.io).
|
14 |
+
Age guessing model from [nateraw/vit-age-classifier](https://huggingface.co/nateraw/vit-age-classifier)
|
15 |
+
|
16 |
+
Totally not creepy, I promise :)
|
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Importing some modules
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
+
import torch.cuda as cuda
|
5 |
+
|
6 |
+
# Loading in the model
|
7 |
+
MODEL = pipeline('image-classification', model='nateraw/vit-age-classifier', device=0 if cuda.is_available() else -1)
|
8 |
+
|
9 |
+
# Main function to classify image
|
10 |
+
def classify_image(image, top_k):
|
11 |
+
# Getting the classification result
|
12 |
+
classification_result = MODEL(image)
|
13 |
+
|
14 |
+
# Reformating the classification result into a dictionary
|
15 |
+
classification_result = {result['label']: result['score'] for result in classification_result[:int(top_k)]}
|
16 |
+
|
17 |
+
# Add some text comment to it lol
|
18 |
+
comment = text_comment(list(classification_result.keys())[0])
|
19 |
+
|
20 |
+
# Returning the classification result
|
21 |
+
return classification_result, comment
|
22 |
+
|
23 |
+
# Snarky comment based on age
|
24 |
+
def text_comment(pred_class):
|
25 |
+
match pred_class:
|
26 |
+
case "3-9":
|
27 |
+
return "Lost your way to the playground?"
|
28 |
+
case "10-19":
|
29 |
+
return "But Mom, I'm not a kid anymore!"
|
30 |
+
case "20-29":
|
31 |
+
return "You're in your prime!"
|
32 |
+
case "30-39":
|
33 |
+
return "Oof, watch out for those wrinkles!"
|
34 |
+
case "40-49":
|
35 |
+
return "You're still young at heart!"
|
36 |
+
case "50-59":
|
37 |
+
return "Retirement is just around the corner!"
|
38 |
+
case "60-69":
|
39 |
+
return "You're a senior citizen now!"
|
40 |
+
case "more than 70":
|
41 |
+
return "Hey Siri, play 'My Way' by Frank Sinatra"
|
42 |
+
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
# Creating the Gradio interface
|
46 |
+
with gr.Blocks() as demo:
|
47 |
+
gr.Markdown("""
|
48 |
+
# I will guess your age based on your picture!
|
49 |
+
---
|
50 |
+
Totally not creepy, I promise :)
|
51 |
+
<br>Made by [Dennis Jonathan](dennisjooo.github.io). A project for REA Mastering AI course.
|
52 |
+
Age guessing model from [nateraw/vit-age-classifier](https://huggingface.co/nateraw/vit-age-classifier)
|
53 |
+
""")
|
54 |
+
|
55 |
+
with gr.Row(equal_height=True):
|
56 |
+
with gr.Column():
|
57 |
+
# Creating the input block
|
58 |
+
image = gr.Image(label="Upload a picture of yourself", type="pil", scale=2)
|
59 |
+
|
60 |
+
# Creating the example block
|
61 |
+
gr.Examples(examples=[
|
62 |
+
"./images/andrew.jpg",
|
63 |
+
"./images/feifei.jpg",
|
64 |
+
"./images/geoff.jpg",
|
65 |
+
"./images/ilya.jpg",
|
66 |
+
"./images/karpathy.jpg",
|
67 |
+
"./images/lex.jpg"
|
68 |
+
], inputs=[image], label="Or choose an example")
|
69 |
+
|
70 |
+
|
71 |
+
with gr.Column():
|
72 |
+
# Getting the top k hyperparameter
|
73 |
+
top_k = gr.Number(label="How many guesses do I get?", value=1)
|
74 |
+
|
75 |
+
# Creating the output block
|
76 |
+
label = gr.Label(label="Hey it's me, your age!")
|
77 |
+
comment = gr.Textbox(label="Based on your age, I think you are...",
|
78 |
+
placeholder="I'm still learning, so I might be wrong!")
|
79 |
+
|
80 |
+
with gr.Row():
|
81 |
+
# Submit button
|
82 |
+
btn = gr.Button("Beep boop, guess my age!")
|
83 |
+
btn.click(classify_image, inputs=[image, top_k], outputs=[label, comment])
|
84 |
+
|
85 |
+
# Clear button
|
86 |
+
clear = gr.Button("Poof begone!")
|
87 |
+
clear.click(lambda: [None, None, None], inputs=[], outputs=[image, label, comment])
|
88 |
+
|
89 |
+
# Launching the interface
|
90 |
+
demo.launch(share=False, debug=True)
|
images/andrew.jpg
ADDED
images/feifei.jpg
ADDED
images/geoff.jpg
ADDED
images/ilya.jpg
ADDED
images/karpathy.jpg
ADDED
images/lex.jpg
ADDED