zhenology commited on
Commit
20a54af
·
verified ·
1 Parent(s): 0999840

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +35 -0
  2. emotion_model.h5 +3 -0
  3. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from tensorflow.keras.preprocessing import image
5
+ from PIL import Image
6
+
7
+ # Load your trained model
8
+ model = tf.keras.models.load_model("emotion_model.h5") # Ensure this model is in the repo
9
+
10
+ # Define emotion labels
11
+ emotion_labels = ['Anger', 'Disgust', 'Fear', 'Happiness', 'Neutral', 'Sadness', 'Surprise', 'Contempt']
12
+
13
+ # Function for inference
14
+ def predict_emotion(img):
15
+ img = img.convert("RGB").resize((48, 48)) # Ensure correct size
16
+ img_array = image.img_to_array(img)
17
+ img_array = np.expand_dims(img_array, axis=0) / 255.0 # Normalize
18
+
19
+ predictions = model.predict(img_array)
20
+ predicted_class = np.argmax(predictions)
21
+ confidence = np.max(predictions)
22
+
23
+ return f"Emotion: {emotion_labels[predicted_class]} (Confidence: {confidence:.2f})"
24
+
25
+ # Gradio UI
26
+ iface = gr.Interface(
27
+ fn=predict_emotion,
28
+ inputs=gr.Image(type="pil"),
29
+ outputs="text",
30
+ title="Emotion Detection",
31
+ description="Upload an image, and the AI will predict the emotion.",
32
+ )
33
+
34
+ # Run app
35
+ iface.launch()
emotion_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:39922632d095a301b9c0122dcf1846eba928c028865cabfe5f2386c36c2ccfb3
3
+ size 289692192
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ tensorflow
2
+ gradio
3
+ numpy
4
+ pillow