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