Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,28 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
|
|
|
| 6 |
|
|
|
|
| 7 |
def detect_emotion(image):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
}
|
| 15 |
-
return {"top_prediction": "No face detected", "all_predictions": []}
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
gr.Interface(
|
| 19 |
fn=detect_emotion,
|
| 20 |
inputs=gr.Image(type="pil"),
|
| 21 |
-
outputs="
|
| 22 |
-
title="
|
| 23 |
-
description="
|
| 24 |
-
)
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
|
| 6 |
+
# Load model and processor
|
| 7 |
+
processor = AutoImageProcessor.from_pretrained("dima806/facial_emotions_image_detection")
|
| 8 |
+
model = AutoModelForImageClassification.from_pretrained("dima806/facial_emotions_image_detection")
|
| 9 |
|
| 10 |
+
# Function to predict emotion
|
| 11 |
def detect_emotion(image):
|
| 12 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
outputs = model(**inputs)
|
| 15 |
+
logits = outputs.logits
|
| 16 |
+
predicted_class = logits.argmax(-1).item()
|
| 17 |
+
return model.config.id2label[predicted_class]
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Gradio interface
|
| 20 |
+
iface = gr.Interface(
|
| 21 |
fn=detect_emotion,
|
| 22 |
inputs=gr.Image(type="pil"),
|
| 23 |
+
outputs="text",
|
| 24 |
+
title="Facial Emotion Detection",
|
| 25 |
+
description="Upload a face image to detect emotion using dima806 model"
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
iface.launch(share=True)
|