Spaces:
Sleeping
Sleeping
Create appie.py
Browse files
appie.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
from tensorflow.keras.preprocessing.image
|
| 6 |
+
import img_to_array import os
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
MODEL_PATH = "emotion_model.h5" # Placeholder path
|
| 10 |
+
if os.path.exists(MODEL_PATH):
|
| 11 |
+
model = load_model(MODEL_PATH)
|
| 12 |
+
else:
|
| 13 |
+
model = None
|
| 14 |
+
st.error("Model file not found! Please provide a trained model.")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
EMOTIONS = ["Angry", "Disgust", "Fear", "Happy", "Neutral", "Sad", "Surprise"]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
MUSIC_RECOMMENDATIONS = { "Happy": ["Happy - Pharrell Williams", "Can't Stop the Feeling - Justin Timberlake"], "Sad": ["Someone Like You - Adele", "Fix You - Coldplay"], "Angry": ["Break Stuff - Limp Bizkit", "Smells Like Teen Spirit - Nirvana"], "Neutral": ["Let It Be - The Beatles", "Imagine - John Lennon"], "Fear": ["Thriller - Michael Jackson", "Disturbia - Rihanna"], "Surprise": ["Shake It Off - Taylor Swift", "Uptown Funk - Bruno Mars"], "Disgust": ["Not Afraid - Eminem", "Lose Yourself - Eminem"] }
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
st.title("AI-Powered Mood-Based Music Recommendation")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
uploaded_file = st.file_uploader("Upload an image of your face", type=["jpg", "png", "jpeg"])
|
| 28 |
+
|
| 29 |
+
if uploaded_file is not None and model: # Read image
|
| 30 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 31 |
+
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 32 |
+
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 33 |
+
image_resized = cv2.resize(image_gray, (48, 48))
|
| 34 |
+
image_array = img_to_array(image_resized)
|
| 35 |
+
image_array = np.expand_dims(image_array, axis=0) / 255.0
|
| 36 |
+
|
| 37 |
+
# Predict emotion
|
| 38 |
+
prediction = model.predict(image_array)
|
| 39 |
+
predicted_emotion = EMOTIONS[np.argmax(prediction)]
|
| 40 |
+
|
| 41 |
+
st.image(image, caption=f"Detected Emotion: {predicted_emotion}", use_column_width=True)
|
| 42 |
+
|
| 43 |
+
# Recommend songs
|
| 44 |
+
st.subheader("Recommended Songs for Your Mood:")
|
| 45 |
+
for song in MUSIC_RECOMMENDATIONS.get(predicted_emotion, []):
|
| 46 |
+
st.write(f"- {song}")
|
| 47 |
+
|
| 48 |
+
else:
|
| 49 |
+
st.info("Please upload an image to proceed."
|