sajid1085 commited on
Commit
efce63a
1 Parent(s): 9e5093a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Importing some modules
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+
5
+ # Loading in the model
6
+ MODEL_AGE = pipeline('image-classification', model='nateraw/vit-age-classifier', device=-1)
7
+ MODEL_EMOTION = pipeline('image-classification', model='dennisjooo/emotion_classification', device=-1)
8
+
9
+ def classify_image(image, top_k):
10
+ # Getting the classification result
11
+ age_result = MODEL_AGE(image)
12
+ emotion_result = MODEL_EMOTION(image)
13
+
14
+ # Reformating the classification result into a dictionary
15
+ age_result = {result['label']: result['score'] for result in age_result[:min(int(top_k), 8)]}
16
+ emotion_result = {result['label']: result['score'] for result in emotion_result[:min(int(top_k), 7)]}
17
+
18
+ # Add some text comment to it lol
19
+ comment = text_comment(list(age_result.keys())[0])
20
+
21
+ # Returning the classification result
22
+ return age_result, comment, emotion_result
23
+
24
+ # Snarky comment based on age
25
+ def text_comment(pred_class):
26
+ match pred_class:
27
+ case "3-9":
28
+ return "Lost your way to the playground?"
29
+ case "10-19":
30
+ return "But Mom, I'm not a kid anymore!"
31
+ case "20-29":
32
+ return "You're in your prime!"
33
+ case "30-39":
34
+ return "Maturity comes with experience not age!"
35
+ case "40-49":
36
+ return "You're still young at heart!"
37
+ case "50-59":
38
+ return "Retirement is just around the corner!"
39
+ case "60-69":
40
+ return "You're a senior citizen now!"
41
+ case "more than 70":
42
+ return "Bye Bye"
43
+
44
+
45
+ if __name__ == "__main__":
46
+ # Definining the title of the interface
47
+ title_text = """
48
+ # I will guess your age and mood based on your picture!
49
+ ---
50
+ Sajid Hussain
51
+ """
52
+
53
+ # Creating the Gradio interface
54
+ with gr.Blocks() as demo:
55
+ gr.Markdown(title_text)
56
+ with gr.Row(equal_height=True):
57
+ with gr.Column():
58
+ # Creating the input block
59
+ image = gr.Image(label="Upload a picture of yourself", type="pil", scale=2)
60
+
61
+ # Creating the example block
62
+ gr.Examples(examples=[
63
+ "/content/Jony-Dep.jfif",
64
+ "/content/Sami.jfif",
65
+ "/content/imrankhan.jpg",
66
+
67
+ ], inputs=[image], label="Or choose an example")
68
+
69
+
70
+ with gr.Column():
71
+ # Getting the top k hyperparameter
72
+ top_k = gr.Number(label="How many guesses do I get?", value=1)
73
+
74
+ # Creating the output block
75
+ age_label = gr.Label(label="Hey it's me, your age!")
76
+ comment = gr.Textbox(label="Based on your age, I think you are...",
77
+ placeholder="I'm still learning, so I might be wrong!")
78
+ emotion_label = gr.Label(label="Hey it's me, your emotion!")
79
+
80
+ with gr.Row():
81
+ # Submit button
82
+ btn = gr.Button("Guess my age and emotion!")
83
+ btn.click(classify_image, inputs=[image, top_k], outputs=[age_label, comment, emotion_label])
84
+
85
+ # Clear button
86
+ clear = gr.Button("Refresh!")
87
+ clear.click(lambda: [None, None, None, None], inputs=[], outputs=[image, age_label, comment, emotion_label])
88
+
89
+ # Launching the interface
90
+ demo.launch(share=True, debug=True)