Spaces:
Runtime error

BSHarsha's picture
Update app.py
fbcdb4e verified
Raw History Blame Contribute Delete
1.19 kB
import gradio as gr
import tensorflow as tf
import numpy as np
from tensorflow.keras.preprocessing.image import load_img, img_to_array
# Load the trained model
model = tf.keras.models.load_model("my_model.h5")
# Class labels (modify according to your dataset)
class_labels = ["Angry", "Disgust", "Fear", "Happy", "Neutral", "Sad", "Surprise"]
# Function to make predictions
def predict_emotion(image):
# Resize image to match model's expected input
image = image.resize((224, 224))
img_array = img_to_array(image) / 255.0 # Normalize
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
# Make prediction
prediction = model.predict(img_array)
predicted_class = class_labels[np.argmax(prediction)]
confidence = np.max(prediction)
return f"Emotion: {predicted_class} (Confidence: {confidence:.2f})"
# Create Gradio interface
interface = gr.Interface(
fn=predict_emotion,
inputs=gr.Image(type="pil"),
outputs=gr.Textbox(label="Prediction"),
title="Facial Emotion Detection",
description="Upload an image, and the model will predict the emotion.",
)
# Run the app
if __name__ == "__main__":
interface.launch()