Spaces:
Runtime error
Runtime error
Delete app.py
#2
by beshaksha - opened
1234567891011121314151617181920212223242526272829303132333435363738
import gradio as grimport tensorflow as tfimport numpy as npfrom tensorflow.keras.preprocessing.image import load_img, img_to_array
# Load the trained modelmodel = 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 predictionsdef 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 interfaceinterface = 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 appif __name__ == "__main__": interface.launch()app.pyDeleted-38