diff --git a/.cache b/.cache deleted file mode 100644 index 92498ea..0000000 --- a/.cache +++ /dev/null @@ -1 +0,0 @@ -{"access_token": "BQCyjMGZf4pCx_eJtw1NPa0Ol3r_-ZLTMZA3VKH7ftRL6IxBKFVGzB4-tVJQ09PuG6eXbeuf2QWfLp4ACtFYo6LnCKyxC1hlIbwbdaY6_sMLrd5kNG6AAW4_-ssHLi7wQVHAu6b8Hbv-ghIYq92BLZPGpl4X4WgaduKfOLwRBXxYZuZNiN5C5EhVCMUOp5dtG-KMsu0FAuQP6AaL4hrzP2J45-np3EmaWg", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "AQDj6FSnK3dfmU2SW2SWaGl8da_ds0BdGhSLvMNjB_v0F_9sW-IbhNfbpokuw5IgbA2IfsvO7e5BVjICYtJLtulq9CRcj3i7anQZBwtE7wZ-8WTVamZskikYmDTrmY9i_hs", "scope": "playlist-read-private user-library-read user-read-recently-played user-top-read", "expires_at": 1693866674} \ No newline at end of file diff --git a/backend/controllers/appController.js b/backend/controllers/appController.js index 573bb70..058f3f7 100644 --- a/backend/controllers/appController.js +++ b/backend/controllers/appController.js @@ -314,4 +314,50 @@ export async function resetPassword(req,res){ } } +/** POST: http://localhost:5001/api/emotions/retrieve */ +/** Middleware for saving emotions for a user */ +export async function saveEmotions(req, res, next) { + try { + const { username, emotions } = req.body; + + // Check if the user exists + const user = await UserModel.findOne({ username }); + + if (!user) { + return res.status(404).send({ error: "User not found" }); + } + + // Update the user's emotions field with the new data + user.emotions = emotions; + + await user.save(); + + next(); + } catch (error) { + return res.status(500).send({ error: "Server Error" }); + } + } + + +/** POST: http://localhost:5001/api/emotions/save */ +/** Middleware for retrieving emotions for a user */ +export async function retrieveEmotions(req, res, next) { + try { + const { username } = req.method === "GET" ? req.query : req.body; + + // Check if the user exists + const user = await UserModel.findOne({ username }); + + if (!user) { + return res.status(404).send({ error: "User not found" }); + } + + // Assuming that emotions are stored as a field in the user model + req.userEmotions = user.emotions; + + next(); + } catch (error) { + return res.status(500).send({ error: "Server Error" }); + } + } \ No newline at end of file diff --git a/backend/controllers/mlController.js b/backend/controllers/mlController.js index 99e3d49..6ec4f83 100644 --- a/backend/controllers/mlController.js +++ b/backend/controllers/mlController.js @@ -130,11 +130,11 @@ export async function speech_to_text(req, res) { */ export async function emotion_analyzer(req, res) { - const { tweet } = req.body; + const { message } = req.body; try { - console.log(tweet) + console.log(message) const process = spawn("/usr/src/app/venv/bin/python3", ["/usr/src/app/ml_models/emotion_detection/emotionScript.py", tweet,]); - // const process = spawn("python3", ["../backend/ml_models/emotion_detection/emotionScript.py", tweet,]); + // const process = spawn("python3", ["../backend/ml_models/emotion_detection/emotionScript.py", message,]); let emotion = "" process.stdout.on("data", (data) => { @@ -155,6 +155,7 @@ export async function emotion_analyzer(req, res) { const jsonData = JSON.parse(emotion); // const joy = jsonData.emotion; // console.log(joy); + // console.log(emotion); res.status(200).send(jsonData); } catch (error) { res.status(500).json({ error: "Failed to parse JSON response" }); diff --git a/backend/ml_models/spotify_recommendation/.cache b/backend/ml_models/spotify_recommendation/.cache index 5991830..4c0e021 100644 --- a/backend/ml_models/spotify_recommendation/.cache +++ b/backend/ml_models/spotify_recommendation/.cache @@ -1 +1 @@ -{"access_token": "BQC80QFI4ychDG5CWRViF_eHXkYOT42vHCZY1aPIIPlhVcy4eKZLJA5c2CUdutVIBQTa-5hrNyKT_G6LY154cQythJwSjrfoM2AbG2dUdgAj8Seb63tdcbVVGQ_bGdTfu0WrCDUHTkzz26k5YmB9RKkP8qTWaXLIe1AQyTSnj43YBHe8Za2b8k-ZNl94NVoc68nAmiVZJ0fg5q9q_CnSU4-vPNolZYhCNI429g", "token_type": "Bearer", "expires_in": 3600, "scope": "playlist-read-private user-library-read user-read-recently-played user-top-read", "expires_at": 1698560410, "refresh_token": "AQDPFQQaeVerMmQwRulEXf-J4KgjjUINngJ-7MU19CyzrhZMYfh20NqYWXpHpc-XVrLBe7_gig_4zOmcnQCE2DvOrjAUUaK5YigF0Wg2C4f-NYpccD2MBMTl7qZ4vm3falU"} \ No newline at end of file +{"access_token": "BQC9OMFKcpyPnUKxRzR2QITzzr8GhYPCH2KCS8hk4na6TntReXKzKR91CKupGAvUBqcg0SLic6eae5RFdO4fv0Z1sPNj1cyRsShXpuV7EixiC3HDNGajiGKqRtcd0Ox8gotnH9tj2W09qijsnUuQ-V_M67mc0qRjjQN7nH5lTUBJTxIyfrt6AU27MDdITmZ4Y2POvDLupBjEWxnB3TYWaGuSo-4SxfCb78njbw", "token_type": "Bearer", "expires_in": 3600, "scope": "playlist-read-private user-library-read user-read-recently-played user-top-read", "expires_at": 1698634377, "refresh_token": "AQDPFQQaeVerMmQwRulEXf-J4KgjjUINngJ-7MU19CyzrhZMYfh20NqYWXpHpc-XVrLBe7_gig_4zOmcnQCE2DvOrjAUUaK5YigF0Wg2C4f-NYpccD2MBMTl7qZ4vm3falU"} \ No newline at end of file diff --git a/backend/ml_models/spotify_recommendation/__pycache__/spotifyRecommendScript.cpython-311.pyc b/backend/ml_models/spotify_recommendation/__pycache__/spotifyRecommendScript.cpython-311.pyc new file mode 100644 index 0000000..ad4b964 Binary files /dev/null and b/backend/ml_models/spotify_recommendation/__pycache__/spotifyRecommendScript.cpython-311.pyc differ diff --git a/backend/ml_models/spotify_recommendation/spotifyRecommendScript.py b/backend/ml_models/spotify_recommendation/spotifyRecommendScript.py index bb91343..48ded91 100644 --- a/backend/ml_models/spotify_recommendation/spotifyRecommendScript.py +++ b/backend/ml_models/spotify_recommendation/spotifyRecommendScript.py @@ -5,12 +5,14 @@ from spotipy.oauth2 import SpotifyOAuth import pandas as pd from sklearn.model_selection import train_test_split +import os +os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress INFO and WARNING messages import tensorflow as tf from sklearn.preprocessing import StandardScaler from tensorflow.keras import layers, models from tensorflow.keras.optimizers.legacy import Adam import pickle -import os + import config as cf import contextlib @@ -152,8 +154,8 @@ def getRecommendation(mood, model, scaler): df_recentSongs = getRecentlyPlayed() # Use contextlib.redirect_stdout to suppress output - with contextlib.redirect_stdout(None): - df2 = pd.DataFrame(model.predict(scaler.fit_transform(df_recentSongs.iloc[:, 1:]))) + # 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) diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 47fcada..a2001e9 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -15,6 +15,7 @@ "antd": "^5.5.1", "axios": "^1.4.0", "buffer": "^6.0.3", + "dotenv": "^16.3.1", "extendable-media-recorder": "^9.1.3", "extendable-media-recorder-wav-encoder": "^7.0.99", "formik": "^2.4.2", @@ -7518,11 +7519,14 @@ } }, "node_modules/dotenv": { - "version": "10.0.0", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", - "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==", + "version": "16.3.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", + "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", "engines": { - "node": ">=10" + "node": ">=12" + }, + "funding": { + "url": "https://github.com/motdotla/dotenv?sponsor=1" } }, "node_modules/dotenv-expand": { @@ -16457,6 +16461,14 @@ } } }, + "node_modules/react-scripts/node_modules/dotenv": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", + "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==", + "engines": { + "node": ">=10" + } + }, "node_modules/react-slick": { "version": "0.29.0", "resolved": "https://registry.npmjs.org/react-slick/-/react-slick-0.29.0.tgz", diff --git a/frontend/package.json b/frontend/package.json index cc3b4cb..dc21a68 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -10,6 +10,7 @@ "antd": "^5.5.1", "axios": "^1.4.0", "buffer": "^6.0.3", + "dotenv": "^16.3.1", "extendable-media-recorder": "^9.1.3", "extendable-media-recorder-wav-encoder": "^7.0.99", "formik": "^2.4.2", diff --git a/frontend/src/pages/AI_Assistant.js b/frontend/src/pages/AI_Assistant.js index 0b63530..3c716d0 100644 --- a/frontend/src/pages/AI_Assistant.js +++ b/frontend/src/pages/AI_Assistant.js @@ -9,6 +9,8 @@ import useFetch from '../hooks/fetch.hook'; import { useAuthStore } from '../store/store'; import { MediaRecorder, register } from 'extendable-media-recorder'; import { connect } from 'extendable-media-recorder-wav-encoder'; +// import 'dotenv/config'; + import axios from 'axios'; @@ -38,14 +40,14 @@ const AI_Assistant = () => { } }, [response]); - useEffect(() => { - axios.get('http://localhost:5001/api/spotify_recommend', { mood: emotion }) - .then((response) => { - setRecommendations(response.data); - console.log(response.data); - }) - .catch((error) => console.error(error)) - }, [emotion]); + // useEffect(() => { + // axios.get('${process.env.SERVER_ENDPOINT}/api/spotify_recommend', { mood: emotion }) + // .then((response) => { + // setRecommendations(response.data); + // console.log(response.data); + // }) + // .catch((error) => console.error(error)) + // }, [emotion]); const startRecording = async () => { if (isRecording) { @@ -80,7 +82,7 @@ const AI_Assistant = () => { // Send the audio data to the Node.js backend axios - .post('http://localhost:5001/api/voice-input', formData) + .post(`${process.env.REACT_APP_SERVER_ENDPOINT}/api/voice-input`, formData) .then((response) => { console.log(response.data.result); let data = response.data.result; @@ -94,7 +96,7 @@ const AI_Assistant = () => { // console.log(chatLog) // Make an HTTP request to the backend API to analyze user input using axios axios - .post('http://localhost:5001/api/analyze', { message: voice_message }) + .post(`${process.env.REACT_APP_SERVER_ENDPOINT}/api/analyze`, { message: voice_message }) .then((response) => { const updatedChatLogWithAI = [...chatLog, { user: 'User', message: voice_message }, { user: 'AI_Consultant', message: response.data.result }]; setChatLog(updatedChatLogWithAI); @@ -103,7 +105,7 @@ const AI_Assistant = () => { .catch((error) => console.error(error)); axios - .post('http://localhost:5001/api/emotion_analyze', { message: voice_message }) + .post(`${process.env.REACT_APP_SERVER_ENDPOINT}/api/emotion_analyze`, { message: voice_message }) .then((response) => { setEmotion(response.data.emotion); // console.log(response.data.emotion) @@ -128,10 +130,13 @@ const AI_Assistant = () => { e.preventDefault(); const updatedChatLog = [...chatLog, { user: 'User', message: message }]; setChatLog(updatedChatLog); + // console.log('{$process.env.REACT_APP_SERVER_ENDPOINT}') + + // Make an HTTP request to the backend API to analyze user input using axios axios - .post('http://localhost:5001/api/analyze', { message: message }) + .post(`${process.env.REACT_APP_SERVER_ENDPOINT}/api/analyze`, { message: message }) .then((response) => { const updatedChatLogWithAI = [...updatedChatLog, { user: 'AI_Consultant', message: response.data.result }]; setChatLog(updatedChatLogWithAI); @@ -141,10 +146,10 @@ const AI_Assistant = () => { .catch((error) => console.error(error)); axios - .post('http://localhost:5001/api/emotion_analyze', { message: message }) + .post(`${process.env.REACT_APP_SERVER_ENDPOINT}/api/emotion_analyze`, { message: message }) .then((response) => { setEmotion(response.data.emotion); - console.log(response.data.emotion) + console.log(emotion) }) .catch((error) => console.error(error)); // Clear the input field after submitting diff --git a/frontend/src/pages/MusicTherapy.js b/frontend/src/pages/MusicTherapy.js index 7ec3f5d..379208f 100644 --- a/frontend/src/pages/MusicTherapy.js +++ b/frontend/src/pages/MusicTherapy.js @@ -27,13 +27,15 @@ import profileImg from '../images/chat-user.svg'; import chevronDown from '../images/chevron-down.svg'; import { useEffect, useState } from 'react'; import { Buffer } from 'buffer'; +// import 'dotenv/config'; import axios from 'axios'; const qs = require('qs'); -const client_id = '07f4d94fc95d4955ad32cdf68dbefa0c'; // Your client id -const client_secret = 'cd95a4c259a94411b20b6929270c8ab8'; // Your secret -// const client_id = process.env.SPOTIFY_CLIENT_ID; // Your client id -// const client_secret = process.env.SPOTIFY_CLIENT_SECRET; // Your secret + +// const client_id = '07f4d94fc95d4955ad32cdf68dbefa0c'; // Your client id +// const client_secret = 'cd95a4c259a94411b20b6929270c8ab8'; // Your secret +const client_id = process.env.SPOTIFY_CLIENT_ID; // Your client id +const client_secret = process.env.SPOTIFY_CLIENT_SECRET; // Your secret const redirect_uri = process.env.SPOTIFY_REDIRECT_URI; const auth_token = Buffer.from(`${client_id}:${client_secret}`, 'utf-8').toString('base64'); diff --git a/frontend/src/pages/YT_RecommendationView.js b/frontend/src/pages/YT_RecommendationView.js index a1759d9..c0d0eb0 100644 --- a/frontend/src/pages/YT_RecommendationView.js +++ b/frontend/src/pages/YT_RecommendationView.js @@ -4,6 +4,8 @@ import Footer from '../components/Footer'; import '../styles/YT_RecommendationView.scss'; import { Link } from 'react-router-dom'; import axios from 'axios'; +// import 'dotenv/config'; + // get_youtube_videos_from_preferences /** POST: http://localhost:5001/api/youtube_list */ @@ -47,7 +49,7 @@ const YT_RecommendationView = () => { body: storedData, }; axios - .post('http://localhost:5001/api/youtube_list', storedData) + .post(`${process.env.REACT_APP_SERVER_ENDPOINT}/api/youtube_list`, storedData) .then((response) => { console.log(typeof response.data); setVideoIds(response.data);