Spaces:
No application file
No application file
Create app.py
#1
by
ahyar002
- opened
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Downloading files from the demo repo
|
2 |
+
import os
|
3 |
+
os.mkdir('images')
|
4 |
+
!wget -q -O images/jokowi.jpeg https://cdn.setneg.go.id/_multimedia/photo/20220218/5008WhatsApp_Image_2022-02-18_at_1.36.50_PM.jpeg
|
5 |
+
!wget -q -O images/megawati.jpeg https://gallery.poskota.co.id/storage/Foto/Foto_20220602_205953_hql.jpeg
|
6 |
+
!wget -q -O images/cipung.jpg https://cdn.idntimes.com/content-images/community/2022/11/rayyanza-695b5fc766d9ed00ece029dcd8177b8e-4c74e93112d56ab97dac735945a7a619_600x400.jpg
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
from PIL import Image
|
10 |
+
from transformers import pipeline
|
11 |
+
|
12 |
+
# import the model
|
13 |
+
pipe_age = pipeline("image-classification", model="nateraw/vit-age-classifier")
|
14 |
+
pipe_emotion = pipeline("image-classification", model="ahyar002/emotion_classification")
|
15 |
+
|
16 |
+
def age_prediction(image):
|
17 |
+
# convert to PIL image
|
18 |
+
pil_image = Image.fromarray(image)
|
19 |
+
# predict the image
|
20 |
+
predict_age = pipe_age(pil_image)
|
21 |
+
predict_emotion = pipe_emotion(pil_image)
|
22 |
+
# tranform the ouput into dictionary
|
23 |
+
transformed_dict_age = {item['label']: item['score'] for item in predict_age}
|
24 |
+
transformed_dict_emotion = {item['label']: item['score'] for item in predict_emotion}
|
25 |
+
|
26 |
+
return transformed_dict_age, transformed_dict_emotion
|
27 |
+
|
28 |
+
|
29 |
+
demo = gr.Interface(age_prediction,
|
30 |
+
inputs = "image",
|
31 |
+
outputs= [gr.Label(num_top_classes=3), gr.Label(num_top_classes=3)],
|
32 |
+
examples=[
|
33 |
+
os.path.join(os.path.abspath(''), "images/jokowi.jpeg"),
|
34 |
+
os.path.join(os.path.abspath(''), "images/megawati.jpeg"),
|
35 |
+
os.path.join(os.path.abspath(''), "images/cipung.jpg"),
|
36 |
+
],
|
37 |
+
)
|
38 |
+
|
39 |
+
if __name__ == "__main__":
|
40 |
+
demo.launch(debug=True)
|