File size: 1,106 Bytes
20a54af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf
import numpy as np
from tensorflow.keras.preprocessing import image
from PIL import Image

# Load your trained model
model = tf.keras.models.load_model("emotion_model.h5")  # Ensure this model is in the repo

# Define emotion labels
emotion_labels = ['Anger', 'Disgust', 'Fear', 'Happiness', 'Neutral', 'Sadness', 'Surprise', 'Contempt']

# Function for inference
def predict_emotion(img):
    img = img.convert("RGB").resize((48, 48))  # Ensure correct size
    img_array = image.img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0) / 255.0  # Normalize

    predictions = model.predict(img_array)
    predicted_class = np.argmax(predictions)
    confidence = np.max(predictions)

    return f"Emotion: {emotion_labels[predicted_class]} (Confidence: {confidence:.2f})"

# Gradio UI
iface = gr.Interface(
    fn=predict_emotion,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="Emotion Detection",
    description="Upload an image, and the AI will predict the emotion.",
)

# Run app
iface.launch()