Spaces:
Runtime error
Runtime error
File size: 2,117 Bytes
edc4276 01e4bba c4d5407 401d1a7 edc4276 01e4bba 1b42e45 edc4276 577a126 c4d5407 577a126 c4d5407 577a126 edc4276 577a126 edc4276 577a126 01e4bba 577a126 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import streamlit as st
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.sequence import pad_sequences
import joblib
import pandas as pd
import numpy as np
# Load your models
emotion_model = load_model('lstm_model.h5')
# Save the scikit-learn model
joblib.dump(knn_model, 'knn_model.pkl')
# Load the model
recommender_model = joblib.load('knn_model.pkl')
# Load the tokenizer (if used during training)
# tokenizer = joblib.load('tokenizer.pkl') # Update with actual file name
# Load the dataset
df = pd.read_csv('df1.csv') # Make sure this is the correct DataFrame
# Set up the title of the app
st.title('Emotion and Audio Feature-based Song Recommendation System')
# Input field for lyrics
st.header('Enter Song Lyrics')
lyrics = st.text_area("Input the lyrics of the song here:")
# Input fields for audio features
st.header('Enter Audio Features')
audio_features = []
for feature_name in df.columns: # Make sure this matches your DataFrame
feature = st.number_input(f"Enter value for {feature_name}:", step=0.01)
audio_features.append(feature)
# Predict and Recommend button
if st.button('Predict Emotion and Recommend Songs'):
if lyrics and all(audio_features):
sequence = tokenizer.texts_to_sequences([lyrics])
padded_sequence = pad_sequences(sequence, maxlen=128)
emotion = emotion_model.predict(padded_sequence).flatten() # Flatten if needed
# Combine emotion and audio features for recommendation
combined_features = np.concatenate([[emotion], audio_features])
# Generate recommendations using the KNN model
distances, indices = recommender_model.kneighbors([combined_features], n_neighbors=5)
recommended_songs = df.iloc[indices.flatten()]
# Display emotion and recommendations
st.write("Emotion Detected:", emotion[0]) # Adjust as per your model's output
st.header('Recommended Songs')
for _, song in recommended_songs.iterrows():
st.write(song) # Adjust based on your dataset
else:
st.error("Please fill in all the fields.")
|