Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/tharoosha/Depresio
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadil-K committed Nov 1, 2023
2 parents f93e73b + 8c89bff commit bebfe93
Show file tree
Hide file tree
Showing 18 changed files with 331 additions and 116 deletions.
31 changes: 29 additions & 2 deletions backend/controllers/appController.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,6 @@ export async function updateRecommendation(req, res) {
}
}



/** GET: http://localhost:5001/api/recommendation/:username */
export async function getRecommendation(req, res) {

Expand Down Expand Up @@ -398,4 +396,33 @@ export async function getRecommendation(req, res) {
// } catch (error) {
// return res.status(500).send({ error: "Failed to retrieve recommendations" });
// }
}

/** PUT: http://localhost:5001/api/updateRecommendation */
export async function updateEmotion(req, res){
try {
const { userId } = req.user;

if (userId) {

const body = req.body;

// update the data
UserModel.updateOne({ _id : userId }, body).exec().then(
(response) => {
res.status(201).send({ msg : "Record Updated...!"})
}
)

}else {
return res.status(401).send({ error : "User Not Found...!"});
}

} catch (error) {
return res.status(500).send({ error: "Failed to update recommendations" });
}
}

export async function getEmotion(req, res){

}
19 changes: 12 additions & 7 deletions backend/controllers/mlController.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,14 @@ export async function youtube_lists(req, res) {
// const process = spawn("/usr/src/app/venv/bin/python3", ["/usr/src/app/ml_models/recommanded_system/youtube_search.py", categories,]);
const process = spawn("python3", ["../backend/ml_models/recommanded_system/youtube_search.py", categories,]);

let youtube_list = []
// let youtube_list = []
let youtube_list = "";


process.stdout.on("data", (data) => {
youtube_list = data;
// youtube_list = data;
youtube_list += data.toString();

});

process.on("close", (code) => {
Expand Down Expand Up @@ -242,29 +247,29 @@ export async function youtube_lists(req, res) {
// Define the controller function to execute the Python script
export async function spotify_recommend(req, res) {
try {
const { mood } = req.body;
const { emotion } = req.body;

// Spawn the Python script as a child process
// const pythonProcess = spawn("/usr/src/app/venv/bin/python3", ["/usr/src/app/ml_models/spotify_recommendation/spotifyRecommendExecution.py", mood,]);
const pythonProcess = spawn("python3", [ "../backend/ml_models/spotify_recommendation/spotifyRecommendExecution.py", mood,]);
const pythonProcess = spawn("python3", [ "../backend/ml_models/spotify_recommendation/spotifyRecommendExecution.py", emotion,]);

let output = "";
// let output = [];

// Listen for data events from the Python script's stdout
pythonProcess.stdout.on("data", (data) => {
console.log(data.toString())
// 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).json(result);
res.status(200).send(output);
// res.status(200).json({"result":output});
} catch (error) {
res.status(500).json({ error: "Failed to parse JSON response" });
Expand Down
5 changes: 5 additions & 0 deletions backend/controllers/testing/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
console.log(typeof {"result": ["1HNkqx9Ahdgi1Ixy2xkKkL", "1ei3hzQmrgealgRKFxIcWn", "7eJMfftS33KTjuF7lTsMCx"]})

type = {"result": ["1HNkqx9Ahdgi1Ixy2xkKkL", "1ei3hzQmrgealgRKFxIcWn", "7eJMfftS33KTjuF7lTsMCx"]}

console.log(type.result)
29 changes: 29 additions & 0 deletions backend/controllers/testing/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import json
import sys

def script_run():
try:
result = ['1HNkqx9Ahdgi1Ixy2xkKkL', '1ei3hzQmrgealgRKFxIcWn', '7eJMfftS33KTjuF7lTsMCx']

output = {"result": result}

output_json = json.dumps(output)
print(json.decode(output))


print(output_json)

except Exception as e:
error_message = str(e)
output = {"error2011": error_message}

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


if __name__ == "__main__":

# input_mood = sys.argv[1]

script_run()
Binary file not shown.
Binary file not shown.
30 changes: 15 additions & 15 deletions backend/ml_models/recommanded_system/combinedScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json

def combined_script(input_mood):
try:
# try:
# Call the function from mood_TO_video.py to get the output JSON
# output_json = train_and_predict_video_types(input_mood)

Expand All @@ -26,27 +26,27 @@ def combined_script(input_mood):
output_json = json.dumps(output)
sys.stdout.flush()
print(result)
except:
error_message = str(e)
output = {"error2011": error_message}

output_json = json.dumps(output)
# print(output_json)
sys.stdout.flush()
return(output_json)
# except:
# error_message = str(e)
# output = {"error2011": error_message}

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

if __name__ == '__main__':
# Check if there is at least one command-line argument (excluding the script name)
if len(sys.argv) > 1:
# if len(sys.argv) > 1:
# The first command-line argument (sys.argv[1]) will be the input_mood
# input_mood = sys.argv[1]

# Call the function with the received input_mood
# combined_script(input_mood)
combined_script("Happiness")
combined_script("Happiness")

else:
# If no input_mood is provided, handle the case accordingly
print(json.dumps({"error": "No mood provided"}))
sys.stdout.flush()
# else:
# # If no input_mood is provided, handle the case accordingly
# print(json.dumps({"error": "No mood provided"}))
# sys.stdout.flush()
# combined_script('Happiness')
4 changes: 2 additions & 2 deletions backend/ml_models/recommanded_system/mood_TO_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ def update_user_feedback(mood, video_type, user_feedback):
text = mood + "," + video_type + "," + str(user_feedback)

# Open the CSV file in 'a' (append) mode and write the text
with open('youtube_data.csv', 'a') as file:
with open('backend/ml_models/recommanded_system/youtube_data.csv', 'a') as file:
file.write('\n' + text)


def train_and_predict_video_types(input_mood):
# Load the dataset with user feedback as NaN initially
df = pd.read_csv('youtube_data.csv')
df = pd.read_csv('backend/ml_models/recommanded_system/youtube_data.csv')
# Fill missing user_feedback values with 0
df['user_feedback'].fillna(0, inplace=True)

Expand Down
23 changes: 19 additions & 4 deletions backend/ml_models/recommanded_system/youtube_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@


def search_youtube_videos(video_types_probabilities):
api_key = os.getenv("YOUTUBE_API_KEY")
# api_key = os.getenv("YOUTUBE_API_KEY")
api_key = "AIzaSyBLplcZSVmjupMn-6drtByHPuFFuQui4MI"

max_videos = sum(video_types_probabilities.values())
videos_info = []

Expand Down Expand Up @@ -137,23 +139,34 @@ def get_youtube_videos_from_preferences(api_key, categories, max_results=5):
def youtube_lists(input):
try:
# Parse the JSON data
api_key = "AIzaSyBLplcZSVmjupMn-6drtByHPuFFuQui4MI"

category_list = input["categories"]
print(category_list)
# print(category_list)
# print(category_list)
# categories = ["Gaming"]
videos = get_youtube_videos_from_preferences(os.getenv("YOUTUBE_API_KEY"), category_list)

videos = get_youtube_videos_from_preferences(api_key, category_list)
# videos = ["FLDAuYJy4Tw", "XGdYhRxHNAQ", "MhZhezlx8ZU", "BejOWDwJYYM", "lg-oBTPvlgU"]

# videos = get_youtube_videos_from_preferences(API_KEY, categories)
# print(videos)
# # for video in videos:
# # print(video)

output = {"result": videos}
# # output = {"result": ["lTxn2BuqyzU", "MU0HhH4dIHs", "i5gbWhRW_ZA", "FUqttxtk8y0", "8PD-Smkxsj0", "nqf8-vB_PWE", "TQgWzalijAw", "JbHk0ZjYM04", "GkIA4ldWRis", "puy5WYx1Wok", "DwuJeGYlYyw", "4GvI7M8PyKs", "n_NfxUQCoXE", "Fjp2TdlTTIU", "SCyFBeIgaDc"]}

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

# videos = videos.split(',')
print(videos)
# print(videos)
# return videos

except Exception as e:
Expand All @@ -170,8 +183,10 @@ def youtube_lists(input):
# data = json.loads(input)
# youtube_lists(input)
input = {"categories" : [input]}
# input = ["Film & Animation","Music","Shows"]
youtube_lists(input)
# print(input)
# print(input)



Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,30 @@
def script_run(input_mood):
try:

file_path = '/usr/src/app/ml_models/spotify_recommendation/tokenizer.pkl'
# file_path = '/usr/src/app/ml_models/spotify_recommendation/tokenizer.pkl'
# file_path = '../backend/ml_models/spotify_recommendation/tokenizer.pkl'
if os.path.exists(file_path):
with open('/usr/src/app/ml_models/spotify_recommendation/tokenizer.pkl', 'rb') as f:
# if os.path.exists(file_path):
# with open('/usr/src/app/ml_models/spotify_recommendation/tokenizer.pkl', 'rb') as f:
# with open('../backend/ml_models/spotify_recommendation/tokenizer.pkl', 'rb') as f:
scaler = pickle.load(f)
# scaler = pickle.load(f)

model = tf.keras.models.load_model('/usr/src/app/ml_models/spotify_recommendation/spotify_model')
# model = tf.keras.models.load_model('/usr/src/app/ml_models/spotify_recommendation/spotify_model')
# 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 = spotifyRecommendScript.getRecommendation(input_mood, model, scaler)

# print({input_mood})
# result = '["1HNkqx9Ahdgi1Ixy2xkKkL", "1ei3hzQmrgealgRKFxIcWn", "7eJMfftS33KTjuF7lTsMCx"]'
# result = '["1bzsi0xA8MxBZICdtB23pO", "6XSCLKQcavtn1PaLAhmo1x", "0sfCRhw6gS3b1WepmTVPZE", "6u8KnNnGthVY4PaWiH2mgt", "4FIGOj3O3cyIf6Dg2TDoVv", "0FSk1OsGWoTCS8lA74x5lF", "55eUtE9v3uatR0y0NijM6N", "1bzsi0xA8MxBZICdtB23pO", "6XSCLKQcavtn1PaLAhmo1x", "0sfCRhw6gS3b1WepmTVPZE"]'
result = '["0wjno8lHmDep5uhLVowoSA", "42maX10SkL9u6KM2dCdgPm", "0ABeQP5ffDRiZTe3aorRGn", "3QPFWCPWmL7fed8jCQ8jty", "59s47nDkFz1UNXujnpzEPt", "4c2JHXheN3t6Wh13lqj91s", "6eABugchKfRPyrzUpv7UzJ", "42maX10SkL9u6KM2dCdgPm", "6PJrnbdGmbiXjFceEFlnaR", "4cFm2ZZcb4mQMXwKCeJgzs"]'
# result = json.dumps(result)
# response = str(result)
output = {"result": result}

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

except Exception as e:
error_message = str(e)
Expand Down Expand Up @@ -87,6 +92,7 @@ def script_run(input_mood):

# Call the function with the received input_mood
script_run(input_mood)
# print({input_mood})

# # script_run("happiness")

Expand Down
Binary file modified backend/ml_models/temp_audio.wav
Binary file not shown.
10 changes: 10 additions & 0 deletions backend/models/User.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ export const UserSchema = new mongoose.Schema({
address: { type: String},
profile: { type: String},
recommendation: { type: String},

emotions: {
Monday: { Joy: Number, Surprise: Number, Anger: Number, Sad: Number, Happy: Number },
Tuesday: { Joy: Number, Surprise: Number, Anger: Number, Sad: Number, Happy: Number },
Wednesday: { Joy: Number, Surprise: Number, Anger: Number, Sad: Number, Happy: Number },
Thursday: { Joy: Number, Surprise: Number, Anger: Number, Sad: Number, Happy: Number },
Friday: { Joy: Number, Surprise: Number, Anger: Number, Sad: Number, Happy: Number },
Saturday: { Joy: Number, Surprise: Number, Anger: Number, Sad: Number, Happy: Number },
Sunday: { Joy: Number, Surprise: Number, Anger: Number, Sad: Number, Happy: Number },
},
});

export default mongoose.model.Users || mongoose.model('User', UserSchema );
21 changes: 8 additions & 13 deletions frontend/src/components/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ import { Chart } from 'react-google-charts';

const EmotionLineChart = () => {
const data = [
['Month', 'Joyful', 'Surprise', 'Anger', 'Sad', 'Happy'],
['January', 20, 15, 10, 5, 8],
['February', 18, 13, 11, 7, 10],
['March', 22, 17, 9, 4, 6],
['April', 25, 12, 15, 6, 9],
['May', 16, 20, 11, 8, 12],
['June', 19, 18, 8, 7, 11],
['July', 23, 16, 13, 5, 9],
['August', 17, 14, 12, 9, 7],
['September', 20, 19, 14, 4, 8],
['October', 18, 15, 10, 6, 10],
['November', 21, 17, 9, 5, 11],
['December', 24, 20, 11, 7, 6],
['Day', 'Joyful', 'Surprise', 'Anger', 'Sad', 'Happy'],
['Monday', 20, 15, 10, 5, 8],
['Tuesday', 18, 13, 11, 7, 10],
['Wednesday', 22, 17, 9, 4, 6],
['Tuesday', 25, 12, 15, 6, 9],
['Friday', 16, 20, 11, 8, 12],
['Saturday', 19, 18, 8, 7, 11],
['Sunday', 23, 16, 13, 5, 9]
];

return (
Expand Down
Loading

0 comments on commit bebfe93

Please sign in to comment.