-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
53 lines (41 loc) · 1.52 KB
/
model.py
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
import streamlit as st
import pandas as pd
import nltk
from nltk.stem.porter import PorterStemmer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
# Load the dataset
df = pd.read_csv('songdata.csv')
df = df.sample(n=5000).drop('link', axis=1).reset_index(drop=True)
# Preprocess text
df['text'] = df['text'].str.lower().replace(r'[^\w\s]','').replace(r'\n',' ', regex=True)
# Tokenization and stemming
stemmer = PorterStemmer()
def tokenization(txt):
tokens = nltk.word_tokenize(txt)
stemming = [stemmer.stem(w) for w in tokens]
return " ".join(stemming)
df['text'] = df['text'].apply(lambda x: tokenization(x))
# TF-IDF Vectorization
tfidfvector = TfidfVectorizer(stop_words='english')
matrix = tfidfvector.fit_transform(df['text'])
similarity = cosine_similarity(matrix)
# Recommendation function
def recommendation(song_df):
idx = df[df['song'] == song_df].index[0]
distances = sorted(list(enumerate(similarity[idx])), reverse=True, key=lambda x: x[1])
songs = []
for m_id in distances[1:21]:
songs.append(df.iloc[m_id[0]]['song'])
return songs
# Streamlit app
st.title("Song Recommendation App")
# Select a song
selected_song = st.selectbox("Select a song:", df['song'].values)
# Button to trigger recommendation
if st.button("Get Recommendations"):
# Get recommendations
recommendations = recommendation(selected_song)
# Display recommendations
st.success("Recommended Songs:")
st.write(recommendations)