Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import requests
|
| 5 |
+
ts_url = 'https://static.promediateknologi.id/crop/0x0:0x0/0x0/webp/photo/p2/01/2023/08/10/taylor-swift-3169402579.png'
|
| 6 |
+
ts_image = Image.open(requests.get(ts_url, stream=True).raw)
|
| 7 |
+
pg_url = "https://media.vogue.co.uk/photos/60f888a382e60565201c7cf4/2:3/w_2560%2Cc_limit/prince-Grorge-eigth-birthday-portrait.jpg"
|
| 8 |
+
pg_image = Image.open(requests.get(pg_url, stream=True).raw)
|
| 9 |
+
img_url = "https://thumbs.dreamstime.com/b/old-man-20313005.jpg"
|
| 10 |
+
img_image = Image.open(requests.get(img_url, stream=True).raw)
|
| 11 |
+
|
| 12 |
+
def age_emot_classifier(input_image):
|
| 13 |
+
|
| 14 |
+
# Init model, transforms
|
| 15 |
+
model_age = ViTForImageClassification.from_pretrained('nateraw/vit-age-classifier')
|
| 16 |
+
transforms_age = ViTFeatureExtractor.from_pretrained('nateraw/vit-age-classifier')
|
| 17 |
+
|
| 18 |
+
model_emot = ViTForImageClassification.from_pretrained("yangswei/visual-emotion-classification")
|
| 19 |
+
transforms_emot = ViTFeatureExtractor.from_pretrained("yangswei/visual-emotion-classification")
|
| 20 |
+
|
| 21 |
+
# Transform our image and pass it through the model
|
| 22 |
+
inputs_age = transforms_age(input_image, return_tensors='pt')
|
| 23 |
+
output_age = model_age(**inputs_age)
|
| 24 |
+
|
| 25 |
+
inputs_emot = transforms_emot(input_image, return_tensors='pt')
|
| 26 |
+
output_emot = model_emot(**inputs_emot)
|
| 27 |
+
|
| 28 |
+
# Predicted Class probabilities
|
| 29 |
+
proba_age = output_age.logits.softmax(1)
|
| 30 |
+
proba_emot = output_emot.logits.softmax(1)
|
| 31 |
+
|
| 32 |
+
# Predicted Classes With Confidences
|
| 33 |
+
labels_age = model_age.config.id2label
|
| 34 |
+
confidences_age = {labels_age[i]: proba_age[0][i].item() for i in range(len(labels_age))}
|
| 35 |
+
|
| 36 |
+
labels_emot = model_emot.config.id2label
|
| 37 |
+
confidences_emot = {labels_emot[i]: proba_emot[0][i].item() for i in range(len(labels_emot))}
|
| 38 |
+
|
| 39 |
+
return confidences_age, confidences_emot
|
| 40 |
+
|
| 41 |
+
output_age = gr.Label(num_top_classes=9, label="Age Prediction")
|
| 42 |
+
output_emotion = gr.Label(num_top_classes=8, label="Emotion Prediction")
|
| 43 |
+
|
| 44 |
+
with gr.Blocks(theme=gr.themes.Glass()) as demo:
|
| 45 |
+
gr.Interface(fn=age_emot_classifier, inputs="image", outputs=[output_age, output_emotion],
|
| 46 |
+
examples=[ts_image, pg_image, img_image])
|
| 47 |
+
|
| 48 |
+
demo.launch()
|