Skip to content

Commit

Permalink
spotify array printing correction
Browse files Browse the repository at this point in the history
  • Loading branch information
tharoosha committed Oct 28, 2023
1 parent 2769ec7 commit 92e6868
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 10 deletions.
8 changes: 4 additions & 4 deletions backend/controllers/mlController.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,18 @@ export async function spotify_recommend(req, res) {

// Listen for data events from the Python script's stdout
pythonProcess.stdout.on("data", (data) => {
output += data;
// output += data;
console.log(data.toString())
output += data.toString();
});

// Listen for the 'close' event to handle the completion of the Python script
pythonProcess.on("close", (code) => {
if (code === 0) {
try {
// const result = JSON.parse(output);
const result = JSON.parse(output);
// res.status(200).json(result);
// const result = JSON.parse(output); // Parse the output string to JSON
res.status(200).send(output);
res.status(200).json(result);
// res.status(200).json({"result":output});
} catch (error) {
res.status(500).json({ error: "Failed to parse JSON response" });
Expand Down
15 changes: 15 additions & 0 deletions backend/ml_models/recommanded_system/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


// # The JSON string
var json_str = {'result': '["090fexGQyBy6tjejeKZSAN", "090fexGQyBy6tjejeKZSAN", "090fexGQyBy6tjejeKZSAN", "090fexGQyBy6tjejeKZSAN"]'}

// Parse the JSON string
// var jsonData = JSON.parse(json_str);

// Access the list inside the 'result' key
var resultList = JSON.parse(json_str.result);

// Now you can work with the list
resultList.forEach(function(item) {
console.log(item);
});
2 changes: 1 addition & 1 deletion backend/ml_models/spotify_recommendation/.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"access_token": "BQC7oaTQMhH1cupHvmFFaUkZ8DeZKc0fN0M96sYkhe6-A35ewKfGDDTjo7QC--dmpLJ1WZfYIp8rl4pnjTwQmw7IQEl0oEwkaHgR7oZqJ98ch0opJaWEtKVuUJUA79EuuyvERBCqxRZz_lbV-kiETs_dWCT5GoZhpFaPNOBKqQL2SRFZ3FzHZ18m80NlxZ_1o-5_oAoYRkq63NUAgp6VB1vkdZKuSS4JBmfnZw", "token_type": "Bearer", "expires_in": 3600, "scope": "playlist-read-private user-library-read user-read-recently-played user-top-read", "expires_at": 1698431800, "refresh_token": "AQDPFQQaeVerMmQwRulEXf-J4KgjjUINngJ-7MU19CyzrhZMYfh20NqYWXpHpc-XVrLBe7_gig_4zOmcnQCE2DvOrjAUUaK5YigF0Wg2C4f-NYpccD2MBMTl7qZ4vm3falU"}
{"access_token": "BQCOQJMEAu3mc1Pk9QX_XLcX7mXJh4lWaz7JvRvEdhhty4euS2Uh3IzKh0R5AJ1uzRqbr6-9qWV4mnl5YooPwXfHl3Z7HdOWmRJvDMoHCZJ37S8yJ3G9jxDAtu8u_yQ3l1yUSzML3n_SI3rP_ny5-Y2WiTKCcufLwossCnnD-HXleWNpdRdsf36zFf1SNNxUTs-Oxv5z3PQxylRKcY0C3KpltfXzu7miNZqFyQ", "token_type": "Bearer", "expires_in": 3600, "scope": "playlist-read-private user-library-read user-read-recently-played user-top-read", "expires_at": 1698518625, "refresh_token": "AQDPFQQaeVerMmQwRulEXf-J4KgjjUINngJ-7MU19CyzrhZMYfh20NqYWXpHpc-XVrLBe7_gig_4zOmcnQCE2DvOrjAUUaK5YigF0Wg2C4f-NYpccD2MBMTl7qZ4vm3falU"}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ def script_run(input_mood):
# model = tf.keras.models.load_model('../backend/ml_models/spotify_recommendation/spotify_model')

result = spotifyRecommendScript.getRecommendation(input_mood, model, scaler)
# result = json.dumps(result)
result = json.dumps(result)
# response = str(result)
# output = {"result": result}
output = {"result": result}

# output_json = json.dumps(output)
# sys.stdout.flush()
print(result)
# print(result)
print(output)

except Exception as e:
error_message = str(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import pickle
import os
import config as cf
import contextlib



Expand Down Expand Up @@ -141,10 +142,16 @@ def initialize():
def getRecommendation(mood, model, scaler):

df_recentSongs = getRecentlyPlayed()
df2 = pd.DataFrame(model.predict(scaler.fit_transform(df_recentSongs.iloc[:, 1:])))

# Use contextlib.redirect_stdout to suppress output
with contextlib.redirect_stdout(None):
df2 = pd.DataFrame(model.predict(scaler.fit_transform(df_recentSongs.iloc[:, 1:])))

# df2 = pd.DataFrame(model.predict(scaler.fit_transform(df_recentSongs.iloc[:, 1:])))
df2['Mood']=df2.apply(get_max_index, axis=1)
df_recentSongs['Mood'] = df2['Mood'].apply(decodeLabels)
filtered_df = df_recentSongs[df_recentSongs['Mood']==mood]

return filtered_df.tail(10)[0].tolist()[::-1]

if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/AI_Assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const AI_Assistant = () => {
.post('http://localhost:5001/api/emotion_analyze', { message: message })
.then((response) => {
setEmotion(response.data.emotion);
// console.log(response.data.emotion)
console.log(response.data.emotion)
})
.catch((error) => console.error(error));
// Clear the input field after submitting
Expand Down

0 comments on commit 92e6868

Please sign in to comment.