Spaces:
Sleeping
Sleeping
| 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() |