Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
from io import BytesIO
|
6 |
+
|
7 |
+
# Load the emotion detection model from Hugging Face's Transformers library
|
8 |
+
emotion_model = pipeline("image-classification", model="EleutherAI/emotion-english", device=0)
|
9 |
+
|
10 |
+
# Define the function to make predictions on user-uploaded images
|
11 |
+
def predict_emotion(image):
|
12 |
+
# Open the uploaded image
|
13 |
+
img = Image.open(BytesIO(image.encode("utf-8")))
|
14 |
+
|
15 |
+
# Make prediction using the emotion detection model
|
16 |
+
result = emotion_model(img)
|
17 |
+
|
18 |
+
# Extract the emotion label with the highest confidence
|
19 |
+
emotion = result[0]['label']
|
20 |
+
|
21 |
+
return f"Predicted emotion: {emotion}"
|
22 |
+
|
23 |
+
# Define the Gradio interface
|
24 |
+
iface = gr.Interface(
|
25 |
+
fn=predict_emotion,
|
26 |
+
inputs=gr.Image(),
|
27 |
+
outputs="text",
|
28 |
+
live=True,
|
29 |
+
interpretation="default",
|
30 |
+
title="Face Expression Predictor",
|
31 |
+
description="Upload an image and get the predicted face expression."
|
32 |
+
)
|
33 |
+
|
34 |
+
# Launch the Gradio app
|
35 |
+
iface.launch()
|