diff --git a/Pickle/freq.pkl b/Pickle/freq.pkl new file mode 100644 index 0000000..266a419 Binary files /dev/null and b/Pickle/freq.pkl differ diff --git a/Pickle/rules.pkl b/Pickle/rules.pkl new file mode 100644 index 0000000..49e3521 Binary files /dev/null and b/Pickle/rules.pkl differ diff --git a/Rest_API/server.py b/Rest_API/server.py index 3d7b6f9..0876073 100644 --- a/Rest_API/server.py +++ b/Rest_API/server.py @@ -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) \ No newline at end of file diff --git a/Train_model/ml.py b/Train_model/ml.py index 9f8c1a1..9ce93a7 100644 --- a/Train_model/ml.py +++ b/Train_model/ml.py @@ -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() \ No newline at end of file diff --git a/Rest_API/client.py b/client.py similarity index 77% rename from Rest_API/client.py rename to client.py index be3c380..d2b8aa8 100644 --- a/Rest_API/client.py +++ b/client.py @@ -4,6 +4,10 @@ api_url = 'http://127.0.0.1:32196/api/recommend' +if len(sys.argv) < 2: + print("Usage: python client.py ...") + sys.exit(1) + input_playlist = sys.argv[1:] data = json.dumps({'songs': input_playlist}) diff --git a/ler.py b/ler.py new file mode 100644 index 0000000..320c011 --- /dev/null +++ b/ler.py @@ -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()}") \ No newline at end of file