Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 5 |
+
|
| 6 |
+
# Load the trained model
|
| 7 |
+
model = tf.keras.models.load_model("my_model.h5")
|
| 8 |
+
|
| 9 |
+
# Class labels (modify according to your dataset)
|
| 10 |
+
class_labels = ["Angry", "Disgust", "Fear", "Happy", "Neutral", "Sad", "Surprise"]
|
| 11 |
+
|
| 12 |
+
# Function to make predictions
|
| 13 |
+
def predict_emotion(image):
|
| 14 |
+
# Resize image to match model's expected input
|
| 15 |
+
image = image.resize((224, 224))
|
| 16 |
+
img_array = img_to_array(image) / 255.0 # Normalize
|
| 17 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
| 18 |
+
|
| 19 |
+
# Make prediction
|
| 20 |
+
prediction = model.predict(img_array)
|
| 21 |
+
predicted_class = class_labels[np.argmax(prediction)]
|
| 22 |
+
confidence = np.max(prediction)
|
| 23 |
+
|
| 24 |
+
return f"Emotion: {predicted_class} (Confidence: {confidence:.2f})"
|
| 25 |
+
|
| 26 |
+
# Create Gradio interface
|
| 27 |
+
interface = gr.Interface(
|
| 28 |
+
fn=predict_emotion,
|
| 29 |
+
inputs=gr.Image(type="pil"),
|
| 30 |
+
outputs=gr.Textbox(label="Prediction"),
|
| 31 |
+
title="Facial Emotion Detection",
|
| 32 |
+
description="Upload an image, and the model will predict the emotion.",
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Run the app
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
interface.launch()
|