Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoOMaia committed Nov 21, 2023
1 parent 89c5a2d commit 275852d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
Binary file added Pickle/freq.pkl
Binary file not shown.
Binary file added Pickle/rules.pkl
Binary file not shown.
19 changes: 12 additions & 7 deletions Rest_API/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@
import pickle

app = Flask(__name__)

rules_path = 'rules.pkl'
rules_path = '../Pickle/rules.pkl'
freq_path = '../Pickle/freq.pkl'

@app.route('/api/recommend', methods=['POST'])
def recommend():
songs = request.json['songs']

rules = pickle.load(open(rules_path, 'rb'))
freq = pickle.load(open(freq_path, 'rb'))
recommended_playlists = []

recommended_songs = []

# Verifica as regras de associação para recomendar playlists
for song in songs:
recommended_songs.append("teste")
filtered_rules = rules[rules['antecedents'] == frozenset({song})]
if not filtered_rules.empty:
recommended_playlists.extend(filtered_rules['consequents'].values[0])

# Remove duplicatas e converte para uma lista única de playlists recomendadas
recommended_playlists = list(set(recommended_playlists))

return jsonify(recommended_songs)
return jsonify({'recommended_playlists': recommended_playlists})

if __name__ == '__main__':
app.run(port=32196)
6 changes: 2 additions & 4 deletions Train_model/ml.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,17 @@ def encode(self, x):

def get_freq(self):
self.freq = apriori(self.df, min_support=min_support, use_colnames=True)
self.freq.to_pickle('freq.pkl')
self.freq.to_pickle('../Pickle/freq.pkl')
print("freq.pkl created successfully")

def get_rules(self):
self.rules = association_rules(self.freq, metric="lift", min_threshold=min_threshold)
self.rules.to_pickle('rules.pkl')
self.rules.to_pickle('../Pickle/rules.pkl')
print("rules.pkl created successfully")


if __name__ == '__main__':
fdm = FreqDatasetMining(FILE_PATH)

fdm.group_by_pid_and_track_uri()

fdm.get_freq()
fdm.get_rules()
4 changes: 4 additions & 0 deletions Rest_API/client.py → client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

api_url = 'http://127.0.0.1:32196/api/recommend'

if len(sys.argv) < 2:
print("Usage: python client.py <song1> ...")
sys.exit(1)

input_playlist = sys.argv[1:]

data = json.dumps({'songs': input_playlist})
Expand Down
13 changes: 13 additions & 0 deletions ler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pandas as pd
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules

df_spotify_ds1 = pd.read_csv("/home/datasets/2023_spotify_ds1.csv")
df_spotify_ds2 = pd.read_csv("/home/datasets/2023_spotify_ds2.csv")
df_songs = pd.read_csv("/home/datasets/2023_spotify_songs.csv")



print(f"spotify_ds1:\n{df_spotify_ds1.head()}")
print(f"spotify_ds2:\n{df_spotify_ds2.head()}")
print(f"songs:\n{df_songs.head()}")

0 comments on commit 275852d

Please sign in to comment.