dennisjooo commited on
Commit
f648847
β€’
1 Parent(s): 6216f76

Initial commit

Browse files
README.md CHANGED
@@ -1,13 +1,18 @@
1
  ---
2
  title: Age And Emotion Classifier
3
- emoji: πŸ“š
4
  colorFrom: red
5
  colorTo: green
6
  sdk: gradio
7
- sdk_version: 3.48.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
1
  ---
2
  title: Age And Emotion Classifier
3
+ emoji: πŸ‘΄πŸ»πŸ˜ 
4
  colorFrom: red
5
  colorTo: green
6
  sdk: gradio
7
+ sdk_version: 3.47.1
8
  app_file: app.py
9
  pinned: false
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
+ Mood-guessing model is a [google/vit-base-patch16-224-in21k](https://huggingface.co/google/vit-base-patch16-224-in21k)
16
+ trained on [FastJobs/Visual_Emotional_Analysis](https://huggingface.co/datasets/FastJobs/Visual_Emotional_Analysis)
17
+
18
+ Totally not creepy, I promise :)
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Importing some modules
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+ import torch
5
+
6
+ # Loading in the model
7
+ MODEL_AGE = pipeline('image-classification', model='nateraw/vit-age-classifier', device=0 if torch.cuda.is_available() else -1)
8
+ MODEL_EMOTION = pipeline('image-classification', model='dennisjooo/emotion_classification', device=0 if torch.cuda.is_available() else -1)
9
+
10
+ def classify_image(image, top_k):
11
+ # Getting the classification result
12
+ age_result = MODEL_AGE(image)
13
+ emotion_result = MODEL_EMOTION(image)
14
+
15
+ # Reformating the classification result into a dictionary
16
+ age_result = {result['label']: result['score'] for result in age_result[:min(int(top_k), 8)]}
17
+ emotion_result = {result['label']: result['score'] for result in emotion_result[:min(int(top_k), 7)]}
18
+
19
+ # Add some text comment to it lol
20
+ comment = text_comment(list(age_result.keys())[0])
21
+
22
+ # Returning the classification result
23
+ return age_result, comment, emotion_result
24
+
25
+ # Snarky comment based on age
26
+ def text_comment(pred_class):
27
+ match pred_class:
28
+ case "3-9":
29
+ return "Lost your way to the playground?"
30
+ case "10-19":
31
+ return "But Mom, I'm not a kid anymore!"
32
+ case "20-29":
33
+ return "You're in your prime!"
34
+ case "30-39":
35
+ return "Oof, watch out for those wrinkles!"
36
+ case "40-49":
37
+ return "You're still young at heart!"
38
+ case "50-59":
39
+ return "Retirement is just around the corner!"
40
+ case "60-69":
41
+ return "You're a senior citizen now!"
42
+ case "more than 70":
43
+ return "Hey Siri, play 'My Way' by Frank Sinatra"
44
+
45
+
46
+ if __name__ == "__main__":
47
+ # Definining the title of the interface
48
+ title_text = """
49
+ # I will guess your age and mood based on your picture!
50
+ ---
51
+ Totally not creepy, I promise :)
52
+ <br>Made by [Dennis Jonathan](dennisjooo.github.io). A project for REA Mastering AI course.
53
+ Age guessing model from [nateraw/vit-age-classifier](https://huggingface.co/nateraw/vit-age-classifier)
54
+ <br>Mood-guessing model is a [google/vit-base-patch16-224-in21k](https://huggingface.co/google/vit-base-patch16-224-in21k)
55
+ trained on [FastJobs/Visual_Emotional_Analysis](https://huggingface.co/datasets/FastJobs/Visual_Emotional_Analysis)
56
+ """
57
+
58
+ # Creating the Gradio interface
59
+ with gr.Blocks() as demo:
60
+ gr.Markdown(title_text)
61
+ with gr.Row(equal_height=True):
62
+ with gr.Column():
63
+ # Creating the input block
64
+ image = gr.Image(label="Upload a picture of yourself", type="pil", scale=2)
65
+
66
+ # Creating the example block
67
+ gr.Examples(examples=[
68
+ "./images/andrew.jpg",
69
+ "./images/feifei.jpg",
70
+ "./images/geoff.jpg",
71
+ "./images/ilya.jpg",
72
+ "./images/karpathy.jpg",
73
+ "./images/lex.jpg"
74
+ ], inputs=[image], label="Or choose an example")
75
+
76
+
77
+ with gr.Column():
78
+ # Getting the top k hyperparameter
79
+ top_k = gr.Number(label="How many guesses do I get?", value=1)
80
+
81
+ # Creating the output block
82
+ age_label = gr.Label(label="Hey it's me, your age!")
83
+ comment = gr.Textbox(label="Based on your age, I think you are...",
84
+ placeholder="I'm still learning, so I might be wrong!")
85
+ emotion_label = gr.Label(label="Hey it's me, your emotion!")
86
+
87
+ with gr.Row():
88
+ # Submit button
89
+ btn = gr.Button("Beep boop, guess my age and emotion!")
90
+ btn.click(classify_image, inputs=[image, top_k], outputs=[age_label, comment, emotion_label])
91
+
92
+ # Clear button
93
+ clear = gr.Button("Poof begone!")
94
+ clear.click(lambda: [None, None, None, None], inputs=[], outputs=[image, age_label, comment, emotion_label])
95
+
96
+ # Launching the interface
97
+ demo.launch(share=True, 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
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transformers>=4.32.1
2
+ torch>=2.1.0